import type { Metadata } from 'next'
import { getTranslations, setRequestLocale } from 'next-intl/server'
import { redirect } from '@/i18n/navigation'
import { getSession } from '@/lib/session'
import { getRealmName } from '@/lib/realm'
import { PageShell } from '@/components/PageShell'
import { ServiceBox } from '@/components/ServiceBox'
import { PromoCodeForm } from '@/components/PromoCodeForm'

export const dynamic = 'force-dynamic'

export async function generateMetadata({ params }: { params: Promise<{ locale: string }> }): Promise<Metadata> {
  const { locale } = await params
  const t = await getTranslations({ locale, namespace: 'Points' })
  return { title: t('promo.title') }
}

export default async function PromoCodePage({ params }: { params: Promise<{ locale: string }> }) {
  const { locale } = await params
  setRequestLocale(locale)
  const t = await getTranslations('Points')

  const session = await getSession()
  if (!session.bnetId) redirect({ href: '/log-in', locale })
  if (!session.username) redirect({ href: '/select-account', locale })

  const realm = await getRealmName()

  return (
    <PageShell title={t('promo.title')}>
      <ServiceBox>
        <br />
        <p>
          {t.rich('promo.intro1', { s: (c) => <span>{c}</span> })}
        </p>
        <p>
          {t('promo.intro2', { realm })}
        </p>
        <p>{t('promo.intro3')}</p>
        <br />
        <fieldset>
          <legend>{t('promo.note')}</legend>
          <div className="separate2">
            <p>
              {t('promo.noteText')}
            </p>
          </div>
        </fieldset>

        <PromoCodeForm />
      </ServiceBox>
    </PageShell>
  )
}
