import type { Metadata } from 'next'
import { getTranslations, setRequestLocale } from 'next-intl/server'
import { redirect, Link } from '@/i18n/navigation'
import { getSession } from '@/lib/session'
import { getDPointsBalance } from '@/lib/dpoints'
import { PageShell } from '@/components/PageShell'
import { ServiceBox } from '@/components/ServiceBox'
import { TransferDPointsForm } from '@/components/TransferDPointsForm'

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('transferDp.title') }
}

export default async function TransferDPointsPage({ 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 balance = await getDPointsBalance(session.accountId!)

  return (
    <PageShell title={t('transferDp.title')}>
      <ServiceBox>
        <br />
        <p>
          {t.rich('transferDp.intro1', { s: (c) => <span>{c}</span> })}
        </p>
        <br />
        <p>
          {t('transferDp.intro2')}
        </p>
        <br />
        <p>
          {t.rich('transferDp.tokenHelp', {
            link: (c) => (
              <Link href="/security-token" target="_blank">
                {c}
              </Link>
            ),
          })}
        </p>
        <br />
        <fieldset>
          <legend>{t('transferDp.note')}</legend>
          <div className="separate2">
            <p>
              {t('transferDp.noteText')}
            </p>
          </div>
        </fieldset>

        <TransferDPointsForm balance={balance} />
      </ServiceBox>
    </PageShell>
  )
}
