import {
  BarChart3, History, LayoutDashboard, MapPin, Package, Receipt, Settings, Table2, TriangleAlert, Upload, Users, UserCog,
  type LucideIcon,
} from "lucide-react";
import type { Role } from "@/types";

export interface NavItem {
  href: string;
  label: string;
  icon: LucideIcon;
  roles: Role[];
  /** Tampil di bottom navigation HP (maks. 4 item per role). */
  mobile?: boolean;
  group?: "utama" | "master" | "sistem";
}

export const NAV_ITEMS: NavItem[] = [
  { href: "/dashboard", label: "Dashboard", icon: LayoutDashboard, roles: ["admin", "kolektor"], mobile: true, group: "utama" },
  { href: "/tagihan", label: "Grid Tagihan", icon: Table2, roles: ["admin", "kolektor"], mobile: true, group: "utama" },
  { href: "/bayar", label: "Input Bayar", icon: Receipt, roles: ["admin", "kolektor"], mobile: true, group: "utama" },
  { href: "/tunggakan", label: "Tunggakan", icon: TriangleAlert, roles: ["admin", "kolektor"], mobile: true, group: "utama" },
  { href: "/pelanggan", label: "Pelanggan", icon: Users, roles: ["admin", "kolektor", "teknisi"], mobile: true, group: "utama" },
  { href: "/laporan", label: "Laporan", icon: BarChart3, roles: ["admin", "kolektor"], group: "utama" },
  { href: "/master/area", label: "Area", icon: MapPin, roles: ["admin"], group: "master" },
  { href: "/master/paket", label: "Paket", icon: Package, roles: ["admin"], group: "master" },
  { href: "/master/pengguna", label: "Pengguna", icon: UserCog, roles: ["admin"], group: "master" },
  { href: "/import", label: "Import Excel", icon: Upload, roles: ["admin"], group: "sistem" },
  { href: "/log-aktivitas", label: "Log Aktivitas", icon: History, roles: ["admin"], group: "sistem" },
  { href: "/pengaturan", label: "Pengaturan", icon: Settings, roles: ["admin"], group: "sistem" },
];

export const NAV_GROUP_LABELS: Record<NonNullable<NavItem["group"]>, string> = {
  utama: "Menu Utama",
  master: "Master Data",
  sistem: "Sistem",
};

export function navForRole(role: Role): NavItem[] {
  return NAV_ITEMS.filter((item) => item.roles.includes(role));
}

/** Item navigasi yang menaungi path tertentu (prefix terpanjang). */
export function findNavItem(pathname: string): NavItem | undefined {
  return [...NAV_ITEMS]
    .sort((a, b) => b.href.length - a.href.length)
    .find((item) => pathname === item.href || pathname.startsWith(item.href + "/"));
}

/** Halaman awal setelah login sesuai peran. */
export function homeForRole(role: Role): string {
  return role === "teknisi" ? "/pelanggan" : "/dashboard";
}

