import { ReactNode } from "react";

export type ServiceItem = {
  id: string;
  label: string;
  href?: string;
  icon: ReactNode;
  phase?: "active" | "soon";
  roles?: string[];
  moduleKey?: string;
};

export type ServiceSection = {
  id: string;
  title: string;
  tone: "primary" | "secondary" | "slate";
  items: ServiceItem[];
};

function Icon({ children, className = "h-6 w-6" }: { children: ReactNode; className?: string }) {
  return (
    <svg viewBox="0 0 24 24" className={className} fill="currentColor" aria-hidden>
      {children}
    </svg>
  );
}

export type ServiceItemAccent = {
  tile: string;
  icon: string;
  iconHover: string;
  labelHover: string;
};

const I = {
  users: (
    <Icon>
      <path d="M16 11c1.66 0 2.99-1.34 2.99-3S17.66 5 16 5s-3 1.34-3 3 1.34 3 3 3zm-8 0c1.66 0 2.99-1.34 2.99-3S9.66 5 8 5 5 6.34 5 8s1.34 3 3 3zm0 2c-2.33 0-7 1.17-7 3.5V19h14v-2.5c0-2.33-4.67-3.5-7-3.5zm8 0c-.29 0-.62.02-.97.05 1.16.84 1.97 1.97 1.97 3.45V19h6v-2.5c0-2.33-4.67-3.5-7-3.5z" />
    </Icon>
  ),
  students: (
    <Icon>
      <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z" />
    </Icon>
  ),
  roles: (
    <Icon>
      <path d="M12 1 3 5v6c0 5.55 3.84 10.74 9 12 5.16-1.26 9-6.45 9-12V5l-9-4zm0 10.99h7c-.53 4.12-3.28 7.79-7 8.94V12H5V6.3l7-3.11v8.8z" />
    </Icon>
  ),
  backpack: (
    <Icon>
      <path d="M20 6h-3V4a2 2 0 0 0-2-2H9a2 2 0 0 0-2 2v2H4a2 2 0 0 0-2 2v11a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2zM9 4h6v2H9V4zm11 15H4V8h16v11z" />
    </Icon>
  ),
  school: (
    <Icon>
      <path d="M12 3 1 9l4 2.18V17c0 .55.45 1 1 1h12c.55 0 1-.45 1-1v-5.82L23 9 12 3zm6 12H6v-4.73l6-3.27 6 3.27V15z" />
    </Icon>
  ),
  classroom: (
    <Icon>
      <path d="M18 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V4a2 2 0 0 0-2-2zM6 4h12v4H6V4zm0 6h12v10H6V10zm2 2v2h2v-2H8zm4 0v2h2v-2h-2z" />
    </Icon>
  ),
  chart: (
    <Icon>
      <path d="M5 9.2h3V19H5V9.2zM10.6 5h2.8v14h-2.8V5zm5.6 8H19v6h-2.8v-6z" />
    </Icon>
  ),
  grade: (
    <Icon>
      <path d="M12 17.27 18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21 12 17.27z" />
    </Icon>
  ),
  exam: (
    <Icon>
      <path d="M19 3H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V5a2 2 0 0 0-2-2zm-7 3h2v2h-2V6zm0 4h2v6h-2v-6zm-4-4h2v10H8V6zm8 0h2v10h-2V6z" />
    </Icon>
  ),
  attendance: (
    <Icon>
      <path d="M9 16.17 4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41L9 16.17z" />
    </Icon>
  ),
  clock: (
    <Icon>
      <path d="M11.99 2C6.47 2 2 6.48 2 12s4.47 10 9.99 10C17.52 22 22 17.52 22 12S17.52 2 11.99 2zM12 20c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8zm.5-13H11v6l5.25 3.15.75-1.23-4.5-2.67V7z" />
    </Icon>
  ),
  calendar: (
    <Icon>
      <path d="M19 4h-1V2h-2v2H8V2H6v2H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6a2 2 0 0 0-2-2zm0 16H5V10h14v10z" />
    </Icon>
  ),
  message: (
    <Icon>
      <path d="M20 2H4a2 2 0 0 0-2 2v18l4-4h14a2 2 0 0 0 2-2V4a2 2 0 0 0-2-2zm-2 12H6v-2h12v2zm0-3H6V9h12v2zm0-3H6V6h12v2z" />
    </Icon>
  ),
  settings: (
    <Icon>
      <path d="M19.14 12.94c.04-.31.06-.63.06-.94 0-.31-.02-.63-.06-.94l2.03-1.58a.49.49 0 0 0 .12-.61l-1.92-3.32a.488.488 0 0 0-.59-.22l-2.39.96c-.5-.38-1.03-.7-1.62-.94l-.36-2.54a.484.484 0 0 0-.48-.41h-3.84c-.24 0-.43.17-.47.41l-.36 2.54c-.59.24-1.13.57-1.62.94l-2.39-.96c-.22-.08-.47 0-.59.22L2.74 8.87c-.12.21-.08.47.12.61l2.03 1.58c-.04.31-.06.63-.06.94s.02.63.06.94l-2.03 1.58a.49.49 0 0 0-.12.61l1.92 3.32c.12.22.37.29.59.22l2.39-.96c.5.38 1.03.7 1.62.94l.36 2.54c.05.24.24.41.48.41h3.84c.24 0 .44-.17.47-.41l.36-2.54c.59-.24 1.13-.56 1.62-.94l2.39.96c.22.08.47 0 .59-.22l1.92-3.32c.12-.22.07-.47-.12-.61l-2.01-1.58zM12 15.6A3.6 3.6 0 1 1 12 8.4a3.6 3.6 0 0 1 0 7.2z" />
    </Icon>
  ),
  palette: (
    <Icon>
      <path d="M12 2C6.49 2 2 6.49 2 12s4.49 10 10 10c1.38 0 2.5-1.12 2.5-2.5 0-.61-.23-1.21-.64-1.67-.4-.46-.64-1.08-.64-1.67 0-1.38 1.12-2.5 2.5-2.5H16c3.31 0 6-2.69 6-6 0-4.41-3.59-8-10-8zm-5.5 11A1.5 1.5 0 1 1 8 11.5 1.5 1.5 0 0 1 6.5 13zm3-4A1.5 1.5 0 1 1 11 7.5 1.5 1.5 0 0 1 9.5 9zm5 0A1.5 1.5 0 1 1 16 7.5 1.5 1.5 0 0 1 14.5 9zm3 4A1.5 1.5 0 1 1 19 11.5 1.5 1.5 0 0 1 17.5 13z" />
    </Icon>
  ),
  finance: (
    <Icon>
      <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1.41 16.09V20h-2.67v-1.93c-1.71-.36-3.16-1.46-3.27-3.4h1.96c.1 1.05.82 1.87 2.65 1.87 1.96 0 2.4-.98 2.4-1.59 0-.83-.44-1.61-2.67-2.14-2.48-.6-4.18-1.62-4.18-3.67 0-1.72 1.39-2.84 3.11-3.21V4h2.67v1.95c1.86.45 2.79 1.86 2.85 3.39H14.3c-.05-1.11-.64-1.87-2.22-1.87-1.5 0-2.4.68-2.4 1.64 0 .84.65 1.39 2.67 1.91s4.18 1.39 4.18 3.91c-.01 1.83-1.38 2.83-3.12 3.16z" />
    </Icon>
  ),
  sms: (
    <Icon>
      <path d="M20 2H4a2 2 0 0 0-2 2v18l4-4h14a2 2 0 0 0 2-2V4a2 2 0 0 0-2-2zm-2 12H6v-2h12v2zm0-3H6V9h12v2zm0-3H6V6h12v2z" />
    </Icon>
  ),
  academy: (
    <Icon>
      <path d="M18 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V4a2 2 0 0 0-2-2zM6 4h5v7l2.5-1.5L16 11V4h2v16H6V4z" />
    </Icon>
  ),
  cafeteria: (
    <Icon>
      <path d="M11 9H9V2H7v7H5V2H3v7c0 2.12 1.66 3.84 3.75 3.97V22h2.5v-9.03C11.34 12.84 13 11.12 13 9V2h-2v7zm5-3v8h2.5v8H21V2c-2.76 0-5 2.24-5 4z" />
    </Icon>
  ),
  desk: (
    <Icon>
      <path d="M4 6h16v2H4V6zm0 5h10v2H4v-2zm0 5h16v2H4v-2zM20 4v2H4V4h16z" />
    </Icon>
  ),
  rules: (
    <Icon>
      <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8l-6-6zm-1 2 5 5h-4a1 1 0 0 1-1-1V4zM8 12h8v2H8v-2zm0 4h5v2H8v-2z" />
    </Icon>
  ),
  card: (
    <Icon>
      <path d="M20 4H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V6a2 2 0 0 0-2-2zm0 14H4v-6h16v6zm0-10H4V6h16v2z" />
    </Icon>
  ),
  seats: (
    <Icon>
      <path d="M4 10h3v7H4V10zm7 0h3v7h-3v-7zm7 0h3v7h-3v-7zM2 20h20v2H2v-2zM12 2 2 7v2h20V7L12 2z" />
    </Icon>
  ),
  toggle: (
    <Icon>
      <path d="M17 7H7a5 5 0 0 0 0 10h10a5 5 0 0 0 0-10zm0 8a3 3 0 1 1 0-6 3 3 0 0 1 0 6z" />
    </Icon>
  ),
  discipline: (
    <Icon>
      <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z" />
    </Icon>
  ),
  promotion: (
    <Icon>
      <path d="M16 6l2.29 2.29-4.88 4.88-4-4L2 16.59 3.41 18l6-6 4 4 6.3-6.29L22 12V6h-6z" />
    </Icon>
  ),
  help: (
    <Icon>
      <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 17h-2v-2h2v2zm2.07-7.75-.9.92C13.45 12.9 13 13.5 13 15h-2v-.5c0-1.1.45-2.1 1.17-2.83l1.24-1.26c.37-.36.59-.86.59-1.41 0-1.1-.9-2-2-2s-2 .9-2 2H8c0-2.21 1.79-4 4-4s4 1.79 4 4c0 .88-.36 1.68-.93 2.25z" />
    </Icon>
  ),
  curriculum: (
    <Icon>
      <path d="M19 3H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V5a2 2 0 0 0-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z" />
    </Icon>
  ),
  books: (
    <Icon>
      <path d="M18 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V4a2 2 0 0 0-2-2zM6 4h5v8l2.5-1.5L16 12V4h2v16H6V4z" />
    </Icon>
  ),
  lock: (
    <Icon>
      <path d="M18 8h-1V6c0-2.76-2.24-5-5-5S7 3.24 7 6v2H6c-1.1 0-2 .9-2 2v10c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V10c0-1.1-.9-2-2-2zm-6 9c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2zM9.1 8V6c0-1.71 1.39-3.1 3.1-3.1s3.1 1.39 3.1 3.1v2H9.1z" />
    </Icon>
  ),
  refresh: (
    <Icon>
      <path d="M17.65 6.35A7.958 7.958 0 0 0 12 4c-4.42 0-8 3.58-8 8s3.58 8 8 8c3.73 0 6.84-2.55 7.73-6h-2.08A5.99 5.99 0 0 1 12 18c-3.31 0-6-2.69-6-6s2.69-6 6-6c1.66 0 3.14.69 4.22 1.78L13 11h7V4l-2.35 2.35z" />
    </Icon>
  ),
};

