import { Link, usePage } from '@inertiajs/react';

type AuthUser = {
  id: number;
  firstName: string;
  lastName: string;
  username: string;
  roleKey: string;
  schoolId: number | null;
};

type NavItem = [string, string, string]; // [href, label, icon]

const superAdminLinks: NavItem[] = [
  ['/super-admin', 'داشبورد', 'bi-speedometer2'],
  ['/super-admin/schools', 'مدارس', 'bi-building'],
  ['/super-admin/modules', 'ماژول‌ها', 'bi-puzzle'],
  ['/super-admin/sms-usage', 'مصرف SMS', 'bi-chat-dots'],
  ['/super-admin/reports', 'گزارش‌ها', 'bi-bar-chart'],
  ['/super-admin/audit-logs', 'لاگ‌های سیستم', 'bi-shield-check'],
];

const schoolAdminLinks: NavItem[] = [
  ['/school', 'داشبورد مدرسه', 'bi-speedometer2'],
  ['/school/users', 'کاربران مدرسه', 'bi-people'],
  ['/school/students', 'دانش‌آموزان', 'bi-person-badge'],
  ['/school/teachers', 'معلمان', 'bi-person-workspace'],
  ['/school/classes', 'کلاس‌ها', 'bi-door-open'],
  ['/school/subjects', 'درس‌ها', 'bi-book'],
  ['/school/schedule', 'برنامه هفتگی', 'bi-calendar2-week'],
  ['/school/attendance', 'حضور و غیاب', 'bi-clipboard-check'],
  ['/school/grades', 'تایید نمرات', 'bi-patch-check'],
  ['/school/report-card', 'کارنامه', 'bi-file-earmark-bar-graph'],
  ['/school/messages', 'پیام‌رسانی', 'bi-envelope'],
  ['/school/online-classes', 'کلاس آنلاین', 'bi-camera-video'],
  ['/school/files', 'فایل‌ها', 'bi-folder2'],
  ['/school/announcements', 'اطلاعیه‌ها', 'bi-megaphone'],
  ['/school/voting', 'رأی‌گیری', 'bi-ballot'],
  ['/school/sms-wallet', 'کیف پول SMS', 'bi-wallet2'],
  ['/school/sms-absents', 'SMS غیبت‌ها', 'bi-send'],
  ['/school/sms-templates', 'template‌های SMS', 'bi-file-text'],
  ['/school/absence-report', 'دانش‌آموزان پرغیبت', 'bi-person-x'],
  ['/school/user-activity', 'فعالیت کاربران', 'bi-activity'],
  ['/school/reports', 'گزارش‌ها', 'bi-bar-chart'],
  ['/school/exports', 'Export Center', 'bi-download'],
  ['/school/import/students', 'وارد کردن دانش‌آموزان', 'bi-file-earmark-arrow-up'],
  ['/school/import/teachers', 'وارد کردن معلمان', 'bi-file-earmark-arrow-up'],
  ['/school/import/users', 'وارد کردن کاربران', 'bi-file-earmark-arrow-up'],
  ['/school/audit-logs', 'لاگ‌های مدرسه', 'bi-clock-history'],
  ['/school/settings', 'تنظیمات مدرسه', 'bi-gear'],
];

const teacherLinks: NavItem[] = [
  ['/teacher', 'کلاس‌های من', 'bi-door-open'],
  ['/teacher/homeworks', 'تکالیف', 'bi-journal-check'],
  ['/teacher/exams', 'امتحانات', 'bi-pencil-square'],
  ['/teacher/grades', 'نمره‌دهی', 'bi-patch-check'],
  ['/school/attendance', 'حضور و غیاب', 'bi-clipboard-check'],
];

const studentLinks: NavItem[] = [
  ['/student', 'داشبورد من', 'bi-house'],
  ['/student/schedule', 'برنامه هفتگی', 'bi-calendar2-week'],
  ['/student/exams', 'امتحانات', 'bi-pencil-square'],
  ['/student/homeworks', 'تکالیف', 'bi-journal-check'],
  ['/student/grades', 'نمرات', 'bi-patch-check'],
  ['/student/attendance', 'حضور و غیاب', 'bi-clipboard-check'],
  ['/student/voting', 'رأی‌گیری', 'bi-ballot'],
];

const supportLinks: NavItem[] = [
  ['/school', 'داشبورد', 'bi-speedometer2'],
  ['/school/users', 'کاربران', 'bi-people'],
  ['/school/students', 'دانش‌آموزان', 'bi-person-badge'],
  ['/school/audit-logs', 'لاگ‌ها', 'bi-clock-history'],
];

const financeLinks: NavItem[] = [
  ['/school', 'داشبورد', 'bi-speedometer2'],
  ['/school/sms-wallet', 'کیف پول SMS', 'bi-wallet2'],
  ['/school/exports', 'Export Center', 'bi-download'],
];

const rootLinks: Record<string, boolean> = {
  '/school': true,
  '/super-admin': true,
  '/teacher': true,
  '/student': true,
};

export function DashboardSidebar() {
  const { props, url } = usePage<{ auth?: { user?: AuthUser } }>();
  const roleKey = props.auth?.user?.roleKey ?? '';

  let links: NavItem[] = [];

  if (roleKey === 'super_admin') {
    links = superAdminLinks;
  } else if (['owner', 'principal', 'moaven'].includes(roleKey)) {
    links = schoolAdminLinks;
  } else if (roleKey === 'support') {
    links = supportLinks;
  } else if (roleKey === 'finance') {
    links = financeLinks;
  } else if (roleKey === 'teacher') {
    links = teacherLinks;
  } else if (roleKey === 'student') {
    links = studentLinks;
  } else {
    links = [['/dashboard', 'داشبورد', 'bi-house']];
  }

  function isActive(href: string): boolean {
    if (rootLinks[href]) return url === href;
    const path = url.split('?')[0];
    return path === href || path.startsWith(href + '/');
  }

  return (
    <nav className="sidebar-nav">
      {links.map(([href, label, icon]) => (
        <Link
          key={href + label}
          href={href}
          className={isActive(href) ? 'active' : undefined}
        >
          <i className={`bi ${icon}`} style={{ marginLeft: '0.5rem', opacity: 0.65, fontSize: '1rem' }} />
          {label}
        </Link>
      ))}
    </nav>
  );
}
