import type { LucideIcon } from "lucide-react";
import {
  LayoutDashboard,
  Calendar,
  Users,
  Stethoscope,
  ClipboardList,
  Building2,
  Pill,
  Settings,
  FileHeart,
  Bell,
  Search,
  Truck,
  BarChart3,
  Shield,
  Clock,
  UserCircle,
  Hospital,
  BedDouble,
  CreditCard,
  MessageSquare,
  Star,
  Heart,
  Activity,
  Package,
  CheckCircle,
  LogIn,
} from "lucide-react";

export type HealthcarePortalId =
  | "patient"
  | "doctor"
  | "reception"
  | "clinic"
  | "hospital"
  | "pharmacy"
  | "admin";

export type PortalNavItem = { href: string; label: string; icon?: LucideIcon };
export type PortalNavGroup = {
  id: string;
  label: string;
  icon: LucideIcon;
  href?: string;
  items?: PortalNavItem[];
};

export const HEALTHCARE_PORTALS: {
  id: HealthcarePortalId;
  label: string;
  description: string;
  basePath: string;
  icon: LucideIcon;
}[] = [
  {
    id: "patient",
    label: "پورتال بیمار",
    description: "نوبت‌ها، پرونده، نسخه",
    basePath: "/healthcare/patient",
    icon: UserCircle,
  },
  {
    id: "doctor",
    label: "پورتال پزشک",
    description: "برنامه، بیماران، ویزیت",
    basePath: "/healthcare/doctor",
    icon: Stethoscope,
  },
  {
    id: "reception",
    label: "پذیرش",
    description: "صف، پذیرش، تقویم",
    basePath: "/healthcare/reception",
    icon: ClipboardList,
  },
  {
    id: "clinic",
    label: "مدیریت کلینیک",
    description: "پرسنل، بخش‌ها، گزارش",
    basePath: "/healthcare/clinic",
    icon: Building2,
  },
  {
    id: "hospital",
    label: "مدیریت بیمارستان",
    description: "بخش‌ها، تخت، منابع",
    basePath: "/healthcare/hospital",
    icon: Hospital,
  },
  {
    id: "pharmacy",
    label: "داروخانه",
    description: "نسخه، آماده‌سازی، ارسال",
    basePath: "/healthcare/pharmacy-portal",
    icon: Pill,
  },
  {
    id: "admin",
    label: "مدیر سیستم",
    description: "تنظیمات، کاربران، گزارش",
    basePath: "/healthcare/admin",
    icon: Shield,
  },
];

export const PATIENT_NAV: PortalNavGroup[] = [
  { id: "dash", label: "داشبورد", icon: LayoutDashboard, href: "/healthcare/patient" },
  {
    id: "appts",
    label: "نوبت‌ها",
    icon: Calendar,
    items: [
      { href: "/healthcare/patient/appointments/upcoming", label: "نوبت‌های آینده" },
      { href: "/healthcare/patient/appointments/past", label: "سوابق نوبت" },
      { href: "/healthcare/patient/appointments/book", label: "رزرو نوبت" },
    ],
  },
  {
    id: "health",
    label: "سلامت",
    icon: FileHeart,
    items: [
      { href: "/healthcare/patient/medical-history", label: "سوابق پزشکی" },
      { href: "/healthcare/patient/prescriptions", label: "نسخه‌ها" },
      { href: "/healthcare/patient/lab-results", label: "نتایج آزمایش" },
    ],
  },
  {
    id: "billing",
    label: "مالی",
    icon: CreditCard,
    items: [
      { href: "/healthcare/patient/invoices", label: "صورتحساب" },
      { href: "/healthcare/patient/payments", label: "پرداخت‌ها" },
    ],
  },
  {
    id: "account",
    label: "حساب",
    icon: Settings,
    items: [
      { href: "/healthcare/patient/profile", label: "پروفایل" },
      { href: "/healthcare/patient/family", label: "اعضای خانواده" },
      { href: "/healthcare/patient/notifications", label: "اعلان‌ها" },
      { href: "/healthcare/patient/messages", label: "پیام‌ها" },
      { href: "/healthcare/patient/favorites", label: "علاقه‌مندی‌ها" },
      { href: "/healthcare/patient/reviews", label: "نظرات" },
      { href: "/healthcare/patient/settings", label: "تنظیمات" },
    ],
  },
];

