import type { Metadata } from 'next'
import { notFound } from 'next/navigation'

import { PermissionGate } from '@/components/console/permission-gate'
import { DecisionReplay } from '@/components/intelligence/decision-replay'
import { DemoDataBadge, PageHeader } from '@/components/ui/display'
import { getCurrentUser } from '@/lib/auth/current-user'
import { getI18n } from '@/lib/i18n/server'
import { getReplay } from '@/lib/services/proof-service'

export const metadata: Metadata = { title: 'Decision Replay' }
export const dynamic = 'force-dynamic'

/**
 * One frozen proof, replayed.
 *
 * Proof and replay live on the same page because they are the same evidence read two
 * ways: the bundle says what was known, the timeline says when. Splitting them would make
 * a presenter navigate away from the answer to reach its justification.
 */
export default async function ProofDetailPage({ params }: { params: Promise<{ code: string }> }) {
  const user = await getCurrentUser()
  const { t } = await getI18n()
  if (!user?.permissions.has('proof.read')) return <PermissionGate />

  const { code } = await params
  const replay = await getReplay(code)
  if (!replay) notFound()

  return (
    <>
      <PageHeader
        title={t('replay.title')}
        subtitle={t('replay.subtitle')}
        badge={<DemoDataBadge label={t('common.simulated')} title={t('replay.note')} />}
      />
      <DecisionReplay replay={replay} />
    </>
  )
}
