import type { Role } from "@prisma/client";
import {
  LayoutDashboard,
  BookOpen,
  Users,
  ClipboardList,
  CalendarCheck,
  GraduationCap,
  Code,
  Bot,
  Sparkles,
  Trophy,
  TrendingUp,
  Megaphone,
  Bell,
  Settings,
  School,
  ClipboardCheck,
  Award,
  ChartColumn,
  FileText,
  ScrollText,
} from "lucide-react";
import type { LucideIcon } from "lucide-react";

/**
 * Navigation model.
 *
 * Single source of truth for the sidebar. Each item declares the minimum roles
 * that may see it; the shell filters by the signed-in user's role, and the same
 * rules are re-enforced server-side by page guards.
 */

export interface NavItem {
  label: string;
  href: string;
  icon: LucideIcon;
  roles: Role[];
  /** Match child routes as active too. */
  exact?: boolean;
}

export interface NavSection {
  title: string;
  items: NavItem[];
}

const ALL: Role[] = ["ADMIN", "TEACHER", "STUDENT"];
const STAFF: Role[] = ["ADMIN", "TEACHER"];
const TEACHING: Role[] = ["ADMIN", "TEACHER", "STUDENT"];

export const NAV_SECTIONS: NavSection[] = [
  {
    title: "Ringkasan",
    items: [
      { label: "Dasbor", href: "/dashboard", icon: LayoutDashboard, roles: ALL, exact: true },
      { label: "Progres Belajar", href: "/dashboard/progress", icon: TrendingUp, roles: ALL },
      { label: "Pencapaian", href: "/dashboard/achievements", icon: Trophy, roles: ALL },
      { label: "Sertifikat", href: "/dashboard/certificates", icon: ScrollText, roles: ALL },
    ],
  },
  {
    title: "Pembelajaran",
    items: [
      { label: "Kursus", href: "/dashboard/courses", icon: BookOpen, roles: TEACHING },
      { label: "Kelas", href: "/dashboard/classes", icon: School, roles: TEACHING },
      { label: "Absensi", href: "/dashboard/attendance", icon: CalendarCheck, roles: TEACHING },
    ],
  },
  {
    title: "Penilaian",
    items: [
      { label: "Tugas", href: "/dashboard/assignments", icon: ClipboardList, roles: ALL },
      { label: "Penilaian", href: "/dashboard/grading", icon: ClipboardCheck, roles: STAFF },
      { label: "Buku Nilai", href: "/dashboard/gradebook", icon: ChartColumn, roles: TEACHING },
    ],
  },
  {
    title: "Laboratorium",
    items: [
      { label: "Proyek Coding", href: "/dashboard/coding", icon: Code, roles: ALL },
      { label: "Simulator Robot", href: "/dashboard/lab", icon: Bot, roles: ALL },
      { label: "Keterampilan", href: "/dashboard/skills", icon: Sparkles, roles: ALL },
    ],
  },
  {
    title: "Komunikasi",
    items: [
      { label: "Pengumuman", href: "/dashboard/announcements", icon: Megaphone, roles: ALL },
      { label: "Notifikasi", href: "/dashboard/notifications", icon: Bell, roles: ALL },
    ],
  },
  {
    title: "Administrasi",
    items: [
      { label: "Pengguna", href: "/dashboard/users", icon: Users, roles: ["ADMIN"] },
      { label: "Asisten AI", href: "/dashboard/ai", icon: Bot, roles: ["ADMIN"] },
      { label: "Kursus Saya", href: "/dashboard/teaching", icon: GraduationCap, roles: STAFF },
      { label: "Laporan", href: "/dashboard/reports", icon: FileText, roles: STAFF },
      { label: "Log Aktivitas", href: "/dashboard/audit", icon: Award, roles: ["ADMIN"] },
      { label: "Pengaturan", href: "/dashboard/settings", icon: Settings, roles: ALL },
    ],
  },
];

/** Filter the nav model down to the sections/items a role may access. */
export function navForRole(role: Role): NavSection[] {
  return NAV_SECTIONS.map((section) => ({
    ...section,
    items: section.items.filter((item) => item.roles.includes(role)),
  })).filter((section) => section.items.length > 0);
}

/** Longest-prefix match so `/dashboard/courses/123` highlights "Kursus". */
export function isNavItemActive(pathname: string, item: NavItem): boolean {
  if (item.exact) return pathname === item.href;
  return pathname === item.href || pathname.startsWith(`${item.href}/`);
}
