import {
  BookOpen,
  Calendar,
  CalendarRange,
  LayoutDashboard,
  Settings,
  Users,
  Shield,
  FileQuestion,
  ClipboardList,
  Library,
  PenTool,
  Briefcase,
  ScrollText,
  Megaphone,
  GraduationCap,
  Mail,
  CalendarCheck,
  CalendarClock,
  Stethoscope,
  BookMarked,
  BookCopy,
  Files,
} from "lucide-react"
import type { LucideIcon } from "lucide-react"
import { Permissions } from "@/shared/types/permissions"
import type { Permission, Role } from "@/shared/types/permissions"

export type { Role }

export type NavItem = {
  title: string
  icon: LucideIcon
  href: string
  permission?: Permission
  items?: { title: string; href: string; permission?: Permission }[]
}

/**
 * 公共导航项：所有已登录用户均可访问的功能模块。
 * 通过权限点（permission）控制可见性，新增角色只需在 NAV_CONFIG 中引用这些公共项，
 * 无需复制粘贴。符合"配置驱动设计"和"严禁 role === 'xxx' 硬编码"的规则。
 */
const COMMON_NAV_ITEMS: NavItem[] = [
  {
    title: "Announcements",
    icon: Megaphone,
    href: "/announcements",
    permission: Permissions.ANNOUNCEMENT_READ,
  },
  {
    title: "Messages",
    icon: Mail,
    href: "/messages",
    permission: Permissions.MESSAGE_READ,
  },
]

