import { headers } from "next/headers"
import { notFound } from "next/navigation"

import { isSignupPage } from "@/constants/routes/signup"
import { env } from "@/env/server"

import HotelPage from "@/components/ContentType/HotelPage"
import LoyaltyPage from "@/components/ContentType/LoyaltyPage"
import CollectionPage from "@/components/ContentType/StaticPages/CollectionPage"
import ContentPage from "@/components/ContentType/StaticPages/ContentPage"
import { setLang } from "@/i18n/serverContext"

import {
  ContentTypeParams,
  LangParams,
  PageArgs,
  UIDParams,
} from "@/types/params"

export { generateMetadata } from "@/utils/generateMetadata"

export default function ContentTypePage({
  params,
}: PageArgs<LangParams & ContentTypeParams & UIDParams, {}>) {
  setLang(params.lang)

  const pathname = headers().get("x-pathname") || ""

  switch (params.contentType) {
    case "collection-page":
      if (env.HIDE_FOR_NEXT_RELEASE) {
        return notFound()
      }
      return <CollectionPage />
    case "content-page": {
      const isSignupRoute = isSignupPage(pathname)

      if (env.HIDE_FOR_NEXT_RELEASE) {
        // Hide content pages for next release for non-signup routes.
        if (!isSignupRoute) {
          return notFound()
        }
      }

      if (!env.SHOW_SIGNUP_FLOW) {
        // Hide content pages for signup routes when signup flow is disabled.
        if (isSignupRoute) {
          return notFound()
        }
      }

      return <ContentPage />
    }
    case "loyalty-page":
      return <LoyaltyPage />
    case "hotel-page":
      if (env.HIDE_FOR_NEXT_RELEASE) {
        return notFound()
      }
      return <HotelPage />
    default:
      const type: never = params.contentType
      console.error(`Unsupported content type given: ${type}`)
      notFound()
  }
}
