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 { getGameCharacters } from '@/lib/characters'
import { getDPointsBalance } from '@/lib/dpoints'
import { getRestoreItemPrice } from '@/lib/prices'
import { PageShell } from '@/components/PageShell'
import { ServiceBox } from '@/components/ServiceBox'
import { RestoreItemsForm } from '@/components/RestoreItemsForm'

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

export default async function RestoreItemsPage({ 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, price, pdBalance] = await Promise.all([
    getGameCharacters(session.accountId!),
    getRestoreItemPrice(),
    getDPointsBalance(session.accountId!),
  ])

  return (
    <PageShell title={t('restoreItems.pageTitle')}>
      <ServiceBox>
        <br />
        <p>
          {t.rich('restoreItems.intro', { s: (c) => <span>{c}</span> })}
        </p>
        <br />
        <p>{t('restoreItems.conditions')}</p>
        <p>
          {t.rich('restoreItems.quality', {
            q3: (c) => <span className="q3">{c}</span>,
            q4: (c) => <span className="q4">{c}</span>,
            q5: (c) => <span className="q5">{c}</span>,
            q6: (c) => <span className="q6">{c}</span>,
            q6b: (c) => <span className="q6">{c}</span>,
          })}
        </p>
        <p>{t('restoreItems.condEquip')}</p>
        <p>{t('restoreItems.condBound')}</p>
        <p>{t('restoreItems.condDeleted')}</p>
        <br />
        <fieldset>
          <NoteLegend />
          <div className="separate2">
            <p>{t('restoreItems.noteConditions')}</p>
            <p>{t('restoreItems.noteCooldown')}</p>
          </div>
        </fieldset>

        <div className="centered">
          <br />
          <br />
          <p>{t.rich('restoreItems.requires', { price, s: (c) => <span>{c}</span>, e: (c) => <span className="yellow-info">{c}</span> })}</p>
          <br />
          <RestoreItemsForm characters={chars.map((c) => ({ name: c.name, classCss: c.classCss }))} price={price} pdBalance={pdBalance} />
        </div>
      </ServiceBox>
    </PageShell>
  )
}
