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

export type NavGroupId = "operations" | "menuSales" | "customers" | "finance" | "management";

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

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

export type NavGroupDef = {
  id: NavGroupId;
  defaultOpen: boolean;
  items: NavItemDef[];
};

export const NAV_GROUPS: NavGroupDef[] = [
  {
    id: "operations",
    defaultOpen: true,
    items: [
      { key: "pos", href: "/pos", icon: LayoutGrid },
      { key: "tables", href: "/tables", icon: UtensilsCrossed },
      { key: "queue", href: "/queue", icon: ListOrdered },
      { key: "kds", href: "/kds", icon: ChefHat },
      { key: "notifications", href: "/notifications", icon: Bell },
      { key: "reservations", href: "/reservations", icon: Calendar },
    ],
  },
  {
    id: "menuSales",
    defaultOpen: true,
    items: [
      { key: "menu", href: "/menu", icon: BookOpen },
      { key: "inventory", href: "/inventory", icon: Package },
      { key: "coupons", href: "/coupons", icon: Ticket },
    ],
  },
  {
    id: "customers",
    defaultOpen: true,
    items: [
      { key: "crm", href: "/crm", icon: Users },
      { key: "sms", href: "/sms", icon: MessageSquare },
      { key: "reviews", href: "/reviews", icon: Star },
      { key: "discover", href: "/discover", icon: Compass },
    ],
  },
  {
    id: "finance",
    defaultOpen: true,
    items: [
      { key: "reports", href: "/reports", icon: BarChart3 },
      { key: "expenses", href: "/expenses", icon: Wallet },
      { key: "shifts", href: "/shifts", icon: Clock },
      { key: "taxes", href: "/taxes", icon: Receipt },
    ],
  },
  {
    id: "management",
    defaultOpen: true,
    items: [
      { key: "hr", href: "/hr", icon: UserCog },
      { key: "branches", href: "/branches", icon: Building2 },
      { key: "subscription", href: "/subscription", icon: CreditCard },
      { key: "settings", href: "/settings", icon: Settings },
      { key: "support", href: "/support", icon: LifeBuoy },
    ],
  },
];

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

/** Branch-scoped staff only see daily operations. */
export const BRANCH_ONLY_NAV_GROUP: NavGroupId = "operations";

export function findNavGroupForPath(pathname: string): NavGroupId | null {
  for (const group of NAV_GROUPS) {
    for (const item of group.items) {
      if (pathname === item.href || pathname.startsWith(`${item.href}/`)) {
        return group.id;
      }
    }
  }
  return null;
}
