import type { LucideIcon } from "lucide-react";
import {
  LayoutDashboard,
  Building2,
  UtensilsCrossed,
  QrCode,
  ChefHat,
  CreditCard,
  Truck,
  BarChart3,
  Settings,
  Shield,
  Users,
  Map,
  Calendar,
  Clock,
  ShoppingCart,
  Package,
  Plug,
  Bell,
  Activity,
  Layers,
} from "lucide-react";

export type HospitalityPortalId = "management";

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 HOSPITALITY_PORTAL = {
  id: "management" as const,
  label: "مدیریت مهمان‌نوازی",
  description: "داشبورد، منو، POS، آشپزخانه و یکپارچه‌سازی‌ها",
  basePath: "/hospitality",
  icon: UtensilsCrossed,
};

export const MANAGEMENT_NAV: PortalNavGroup[] = [
  { id: "dash", label: "داشبورد", icon: LayoutDashboard, href: "/hospitality" },
  {
    id: "ops",
    label: "عملیات",
    icon: Activity,
    items: [
      { href: "/hospitality/ops", label: "مرکز عملیات" },
      { href: "/hospitality/ops/kitchen", label: "داشبورد آشپزخانه", feature: "kitchen" },
      { href: "/hospitality/ops/waiter", label: "داشبورد گارسون", feature: "table_service" },
      { href: "/hospitality/ops/cashier", label: "داشبورد صندوقدار", feature: "pos_lite" },
      { href: "/hospitality/onboarding", label: "راه‌اندازی رستوران" },
    ],
  },
  {
    id: "venue",
    label: "مکان و سالن",
    icon: Building2,
    items: [
      { href: "/hospitality/branches", label: "مکان‌ها" },
      { href: "/hospitality/branches/list", label: "شعب" },
      { href: "/hospitality/dining-areas", label: "سالن‌ها" },
      { href: "/hospitality/tables", label: "میزها" },
      { href: "/hospitality/floor-map", label: "نقشه سالن", feature: "pos_pro" },
    ],
  },
  {
    id: "service",
    label: "خدمات میز",
    icon: Calendar,
    feature: "table_service",
    items: [
      { href: "/hospitality/reservations", label: "رزروها", feature: "reservation" },
      { href: "/hospitality/queue", label: "صف انتظار", feature: "table_service" },
      { href: "/hospitality/customers", label: "مشتریان", feature: "crm_connector" },
      { href: "/hospitality/guests", label: "پروفایل مهمان", feature: "qr_menu" },
    ],
  },
  {
    id: "menu",
    label: "منوی دیجیتال",
    icon: UtensilsCrossed,
    feature: "digital_menu",
    items: [
      { href: "/hospitality/menu", label: "منو" },
      { href: "/hospitality/menu/categories", label: "دسته‌بندی" },
      { href: "/hospitality/menu/items", label: "آیتم‌ها" },
      { href: "/hospitality/menu/modifiers", label: "افزودنی‌ها" },
      { href: "/hospitality/menu/modifier-options", label: "گزینه‌ها" },
      { href: "/hospitality/menu/availability", label: "دسترسی" },
      { href: "/hospitality/menu/media", label: "رسانه" },
    ],
  },
  {
    id: "qr",
    label: "QR و سفارش",
    icon: QrCode,
    feature: "qr_menu",
    items: [
      { href: "/hospitality/qr/menu", label: "منوی QR", feature: "qr_menu" },
      { href: "/hospitality/qr/ordering", label: "سفارش QR", feature: "qr_ordering" },
      { href: "/hospitality/ordering/cart", label: "سبد", feature: "qr_ordering" },
      { href: "/hospitality/ordering/checkout", label: "تسویه", feature: "qr_ordering" },
    ],
  },
  {
    id: "kitchen",
    label: "آشپزخانه",
    icon: ChefHat,
    feature: "kitchen",
    items: [
      { href: "/hospitality/kitchen/display", label: "نمایشگر" },
      { href: "/hospitality/kitchen/queue", label: "صف" },
      { href: "/hospitality/kitchen/preparation", label: "آماده‌سازی" },
    ],
  },
  {
    id: "pos",
    label: "صندوق",
    icon: CreditCard,
    feature: "pos_lite",
    items: [
      { href: "/hospitality/pos/lite", label: "POS Lite", feature: "pos_lite" },
      { href: "/hospitality/pos/pro", label: "POS Pro", feature: "pos_pro" },
      { href: "/hospitality/pos/register", label: "صندوق", feature: "pos_lite" },
      { href: "/hospitality/pos/payments", label: "پرداخت", feature: "pos_pro" },
      { href: "/hospitality/pos/discounts", label: "تخفیف", feature: "pos_pro" },
      { href: "/hospitality/pos/coupons", label: "کوپن", feature: "pos_pro" },
      { href: "/hospitality/pos/promotions", label: "پروموشن", feature: "pos_pro" },
    ],
  },
  {
    id: "orders",
    label: "سفارش‌ها",
    icon: ShoppingCart,
    items: [
      { href: "/hospitality/orders/pickup", label: "بیرون‌بر", feature: "qr_ordering" },
      { href: "/hospitality/orders/online", label: "آنلاین", feature: "qr_ordering" },
      { href: "/hospitality/orders/timeline", label: "خط زمانی", feature: "kitchen" },
      { href: "/hospitality/invoices", label: "فاکتور", feature: "pos_lite" },
      { href: "/hospitality/receipts", label: "رسید", feature: "pos_lite" },
    ],
  },
  {
    id: "integrations",
    label: "یکپارچه‌سازی",
    icon: Plug,
    items: [
      { href: "/hospitality/integrations/delivery", label: "پیک", feature: "delivery_connector" },
      { href: "/hospitality/integrations/accounting", label: "حسابداری", feature: "accounting_connector" },
      { href: "/hospitality/integrations/crm", label: "CRM", feature: "crm_connector" },
      { href: "/hospitality/integrations/loyalty", label: "وفاداری", feature: "loyalty_connector" },
      { href: "/hospitality/integrations/communication", label: "ارتباطات", feature: "communication_connector" },
      { href: "/hospitality/integrations/website", label: "وب‌سایت", feature: "website_connector" },
      { href: "/hospitality/integrations/dispatches", label: "ارسال کانکتور" },
      { href: "/hospitality/integrations/marketplace", label: "مارکت‌پلیس (foundation نیست)" },
    ],
  },
  {
    id: "insights",
    label: "گزارش و تحلیل",
    icon: BarChart3,
    feature: "analytics",
    items: [
      { href: "/hospitality/reports", label: "گزارش‌ها", feature: "analytics" },
      { href: "/hospitality/analytics", label: "آنالیتیکس", feature: "analytics" },
      { href: "/hospitality/inventory", label: "موجودی", feature: "analytics" },
    ],
  },
  {
    id: "admin",
    label: "مدیریت سیستم",
    icon: Settings,
    items: [
      { href: "/hospitality/settings", label: "تنظیمات" },
      { href: "/hospitality/bundles", label: "بسته‌ها" },
      { href: "/hospitality/roles", label: "نقش‌ها" },
      { href: "/hospitality/permissions", label: "مجوزها" },
      { href: "/hospitality/audit", label: "ممیزی" },
      { href: "/hospitality/notifications", label: "اعلان‌ها" },
      { href: "/hospitality/health", label: "سلامت" },
      { href: "/hospitality/capabilities", label: "قابلیت‌ها" },
    ],
  },
];

export function navForHospitality(): PortalNavGroup[] {
  return MANAGEMENT_NAV;
}

function featureEnabled(features: Record<string, boolean> | undefined, feature?: string | null) {
  if (!feature) return true;
  if (!features) return true;
  return features[feature] !== false;
}

export function filterNavByCapabilities(
  nav: PortalNavGroup[],
  features?: Record<string, boolean>
): PortalNavGroup[] {
  return nav
    .map((group) => {
      if (group.feature && !featureEnabled(features, group.feature)) return null;
      if (group.items) {
        const items = group.items.filter((i) => featureEnabled(features, i.feature));
        if (items.length === 0 && !group.href) return null;
        return { ...group, items };
      }
      return group;
    })
    .filter(Boolean) as PortalNavGroup[];
}
