import { unstable_cache } from 'next/cache'
import ArticleBody from '@/modules/newsModule/components/ArticleBody'
import { getLatestTerms } from '@/sanity/cms'
import { getSanityOrgCode } from '@/sanity/tenantOrg.server'

const JURISDICTION_LIST = ['ID', 'SG', 'MY', 'GLOBAL', 'VN']
const LOCALE_LIST = ['en-US', 'id-ID']

export default async function TermsAndConditionsPage() {
  const orgCode = await getSanityOrgCode()
  const jurisdiction = JURISDICTION_LIST[0]
  const locale = LOCALE_LIST[0]

  const fetchTerms = unstable_cache(
    async () => getLatestTerms(orgCode, jurisdiction, locale) || null,
    ['terms', orgCode, jurisdiction, locale],
    { tags: [`terms:${orgCode}:${jurisdiction}:${locale}`] }
  )

  const terms = orgCode ? await fetchTerms() : null

  return (
    <main className="mx-auto max-w-3xl px-4 py-10 sm:px-6 lg:px-8 text-slate-800 dark:text-slate-200">
      <header className="mb-8">
        <h1 className="text-3xl font-bold tracking-tight text-slate-900 dark:text-slate-100">Terms & Conditions</h1>
        {terms?.effectiveFrom || terms?.versionLabel ? (
          <p className="mt-2 text-sm text-slate-600 dark:text-slate-300">
            {terms?.versionLabel ? `Version ${terms.versionLabel}` : null}
            {terms?.versionLabel && terms?.effectiveFrom ? ' • ' : ''}
            {terms?.effectiveFrom
              ? `Effective ${new Date(terms.effectiveFrom).toLocaleDateString(undefined, { year: 'numeric', month: 'long', day: '2-digit' })}`
              : null}
          </p>
        ) : null}
      </header>

      {terms?.body ? (
        <ArticleBody value={terms.body} />
      ) : (
        <div className="text-slate-600 dark:text-slate-300">Terms & Conditions belum tersedia.</div>
      )}
    </main>
  )
}