export const DOCTOR_NAV: PortalNavGroup[] = [
  { id: "dash", label: "داشبورد", icon: LayoutDashboard, href: "/healthcare/doctor" },
  {
    id: "schedule",
    label: "برنامه",
    icon: Calendar,
    items: [
      { href: "/healthcare/doctor/schedule/today", label: "امروز" },
      { href: "/healthcare/doctor/schedule/calendar", label: "تقویم" },
      { href: "/healthcare/doctor/availability", label: "دسترسی" },
      { href: "/healthcare/doctor/leaves", label: "مرخصی" },
    ],
  },
  {
    id: "clinical",
    label: "بالینی",
    icon: Stethoscope,
    items: [
      { href: "/healthcare/doctor/appointments", label: "نوبت‌ها" },
      { href: "/healthcare/doctor/patients", label: "بیماران" },
      { href: "/healthcare/doctor/medical-records", label: "پرونده‌ها" },
      { href: "/healthcare/doctor/prescriptions", label: "نسخه‌ها" },
    ],
  },
  {
    id: "insights",
    label: "تحلیل",
    icon: BarChart3,
    items: [
      { href: "/healthcare/doctor/reports", label: "گزارش‌ها" },
      { href: "/healthcare/doctor/analytics", label: "آنالیتیکس" },
      { href: "/healthcare/doctor/income", label: "درآمد" },
    ],
  },
  {
    id: "comms",
    label: "ارتباط",
    icon: Bell,
    items: [
      { href: "/healthcare/doctor/notifications", label: "اعلان‌ها" },
      { href: "/healthcare/doctor/messages", label: "پیام‌ها" },
      { href: "/healthcare/doctor/settings", label: "تنظیمات" },
    ],
  },
];

export const RECEPTION_NAV: PortalNavGroup[] = [
  { id: "dash", label: "داشبورد", icon: LayoutDashboard, href: "/healthcare/reception" },
  { id: "queue", label: "صف نوبت", icon: Clock, href: "/healthcare/reception/queue" },
  { id: "walkin", label: "مراجعه حضوری", icon: Users, href: "/healthcare/reception/walk-in" },
  { id: "calendar", label: "تقویم", icon: Calendar, href: "/healthcare/reception/calendar" },
  { id: "doctors", label: "پزشکان", icon: Stethoscope, href: "/healthcare/reception/doctors" },
  { id: "patients", label: "بیماران", icon: UserCircle, href: "/healthcare/reception/patients" },
  { id: "checkin", label: "پذیرش", icon: LogIn, href: "/healthcare/reception/check-in" },
  { id: "checkout", label: "ترخیص", icon: CheckCircle, href: "/healthcare/reception/check-out" },
  { id: "payments", label: "پرداخت", icon: CreditCard, href: "/healthcare/reception/payments" },
  { id: "reports", label: "گزارش", icon: BarChart3, href: "/healthcare/reception/reports" },
];

export const CLINIC_NAV: PortalNavGroup[] = [
  { id: "dash", label: "داشبورد", icon: LayoutDashboard, href: "/healthcare/clinic" },
  { id: "doctors", label: "پزشکان", icon: Stethoscope, href: "/healthcare/clinic/doctors" },
  { id: "staff", label: "پذیرش", icon: Users, href: "/healthcare/clinic/receptionists" },
  { id: "depts", label: "بخش‌ها", icon: Building2, href: "/healthcare/clinic/departments" },
  { id: "rooms", label: "اتاق‌ها", icon: BedDouble, href: "/healthcare/clinic/rooms" },
  { id: "schedules", label: "برنامه‌ها", icon: Calendar, href: "/healthcare/clinic/schedules" },
  { id: "resources", label: "منابع", icon: Package, href: "/healthcare/clinic/resources" },
  { id: "patients", label: "بیماران", icon: UserCircle, href: "/healthcare/clinic/patients" },
  { id: "reports", label: "گزارش", icon: BarChart3, href: "/healthcare/clinic/reports" },
  { id: "crm", label: "CRM", icon: Heart, href: "/healthcare/clinic/crm-integration" },
  { id: "accounting", label: "حسابداری", icon: CreditCard, href: "/healthcare/clinic/accounting-integration" },
];

