import { useQuery } from "@tanstack/react-query";
import { api, type AckAggregates } from "@/lib/api";

/**
 * Lightweight 999 ACK aggregates for sidebar / nav badges.
 *
 * Hits `/api/acks?limit=1` so the server-side aggregates (which are
 * computed by iterating every persisted row) come back without
 * pulling 100 rows for a sidebar badge. The 1-row payload is the
 * smallest the endpoint accepts (limit ge=1).
 *
 * Used by the Sidebar's "999 ACKs" nav badge to surface the
 * total rejected segment count without forcing a full page fetch.
 * 60s stale time matches the page-level fetch cadence — the badge
 * refreshes when the user navigates to the page anyway.
 *
 * Distinct from useAcks (which fetches paginated rows + aggregates
 * for the page body). The query key intentionally differs so React
 * Query doesn't dedupe the two into a single entry.
 */
export function useAckStats() {
  return useQuery<{ aggregates: AckAggregates }>({
    queryKey: ["acks", "stats"],
    queryFn: () => api.listAcks({ limit: 1 }) as Promise<{ aggregates: AckAggregates }>,
    enabled: api.isConfigured,
    staleTime: 60_000,
    refetchOnWindowFocus: false,
  });
}