const ACCENT_PALETTE: ServiceItemAccent[] = [
  { tile: "hover:border-sky-200 hover:bg-sky-50/80", icon: "bg-sky-100 text-sky-600", iconHover: "group-hover:bg-sky-600 group-hover:text-white", labelHover: "group-hover:text-sky-700" },
  { tile: "hover:border-violet-200 hover:bg-violet-50/80", icon: "bg-violet-100 text-violet-600", iconHover: "group-hover:bg-violet-600 group-hover:text-white", labelHover: "group-hover:text-violet-700" },
  { tile: "hover:border-amber-200 hover:bg-amber-50/80", icon: "bg-amber-100 text-amber-600", iconHover: "group-hover:bg-amber-500 group-hover:text-white", labelHover: "group-hover:text-amber-700" },
  { tile: "hover:border-emerald-200 hover:bg-emerald-50/80", icon: "bg-emerald-100 text-emerald-600", iconHover: "group-hover:bg-emerald-600 group-hover:text-white", labelHover: "group-hover:text-emerald-700" },
  { tile: "hover:border-rose-200 hover:bg-rose-50/80", icon: "bg-rose-100 text-rose-600", iconHover: "group-hover:bg-rose-600 group-hover:text-white", labelHover: "group-hover:text-rose-700" },
  { tile: "hover:border-teal-200 hover:bg-teal-50/80", icon: "bg-teal-100 text-teal-600", iconHover: "group-hover:bg-teal-600 group-hover:text-white", labelHover: "group-hover:text-teal-700" },
  { tile: "hover:border-indigo-200 hover:bg-indigo-50/80", icon: "bg-indigo-100 text-indigo-600", iconHover: "group-hover:bg-indigo-600 group-hover:text-white", labelHover: "group-hover:text-indigo-700" },
  { tile: "hover:border-orange-200 hover:bg-orange-50/80", icon: "bg-orange-100 text-orange-600", iconHover: "group-hover:bg-orange-500 group-hover:text-white", labelHover: "group-hover:text-orange-700" },
];

