import type { Metadata } from 'next'
import { NoteLegend } from '@/components/NoteLegend'
import { getTranslations, setRequestLocale } from 'next-intl/server'
import { redirect, Link } from '@/i18n/navigation'
import { getSession } from '@/lib/session'
import { getGameCharacters } from '@/lib/characters'
import { getTransferPrice } from '@/lib/prices'
import { PageShell } from '@/components/PageShell'
import { ServiceBox } from '@/components/ServiceBox'
import { TransferForm } from '@/components/TransferForm'

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('transfer.pageTitle') }
}

export default async function TransferCharacterPage({ 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: '/login', locale })
  if (!session.username) redirect({ href: '/select-account', locale })

  const [chars, price] = await Promise.all([getGameCharacters(session.accountId!), getTransferPrice()])

  return (
    <PageShell title={t('transfer.pageTitle')}>
      <ServiceBox>
        <br />
        <p>
          {t.rich('transfer.intro', { s: (c) => <span>{c}</span> })}
        </p>
        <br />
        <p>{t.rich('transfer.dkHeading', { dk: (c) => <span className="death-knight">{c}</span> })}</p>
        <p>{t('transfer.dkCond1')}</p>
        <p>{t('transfer.dkCond2')}</p>
        <br />
        <p>{t('transfer.securityLock')}</p>
        <br />
        <p>
          {t('transfer.buttonExplain')}
        </p>
        <p>{t('transfer.afterTransfer')}</p>
        <br />
        <p>{t.rich('transfer.securityTokenHint', { link: (c) => <Link href="/security-token" target="_blank">{c}</Link> })}</p>
        <p>{t.rich('transfer.twoFaHint', { link: (c) => <Link href="/security-2falogin" target="_blank">{c}</Link> })}</p>
        <br />
        <fieldset>
          <NoteLegend />
          <div className="separate2">
            <p>{t('transfer.noteOffline')}</p>
            <p>
              {t('transfer.noteIrreversible')}
            </p>
          </div>
        </fieldset>

        <div className="centered">
          <br />
          <br />
          <p>{t.rich('transfer.requires', { price, s: (c) => <span>{c}</span>, e: (c) => <span className="yellow-info">{c}</span> })}</p>
        </div>

        <TransferForm characters={chars.map((c) => ({ name: c.name, classCss: c.classCss }))} price={price} />
      </ServiceBox>
    </PageShell>
  )
}
