import type { Metadata } from 'next'
import { NoteLegend } from '@/components/NoteLegend'
import { getTranslations, setRequestLocale } from 'next-intl/server'
import { redirect } from '@/i18n/navigation'
import { getSession } from '@/lib/session'
import { getRestorableCharacters } from '@/lib/restore-character'
import { PageShell } from '@/components/PageShell'
import { ServiceBox } from '@/components/ServiceBox'
import { RestoreCharacterForm } from '@/components/RestoreCharacterForm'

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: 'CharService' })
  return { title: t('restoreChar.pageTitle') }
}

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

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

  const chars = await getRestorableCharacters(session.accountId!)

  return (
    <PageShell title={t('restoreChar.pageTitle')}>
      <ServiceBox>
        <br />
        <p>
          {t.rich('restoreChar.intro', { s: (c) => <span>{c}</span> })}
        </p>
        <br />
        <p>{t('restoreChar.conditions')}</p>
        <p>{t('restoreChar.cond1')}</p>
        <p>{t('restoreChar.cond2')}</p>
        <p>{t.rich('restoreChar.dkHeading', { dk: (c) => <span className="death-knight">{c}</span> })}</p>
        <p>{t('restoreChar.dkCond')}</p>
        <br />
        <p>{t('restoreChar.afterRestore')}</p>
        <br />
        <fieldset>
          <NoteLegend />
          <div className="separate2">
            <p>{t('restoreChar.noteConditions')}</p>
            <p>{t('restoreChar.noteCooldown')}</p>
          </div>
        </fieldset>

        <RestoreCharacterForm characters={chars} />
      </ServiceBox>
    </PageShell>
  )
}