export const SERVICE_ITEM_ACCENTS: Record<string, ServiceItemAccent> = {
  staff: ACCENT_PALETTE[0],
  roles: ACCENT_PALETTE[6],
  schools: ACCENT_PALETTE[1],
  buildings: ACCENT_PALETTE[5],
  "my-classes": ACCENT_PALETTE[2],
  "my-periods": ACCENT_PALETTE[3],
  "academic-years": ACCENT_PALETTE[4],
  calendar: ACCENT_PALETTE[5],
  "study-fields": ACCENT_PALETTE[3],
  curriculum: ACCENT_PALETTE[1],
  "curriculum-extras": ACCENT_PALETTE[4],
  "my-subjects": ACCENT_PALETTE[1],
  reports: ACCENT_PALETTE[5],
  finance: ACCENT_PALETTE[7],
  sms: ACCENT_PALETTE[0],
  branding: ACCENT_PALETTE[2],
  "school-branding": ACCENT_PALETTE[2],
  "school-foreign-nationals": ACCENT_PALETTE[5],
  "school-password": ACCENT_PALETTE[6],
  "school-updates": ACCENT_PALETTE[0],
  "school-settings": ACCENT_PALETTE[6],
  "org-settings": ACCENT_PALETTE[6],
  "pre-registration": ACCENT_PALETTE[1],
  students: ACCENT_PALETTE[0],
  classes: ACCENT_PALETTE[1],
  "manager-classes": ACCENT_PALETTE[2],
  "class-list": ACCENT_PALETTE[2],
  "grades-mgmt": ACCENT_PALETTE[3],
  exams: ACCENT_PALETTE[4],
  attendance: ACCENT_PALETTE[5],
  homework: ACCENT_PALETTE[6],
  performance: ACCENT_PALETTE[5],
  academy: ACCENT_PALETTE[7],
  branches: ACCENT_PALETTE[1],
  messages: ACCENT_PALETTE[0],
  cafeteria: ACCENT_PALETTE[7],
  desk: ACCENT_PALETTE[2],
  grades: ACCENT_PALETTE[3],
  payment: ACCENT_PALETTE[4],
  seats: ACCENT_PALETTE[5],
  settings: ACCENT_PALETTE[6],
  interview: ACCENT_PALETTE[3],
  funnel: ACCENT_PALETTE[5],
  "report-cards": ACCENT_PALETTE[3],
  "grades-pending": ACCENT_PALETTE[2],
  "grades-teacher": ACCENT_PALETTE[4],
  "grades-completion": ACCENT_PALETTE[5],
  subjects: ACCENT_PALETTE[1],
  "class-logs": ACCENT_PALETTE[6],
  discipline: ACCENT_PALETTE[7],
  promotion: ACCENT_PALETTE[2],
  assignments: ACCENT_PALETTE[6],
  books: ACCENT_PALETTE[0],
  support: ACCENT_PALETTE[5],
  "online-class": ACCENT_PALETTE[0],
  "oc-rooms": ACCENT_PALETTE[2],
  "oc-sessions": ACCENT_PALETTE[3],
  "oc-providers": ACCENT_PALETTE[1],
  "oc-live": ACCENT_PALETTE[5],
  "oc-alerts": ACCENT_PALETTE[7],
  "oc-settings": ACCENT_PALETTE[6],
  "oc-reports": ACCENT_PALETTE[4],
};

