import type { LucideIcon } from "lucide-react";
import {
  LayoutDashboard,
  BriefcaseBusiness,
  Building2,
  UsersRound,
  FolderKanban,
  CalendarCheck2,
  ListTodo,
  CreditCard,
  MessagesSquare,
  Files,
  BarChart3,
  Settings,
  UserCog,
  HeadsetIcon,
  CircleUserRound,
  Inbox,
  Scale,
} from "lucide-react";

export type TenantMenuItem = {
  key: string;
  href: string;
  icon: LucideIcon;
  requiredRoleIds?: number[];
  tenantAdminOnly?: boolean;
};

export const TENANT_MENU: TenantMenuItem[] = [
  { key: "dashboard", href: "/dashboard", icon: LayoutDashboard },

  // --- Référentiels ---
  { key: "portfolio",  href: "/portfolio",  icon: BriefcaseBusiness },
  { key: "customers",  href: "/customers",  icon: Building2 },
  { key: "creditors",  href: "/creditors",  icon: Scale },
  { key: "debtors",    href: "/debtors",    icon: UsersRound },

  // --- Recouvrement ---
  { key: "cases", href: "/cases", icon: FolderKanban },
  //{ key: "promises", href: "/collections/promises", icon: CalendarCheck2 },
  //{ key: "tasks", href: "/collections/tasks", icon: ListTodo },
  //{ key: "payments", href: "/collections/payments", icon: CreditCard },

  // --- Comptabilité / Trésorerie ---
  // Page: app/[locale]/(app)/treasury/page.tsx
  { key: "treasury", href: "/treasury", icon: CreditCard },

  // --- Support ---
  { key: "communications", href: "/communications", icon: MessagesSquare },
  { key: "support",        href: "/support",        icon: HeadsetIcon },
  //{ key: "documents", href: "/documents", icon: Files },

  // --- Pilotage ---
  //{ key: "reports", href: "/reports", icon: BarChart3 },

  // --- Mail entrant ---
  { key: "mail_inbox", href: "/mail-inbox", icon: Inbox },

  // --- Admin ---
  { key: "settings", href: "/settings", icon: CircleUserRound, tenantAdminOnly: true },
  { key: "team", href: "/team", icon: UserCog, tenantAdminOnly: true },
];

export function getTenantMenu(opts: { isTenantAdmin: boolean; roleIds?: number[] }): TenantMenuItem[] {
  const roleSet = new Set(opts.roleIds ?? []);
  return TENANT_MENU.filter((it) => {
    if (it.tenantAdminOnly && !opts.isTenantAdmin) return false;
    if (it.requiredRoleIds?.length) {
      return it.requiredRoleIds.some((r) => roleSet.has(r));
    }
    return true;
  });
}