export const NAV_CONFIG: Partial<Record<Role, NavItem[]>> = {
  admin: [
    {
      title: "Dashboard",
      icon: LayoutDashboard,
      href: "/admin/dashboard",
      permission: Permissions.SCHOOL_MANAGE,
    },
    {
      title: "School Management",
      icon: Shield,
      href: "/admin/school",
      permission: Permissions.SCHOOL_MANAGE,
      items: [
        { title: "Schools", href: "/admin/school/schools" },
        { title: "Grades", href: "/admin/school/grades" },
        { title: "Grade Insights", href: "/admin/school/grades/insights" },
        { title: "Departments", href: "/admin/school/departments" },
        { title: "Classes", href: "/admin/school/classes" },
        { title: "Academic Year", href: "/admin/school/academic-year" },
      ]
    },
    {
      title: "Users",
      icon: Users,
      href: "/admin/users",
      permission: Permissions.USER_MANAGE,
      items: [
        { title: "User List", href: "/admin/users" },
        { title: "Import Users", href: "/admin/users/import", permission: Permissions.USER_MANAGE },
      ]
    },
    {
      title: "Teaching",
      icon: BookCopy,
      href: "/admin/course-plans",
      permission: Permissions.COURSE_PLAN_MANAGE,
      items: [
        { title: "Course Plans", href: "/admin/course-plans", permission: Permissions.COURSE_PLAN_MANAGE },
        { title: "Electives", href: "/admin/elective", permission: Permissions.ELECTIVE_MANAGE },
      ]
    },
    {
      title: "Scheduling",
      icon: CalendarClock,
      href: "/admin/scheduling/rules",
      permission: Permissions.SCHEDULE_ADJUST,
      items: [
        { title: "Rules", href: "/admin/scheduling/rules", permission: Permissions.SCHEDULE_ADJUST },
        { title: "Auto Schedule", href: "/admin/scheduling/auto", permission: Permissions.SCHEDULE_AUTO },
        { title: "Change Requests", href: "/admin/scheduling/changes", permission: Permissions.SCHEDULE_ADJUST },
      ]
    },
    {
      title: "Attendance",
      icon: CalendarCheck,
      href: "/admin/attendance",
      permission: Permissions.ATTENDANCE_READ,
    },
    {
      title: "Announcements",
      icon: Megaphone,
      href: "/admin/announcements",
      permission: Permissions.ANNOUNCEMENT_MANAGE,
    },
    {
      title: "文件管理",
      icon: Files,
      href: "/admin/files",
      permission: Permissions.FILE_READ,
    },
    {
      title: "Audit Logs",
      icon: ScrollText,
      href: "/admin/audit-logs",
      permission: Permissions.AUDIT_LOG_READ,
      items: [
        { title: "Operation Logs", href: "/admin/audit-logs" },
        { title: "Login Logs", href: "/admin/audit-logs/login-logs" },
        { title: "Data Changes", href: "/admin/audit-logs/data-changes" },
      ]
    },
    ...COMMON_NAV_ITEMS,
    {
      title: "Settings",
      icon: Settings,
      href: "/admin/settings",
      permission: Permissions.SETTINGS_ADMIN,
    },
  ],
  teacher: [
    {
      title: "仪表盘",
      icon: LayoutDashboard,
      href: "/teacher/dashboard",
    },
    {
      title: "教材",
      icon: Library,
      href: "/teacher/textbooks",
      permission: Permissions.TEXTBOOK_READ,
    },
    {
      title: "考试",
      icon: FileQuestion,
      href: "/teacher/exams",
      permission: Permissions.EXAM_CREATE,
      items: [
        { title: "全部考试", href: "/teacher/exams/all" },
        { title: "创建考试", href: "/teacher/exams/create", permission: Permissions.EXAM_CREATE },
      ]
    },
    {
      title: "作业",
      icon: PenTool,
      href: "/teacher/homework",
      permission: Permissions.HOMEWORK_CREATE,
      items: [
        { title: "作业列表", href: "/teacher/homework/assignments" },
        { title: "提交记录", href: "/teacher/homework/submissions" },
      ]
    },
    {
      title: "成绩",
      icon: GraduationCap,
      href: "/teacher/grades",
      permission: Permissions.GRADE_RECORD_MANAGE,
      items: [
        { title: "全部成绩", href: "/teacher/grades" },
        { title: "批量录入", href: "/teacher/grades/entry", permission: Permissions.GRADE_RECORD_MANAGE },
        { title: "成绩统计", href: "/teacher/grades/stats", permission: Permissions.GRADE_RECORD_READ },
        { title: "成绩分析", href: "/teacher/grades/analytics", permission: Permissions.GRADE_RECORD_READ },
      ]
    },
    {
      title: "题库",
      icon: ClipboardList,
      href: "/teacher/questions",
      permission: Permissions.QUESTION_READ,
    },
    {
      title: "班级管理",
      icon: Users,
      href: "/teacher/classes",
      permission: Permissions.CLASS_READ,
      items: [
        { title: "我的班级", href: "/teacher/classes/my" },
        { title: "学生", href: "/teacher/classes/students" },
        { title: "课表", href: "/teacher/classes/schedule", permission: Permissions.CLASS_SCHEDULE },
      ]
    },
    {
      title: "课程计划",
      icon: CalendarRange,
      href: "/teacher/course-plans",
      permission: Permissions.COURSE_PLAN_READ,
    },
    {
      title: "我的备课",
      icon: PenTool,
      href: "/teacher/lesson-plans",
      permission: Permissions.LESSON_PLAN_READ,
    },
    {
      title: "考勤",
      icon: CalendarCheck,
      href: "/teacher/attendance",
      permission: Permissions.ATTENDANCE_MANAGE,
      items: [
        { title: "考勤记录", href: "/teacher/attendance" },
        { title: "录入考勤", href: "/teacher/attendance/sheet", permission: Permissions.ATTENDANCE_MANAGE },
        { title: "考勤统计", href: "/teacher/attendance/stats", permission: Permissions.ATTENDANCE_READ },
      ]
    },
    {
      title: "调课申请",
      icon: CalendarClock,
      href: "/teacher/schedule-changes",
      permission: Permissions.SCHEDULE_ADJUST,
    },
    {
      title: "学情诊断",
      icon: Stethoscope,
      href: "/teacher/diagnostic",
      permission: Permissions.DIAGNOSTIC_READ,
    },
    {
      title: "选修课",
      icon: BookMarked,
      href: "/teacher/elective",
      permission: Permissions.ELECTIVE_MANAGE,
    },
    {
      title: "年级管理",
      icon: Briefcase,
      href: "/management",
      permission: Permissions.GRADE_MANAGE,
      items: [
        { title: "年级班级", href: "/management/grade/classes" },
        { title: "年级洞察", href: "/management/grade/insights" },
      ]
    },
    ...COMMON_NAV_ITEMS,
  ],
  grade_head: [
    {
      title: "仪表盘",
      icon: LayoutDashboard,
      href: "/management",
      permission: Permissions.GRADE_MANAGE,
    },
    {
      title: "年级管理",
      icon: Briefcase,
      href: "/management",
      permission: Permissions.GRADE_MANAGE,
      items: [
        { title: "年级班级", href: "/management/grade/classes" },
        { title: "年级洞察", href: "/management/grade/insights" },
      ]
    },
    {
      title: "考勤",
      icon: CalendarCheck,
      href: "/teacher/attendance",
      permission: Permissions.ATTENDANCE_READ,
      items: [
        { title: "考勤记录", href: "/teacher/attendance" },
        { title: "考勤统计", href: "/teacher/attendance/stats", permission: Permissions.ATTENDANCE_READ },
      ]
    },
    {
      title: "成绩",
      icon: GraduationCap,
      href: "/teacher/grades",
      permission: Permissions.GRADE_RECORD_READ,
      items: [
        { title: "成绩统计", href: "/teacher/grades/stats", permission: Permissions.GRADE_RECORD_READ },
        { title: "成绩分析", href: "/teacher/grades/analytics", permission: Permissions.GRADE_RECORD_READ },
      ]
    },
    ...COMMON_NAV_ITEMS,
  ],
  teaching_head: [
    {
      title: "仪表盘",
      icon: LayoutDashboard,
      href: "/management",
      permission: Permissions.GRADE_MANAGE,
    },
    {
      title: "年级管理",
      icon: Briefcase,
      href: "/management",
      permission: Permissions.GRADE_MANAGE,
      items: [
        { title: "年级班级", href: "/management/grade/classes" },
        { title: "年级洞察", href: "/management/grade/insights" },
      ]
    },
    {
      title: "考勤",
      icon: CalendarCheck,
      href: "/teacher/attendance",
      permission: Permissions.ATTENDANCE_READ,
      items: [
        { title: "考勤记录", href: "/teacher/attendance" },
        { title: "考勤统计", href: "/teacher/attendance/stats", permission: Permissions.ATTENDANCE_READ },
      ]
    },
    {
      title: "成绩",
      icon: GraduationCap,
      href: "/teacher/grades",
      permission: Permissions.GRADE_RECORD_READ,
      items: [
        { title: "成绩统计", href: "/teacher/grades/stats", permission: Permissions.GRADE_RECORD_READ },
        { title: "成绩分析", href: "/teacher/grades/analytics", permission: Permissions.GRADE_RECORD_READ },
      ]
    },
    ...COMMON_NAV_ITEMS,
  ],
  student: [
    {
      title: "Dashboard",
      icon: LayoutDashboard,
      href: "/student/dashboard",
    },
    {
      title: "My Learning",
      icon: BookOpen,
      href: "/student/learning",
      permission: Permissions.HOMEWORK_SUBMIT,
      items: [
        { title: "Courses", href: "/student/learning/courses" },
        { title: "Assignments", href: "/student/learning/assignments", permission: Permissions.HOMEWORK_SUBMIT },
        { title: "Textbooks", href: "/student/learning/textbooks", permission: Permissions.TEXTBOOK_READ },
      ]
    },
    {
      title: "Schedule",
      icon: Calendar,
      href: "/student/schedule",
      permission: Permissions.CLASS_SCHEDULE,
    },
    {
      title: "My Grades",
      icon: GraduationCap,
      href: "/student/grades",
      permission: Permissions.GRADE_RECORD_READ,
    },
    {
      title: "Attendance",
      icon: CalendarCheck,
      href: "/student/attendance",
      permission: Permissions.ATTENDANCE_READ,
    },
    {
      title: "Diagnostic",
      icon: Stethoscope,
      href: "/student/diagnostic",
      permission: Permissions.DIAGNOSTIC_READ,
    },
    {
      title: "Electives",
      icon: BookMarked,
      href: "/student/elective",
      permission: Permissions.ELECTIVE_SELECT,
    },
    ...COMMON_NAV_ITEMS,
  ],
  parent: [
    {
      title: "Dashboard",
      icon: LayoutDashboard,
      href: "/parent/dashboard",
    },
    {
      title: "Grades",
      icon: GraduationCap,
      href: "/parent/grades",
      permission: Permissions.GRADE_RECORD_READ,
    },
    {
      title: "Attendance",
      icon: CalendarCheck,
      href: "/parent/attendance",
      permission: Permissions.ATTENDANCE_READ,
    },
    {
      title: "Leave Request",
      icon: CalendarRange,
      href: "/parent/leave",
    },
    ...COMMON_NAV_ITEMS,
  ]
}
