import type { LucideIcon } from "lucide-react";
import {
  Boxes,
  Calculator,
  ClipboardList,
  Coins,
  Landmark,
  Flag,
  Layers3,
  LayoutDashboard,
  PackageSearch,
  Pin,
  Percent,
  ScanSearch,
  Settings,
  Shuffle,
  ScrollText,
  ShoppingCart,
  Store,
  Waves,
  Truck,
  Users
} from "lucide-react";

import type { AppRole } from "@/config/access-control";

export type NavItem = {
  type: "link";
  href: string;
  label: string;
  description: string;
  icon: LucideIcon;
  roles: AppRole[];
};

export type NavGroup = {
  type: "group";
  key: string;
  label: string;
  description: string;
  icon: LucideIcon;
  roles: AppRole[];
  children: NavItem[];
};

export type NavEntry = NavItem | NavGroup;

export const primaryNavigation: NavEntry[] = [
  {
    type: "link",
    href: "/dashboard",
    label: "Dashboard",
    description: "Ringkasan stok, margin, dan alert operasional.",
    icon: LayoutDashboard,
    roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING", "WAREHOUSE", "QC", "SALES"]
  },
  {
    type: "link",
    href: "/users",
    label: "Pengguna",
    description: "Kelola akun pengguna aplikasi dan reset akses.",
    icon: Users,
    roles: ["ADMIN", "SYSTEM_ADMIN"]
  },
  {
    type: "group",
    key: "master-data",
    label: "Master Data",
    description: "Seluruh master data operasional aplikasi.",
    icon: Layers3,
    roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING", "WAREHOUSE", "QC", "SALES"],
    children: [
      {
        type: "link",
        href: "/agents",
        label: "Agen",
        description: "Master agen dengan identitas, saldo, rekening, dan skema bagi hasil.",
        icon: Users,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING", "SALES"]
      },
      {
        type: "link",
        href: "/sales",
        label: "Sales",
        description: "Master sales dengan identitas, saldo, dan rekening bank.",
        icon: Users,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING", "SALES"]
      },
      {
        type: "link",
        href: "/buyers",
        label: "Pembeli",
        description: "Master buyer dan kebutuhan penjualan.",
        icon: Users,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "SALES"]
      },
      {
        type: "link",
        href: "/employees",
        label: "Pegawai",
        description: "Master karyawan dan kontak operasional.",
        icon: Users,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING", "WAREHOUSE", "QC", "SALES"]
      },
      {
        type: "link",
        href: "/grades",
        label: "Grade",
        description: "Master grade baru dengan pembeda mangkok dan standar harga.",
        icon: Layers3,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING", "QC", "SALES"]
      },
      {
        type: "link",
        href: "/warehouses",
        label: "Gudang",
        description: "Master gudang operasional.",
        icon: Boxes,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "WAREHOUSE"]
      },
      {
        type: "link",
        href: "/warehouse-locations",
        label: "Lokasi Gudang",
        description: "Lokasi penyimpanan di dalam gudang.",
        icon: Pin,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "WAREHOUSE"]
      },
      {
        type: "link",
        href: "/adjustment-reasons",
        label: "Alasan Penyesuaian",
        description: "Master alasan adjustment dan shrinkage.",
        icon: Flag,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "WAREHOUSE", "QC"]
      },
      {
        type: "link",
        href: "/banks",
        label: "Bank",
        description: "Master bank untuk dropdown pembayaran supplier dan buyer.",
        icon: Landmark,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING", "SALES"]
      },
      {
        type: "link",
        href: "/couriers",
        label: "Jasa Pengiriman",
        description: "Master jasa pengiriman untuk pengiriman, penjemputan, dan kontak operasional.",
        icon: Truck,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING", "WAREHOUSE", "SALES"]
      },
      {
        type: "link",
        href: "/washing-places",
        label: "Tempat Cuci",
        description: "Master tempat washing beserta alamat dan multi contact person.",
        icon: Store,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING", "WAREHOUSE", "QC"]
      },
      {
        type: "link",
        href: "/currencies",
        label: "Mata Uang",
        description: "Master mata uang default sistem dan referensi finansial.",
        icon: Coins,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER"]
      },
      {
        type: "link",
        href: "/profit-share-schemes",
        label: "Skema Bagi Hasil",
        description: "Master skema bagi hasil agen dan perusahaan.",
        icon: Percent,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER"]
      }
    ]
  },
  {
    type: "group",
    key: "purchases",
    label: "Pembelian",
    description: "Pembelian reguler dan buyout kantor dari jalur agen.",
    icon: ClipboardList,
    roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING"],
    children: [
      {
        type: "link",
        href: "/purchases",
        label: "Pembelian Reguler",
        description: "Pembelian biasa multi line untuk barang masuk reguler.",
        icon: ClipboardList,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING"]
      },
      {
        type: "link",
        href: "/purchases/office-buyout",
        label: "Pembelian Kantor / Buyout Agen",
        description: "Putus rantai bagi hasil dengan membuat lot kantor baru dari lot agen.",
        icon: ScrollText,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING"]
      }
    ]
  },
  {
    type: "link",
    href: "/fund-requests",
    label: "Permintaan Dana",
    description: "Transfer dana modal atau bagi hasil ke agen dengan snapshot rekening dan bukti transfer.",
    icon: Coins,
    roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING"]
  },
  {
    type: "link",
    href: "/purchase-analysis",
    label: "Analisis Pembelian",
    description: "Sheet costing, kadar, dan laba rugi per purchase.",
    icon: Calculator,
    roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING"]
  },
  {
    type: "link",
    href: "/purchase-realization",
    label: "Realisasi Pembelian",
    description: "Realisasi hasil purchase dari opening cost sampai closing final.",
    icon: Calculator,
    roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING"]
  },
  {
    type: "link",
    href: "/lots",
    label: "Lot Persediaan",
    description: "Pusat traceability dan costing per lot.",
    icon: Boxes,
    roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "WAREHOUSE", "QC", "SALES"]
  },
  {
    type: "group",
    key: "stock-activities",
    label: "Aktivitas Stok",
    description: "Aktivitas operasional stok seperti mixing, adjustment, dan washing.",
    icon: Shuffle,
    roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING", "WAREHOUSE", "QC"],
    children: [
      {
        type: "link",
        href: "/sorting",
        label: "Mixing Lot",
        description: "Mixing, regrade, dan transformasi lot.",
        icon: PackageSearch,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "QC"]
      },
      {
        type: "link",
        href: "/stock-adjustments",
        label: "Penyesuaian Stok",
        description: "Koreksi qty lot karena susut, damage, atau penambahan.",
        icon: ClipboardList,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "WAREHOUSE", "QC"]
      },
      {
        type: "link",
        href: "/washing",
        label: "Pencucian",
        description: "Kirim lot ke tempat cuci dan selesaikan hasilnya ke gudang.",
        icon: Waves,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING", "WAREHOUSE", "QC"]
      }
    ]
  },
  {
    type: "group",
    key: "outbound-transactions",
    label: "Transaksi Keluar",
    description: "Seluruh transaksi keluar barang untuk penjualan dan titip jual.",
    icon: ShoppingCart,
    roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING", "WAREHOUSE", "SALES"],
    children: [
      {
        type: "link",
        href: "/sales-regular",
        label: "Penjualan Reguler",
        description: "Penjualan outbound standar dengan multi item, multi mata uang, retur, dan susut.",
        icon: ShoppingCart,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "SALES"]
      },
      {
        type: "link",
        href: "/sales-jit",
        label: "Penjualan JIT",
        description: "Alur penjualan just in time dengan pemrosesan cepat berbasis kebutuhan langsung.",
        icon: ShoppingCart,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "SALES"]
      },
      {
        type: "link",
        href: "/consignments",
        label: "Titip Jual",
        description: "Barang keluar dibawa sales dengan closing per item saat terjual atau kembali.",
        icon: ClipboardList,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER", "PURCHASING", "WAREHOUSE", "SALES"]
      }
    ]
  },
  {
    type: "group",
    key: "settings",
    label: "Pengaturan",
    description: "Konfigurasi sistem dan jejak aktivitas aplikasi.",
    icon: Settings,
    roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER"],
    children: [
      {
        type: "link",
        href: "/settings",
        label: "Sistem",
        description: "Konfigurasi sistem, numbering, email, dan preferensi aplikasi.",
        icon: Settings,
        roles: ["ADMIN", "SYSTEM_ADMIN", "OWNER"]
      },
      {
        type: "link",
        href: "/audit-trail",
        label: "Jejak Audit",
        description: "Jejak aktivitas user dan perubahan transaksi.",
        icon: ScrollText,
        roles: ["ADMIN", "SYSTEM_ADMIN"]
      }
    ]
  }
];

export function isNavGroup(item: NavEntry): item is NavGroup {
  return item.type === "group";
}

export function getNavigationForRole(role: AppRole) {
  return primaryNavigation
    .map((item) => {
      if (item.type === "link") {
        return item.roles.includes(role) ? item : null;
      }

      const children = item.children.filter((child) => child.roles.includes(role));
      if (!item.roles.includes(role) || children.length === 0) return null;
      return { ...item, children };
    })
    .filter(Boolean) as NavEntry[];
}
