import type { Metadata } from 'next'

import { PermissionGate } from '@/components/console/permission-gate'
import { VerificationBoard } from '@/components/intelligence/verification-board'
import { DemoDataBadge, PageHeader } from '@/components/ui/display'
import { getCurrentUser } from '@/lib/auth/current-user'
import { getI18n } from '@/lib/i18n/server'
import { getVerificationReport, listVerifications } from '@/lib/services/verification-service'
import { listTwins } from '@/lib/services/twin-service'

export const metadata: Metadata = { title: 'Model vs Reality' }
export const dynamic = 'force-dynamic'

/**
 * Model versus reality (§7).
 *
 * The page that makes the rest of the platform falsifiable. Everything on it is a
 * measurement of the platform's own claims, including the uncomfortable measurements:
 * how many predictions are still unchecked, and whether the confidence figures elsewhere
 * are earned.
 */
export default async function VerificationPage() {
  const user = await getCurrentUser()
  const { t } = await getI18n()
  if (!user?.permissions.has('verification.read')) return <PermissionGate />

  const [verifications, report, twins] = await Promise.all([
    listVerifications({ limit: 40 }),
    getVerificationReport({}),
    listTwins(),
  ])

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

      <VerificationBoard
        verifications={verifications}
        quality={report.quality}
        calibration={report.calibration}
        series={report.series}
        twins={twins.map((twin) => ({ code: twin.code, name: twin.name, nameAr: twin.nameAr }))}
        canRun={user.permissions.has('verification.run')}
      />
    </>
  )
}