export function getServiceItemAccent(id: string): ServiceItemAccent {
  if (SERVICE_ITEM_ACCENTS[id]) return SERVICE_ITEM_ACCENTS[id];
  let hash = 0;
  for (let i = 0; i < id.length; i++) hash = (hash + id.charCodeAt(i)) % ACCENT_PALETTE.length;
  return ACCENT_PALETTE[hash];
}

const HOLDING_ROLES = ["developer", "super_admin"] as const;

const MANAGER_ROLES = [
  "developer",
  "super_admin",
  "branch_manager",
  "grade_coordinator",
  "deputy_education",
  "deputy_discipline",
  "deputy_executive",
] as const;

const TEACHER_AND_MANAGERS = [...MANAGER_ROLES, "teacher", "guest_teacher"] as const;

export const DASHBOARD_SECTIONS: ServiceSection[] = [
  {
    id: "education",
    title: "خدمات آموزشی",
    tone: "secondary",
    items: [
      { id: "pre-registration", label: "پیش‌ثبت‌نام", href: "/pre-registration", icon: I.backpack, phase: "active", roles: [...MANAGER_ROLES], moduleKey: "sis.pre_registration" },
      { id: "students", label: "دانش‌آموزان", href: "/students", icon: I.students, phase: "active", roles: [...TEACHER_AND_MANAGERS] },
      { id: "school-buildings", label: "ساختمان‌ها", href: "/buildings", icon: I.classroom, phase: "active", roles: ["branch_manager"], moduleKey: "module.building" },
      { id: "my-classes", label: "کلاس‌های من", href: "/dashboard", icon: I.classroom, phase: "active", roles: ["teacher", "guest_teacher"] },
      { id: "attendance", label: "حضور و غیاب", href: "/academic/attendance", icon: I.attendance, phase: "active", roles: [...MANAGER_ROLES], moduleKey: "module.attendance" },
      { id: "my-attendance", label: "حضور کلاسی", href: "/dashboard", icon: I.attendance, phase: "active", roles: ["teacher", "guest_teacher"] },
      { id: "homework", label: "تکالیف", href: "/academic/assignments", icon: I.rules, phase: "active", roles: [...TEACHER_AND_MANAGERS], moduleKey: "module.assignment" },
      { id: "class-logs", label: "دفتر کلاسی", href: "/academic/class-logs", icon: I.desk, phase: "active", roles: [...TEACHER_AND_MANAGERS] },
      { id: "discipline", label: "انضباط", href: "/academic/discipline", icon: I.discipline, phase: "active", roles: [...TEACHER_AND_MANAGERS], moduleKey: "module.discipline" },
      { id: "promotion", label: "ارتقای پایه", href: "/academic/promotion", icon: I.promotion, phase: "active", roles: [...MANAGER_ROLES] },
      { id: "exams", label: "آزمون‌ها", icon: I.exam, phase: "soon", roles: [...MANAGER_ROLES], moduleKey: "module.exam" },
      { id: "grades-mgmt", label: "نمرات و کارنامه", href: "/academic", icon: I.grade, phase: "active", roles: [...TEACHER_AND_MANAGERS], moduleKey: "module.academic" },
      { id: "academy", label: "آکادمی", href: "/academic/academy", icon: I.academy, phase: "active", roles: [...MANAGER_ROLES], moduleKey: "module.academy" },
      // بدون moduleKey تا با allowlist قدیمی سازمان از داشبورد مدیر کل پنهان نشود؛ API همچنان module:online_class را چک می‌کند.
      { id: "online-class", label: "کلاس آنلاین", href: "/virtual-classes", icon: I.classroom, phase: "active", roles: [...MANAGER_ROLES, "teacher", "guest_teacher"] },
    ],
  },
  {
    id: "admin",
    title: "خدمات اداری و مالی",
    tone: "primary",
    items: [
      { id: "staff", label: "کارمندان", href: "/staff", icon: I.users, phase: "active", roles: [...HOLDING_ROLES, "branch_manager"] },
      { id: "roles", label: "نقش‌ها و دسترسی", href: "/roles", icon: I.roles, phase: "active", roles: [...HOLDING_ROLES, "branch_manager"] },
      { id: "finance", label: "امور مالی", icon: I.finance, phase: "soon", roles: [...MANAGER_ROLES], moduleKey: "module.finance" },
      { id: "reports", label: "گزارش عملکرد", href: "/reports/performance", icon: I.chart, phase: "active", roles: [...TEACHER_AND_MANAGERS], moduleKey: "module.reports" },
    ],
  },
  {
    id: "facilities",
    title: "خدمات جانبی و ارتباطات",
    tone: "slate",
    items: [
      { id: "sms", label: "پیامک و اطلاع‌رسانی", href: "/messaging", icon: I.sms, phase: "active", roles: [...MANAGER_ROLES], moduleKey: "module.sms" },
      { id: "messages", label: "شبکه اجتماعی", href: "/social", icon: I.message, phase: "active", roles: [...TEACHER_AND_MANAGERS], moduleKey: "module.social" },
      { id: "cafeteria", label: "بوفه", icon: I.cafeteria, phase: "soon", roles: [...MANAGER_ROLES], moduleKey: "module.cafeteria" },
    ],
  },
  {
    id: "settings",
    title: "تنظیمات و تعاریف پایه",
    tone: "primary",
    items: [
      { id: "class-list", label: "لیست کلاس‌ها", href: "/my-school/classes", icon: I.classroom, phase: "active", roles: [...HOLDING_ROLES, "branch_manager"] },
      { id: "schools", label: "مدارس و شعب هلدینگ", href: "/schools", icon: I.school, phase: "active", roles: [...HOLDING_ROLES] },
      { id: "buildings", label: "ساختمان‌ها", href: "/buildings", icon: I.classroom, phase: "active", roles: [...HOLDING_ROLES, "branch_manager"], moduleKey: "module.building" },
      { id: "my-subjects", label: "فهرست دروس", href: "/my-school/subjects", icon: I.curriculum, phase: "active", roles: ["branch_manager"] },
      { id: "my-periods", label: "زنگ‌ها", href: "/my-school/periods", icon: I.clock, phase: "active", roles: ["branch_manager"] },
      { id: "academic-years", label: "سال تحصیلی", href: "/academic-years", icon: I.calendar, phase: "active", roles: [...HOLDING_ROLES] },
      { id: "calendar", label: "تقویم و تعطیلات", href: "/calendar", icon: I.calendar, phase: "active", roles: [...HOLDING_ROLES, "branch_manager"], moduleKey: "module.calendar" },
      { id: "education-stages", label: "مقاطع و پایه‌ها", href: "/education-stages", icon: I.school, phase: "active", roles: [...HOLDING_ROLES] },
      { id: "study-fields", label: "رشته‌های تحصیلی", href: "/study-fields", icon: I.grade, phase: "active", roles: [...HOLDING_ROLES, "deputy_education"] },
      { id: "curriculum", label: "برنامه درسی پایه", href: "/academic/curriculum", icon: I.curriculum, phase: "active", roles: [...HOLDING_ROLES, "deputy_education"], moduleKey: "module.academic" },
      { id: "curriculum-extras", label: "دروس فوق‌العاده", href: "/academic/curriculum/extras", icon: I.exam, phase: "active", roles: [...HOLDING_ROLES, "deputy_education"], moduleKey: "module.academic" },
      { id: "books", label: "کتاب‌ها", href: "/academic/curriculum/books", icon: I.books, phase: "active", roles: [...MANAGER_ROLES], moduleKey: "module.academic" },
      { id: "school-settings", label: "تنظیمات مدرسه", href: "/settings/school", icon: I.school, phase: "active", roles: [...HOLDING_ROLES, "branch_manager"] },
      { id: "org-settings", label: "ماژول‌های سامانه", href: "/settings", icon: I.settings, phase: "active", roles: ["developer"] },
      { id: "support", label: "مرکز راهنما", href: "/support", icon: I.help, phase: "active", roles: [...TEACHER_AND_MANAGERS] },
    ],
  },
];

