import {
  BookOpen,
  Calendar,
  CalendarRange,
  LayoutDashboard,
  Settings,
  Users,
  Shield,
  FileQuestion,
  ClipboardList,
  Library,
  PenTool,
  Briefcase,
  ScrollText,
  Megaphone,
  GraduationCap,
  Mail,
  CalendarCheck,
  CalendarClock,
  Stethoscope,
  BookMarked,
  BookCopy,
  Files,
  BookX,
  Sparkles,
} 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 = {
  /** i18n key path relative to the "nav" namespace, e.g. "student.dashboard" */
  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: "common.announcements",
    icon: Megaphone,
    href: "/announcements",
    permission: Permissions.ANNOUNCEMENT_READ,
  },
  {
    title: "common.messages",
    icon: Mail,
    href: "/messages",
    permission: Permissions.MESSAGE_READ,
  },
  {
    title: "common.aiSettings",
    icon: Sparkles,
    href: "/admin/ai-settings",
    permission: Permissions.AI_CHAT,
  },
]

export const NAV_CONFIG: Partial<Record<Role, NavItem[]>> = {
  admin: [
    {
      title: "admin.dashboard",
      icon: LayoutDashboard,
      href: "/admin/dashboard",
      permission: Permissions.SCHOOL_MANAGE,
    },
    {
      title: "admin.schoolManagement",
      icon: Shield,
      href: "/admin/school",
      permission: Permissions.SCHOOL_MANAGE,
      items: [
        { title: "admin.schools", href: "/admin/school/schools" },
        { title: "admin.grades", href: "/admin/school/grades" },
        { title: "admin.gradeInsights", href: "/admin/school/grades/insights" },
        { title: "admin.departments", href: "/admin/school/departments" },
        { title: "admin.classes", href: "/admin/school/classes" },
        { title: "admin.academicYear", href: "/admin/school/academic-year" },
      ]
    },
    {
      title: "admin.users",
      icon: Users,
      href: "/admin/users",
      permission: Permissions.USER_MANAGE,
      items: [
        { title: "admin.userList", href: "/admin/users" },
        { title: "admin.importUsers", href: "/admin/users/import", permission: Permissions.USER_MANAGE },
        { title: "admin.roles", href: "/admin/roles", permission: Permissions.ROLE_READ },
        { title: "admin.permissions", href: "/admin/permissions", permission: Permissions.PERMISSION_READ },
      ]
    },
    {
      title: "admin.teaching",
      icon: BookCopy,
      href: "/admin/course-plans",
      permission: Permissions.COURSE_PLAN_MANAGE,
      items: [
        { title: "admin.coursePlans", href: "/admin/course-plans", permission: Permissions.COURSE_PLAN_MANAGE },
        { title: "admin.electives", href: "/admin/elective", permission: Permissions.ELECTIVE_MANAGE },
      ]
    },
    {
      title: "admin.scheduling",
      icon: CalendarClock,
      href: "/admin/scheduling/rules",
      permission: Permissions.SCHEDULE_ADJUST,
      items: [
        { title: "admin.rules", href: "/admin/scheduling/rules", permission: Permissions.SCHEDULE_ADJUST },
        { title: "admin.autoSchedule", href: "/admin/scheduling/auto", permission: Permissions.SCHEDULE_AUTO },
        { title: "admin.changeRequests", href: "/admin/scheduling/changes", permission: Permissions.SCHEDULE_ADJUST },
      ]
    },
    {
      title: "admin.attendance",
      icon: CalendarCheck,
      href: "/admin/attendance",
      permission: Permissions.ATTENDANCE_READ,
    },
    {
      title: "admin.announcements",
      icon: Megaphone,
      href: "/admin/announcements",
      permission: Permissions.ANNOUNCEMENT_MANAGE,
    },
    {
      title: "admin.files",
      icon: Files,
      href: "/admin/files",
      permission: Permissions.FILE_READ,
    },
    {
      title: "admin.errorBook",
      icon: BookX,
      href: "/admin/error-book",
      permission: Permissions.ERROR_BOOK_ANALYTICS_READ,
    },
    {
      title: "admin.lessonPlans",
      icon: PenTool,
      href: "/admin/lesson-plans",
      permission: Permissions.LESSON_PLAN_READ,
    },
    {
      title: "admin.auditLogs",
      icon: ScrollText,
      href: "/admin/audit-logs",
      permission: Permissions.AUDIT_LOG_READ,
      items: [
        { title: "admin.operationLogs", href: "/admin/audit-logs" },
        { title: "admin.loginLogs", href: "/admin/audit-logs/login-logs" },
        { title: "admin.dataChanges", href: "/admin/audit-logs/data-changes" },
      ]
    },
    ...COMMON_NAV_ITEMS,
    {
      title: "admin.settings",
      icon: Settings,
      href: "/admin/settings",
      permission: Permissions.SETTINGS_ADMIN,
    },
  ],
  teacher: [
    {
      title: "teacher.dashboard",
      icon: LayoutDashboard,
      href: "/teacher/dashboard",
    },
    {
      title: "teacher.textbooks",
      icon: Library,
      href: "/teacher/textbooks",
      permission: Permissions.TEXTBOOK_READ,
    },
    {
      title: "teacher.exams",
      icon: FileQuestion,
      href: "/teacher/exams",
      permission: Permissions.EXAM_CREATE,
      items: [
        { title: "teacher.allExams", href: "/teacher/exams/all" },
        { title: "teacher.createExam", href: "/teacher/exams/create", permission: Permissions.EXAM_CREATE },
      ]
    },
    {
      title: "teacher.homework",
      icon: PenTool,
      href: "/teacher/homework",
      permission: Permissions.HOMEWORK_CREATE,
      items: [
        { title: "teacher.homeworkList", href: "/teacher/homework/assignments" },
        { title: "teacher.submissions", href: "/teacher/homework/submissions" },
      ]
    },
    {
      title: "teacher.grades",
      icon: GraduationCap,
      href: "/teacher/grades",
      permission: Permissions.GRADE_RECORD_MANAGE,
      items: [
        { title: "teacher.allGrades", href: "/teacher/grades" },
        { title: "teacher.batchEntry", href: "/teacher/grades/entry", permission: Permissions.GRADE_RECORD_MANAGE },
        { title: "teacher.gradeStats", href: "/teacher/grades/stats", permission: Permissions.GRADE_RECORD_READ },
        { title: "teacher.gradeAnalytics", href: "/teacher/grades/analytics", permission: Permissions.GRADE_RECORD_READ },
      ]
    },
    {
      title: "teacher.questions",
      icon: ClipboardList,
      href: "/teacher/questions",
      permission: Permissions.QUESTION_READ,
    },
    {
      title: "teacher.classManagement",
      icon: Users,
      href: "/teacher/classes",
      permission: Permissions.CLASS_READ,
      items: [
        { title: "teacher.myClasses", href: "/teacher/classes/my" },
        { title: "teacher.students", href: "/teacher/classes/students" },
        { title: "teacher.schedule", href: "/teacher/classes/schedule", permission: Permissions.CLASS_SCHEDULE },
      ]
    },
    {
      title: "teacher.coursePlans",
      icon: CalendarRange,
      href: "/teacher/course-plans",
      permission: Permissions.COURSE_PLAN_READ,
    },
    {
      title: "teacher.lessonPlans",
      icon: PenTool,
      href: "/teacher/lesson-plans",
      permission: Permissions.LESSON_PLAN_READ,
    },
    {
      title: "teacher.attendance",
      icon: CalendarCheck,
      href: "/teacher/attendance",
      permission: Permissions.ATTENDANCE_MANAGE,
      items: [
        { title: "teacher.attendanceRecords", href: "/teacher/attendance" },
        { title: "teacher.attendanceEntry", href: "/teacher/attendance/sheet", permission: Permissions.ATTENDANCE_MANAGE },
        { title: "teacher.attendanceStats", href: "/teacher/attendance/stats", permission: Permissions.ATTENDANCE_READ },
      ]
    },
    {
      title: "teacher.scheduleChanges",
      icon: CalendarClock,
      href: "/teacher/schedule-changes",
      permission: Permissions.SCHEDULE_ADJUST,
    },
    {
      title: "teacher.diagnostic",
      icon: Stethoscope,
      href: "/teacher/diagnostic",
      permission: Permissions.DIAGNOSTIC_READ,
    },
    {
      title: "teacher.errorBook",
      icon: BookX,
      href: "/teacher/error-book",
      permission: Permissions.ERROR_BOOK_ANALYTICS_READ,
    },
    {
      title: "teacher.electives",
      icon: BookMarked,
      href: "/teacher/elective",
      permission: Permissions.ELECTIVE_MANAGE,
    },
    {
      title: "teacher.gradeManagement",
      icon: Briefcase,
      href: "/management",
      permission: Permissions.GRADE_MANAGE,
      items: [
        { title: "teacher.gradeClasses", href: "/management/grade/classes" },
        { title: "teacher.gradeDashboard", href: "/management/grade/dashboard" },
        { title: "teacher.gradeInsights", href: "/management/grade/insights" },
      ]
    },
    ...COMMON_NAV_ITEMS,
  ],
  grade_head: [
    {
      title: "teacher.dashboard",
      icon: LayoutDashboard,
      href: "/management",
      permission: Permissions.GRADE_MANAGE,
    },
    {
      title: "teacher.gradeManagement",
      icon: Briefcase,
      href: "/management",
      permission: Permissions.GRADE_MANAGE,
      items: [
        { title: "teacher.gradeClasses", href: "/management/grade/classes" },
        { title: "teacher.gradeDashboard", href: "/management/grade/dashboard" },
        { title: "teacher.gradeInsights", href: "/management/grade/insights" },
      ]
    },
    {
      title: "teacher.attendance",
      icon: CalendarCheck,
      href: "/teacher/attendance",
      permission: Permissions.ATTENDANCE_READ,
      items: [
        { title: "teacher.attendanceRecords", href: "/teacher/attendance" },
        { title: "teacher.attendanceStats", href: "/teacher/attendance/stats", permission: Permissions.ATTENDANCE_READ },
      ]
    },
    {
      title: "teacher.grades",
      icon: GraduationCap,
      href: "/teacher/grades",
      permission: Permissions.GRADE_RECORD_READ,
      items: [
        { title: "teacher.gradeStats", href: "/teacher/grades/stats", permission: Permissions.GRADE_RECORD_READ },
        { title: "teacher.gradeAnalytics", href: "/teacher/grades/analytics", permission: Permissions.GRADE_RECORD_READ },
      ]
    },
    {
      title: "teacher.errorBook",
      icon: BookX,
      href: "/teacher/error-book",
      permission: Permissions.ERROR_BOOK_ANALYTICS_READ,
    },
    ...COMMON_NAV_ITEMS,
  ],
  teaching_head: [
    {
      title: "teacher.dashboard",
      icon: LayoutDashboard,
      href: "/management",
      permission: Permissions.GRADE_MANAGE,
    },
    {
      title: "teacher.gradeManagement",
      icon: Briefcase,
      href: "/management",
      permission: Permissions.GRADE_MANAGE,
      items: [
        { title: "teacher.gradeClasses", href: "/management/grade/classes" },
        { title: "teacher.gradeDashboard", href: "/management/grade/dashboard" },
        { title: "teacher.gradeInsights", href: "/management/grade/insights" },
      ]
    },
    {
      title: "teacher.attendance",
      icon: CalendarCheck,
      href: "/teacher/attendance",
      permission: Permissions.ATTENDANCE_READ,
      items: [
        { title: "teacher.attendanceRecords", href: "/teacher/attendance" },
        { title: "teacher.attendanceStats", href: "/teacher/attendance/stats", permission: Permissions.ATTENDANCE_READ },
      ]
    },
    {
      title: "teacher.grades",
      icon: GraduationCap,
      href: "/teacher/grades",
      permission: Permissions.GRADE_RECORD_READ,
      items: [
        { title: "teacher.gradeStats", href: "/teacher/grades/stats", permission: Permissions.GRADE_RECORD_READ },
        { title: "teacher.gradeAnalytics", href: "/teacher/grades/analytics", permission: Permissions.GRADE_RECORD_READ },
      ]
    },
    {
      title: "teacher.errorBook",
      icon: BookX,
      href: "/teacher/error-book",
      permission: Permissions.ERROR_BOOK_ANALYTICS_READ,
    },
    ...COMMON_NAV_ITEMS,
  ],
  student: [
    {
      title: "student.dashboard",
      icon: LayoutDashboard,
      href: "/student/dashboard",
    },
    {
      title: "student.myLearning",
      icon: BookOpen,
      href: "/student/learning",
      permission: Permissions.HOMEWORK_SUBMIT,
      items: [
        { title: "student.courses", href: "/student/learning/courses" },
        { title: "student.assignments", href: "/student/learning/assignments", permission: Permissions.HOMEWORK_SUBMIT },
        { title: "student.textbooks", href: "/student/learning/textbooks", permission: Permissions.TEXTBOOK_READ },
      ]
    },
    {
      title: "student.schedule",
      icon: Calendar,
      href: "/student/schedule",
      permission: Permissions.CLASS_SCHEDULE,
    },
    {
      title: "student.myGrades",
      icon: GraduationCap,
      href: "/student/grades",
      permission: Permissions.GRADE_RECORD_READ,
    },
    {
      title: "student.lessonPlans",
      icon: PenTool,
      href: "/student/lesson-plans",
      permission: Permissions.LESSON_PLAN_READ,
    },
    {
      title: "student.attendance",
      icon: CalendarCheck,
      href: "/student/attendance",
      permission: Permissions.ATTENDANCE_READ,
    },
    {
      title: "student.diagnostic",
      icon: Stethoscope,
      href: "/student/diagnostic",
      permission: Permissions.DIAGNOSTIC_READ,
    },
    {
      title: "student.errorBook",
      icon: BookX,
      href: "/student/error-book",
      permission: Permissions.ERROR_BOOK_READ,
    },
    {
      title: "student.electives",
      icon: BookMarked,
      href: "/student/elective",
      permission: Permissions.ELECTIVE_SELECT,
    },
    ...COMMON_NAV_ITEMS,
  ],
  parent: [
    {
      title: "parent.dashboard",
      icon: LayoutDashboard,
      href: "/parent/dashboard",
    },
    {
      title: "parent.grades",
      icon: GraduationCap,
      href: "/parent/grades",
      permission: Permissions.GRADE_RECORD_READ,
    },
    {
      title: "parent.lessonPlans",
      icon: PenTool,
      href: "/parent/lesson-plans",
      permission: Permissions.LESSON_PLAN_READ,
    },
    {
      title: "parent.attendance",
      icon: CalendarCheck,
      href: "/parent/attendance",
      permission: Permissions.ATTENDANCE_READ,
    },
    {
      title: "parent.errorBook",
      icon: BookX,
      href: "/parent/error-book",
      permission: Permissions.ERROR_BOOK_READ,
    },
    {
      title: "parent.leaveRequest",
      icon: CalendarRange,
      href: "/parent/leave",
    },
    ...COMMON_NAV_ITEMS,
  ]
}
