import { notFound } from 'next/navigation'
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 { getPaidService } from '@/lib/paid-services'
import { PageShell } from '@/components/PageShell'
import { ServiceBox } from '@/components/ServiceBox'
import { PaidServiceForm } from '@/components/PaidServiceForm'

/** Contenido común de una página de servicio de pago (guardas + form). */
export async function ServicePageContent({ service, locale }: { service: string; locale: string }) {
  setRequestLocale(locale)
  const cfg = getPaidService(service)
  if (!cfg) notFound()

  const t = await getTranslations('Paid')
  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!),
    cfg!.price({}),
    getDPointsBalance(session.accountId!),
  ])

  return (
    <PageShell title={t(`${service}.title`)}>
      <ServiceBox>
        <PaidServiceForm
          characters={chars.map((c) => ({ name: c.name, classCss: c.classCss }))}
          checkoutEndpoint={`/api/character/${service}/checkout`}
          payLabel={t(`${service}.pay`, { price })}
          buttonClass={`${service}-button`}
          priceEur={price}
          pdBalance={pdBalance}
        />
      </ServiceBox>
    </PageShell>
  )
}
