import type { LucideIcon } from "lucide-react";
import {
  LayoutDashboard,
  Truck,
  Users,
  Map,
  Package,
  Building2,
  Settings,
  Shield,
  Activity,
  BarChart3,
  Bell,
  Wallet,
  Route,
  Radio,
  Zap,
  ClipboardList,
  Car,
  Clock,
  AlertTriangle,
  Headphones,
  Key,
  Plug,
  Store,
  Brain,
  Cog,
} from "lucide-react";

export type DeliveryPortalId = "operations" | "admin";

export type PortalNavItem = {
  href: string;
  label: string;
  feature?: string | null;
};

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

export const DELIVERY_PORTAL = {
  id: "operations" as const,
  label: "تربت درایور",
  description: "پلتفرم لجستیک، پیک و ناوگان سازمانی",
  basePath: "/delivery",
  icon: Truck,
};

export const OPERATIONS_NAV: PortalNavGroup[] = [
  {
    id: "dashboards",
    label: "داشبوردها",
    icon: LayoutDashboard,
    items: [
      { href: "/delivery", label: "داشبورد اجرایی" },
      { href: "/delivery/dispatcher", label: "دیسپچر", feature: "dispatch_engine" },
      { href: "/delivery/operations", label: "عملیات زنده", feature: "tracking" },
      { href: "/delivery/fleet-dashboard", label: "ناوگان", feature: "fleet" },
    ],
  },
  {
    id: "drivers",
    label: "رانندگان",
    icon: Users,
    feature: "drivers",
    items: [
      { href: "/delivery/drivers", label: "فهرست رانندگان", feature: "drivers" },
      { href: "/delivery/drivers/availability", label: "دسترسی", feature: "fleet" },
      { href: "/delivery/drivers/performance", label: "عملکرد", feature: "tracking" },
    ],
  },
  {
    id: "orders",
    label: "سفارش‌ها",
    icon: Package,
    feature: "dispatch_engine",
    items: [
      { href: "/delivery/orders", label: "سفارش‌ها", feature: "dispatch_engine" },
      { href: "/delivery/dispatch/queue", label: "صف دیسپچ", feature: "dispatch_engine" },
      { href: "/delivery/dispatch/assignment", label: "مرکز تخصیص", feature: "dispatch_engine" },
      { href: "/delivery/pickups", label: "جمع‌آوری", feature: "dispatch_engine" },
      { href: "/delivery/deliveries", label: "تحویل‌ها", feature: "dispatch_engine" },
      { href: "/delivery/returns", label: "مرجوعی", feature: "dispatch_engine" },
      { href: "/delivery/failed", label: "تحویل ناموفق", feature: "dispatch_engine" },
    ],
  },
  {
    id: "tracking",
    label: "رهگیری",
    icon: Radio,
    feature: "tracking",
    items: [
      { href: "/delivery/tracking/live", label: "رهگیری زنده", feature: "tracking" },
      { href: "/delivery/tracking/customer", label: "رهگیری مشتری", feature: "tracking" },
    ],
  },
  {
    id: "maps",
    label: "نقشه",
    icon: Map,
    feature: "routing_engine",
    items: [
      { href: "/delivery/maps/view", label: "نمای نقشه", feature: "routing_engine" },
      { href: "/delivery/maps/routes/planner", label: "برنامه‌ریزی مسیر", feature: "routing_engine" },
      { href: "/delivery/maps/routes/optimization", label: "بهینه‌سازی", feature: "routing_engine" },
      { href: "/delivery/maps/geofence", label: "ژئوفنس", feature: "routing_engine" },
      { href: "/delivery/maps/zones", label: "مناطق تحویل", feature: "routing_engine" },
    ],
  },
  {
    id: "fleet",
    label: "ناوگان",
    icon: Car,
    feature: "fleet",
    items: [
      { href: "/delivery/vehicles", label: "خودروها", feature: "fleet" },
      { href: "/delivery/vehicle-types", label: "انواع خودرو", feature: "fleet" },
      { href: "/delivery/fleet/maintenance", label: "نگهداری", feature: "fleet" },
      { href: "/delivery/schedules", label: "برنامه‌ها", feature: "fleet" },
      { href: "/delivery/shifts", label: "شیفت‌ها", feature: "fleet" },
      { href: "/delivery/tasks", label: "وظایف", feature: "dispatch_engine" },
    ],
  },
  {
    id: "foundation",
    label: "زیرساخت",
    icon: Building2,
    items: [
      { href: "/delivery/organizations", label: "سازمان‌ها" },
      { href: "/delivery/branches", label: "شعب / هاب‌ها" },
      { href: "/delivery/configurations", label: "پیکربندی" },
    ],
  },
  {
    id: "finance",
    label: "مالی",
    icon: Wallet,
    feature: "settlement",
    items: [
      { href: "/delivery/cod", label: "COD", feature: "settlement" },
      { href: "/delivery/settlements", label: "تسویه", feature: "settlement" },
      { href: "/delivery/payments", label: "پرداخت‌ها", feature: "settlement" },
      { href: "/delivery/wallet", label: "کیف پول راننده", feature: "settlement" },
      { href: "/delivery/commissions", label: "کمیسیون", feature: "settlement" },
    ],
  },
  {
    id: "integrations",
    label: "یکپارچه‌سازی",
    icon: Plug,
    items: [
      { href: "/delivery/integrations/providers", label: "ارائه‌دهندگان خارجی" },
      { href: "/delivery/integrations/routing", label: "موتور مسیریابی" },
      { href: "/delivery/vendor-portal", label: "پورتال فروشنده", feature: "dispatch_engine" },
    ],
  },
  {
    id: "insights",
    label: "گزارش و تحلیل",
    icon: BarChart3,
    feature: "tracking",
    items: [
      { href: "/delivery/reports", label: "گزارش‌ها", feature: "tracking" },
      { href: "/delivery/analytics", label: "آنالیتیکس", feature: "tracking" },
      { href: "/delivery/analytics/heatmaps", label: "نقشه حرارتی", feature: "tracking" },
      { href: "/delivery/analytics/kpis", label: "KPIها", feature: "tracking" },
    ],
  },
  {
    id: "support",
    label: "پشتیبانی",
    icon: Headphones,
    feature: "dispatch_engine",
    items: [
      { href: "/delivery/notifications", label: "اعلان‌ها", feature: "dispatch_engine" },
      { href: "/delivery/incidents", label: "حوادث", feature: "dispatch_engine" },
      { href: "/delivery/support", label: "تیکت‌ها", feature: "dispatch_engine" },
    ],
  },
  {
    id: "ai",
    label: "هوش مصنوعی",
    icon: Brain,
    feature: "ai",
    items: [
      { href: "/delivery/ai/recommendations", label: "پیشنهادات", feature: "ai" },
      { href: "/delivery/automation", label: "اتوماسیون", feature: "ai" },
    ],
  },
  {
    id: "admin",
    label: "مدیریت",
    icon: Settings,
    items: [
      { href: "/delivery/settings", label: "تنظیمات" },
      { href: "/delivery/permissions", label: "مجوزها" },
      { href: "/delivery/roles", label: "نقش‌ها" },
      { href: "/delivery/audit", label: "ممیزی" },
      { href: "/delivery/api-keys", label: "کلید API" },
      { href: "/delivery/health", label: "سلامت سرویس" },
      { href: "/delivery/capabilities", label: "قابلیت‌ها" },
    ],
  },
];

export function navForDelivery(): PortalNavGroup[] {
  return OPERATIONS_NAV;
}

export function filterNavByCapabilities(
  nav: PortalNavGroup[],
  features?: Record<string, boolean>
): PortalNavGroup[] {
  if (!features) return nav;

  const isEnabled = (feature?: string | null) => {
    if (!feature) return true;
    return features[feature] === true;
  };

  return nav
    .map((group) => {
      if (group.feature && !isEnabled(group.feature)) return null;
      if (group.items) {
        const items = group.items.filter((i) => isEnabled(i.feature));
        if (items.length === 0 && !group.href) return null;
        return { ...group, items };
      }
      return group;
    })
    .filter(Boolean) as PortalNavGroup[];
}
