import type { LucideIcon } from "lucide-react";
import {
  LayoutDashboard,
  LayoutGrid,
  UtensilsCrossed,
  Users,
  Ticket,
  Package,
  BookOpen,
  UserCog,
  BarChart3,
  Calendar,
  Star,
  MessageSquare,
  Receipt,
  Settings,
  ChefHat,
  ListOrdered,
  Building2,
  CreditCard,
  Wallet,
  Clock,
  LifeBuoy,
} from "lucide-react";

export type NavGroupId = "main" | "customers" | "management";

export type NavItemKey =
  | "home"
  | "pos"
  | "tables"
  | "kds"
  | "queue"
  | "reservations"
  | "menu"
  | "reports"
  | "crm"
  | "coupons"
  | "sms"
  | "reviews"
  | "inventory"
  | "expenses"
  | "shifts"
  | "taxes"
  | "hr"
  | "branches"
  | "subscription"
  | "support"
  | "settings";

export type NavItemDef = {
  key: NavItemKey;
  href: string;
  icon: LucideIcon;
};

export type NavGroupDef = {
  id: NavGroupId;
  /** Flat groups render without a header and are always expanded. */
  flat?: boolean;
  defaultOpen: boolean;
  items: NavItemDef[];
};

/**
 * Frequency-based IA: the flat "main" group holds everything a café touches
 * daily (always visible, no clicks to reach); the two collapsible groups hold
 * weekly/monthly destinations; FOOTER_NAV holds rare utility pages.
 */
export const NAV_GROUPS: NavGroupDef[] = [
  {
    id: "main",
    flat: true,
    defaultOpen: true,
    items: [
      { key: "home", href: "/", icon: LayoutDashboard },
      { key: "pos", href: "/pos", icon: LayoutGrid },
      { key: "tables", href: "/tables", icon: UtensilsCrossed },
      { key: "kds", href: "/kds", icon: ChefHat },
      { key: "queue", href: "/queue", icon: ListOrdered },
      { key: "reservations", href: "/reservations", icon: Calendar },
      { key: "menu", href: "/menu", icon: BookOpen },
      { key: "reports", href: "/reports", icon: BarChart3 },
    ],
  },
  {
    id: "customers",
    defaultOpen: false,
    items: [
      { key: "crm", href: "/crm", icon: Users },
      { key: "coupons", href: "/coupons", icon: Ticket },
      { key: "sms", href: "/sms", icon: MessageSquare },
      { key: "reviews", href: "/reviews", icon: Star },
      // NOTE: café discovery (browsing other cafés) lives in Koja, not the owner
      // panel. The owner manages their OWN Koja listing from Settings.
    ],
  },
  {
    id: "management",
    defaultOpen: false,
    items: [
      { key: "inventory", href: "/inventory", icon: Package },
      { key: "expenses", href: "/expenses", icon: Wallet },
      { key: "shifts", href: "/shifts", icon: Clock },
      { key: "taxes", href: "/taxes", icon: Receipt },
      { key: "hr", href: "/hr", icon: UserCog },
      { key: "branches", href: "/branches", icon: Building2 },
    ],
  },
];

/** Compact utility links rendered in the sidebar footer (outside the scroll area). */
export const FOOTER_NAV: NavItemDef[] = [
  { key: "settings", href: "/settings", icon: Settings },
  { key: "subscription", href: "/subscription", icon: CreditCard },
  { key: "support", href: "/support", icon: LifeBuoy },
];

/** Every nav destination (groups + footer) — used by the route guard. */
export const ALL_NAV_ITEMS: NavItemDef[] = [
  ...NAV_GROUPS.flatMap((g) => g.items),
  ...FOOTER_NAV,
];

export const NAV_GROUPS_STORAGE_KEY = "meezi:nav-groups:v5";

/** Branch-scoped staff only see daily-operations destinations. */
export const BRANCH_ALLOWED_NAV_KEYS: ReadonlySet<NavItemKey> = new Set<NavItemKey>([
  "home",
  "pos",
  "tables",
  "kds",
  "queue",
  "reservations",
]);

export function findNavGroupForPath(pathname: string): NavGroupId | null {
  for (const group of NAV_GROUPS) {
    if (group.flat) continue; // flat groups have no open/closed state
    for (const item of group.items) {
      if (pathname === item.href || pathname.startsWith(`${item.href}/`)) {
        return group.id;
      }
    }
  }
  return null;
}