/** میانبرهای صفحه «تنظیمات مدرسه» */
export const SCHOOL_SETTINGS_ITEMS: ServiceItem[] = [
  { id: "school-branding", label: "برند و ظاهر", href: "/settings/school/branding", icon: I.palette, phase: "active", roles: [...HOLDING_ROLES, "branch_manager"] },
  { id: "school-foreign-nationals", label: "امکان ثبت نام اتباع", href: "/settings/school/foreign-nationals", icon: I.students, phase: "active", roles: [...HOLDING_ROLES] },
  { id: "school-password", label: "سیاست رمز عبور", href: "/settings/school/password", icon: I.lock, phase: "active", roles: [...HOLDING_ROLES, "branch_manager"] },
  { id: "school-updates", label: "به‌روزرسانی‌ها", href: "/settings/school/updates", icon: I.refresh, phase: "active", roles: [...HOLDING_ROLES] },
];

export const PRE_REGISTRATION_SETTINGS_ITEMS: ServiceItem[] = [
  { id: "org-defaults", label: "اعمال گروهی تنظیمات", href: "/pre-registration/org-defaults", icon: I.rules, phase: "active", roles: [...HOLDING_ROLES] },
  { id: "payment", label: "پرداخت و درگاه", href: "/pre-registration/payment", icon: I.card, phase: "active", roles: [...MANAGER_ROLES] },
];

