import { getTranslations, setRequestLocale } from 'next-intl/server'
import { NoteLegend } from '@/components/NoteLegend'
import { redirect } from '@/i18n/navigation'
import { getSession } from '@/lib/session'
import { getGameCharacters } from '@/lib/characters'
import { PageShell } from '@/components/PageShell'
import { ServiceBox } from '@/components/ServiceBox'
import { CharacterActionForm } from '@/components/CharacterActionForm'

export const dynamic = 'force-dynamic'

export default async function UnstuckPage({ params }: { params: Promise<{ locale: string }> }) {
  const { locale } = await params
  setRequestLocale(locale)
  const t = await getTranslations('Services')
  const session = await getSession()
  if (!session.bnetId) redirect({ href: '/log-in', locale })
  if (!session.username) redirect({ href: '/select-account', locale })
  const chars = await getGameCharacters(session.accountId!)

  return (
    <PageShell title={t('unstuckTitle')}>
      <ServiceBox>
        <br />
        <p>{t.rich('unstuckInfo1', { b: (c) => <span>{c}</span> })}</p>
        <p>{t('unstuckInfo2')}</p>
        <br />
        <fieldset>
          <NoteLegend />
          <div className="separate2">
            <p>{t('unstuckNote1')}</p>
            <p>{t('unstuckNote2')}</p>
          </div>
        </fieldset>

        <CharacterActionForm
          characters={chars.map((c) => ({ name: c.name, classCss: c.classCss }))}
          endpoint="/api/character/unstuck"
          actionLabel={t('unstuckAction')}
          processingLabel={t('unstuckProcessing')}
          doneLabel={t('unstuckDone')}
          promptText={t('unstuckChoose')}
          buttonClass="unstuck-button"
        />
      </ServiceBox>
    </PageShell>
  )
}
