import { getTranslations, setRequestLocale } from 'next-intl/server'
import { claimPaidCheckout } from '@/lib/stripe'
import { executeSoapCommand } from '@/lib/soap'

export const dynamic = 'force-dynamic'

export default async function RenameSuccessPage({
  params,
  searchParams,
}: {
  params: Promise<{ locale: string }>
  searchParams: Promise<{ session_id?: string }>
}) {
  const { locale } = await params
  setRequestLocale(locale)
  const { session_id } = await searchParams
  const t = await getTranslations('Rename')

  // Verifica el pago (una sola vez) y ejecuta el comando SOAP.
  let okName: string | null = null
  if (session_id) {
    const claim = await claimPaidCheckout(session_id)
    if (claim) {
      const resp = await executeSoapCommand(`.char rename ${claim.characterName}`)
      if (resp !== null) okName = claim.characterName
    }
  }

  return (
    <main className="mx-auto max-w-lg px-4 py-12 text-center">
      <h1 className="mb-6 text-2xl font-bold text-amber-500">{t('successTitle')}</h1>
      {okName ? (
        <p className="text-green-400">{t('success', { name: okName })}</p>
      ) : (
        <p className="text-red-400">{t('error')}</p>
      )}
    </main>
  )
}