export const PRE_REGISTRATION_SECTION: ServiceSection = {
  id: "pre-registration",
  title: "مدیریت پیش‌ثبت‌نام",
  tone: "secondary",
  items: [
    { id: "desk", label: "میز پذیرش و بررسی", href: "/pre-registration/admissions", icon: I.desk, phase: "active", roles: [...MANAGER_ROLES], moduleKey: "sis.pre_registration" },
    { id: "interest", label: "درخواست اطلاع ثبت‌نام", href: "/pre-registration/interest", icon: I.sms, phase: "active", roles: [...MANAGER_ROLES], moduleKey: "sis.pre_registration" },
    { id: "settings", label: "تنظیمات پیش‌ثبت‌نام", href: "/pre-registration/settings", icon: I.toggle, phase: "active", roles: [...MANAGER_ROLES], moduleKey: "sis.pre_registration" },
    { id: "interview", label: "زمان‌بندی مصاحبه", href: "/pre-registration/interview", icon: I.calendar, phase: "active", roles: [...MANAGER_ROLES], moduleKey: "sis.pre_registration" },
    { id: "funnel", label: "گزارش قیف تبدیل", icon: I.chart, phase: "soon", roles: [...MANAGER_ROLES], moduleKey: "sis.pre_registration" },
  ],
};

