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

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

import ContentPage from "@/components/ContentType/StaticPages/ContentPage"
import { getLang } from "@/i18n/serverContext"
import { isLoggedInUser } from "@/utils/isLoggedInUser"

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

export default async function ContentPagePage() {
  const lang = await getLang()
  const headersList = await headers()
  const pathname = headersList.get("x-pathname") || ""
  const isSignupRoute = isSignupPage(pathname)

  if (isSignupRoute) {
    if (!env.SHOW_SIGNUP_FLOW) {
      notFound()
    }

    const isLoggedIn = await isLoggedInUser()

    if (isLoggedIn) {
      redirect(overview[lang])
    }
  }

  return <ContentPage />
}
