import { getTranslations, setRequestLocale } from 'next-intl/server'
import { redirect, Link } from '@/i18n/navigation'
import { getSession } from '@/lib/session'
import { isAdmin } from '@/lib/admin'
import { listPromoCodes } from '@/lib/admin-promo'
import { PageShell } from '@/components/PageShell'
import { AdminPromoManager } from '@/components/AdminPromoManager'

export const dynamic = 'force-dynamic'

export default async function AdminPromoPage({ params }: { params: Promise<{ locale: string }> }) {
  const { locale } = await params
  setRequestLocale(locale)
  const t = await getTranslations('Admin')
  const session = await getSession()
  if (!session.bnetId) redirect({ href: '/log-in', locale })
  if (!(await isAdmin(session))) redirect({ href: '/', locale })
  const codes = await listPromoCodes()
  return (
    <PageShell title={t('promo')}>
      <div className="box-content">
        <div className="body-box-content">
          <Link className="back-to-account" href="/admin">{t('backToPanel')}</Link>
          <AdminPromoManager initial={codes} />
        </div>
      </div>
    </PageShell>
  )
}