export const HOSPITAL_NAV: PortalNavGroup[] = [
  { id: "dash", label: "داشبورد", icon: LayoutDashboard, href: "/healthcare/hospital" },
  { id: "depts", label: "بخش‌ها", icon: Building2, href: "/healthcare/hospital/departments" },
  { id: "doctors", label: "پزشکان", icon: Stethoscope, href: "/healthcare/hospital/doctors" },
  { id: "beds", label: "تخت‌ها", icon: BedDouble, href: "/healthcare/hospital/beds" },
  { id: "clinics", label: "کلینیک‌ها", icon: Hospital, href: "/healthcare/hospital/clinics" },
  { id: "appts", label: "نوبت‌ها", icon: Calendar, href: "/healthcare/hospital/appointments" },
  { id: "staff", label: "پرسنل", icon: Users, href: "/healthcare/hospital/staff" },
  { id: "resources", label: "منابع", icon: Package, href: "/healthcare/hospital/resources" },
  { id: "reports", label: "گزارش", icon: BarChart3, href: "/healthcare/hospital/reports" },
];

export const PHARMACY_NAV: PortalNavGroup[] = [
  { id: "dash", label: "داشبورد", icon: LayoutDashboard, href: "/healthcare/pharmacy-portal" },
  { id: "incoming", label: "نسخه‌های ورودی", icon: Pill, href: "/healthcare/pharmacy-portal/incoming" },
  { id: "prep", label: "صف آماده‌سازی", icon: Activity, href: "/healthcare/pharmacy-portal/preparation" },
  { id: "ready", label: "آماده تحویل", icon: CheckCircle, href: "/healthcare/pharmacy-portal/ready" },
  { id: "delivered", label: "تحویل‌شده", icon: Package, href: "/healthcare/pharmacy-portal/delivered" },
  { id: "delivery", label: "درخواست ارسال", icon: Truck, href: "/healthcare/pharmacy-portal/delivery" },
  { id: "reports", label: "گزارش", icon: BarChart3, href: "/healthcare/pharmacy-portal/reports" },
];

export const ADMIN_NAV: PortalNavGroup[] = [
  { id: "dash", label: "داشبورد", icon: LayoutDashboard, href: "/healthcare/admin" },
  { id: "orgs", label: "سازمان‌ها", icon: Building2, href: "/healthcare/admin/organizations" },
  { id: "clinics", label: "کلینیک‌ها", icon: Hospital, href: "/healthcare/admin/clinics" },
  { id: "doctors", label: "پزشکان", icon: Stethoscope, href: "/healthcare/admin/doctors" },
  { id: "patients", label: "بیماران", icon: Users, href: "/healthcare/admin/patients" },
  { id: "pharmacies", label: "داروخانه‌ها", icon: Pill, href: "/healthcare/admin/pharmacies" },
  { id: "users", label: "کاربران", icon: UserCircle, href: "/healthcare/admin/users" },
  { id: "permissions", label: "دسترسی‌ها", icon: Shield, href: "/healthcare/admin/permissions" },
  { id: "reports", label: "گزارش", icon: BarChart3, href: "/healthcare/admin/reports" },
  { id: "analytics", label: "آنالیتیکس", icon: Activity, href: "/healthcare/admin/analytics" },
  { id: "logs", label: "لاگ سیستم", icon: FileHeart, href: "/healthcare/admin/logs" },
  { id: "monitoring", label: "مانیتورینگ", icon: Activity, href: "/healthcare/admin/monitoring" },
  { id: "settings", label: "تنظیمات", icon: Settings, href: "/healthcare/admin/settings" },
];

export const PUBLIC_NAV = [
  { href: "/healthcare/site", label: "خانه" },
  { href: "/healthcare/site/features", label: "امکانات" },
  { href: "/healthcare/site/pricing", label: "قیمت‌گذاری" },
  { href: "/healthcare/site/faq", label: "سوالات" },
  { href: "/healthcare/site/about", label: "درباره" },
  { href: "/healthcare/site/contact", label: "تماس" },
  { href: "/healthcare/site/find-doctor", label: "یافتن پزشک" },
  { href: "/healthcare/site/find-clinic", label: "یافتن کلینیک" },
];

export function navForPortal(portal: HealthcarePortalId): PortalNavGroup[] {
  switch (portal) {
    case "patient":
      return PATIENT_NAV;
    case "doctor":
      return DOCTOR_NAV;
    case "reception":
      return RECEPTION_NAV;
    case "clinic":
      return CLINIC_NAV;
    case "hospital":
      return HOSPITAL_NAV;
    case "pharmacy":
      return PHARMACY_NAV;
    case "admin":
      return ADMIN_NAV;
    default:
      return [];
  }
}

export function detectPortal(pathname: string): HealthcarePortalId | null {
  for (const p of HEALTHCARE_PORTALS) {
    if (pathname === p.basePath || pathname.startsWith(`${p.basePath}/`)) return p.id;
  }
  return null;
}
