"use client";

import { useQueryClient } from "@tanstack/react-query";
import {
  Bell,
  CalendarClock,
  ClipboardList,
  Gauge,
  HandCoins,
  LayoutDashboard,
  LineChart,
  MapPin,
  MessageSquareText,
  Package,
  PlusSquare,
  ShieldCheck,
  SlidersHorizontal,
  Store,
  Truck,
  Webhook,
  UserCog,
  Users,
  Zap,
} from "lucide-react";
import { useCan } from "@/features/auth/use-can";
import { useT } from "@/i18n";
import { queryKeys } from "@/lib/query-keys";
import { getNotifications } from "@/services/notifications";
import { BELL_PREVIEW_ROWS } from "@/features/notifications/notification-bell";
import { getOrders } from "@/services/orders";
import { getDrivers } from "@/services/drivers";
import { getVehicles } from "@/services/vehicles";
import { getDashboardStats } from "@/services/stats";
import { getPushStats } from "@/services/push";
import { getStoreClients } from "@/services/store-clients";

export interface NavItem {
  href: string;
  label: string;
  icon: React.ComponentType<{ className?: string }>;
  visible: () => boolean;
  exact?: boolean;
  prefetch?: () => void;
}

export interface NavGroup {
  label: string;
  items: NavItem[];
}

export type NavKind = "operations" | "administration";

/**
 * Single source of truth for the dashboard menu. Both the sidebar and the ⌘K
 * command palette read from here, so navigation and permissions never drift.
 * `prefetch` warms the exact TanStack Query key each destination page uses.
 */
export function useNavItems(): NavGroup[] {
  const t = useT();
  const { can } = useCan();
  const queryClient = useQueryClient();

  const prefetch = (href: string) => {
    switch (href) {
      case "/overview": {
        void queryClient.prefetchQuery({
          queryKey: queryKeys.stats,
          queryFn: async () => (await getDashboardStats()).Model ?? null,
        });
        break;
      }
      case "/orders": {
        const filters = { page: 1, rows: 10 };
        void queryClient.prefetchQuery({
          queryKey: queryKeys.orders.list(filters),
          queryFn: () => getOrders(filters),
        });
        break;
      }
      case "/captains": {
        const filters = { page: 1, rows: 10 };
        void queryClient.prefetchQuery({
          queryKey: queryKeys.drivers.list(filters),
          queryFn: () => getDrivers(filters),
        });
        break;
      }
      case "/applications": {
        const filters = { page: 1, rows: 25, status: "pending" as const };
        void queryClient.prefetchQuery({
          queryKey: queryKeys.drivers.list(filters),
          queryFn: () => getDrivers(filters),
        });
        break;
      }
      case "/vehicles": {
        const filters = { page: 1, rows: 10 };
        void queryClient.prefetchQuery({
          queryKey: queryKeys.vehicles.list(filters),
          queryFn: () => getVehicles(filters),
        });
        break;
      }
      case "/notifications": {
        // The bell's preview window — the dropdown and the page share this cache key.
        const filters = { page: 1, rows: BELL_PREVIEW_ROWS };
        void queryClient.prefetchQuery({
          queryKey: queryKeys.notifications.list(filters),
          queryFn: () => getNotifications(filters),
        });
        break;
      }
      case "/stores": {
        const filters = { page: 1, rows: 10 };
        void queryClient.prefetchQuery({
          queryKey: queryKeys.storeClients.list(filters),
          queryFn: () => getStoreClients(filters),
        });
        break;
      }
      case "/push": {
        void queryClient.prefetchQuery({
          queryKey: queryKeys.push.stats,
          queryFn: async () => (await getPushStats()).Model ?? null,
        });
        break;
      }
    }
  };

  const operations: NavItem[] = [
    {
      href: "/overview",
      label: t("nav.overview"),
      icon: LayoutDashboard,
      visible: () => true,
      exact: true,
      prefetch: () => prefetch("/overview"),
    },
    {
      href: "/orders",
      label: t("nav.orders"),
      icon: Package,
      visible: () => can(["orders.view", "orders.create", "orders.assign"]),
      prefetch: () => prefetch("/orders"),
    },
    {
      href: "/orders/new",
      label: t("nav.newOrder"),
      icon: PlusSquare,
      visible: () => can("orders.create"),
    },
    {
      href: "/captains",
      label: t("nav.captains"),
      icon: Users,
      visible: () => can(["drivers.view", "drivers.update", "drivers.review"]),
      prefetch: () => prefetch("/captains"),
    },
    {
      href: "/vehicles",
      label: t("nav.vehicles"),
      icon: Truck,
      visible: () => can(["vehicles.view", "vehicles.create", "vehicles.update"]),
      prefetch: () => prefetch("/vehicles"),
    },
    {
      href: "/applications",
      label: t("nav.applications"),
      icon: ClipboardList,
      visible: () => can("drivers.review"),
      prefetch: () => prefetch("/applications"),
    },
    {
      href: "/notifications",
      label: t("nav.notifications"),
      icon: Bell,
      visible: () => can("notifications.view"),
      prefetch: () => prefetch("/notifications"),
    },
    {
      href: "/push",
      label: t("nav.push"),
      icon: Zap,
      visible: () => can("push.view"),
      prefetch: () => prefetch("/push"),
    },
    {
      href: "/stores",
      label: t("nav.stores"),
      icon: Store,
      visible: () => can(["store_clients.view", "store_clients.review", "store_clients.manage"]),
      prefetch: () => prefetch("/stores"),
    },
    {
      href: "/integration",
      label: t("nav.integration"),
      icon: Webhook,
      visible: () => can(["integration.view", "integration.replay"]),
    },
    {
      href: "/tracking",
      label: t("nav.tracking"),
      icon: MapPin,
      visible: () => can("drivers.view"),
    },
    {
      href: "/payments",
      label: t("nav.payments"),
      icon: HandCoins,
      // The cash desk, not the orders list: an admin who may read orders has no business
      // seeing what each captain owes, and one who runs the desk may not need orders at all.
      visible: () => can("captain_ledger.view"),
    },
    {
      href: "/attendance",
      label: t("nav.attendance"),
      icon: CalendarClock,
      visible: () => can("drivers.view"),
    },
    {
      href: "/performance",
      label: t("nav.performance"),
      icon: LineChart,
      // The dispatch counters, not the order list: this is the permission the KPI endpoint
      // itself checks, so the link never leads to a screen that answers 403.
      visible: () => can("dispatch.view"),
    },
    {
      href: "/support",
      label: t("nav.support"),
      icon: MessageSquareText,
      visible: () => true,
    },
  ];

  const administration: NavItem[] = [
    {
      href: "/settings",
      label: t("nav.myProfile"),
      icon: Gauge,
      visible: () => true,
    },
    {
      href: "/settings/admins",
      label: t("nav.admins"),
      icon: UserCog,
      visible: () => can(["admins.view", "admins.create", "admins.update"]),
    },
    {
      href: "/settings/roles",
      label: t("nav.roles"),
      icon: ShieldCheck,
      visible: () => can(["roles.view", "roles.create", "roles.update"]),
    },
    {
      href: "/settings/dispatch",
      label: t("nav.dispatchSettings"),
      icon: SlidersHorizontal,
      // Reading the dials is enough to be shown the page; changing one is checked on the screen,
      // and again by the server.
      visible: () => can("dispatch.view"),
    },
  ];

  return [
    { label: t("nav.operations"), items: operations },
    { label: t("nav.administration"), items: administration },
  ];
}