export const ACADEMIC_SECTION: ServiceSection = {
  id: "academic",
  title: "مدیریت آموزشی",
  tone: "secondary",
  items: [
    { id: "report-cards", label: "کارنامه‌ها", href: "/academic/report-cards", icon: I.grade, phase: "active", roles: [...TEACHER_AND_MANAGERS] },
    { id: "subjects", label: "برنامه درسی پایه", href: "/academic/curriculum", icon: I.curriculum, phase: "active", roles: [...HOLDING_ROLES, "deputy_education"], moduleKey: "module.academic" },
    { id: "class-logs", label: "دفتر کلاسی", href: "/academic/class-logs", icon: I.desk, phase: "active", roles: [...TEACHER_AND_MANAGERS] },
    { id: "attendance", label: "گزارش حضور و غیاب", href: "/academic/attendance", icon: I.attendance, phase: "active", roles: [...MANAGER_ROLES], moduleKey: "module.attendance" },
    { id: "discipline", label: "انضباط", href: "/academic/discipline", icon: I.discipline, phase: "active", roles: [...TEACHER_AND_MANAGERS], moduleKey: "module.discipline" },
    { id: "promotion", label: "ارتقای پایه", href: "/academic/promotion", icon: I.promotion, phase: "active", roles: [...MANAGER_ROLES] },
    { id: "academy", label: "آکادمی", href: "/academic/academy", icon: I.academy, phase: "active", roles: [...MANAGER_ROLES], moduleKey: "module.academy" },
    { id: "assignments", label: "تکالیف", href: "/academic/assignments", icon: I.rules, phase: "active", roles: [...TEACHER_AND_MANAGERS], moduleKey: "module.assignment" },
  ],
};

/** هب کلاس آنلاین — همه بخش‌های ماژول از اینجا در دسترس‌اند. */
export const ONLINE_CLASS_SECTION: ServiceSection = {
  id: "online-class",
  title: "کلاس آنلاین",
  tone: "secondary",
  items: [
    { id: "oc-rooms", label: "اتاق‌ها و کلاس‌ها", href: "/virtual-classes/manage?tab=rooms", icon: I.classroom, phase: "active", roles: [...MANAGER_ROLES, "teacher", "guest_teacher"] },
    { id: "oc-sessions", label: "جلسات", href: "/virtual-classes/manage?tab=sessions", icon: I.calendar, phase: "active", roles: [...MANAGER_ROLES, "teacher", "guest_teacher"] },
    { id: "oc-providers", label: "سرویس‌ها و سرورها", href: "/virtual-classes/providers", icon: I.settings, phase: "active", roles: [...HOLDING_ROLES, "branch_manager", "deputy_education"] },
    { id: "oc-live", label: "جلسات زنده و سلامت", href: "/virtual-classes/manage?tab=live", icon: I.clock, phase: "active", roles: [...MANAGER_ROLES] },
    { id: "oc-alerts", label: "هشدارها", href: "/virtual-classes/manage?tab=alerts", icon: I.discipline, phase: "active", roles: [...MANAGER_ROLES] },
    { id: "oc-reports", label: "گزارش‌ها", href: "/virtual-classes/manage?tab=sessions", icon: I.chart, phase: "active", roles: [...MANAGER_ROLES] },
    { id: "oc-settings", label: "تنظیمات", href: "/virtual-classes/settings", icon: I.toggle, phase: "active", roles: [...HOLDING_ROLES, "branch_manager", "deputy_education"] },
  ],
};

