import type { LucideIcon } from "lucide-react";
import {
  Activity,
  BarChart3,
  Building2,
  Calendar,
  FileText,
  Handshake,
  HeartPulse,
  Kanban,
  LayoutDashboard,
  Mail,
  Phone,
  Settings,
  Shield,
  Tag,
  Target,
  TrendingUp,
  UserCircle,
  Users,
  Zap,
} from "lucide-react";

export type CrmPortalId = "sales";

export type PortalNavItem = {
  id: string;
  label: string;
  href: string;
  shortcut?: string;
};

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

export const CRM_PORTALS: {
  id: CrmPortalId;
  label: string;
  description: string;
  basePath: string;
  icon: LucideIcon;
}[] = [
  {
    id: "sales",
    label: "فروش و CRM",
    description: "مدیریت سرنخ، مخاطب، پipeline و فعالیت‌های فروش",
    basePath: "/crm/sales",
    icon: Handshake,
  },
];

export const SALES_NAV: PortalNavGroup[] = [
  {
    id: "dashboards",
    label: "داشبوردها",
    icon: LayoutDashboard,
    items: [
      { id: "overview", label: "نمای کلی", href: "/crm/sales/dashboard/overview" },
      { id: "sales-dash", label: "فروش", href: "/crm/sales/dashboard/sales" },
      { id: "pipeline-dash", label: "قیف فروش", href: "/crm/sales/dashboard/pipeline" },
      { id: "lead-dash", label: "سرنخ‌ها", href: "/crm/sales/dashboard/leads" },
      { id: "activity-dash", label: "فعالیت‌ها", href: "/crm/sales/dashboard/activities" },
      { id: "calendar-dash", label: "تقویم", href: "/crm/sales/dashboard/calendar" },
    ],
  },
  {
    id: "entities",
    label: "مشتریان",
    icon: Users,
    items: [
      { id: "customers", label: "مشتریان", href: "/crm/sales/customers" },
      { id: "companies", label: "شرکت‌ها", href: "/crm/sales/companies" },
      { id: "contacts", label: "مخاطبین", href: "/crm/sales/contacts" },
      { id: "leads", label: "سرنخ‌ها", href: "/crm/sales/leads" },
    ],
  },
  {
    id: "deals",
    label: "فروش",
    icon: TrendingUp,
    items: [
      { id: "deals", label: "معاملات", href: "/crm/sales/deals" },
      { id: "kanban", label: "کانبان", href: "/crm/sales/kanban" },
      { id: "pipelines", label: "قیف‌ها", href: "/crm/sales/pipelines" },
      { id: "quotes", label: "پیش‌فاکتورها", href: "/crm/sales/quotes" },
    ],
  },
  {
    id: "collaboration",
    label: "همکاری",
    icon: Activity,
    items: [
      { id: "activities", label: "فعالیت‌ها", href: "/crm/sales/activities" },
      { id: "tasks", label: "وظایف", href: "/crm/sales/tasks" },
      { id: "meetings", label: "جلسات", href: "/crm/sales/meetings" },
      { id: "calls", label: "تماس‌ها", href: "/crm/sales/calls" },
      { id: "emails", label: "ایمیل‌ها", href: "/crm/sales/emails" },
      { id: "notes", label: "یادداشت‌ها", href: "/crm/sales/notes" },
      { id: "timeline", label: "خط زمانی", href: "/crm/sales/timeline" },
      { id: "documents", label: "اسناد", href: "/crm/sales/documents" },
    ],
  },
  {
    id: "marketing",
    label: "بازاریابی",
    icon: Target,
    items: [
      { id: "campaigns", label: "کمپین‌ها", href: "/crm/sales/campaigns" },
      { id: "segments", label: "سگمنت‌ها", href: "/crm/sales/segments" },
      { id: "tags", label: "برچسب‌ها", href: "/crm/sales/tags" },
    ],
  },
  {
    id: "data",
    label: "داده",
    icon: FileText,
    items: [
      { id: "custom-fields", label: "فیلدهای سفارشی", href: "/crm/sales/custom-fields" },
      { id: "import", label: "ورود داده", href: "/crm/sales/import" },
      { id: "export", label: "خروجی", href: "/crm/sales/export" },
    ],
  },
  {
    id: "insights",
    label: "گزارش‌ها",
    icon: BarChart3,
    items: [
      { id: "reports", label: "گزارش‌ها", href: "/crm/sales/reports" },
      { id: "analytics", label: "تحلیل‌ها", href: "/crm/sales/analytics" },
      { id: "automation", label: "اتوماسیون", href: "/crm/sales/automation" },
    ],
  },
  {
    id: "admin",
    label: "مدیریت",
    icon: Shield,
    items: [
      { id: "notifications", label: "اعلان‌ها", href: "/crm/sales/notifications" },
      { id: "roles", label: "نقش‌ها", href: "/crm/sales/roles" },
      { id: "permissions", label: "دسترسی‌ها", href: "/crm/sales/permissions" },
      { id: "integrations", label: "یکپارچه‌سازی", href: "/crm/sales/integrations" },
      { id: "api-keys", label: "کلید API", href: "/crm/sales/api-keys" },
      { id: "audit-logs", label: "لاگ ممیزی", href: "/crm/sales/audit-logs" },
      { id: "settings", label: "تنظیمات", href: "/crm/sales/settings" },
      { id: "health", label: "سلامت سرویس", href: "/crm/sales/health" },
      { id: "capabilities", label: "قابلیت‌ها", href: "/crm/sales/capabilities" },
    ],
  },
];

export function navForPortal(portalId: CrmPortalId): PortalNavGroup[] {
  if (portalId === "sales") return SALES_NAV;
  return [];
}

export function detectPortal(pathname: string): CrmPortalId | null {
  if (pathname.startsWith("/crm/sales")) return "sales";
  return null;
}

/** Keyboard shortcuts map */
export const CRM_SHORTCUTS: Record<string, string> = {
  "g o": "/crm/sales/dashboard/overview",
  "g l": "/crm/sales/leads",
  "g c": "/crm/sales/contacts",
  "g d": "/crm/sales/deals",
  "g k": "/crm/sales/kanban",
};
