/**
 * The console's navigation map.
 *
 * Each entry declares the permission it needs, and the sidebar renders only what the
 * signed-in user can actually reach. This is a usability affordance, not the access
 * control: the API route behind each page performs the same check server-side and
 * records every denial (§34).
 */

export interface NavItem {
  href: string
  /** Translation key under `nav.`. */
  key: string
  permission: string
  icon: string
}

export interface NavGroup {
  /** Translation key under `nav.groups.`. */
  key: string
  items: NavItem[]
}

export const NAV_GROUPS: NavGroup[] = [
  {
    key: 'operations',
    items: [
      { href: '/command-center', key: 'commandCenter', permission: 'grid.read', icon: 'command' },
      { href: '/executive', key: 'executive', permission: 'executive.read', icon: 'executive' },
      { href: '/grid', key: 'grid', permission: 'grid.read', icon: 'grid' },
      { href: '/map', key: 'map', permission: 'grid.map.read', icon: 'map' },
      { href: '/alerts', key: 'alerts', permission: 'alerts.read', icon: 'alert' },
      { href: '/incidents', key: 'incidents', permission: 'incidents.read', icon: 'incident' },
    ],
  },
  {
    key: 'intelligence',
    items: [
      { href: '/predictions', key: 'predictions', permission: 'predictions.read', icon: 'prediction' },
      { href: '/failure-dna', key: 'failureDna', permission: 'dna.read', icon: 'dna' },
      { href: '/time-machine', key: 'timeMachine', permission: 'simulations.read', icon: 'clock' },
      { href: '/scenarios', key: 'scenarios', permission: 'scenarios.read', icon: 'scenario' },
      { href: '/simulations', key: 'simulations', permission: 'simulations.read', icon: 'simulation' },
      { href: '/forecast', key: 'forecast', permission: 'forecast.read', icon: 'forecast' },
      { href: '/renewables', key: 'renewables', permission: 'renewables.read', icon: 'renewable' },
      { href: '/copilot', key: 'copilot', permission: 'copilot.use', icon: 'copilot' },
    ],
  },
  {
    key: 'decisionIntelligence',
    items: [
      { href: '/digital-twin', key: 'digitalTwin', permission: 'twin.read', icon: 'twin' },
      { href: '/live-grid', key: 'liveGrid', permission: 'twin.read', icon: 'twin' },
      { href: '/grid-physics', key: 'physics', permission: 'twin.read', icon: 'model' },
      { href: '/compare-futures', key: 'compareFutures', permission: 'twin.simulate', icon: 'scenario' },
      { href: '/sensors', key: 'sensors', permission: 'twin.read', icon: 'integration' },
      { href: '/ai-operations', key: 'aiOperations', permission: 'agents.read', icon: 'agents' },
      { href: '/war-room', key: 'warRoom', permission: 'warroom.access', icon: 'warroom' },
      { href: '/cascade', key: 'cascade', permission: 'simulations.read', icon: 'cascade' },
      { href: '/counterfactual', key: 'counterfactual', permission: 'counterfactual.run', icon: 'counterfactual' },
      { href: '/self-healing', key: 'selfHealing', permission: 'optimization.run', icon: 'selfhealing' },
      { href: '/risk-radar', key: 'riskRadar', permission: 'grid.read', icon: 'radar' },
      { href: '/knowledge-graph', key: 'knowledgeGraph', permission: 'knowledge.read', icon: 'knowledge' },
      { href: '/decisions', key: 'decisions', permission: 'decisions.read', icon: 'decisions' },
      { href: '/resilience', key: 'resilience', permission: 'grid.read', icon: 'resilience' },
      { href: '/discovery', key: 'discovery', permission: 'discovery.read', icon: 'radar' },
      { href: '/risk-hunter', key: 'riskHunter', permission: 'riskhunter.read', icon: 'radar' },
      { href: '/evidence', key: 'evidence', permission: 'evidence.read', icon: 'knowledge' },
      { href: '/proofs', key: 'proofs', permission: 'proof.read', icon: 'trust' },
      { href: '/reports', key: 'reports', permission: 'grid.read', icon: 'reports' },
    ],
  },
  {
    // The honesty surfaces. Grouped together on purpose: they answer "should I believe
    // any of the above", and burying them among the analysis pages would make them look
    // like just another feature rather than the check on the others.
    key: 'assurance',
    items: [
      { href: '/verification', key: 'verification', permission: 'verification.read', icon: 'trust' },
      { href: '/trust', key: 'trust', permission: 'models.read', icon: 'trust' },
      { href: '/data-health', key: 'dataHealth', permission: 'datahealth.read', icon: 'model' },
      { href: '/ai-governance', key: 'aiGovernance', permission: 'governance.read', icon: 'settings' },
      { href: '/learning', key: 'learning', permission: 'learning.read', icon: 'dna' },
    ],
  },
  {
    key: 'assets',
    items: [
      { href: '/assets', key: 'assets', permission: 'assets.read', icon: 'asset' },
      { href: '/maintenance', key: 'maintenance', permission: 'maintenance.read', icon: 'maintenance' },
      { href: '/models', key: 'models', permission: 'models.read', icon: 'model' },
    ],
  },
  {
    key: 'administration',
    items: [
      { href: '/integrations', key: 'integrations', permission: 'integrations.read', icon: 'integration' },
      { href: '/audit', key: 'audit', permission: 'audit.read', icon: 'audit' },
      { href: '/settings', key: 'settings', permission: 'settings.read', icon: 'settings' },
      { href: '/users', key: 'users', permission: 'users.read', icon: 'users' },
      { href: '/roles', key: 'roles', permission: 'roles.read', icon: 'roles' },
    ],
  },
]

/** Presentation entry points, kept out of the main list so they stay prominent. */
export const SPECIAL_ITEMS: NavItem[] = [
  { href: '/hackathon', key: 'hackathon', permission: 'presentation.use', icon: 'hackathon' },
  { href: '/demo', key: 'demo', permission: 'demo.run', icon: 'demo' },
  { href: '/control-room', key: 'controlRoom', permission: 'presentation.use', icon: 'screen' },
]

export function visibleGroups(permissions: Set<string>): NavGroup[] {
  return NAV_GROUPS.map((group) => ({
    ...group,
    items: group.items.filter((item) => permissions.has(item.permission)),
  })).filter((group) => group.items.length > 0)
}

export function visibleSpecials(permissions: Set<string>): NavItem[] {
  return SPECIAL_ITEMS.filter((item) => permissions.has(item.permission))
}
