import type { LinkProps } from "@tanstack/react-router";
import {
  Activity,
  BarChart3,
  Bell,
  BookOpen,
  Building2,
  CalendarCheck,
  ClipboardCheck,
  ClipboardList,
  Copy,
  DatabaseBackup,
  FileBarChart,
  FileClock,
  Gauge,
  GraduationCap,
  Home,
  Mail,
  MapPinned,
  MessageSquareWarning,
  PhoneCall,
  ScrollText,
  Settings,
  ShieldCheck,
  SlidersHorizontal,
  Stethoscope,
  Target,
  UserCheck,
  Users,
  type LucideIcon,
} from "lucide-react";
import type { RoleCode } from "@/api/types";
import { isTpRole, P } from "@/lib/roles";

export interface NavItem {
  label: string;
  to: NonNullable<LinkProps["to"]>;
  icon: LucideIcon;
  perm?: string;
  exact?: boolean;
}
export interface NavGroup {
  label: string;
  items: NavItem[];
}

/** Navigation per audience (frontend prompt §3.2). Items are hidden unless the user holds `perm`. */
export function navFor(role: RoleCode): NavGroup[] {
  if (isTpRole(role))
    return [
      {
        label: "Home",
        items: [
          { label: "My dashboard", to: "/p/$pid/home", icon: Home },
          { label: "Notifications", to: "/p/$pid/notifications", icon: Bell },
        ],
      },
      {
        label: "My programme",
        items: [
          {
            label: "Trainings & classes",
            to: "/p/$pid/trainings",
            icon: GraduationCap,
            perm: P.trainingView,
          },
          { label: "Trainees", to: "/p/$pid/trainees", icon: Users, perm: P.traineeView },
          {
            label: "Attendance",
            to: "/p/$pid/attendance",
            icon: CalendarCheck,
            perm: P.attendanceView,
          },
          {
            label: "Outcome reports",
            to: "/p/$pid/outcomes",
            icon: ClipboardCheck,
            perm: P.outcomeView,
          },
          {
            label: "Activities & evidence",
            to: "/p/$pid/obligations",
            icon: Activity,
            perm: P.activityView,
          },
          {
            label: "Grievances",
            to: "/p/$pid/grievances",
            icon: MessageSquareWarning,
            perm: P.grievanceView,
          },
        ],
      },
      {
        label: "Results",
        items: [
          {
            label: "Data health",
            to: "/p/$pid/data-health",
            icon: Stethoscope,
            perm: P.datahealthView,
          },
        ],
      },
      {
        label: "Organisation",
        items: [
          {
            label: "Provider profile",
            to: "/p/$pid/my-provider",
            icon: Building2,
            perm: P.providerView,
          },
          {
            label: "Breakdowns",
            to: "/p/$pid/breakdowns",
            icon: FileClock,
            perm: P.breakdownRequest,
          },
        ],
      },
    ];
  if (role === "verification_enumerator")
    return [
      {
        label: "Verification",
        items: [
          { label: "My work", to: "/p/$pid/verification/my-work", icon: PhoneCall },
          { label: "Notifications", to: "/p/$pid/notifications", icon: Bell },
        ],
      },
    ];
  if (role === "verification_supervisor" || role === "data_quality_officer")
    return [
      {
        label: "Verification",
        items: [
          { label: "Runs", to: "/p/$pid/verification", icon: ShieldCheck, exact: true },
          {
            label: "Review queue",
            to: "/p/$pid/verification/review",
            icon: ClipboardList,
            perm: P.verRecord,
          },
          {
            label: "Plans",
            to: "/p/$pid/verification/plans",
            icon: SlidersHorizontal,
            perm: P.verPlan,
          },
          { label: "Results", to: "/p/$pid/verification/results", icon: BarChart3 },
          { label: "Notifications", to: "/p/$pid/notifications", icon: Bell },
        ],
      },
      {
        label: "Reference",
        items: [
          {
            label: "Programme dashboard",
            to: "/p/$pid/dashboard",
            icon: Gauge,
            perm: P.indicatorView,
          },
          { label: "Indicators", to: "/p/$pid/indicators", icon: BookOpen, perm: P.indicatorView },
          { label: "Duplicates", to: "/p/$pid/duplicates", icon: Copy, perm: P.duplicateReview },
          {
            label: "Data health",
            to: "/p/$pid/data-health",
            icon: Stethoscope,
            perm: P.datahealthView,
          },
          { label: "Audit log", to: "/p/$pid/admin/audit", icon: ScrollText, perm: P.auditView },
        ],
      },
    ];
  return [
    {
      label: "Overview",
      items: [
        {
          label: "Programme dashboard",
          to: "/p/$pid/dashboard",
          icon: Gauge,
          perm: P.indicatorView,
        },
        { label: "Notifications", to: "/p/$pid/notifications", icon: Bell },
      ],
    },
    {
      label: "Delivery",
      items: [
        {
          label: "Training providers",
          to: "/p/$pid/providers",
          icon: Building2,
          perm: P.providerView,
        },
        {
          label: "Trainings & classes",
          to: "/p/$pid/trainings",
          icon: GraduationCap,
          perm: P.trainingView,
        },
        { label: "Trainees", to: "/p/$pid/trainees", icon: Users, perm: P.traineeView },
        {
          label: "Attendance",
          to: "/p/$pid/attendance",
          icon: CalendarCheck,
          perm: P.attendanceView,
        },
        { label: "Outcomes", to: "/p/$pid/outcomes", icon: ClipboardCheck, perm: P.outcomeView },
        { label: "Activities", to: "/p/$pid/obligations", icon: Activity, perm: P.activityView },
        {
          label: "Breakdowns",
          to: "/p/$pid/breakdowns",
          icon: FileClock,
          perm: P.breakdownApprove,
        },
      ],
    },
    {
      label: "Results",
      items: [
        { label: "Indicators", to: "/p/$pid/indicators", icon: BookOpen, perm: P.indicatorView },
        { label: "Targets", to: "/p/$pid/targets", icon: Target, perm: P.targetManage },
        { label: "KPI report", to: "/p/$pid/kpi-report", icon: BarChart3, perm: P.indicatorView },
        {
          label: "Verification",
          to: "/p/$pid/verification",
          icon: ShieldCheck,
          perm: P.verSubjects,
        },
        {
          label: "Verification results",
          to: "/p/$pid/verification/results",
          icon: ClipboardList,
          perm: P.verAggregate,
        },
        {
          label: "Grievances",
          to: "/p/$pid/grievances",
          icon: MessageSquareWarning,
          perm: P.grievanceView,
        },
      ],
    },
    {
      label: "Quality",
      items: [
        { label: "Duplicates", to: "/p/$pid/duplicates", icon: Copy, perm: P.duplicateReview },
        {
          label: "Data health",
          to: "/p/$pid/data-health",
          icon: Stethoscope,
          perm: P.datahealthView,
        },
      ],
    },
    {
      label: "Reports",
      items: [
        { label: "Report centre", to: "/p/$pid/reports", icon: FileBarChart, perm: P.reportView },
      ],
    },
    {
      label: "Admin",
      items: [
        { label: "Users & access", to: "/p/$pid/admin/users", icon: Users, perm: P.userManage },
        {
          label: "Project settings",
          to: "/p/$pid/admin/settings",
          icon: Settings,
          perm: P.settingsManage,
        },
        {
          label: "Master data",
          to: "/p/$pid/admin/master-data",
          icon: MapPinned,
          perm: P.masterdataManage,
        },
        { label: "Mail & reminders", to: "/p/$pid/admin/mail", icon: Mail, perm: P.settingsManage },
        {
          label: "Backups",
          to: "/p/$pid/admin/backups",
          icon: DatabaseBackup,
          perm: P.settingsManage,
        },
        { label: "Audit log", to: "/p/$pid/admin/audit", icon: ScrollText, perm: P.auditView },
      ],
    },
  ];
}
