import type { LucideIcon } from "lucide-react";
import {
  LayoutDashboard,
  MessageSquare,
  Server,
  FileText,
  Users,
  Send,
  ListOrdered,
  BarChart3,
  Activity,
  Settings,
  Shield,
  Key,
  Plug,
  Mail,
  Bell,
  Phone,
  Video,
  MessagesSquare,
  Workflow,
  Brain,
  Webhook,
  Layers,
  History,
  BookOpen,
  Target,
  Radio,
  Lock,
} from "lucide-react";

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

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

export const COMMUNICATION_PORTAL = {
  id: "communication" as const,
  label: "تربت ارتباط",
  description: "پلتفرم Enterprise Communication — SMS Production Ready",
  basePath: "/communication",
  icon: MessageSquare,
};

export const COMMUNICATION_NAV: PortalNavGroup[] = [
  {
    id: "dashboards",
    label: "داشبوردها",
    icon: LayoutDashboard,
    items: [
      { href: "/communication", label: "داشبورد اجرایی" },
      { href: "/communication/dashboard", label: "داشبورد ارتباطات" },
      { href: "/communication/providers/dashboard", label: "داشبورد ارائه‌دهندگان" },
      { href: "/communication/sms", label: "داشبورد SMS", feature: "sms" },
    ],
  },
  {
    id: "sms",
    label: "پلتفرم SMS",
    icon: Send,
    feature: "sms",
    items: [
      { href: "/communication/sms/providers", label: "ارائه‌دهندگان SMS", feature: "sms" },
      { href: "/communication/sms/templates", label: "قالب‌ها", feature: "sms" },
      {
        href: "/communication/sms/template-categories",
        label: "دسته‌بندی قالب‌ها",
        locked: true,
        lockKey: "template_categories",
      },
      { href: "/communication/sms/senders", label: "Sender ID", feature: "sms" },
      { href: "/communication/sms/campaigns", label: "کمپین‌های SMS", feature: "sms" },
      { href: "/communication/sms/send", label: "ارسال پیام", feature: "sms" },
      { href: "/communication/sms/queue", label: "صف SMS", feature: "queue_engine" },
      { href: "/communication/sms/delivery-reports", label: "گزارش تحویل", feature: "delivery_tracking" },
      { href: "/communication/sms/messages", label: "تاریخچه پیام", feature: "sms" },
    ],
  },
  {
    id: "contacts",
    label: "مخاطبین",
    icon: Users,
    feature: "dynamic_contact_sources",
    items: [
      { href: "/communication/contacts/recipients", label: "گیرندگان", feature: "dynamic_contact_sources" },
      { href: "/communication/contacts/sources", label: "منابع پویا", feature: "dynamic_contact_sources" },
      { href: "/communication/contacts/address-books", label: "دفترچه آدرس", feature: "dynamic_contact_sources" },
    ],
  },
  {
    id: "channels",
    label: "کانال‌های آینده",
    icon: Layers,
    items: [
      { href: "/communication/email", label: "ایمیل", locked: true, lockKey: "email" },
      { href: "/communication/whatsapp", label: "واتساپ", locked: true, lockKey: "whatsapp" },
      { href: "/communication/telegram", label: "تلگرام", locked: true, lockKey: "telegram" },
      { href: "/communication/rubika", label: "روبیکا", locked: true, lockKey: "rubika" },
      { href: "/communication/push", label: "Push", locked: true, lockKey: "push" },
      { href: "/communication/voice", label: "تماس صوتی", locked: true, lockKey: "voice" },
      { href: "/communication/video", label: "تماس ویدیویی", locked: true, lockKey: "video" },
      { href: "/communication/live-chat", label: "Live Chat", locked: true, lockKey: "live_chat" },
      { href: "/communication/inbox", label: "Omnichannel Inbox", locked: true, lockKey: "omnichannel_inbox" },
    ],
  },
  {
    id: "automation",
    label: "اتوماسیون",
    icon: Workflow,
    items: [
      { href: "/communication/automation", label: "Marketing Automation", locked: true, lockKey: "marketing_automation" },
      { href: "/communication/automation/triggers", label: "Automation Triggers", locked: true, lockKey: "automation_triggers" },
      { href: "/communication/journey-builder", label: "Journey Builder", locked: true, lockKey: "journey_builder" },
      { href: "/communication/ai-campaigns", label: "AI Campaigns", locked: true, lockKey: "ai_campaigns" },
      { href: "/communication/campaign-orchestrator", label: "Campaign Orchestrator", locked: true, lockKey: "campaign_orchestrator" },
    ],
  },
  {
    id: "insights",
    label: "گزارش و تحلیل",
    icon: BarChart3,
    items: [
      { href: "/communication/reports", label: "گزارش‌ها", feature: "metrics" },
      { href: "/communication/analytics", label: "آنالیتیکس", feature: "metrics" },
      { href: "/communication/otp", label: "OTP", feature: "otp" },
      { href: "/communication/conversations", label: "خط زمانی مکالمات", locked: true, lockKey: "conversations" },
    ],
  },
  {
    id: "platform",
    label: "پلتفرم",
    icon: Server,
    items: [
      { href: "/communication/monitoring", label: "مانیتoring", feature: "metrics" },
      { href: "/communication/health", label: "سلامت سرویس" },
      { href: "/communication/capabilities", label: "قابلیت‌ها" },
      { href: "/communication/webhooks", label: "Webhook Center", locked: true, lockKey: "webhook_center" },
      { href: "/communication/connectors", label: "Connectors", locked: true, lockKey: "connectors" },
      { href: "/communication/notifications", label: "Notification Center", locked: true, lockKey: "notification_center" },
    ],
  },
  {
    id: "admin",
    label: "مدیریت",
    icon: Settings,
    items: [
      { href: "/communication/settings", label: "تنظیمات", feature: "provider_framework" },
      { href: "/communication/permissions", label: "مجوزها" },
      { href: "/communication/roles", label: "نقش‌ها", locked: true, lockKey: "roles" },
      { href: "/communication/audit", label: "ممیزی", locked: true, lockKey: "audit" },
      { href: "/communication/api-keys", label: "کلید API", locked: true, lockKey: "api_keys" },
      { href: "/communication/integrations", label: "Integrations", locked: true, lockKey: "integrations_hub" },
    ],
  },
];

export function navForCommunication(): PortalNavGroup[] {
  return COMMUNICATION_NAV;
}

/** Show all nav items; do not hide locked/future modules. */
export function filterNavForDisplay(
  nav: PortalNavGroup[],
  features?: Record<string, boolean>
): PortalNavGroup[] {
  return nav.map((group) => ({
    ...group,
    items: group.items?.map((item) => ({
      ...item,
      locked:
        item.locked ||
        (item.feature ? features?.[item.feature] !== true : false) ||
        false,
    })),
  }));
}

export { Lock };
