import type { Metadata } from 'next'

import { PermissionGate } from '@/components/console/permission-gate'
import { RiskHunter } from '@/components/intelligence/risk-hunter'
import { DemoDataBadge, PageHeader } from '@/components/ui/display'
import { getCurrentUser } from '@/lib/auth/current-user'
import { REGIONS } from '@/lib/data/regions'
import { getI18n } from '@/lib/i18n/server'
import { listHunts } from '@/lib/services/risk-hunter-service'

export const metadata: Metadata = { title: 'Risk Hunter' }
export const dynamic = 'force-dynamic'

/**
 * The Autonomous Risk Hunter (4.1).
 *
 * The page holds no search of its own — it orchestrates engines that already exist
 * (scenario discovery, the cascade tree, the counterfactual solver and asset criticality)
 * and points them at a question nobody asked. Adding a seventh search algorithm would have
 * been the wrong answer to "find risks nobody asked about".
 */
export default async function RiskHunterPage() {
  const user = await getCurrentUser()
  const { t } = await getI18n()
  if (!user?.permissions.has('riskhunter.read')) return <PermissionGate />

  const hunts = await listHunts(8)

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

      <RiskHunter
        initialRuns={hunts}
        regions={REGIONS.map((region) => ({
          code: region.code,
          name: region.name,
          nameAr: region.nameAr,
        }))}
        canRun={user.permissions.has('riskhunter.run')}
      />
    </>
  )
}
