import { serverClient } from "@/lib/trpc/server"

import Blocks from "@/components/Blocks"
import Title from "@/components/TempDesignSystem/Text/Title"
import TrackingSDK from "@/components/TrackingSDK"
import { getIntl } from "@/i18n"
import { setLang } from "@/i18n/serverContext"

import styles from "./page.module.css"

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

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

export default async function MyPages({
  params,
}: PageArgs<LangParams & { path: string[] }>) {
  setLang(params.lang)

  const accountPageRes = await serverClient().contentstack.accountPage.get()
  const intl = await getIntl()

  if (!accountPageRes) {
    return null
  }

  const { tracking, accountPage } = accountPageRes

  return (
    <>
      <main className={styles.blocks}>
        <Title>{accountPage.heading}</Title>
        {accountPage.content?.length ? (
          <Blocks blocks={accountPage.content} />
        ) : (
          <p>{intl.formatMessage({ id: "No content published" })}</p>
        )}
      </main>
      <TrackingSDK pageData={tracking} />
    </>
  )
}