/** گروه‌های صفحه هب آموزشی؛ سایدبار همچنان از ACADEMIC_SECTION استفاده می‌کند. */
export const ACADEMIC_HUB_SECTIONS: ServiceSection[] = [
  {
    id: "grades-report-cards",
    title: "نمرات و کارنامه",
    tone: "secondary",
    items: [
      { id: "report-cards", label: "کارنامه‌ها", href: "/academic/report-cards", icon: I.grade, phase: "active", roles: [...TEACHER_AND_MANAGERS] },
    ],
  },
  {
    id: "class-ops",
    title: "عملیات کلاسی",
    tone: "primary",
    items: [
      { id: "attendance", label: "گزارش حضور و غیاب", href: "/academic/attendance", icon: I.attendance, phase: "active", roles: [...MANAGER_ROLES], moduleKey: "module.attendance" },
      { id: "class-logs", label: "دفتر کلاسی", href: "/academic/class-logs", icon: I.desk, phase: "active", roles: [...TEACHER_AND_MANAGERS] },
      { id: "assignments", label: "تکالیف", href: "/academic/assignments", icon: I.rules, phase: "active", roles: [...TEACHER_AND_MANAGERS], moduleKey: "module.assignment" },
      { id: "discipline", label: "انضباط", href: "/academic/discipline", icon: I.discipline, phase: "active", roles: [...TEACHER_AND_MANAGERS], moduleKey: "module.discipline" },
      { id: "promotion", label: "ارتقای پایه", href: "/academic/promotion", icon: I.promotion, phase: "active", roles: [...MANAGER_ROLES] },
    ],
  },
  {
    id: "curriculum-hub",
    title: "برنامه درسی",
    tone: "slate",
    items: [
      { id: "curriculum", label: "برنامه درسی پایه", href: "/academic/curriculum", icon: I.curriculum, phase: "active", roles: [...HOLDING_ROLES, "deputy_education"], moduleKey: "module.academic" },
      { id: "curriculum-extras", label: "دروس فوق‌العاده", href: "/academic/curriculum/extras", icon: I.exam, phase: "active", roles: [...HOLDING_ROLES, "deputy_education"], moduleKey: "module.academic" },
      { id: "books", label: "کتاب‌ها", href: "/academic/curriculum/books", icon: I.books, phase: "active", roles: [...MANAGER_ROLES], moduleKey: "module.academic" },
      { id: "academy", label: "آکادمی", href: "/academic/academy", icon: I.academy, phase: "active", roles: [...MANAGER_ROLES], moduleKey: "module.academy" },
    ],
  },
];

export function filterItems(
  items: ServiceItem[],
  roles: string[],
  enabledModuleKeys?: string[] | null,
): ServiceItem[] {
  return items.filter((item) => {
    if (item.moduleKey && enabledModuleKeys && !enabledModuleKeys.includes(item.moduleKey)) {
      return false;
    }
    if (!item.roles?.length) return true;
    return item.roles.some((role) => roles.includes(role));
  });
}

export function filterSections(
  sections: ServiceSection[],
  roles: string[],
  enabledModuleKeys?: string[] | null,
): ServiceSection[] {
  return sections
    .map((section) => ({
      ...section,
      items: filterItems(section.items, roles, enabledModuleKeys),
    }))
    .filter((section) => section.items.length > 0);
}

export type SidebarLink = {
  id: string;
  label: string;
  href?: string;
  phase?: "active" | "soon";
  roles?: string[];
};

export type SidebarAccordionGroup = {
  id: string;
  title: string;
  items: SidebarLink[];
};

function sectionToLinks(section: ServiceSection): SidebarLink[] {
  return section.items.map((item) => ({
    id: item.id,
    label: item.label,
    href: item.href,
    phase: item.phase,
    roles: item.roles,
  }));
}

export function buildSidebarGroups(
  roles: string[],
  enabledModuleKeys?: string[] | null,
): SidebarAccordionGroup[] {
  return filterSections(DASHBOARD_SECTIONS, roles, enabledModuleKeys).map((section) => ({
    id: section.id,
    title: section.title,
    items: sectionToLinks(section),
  }));
}
