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

/**
 * Fetch the aggregate payer stats shown in the payer peek modal
 * (SP21 universal drill-down).
 *
 * Caches for 60s — the backend caches the same way, so re-asks inside
 * that window are free. `retry: 1` because peek content is a low-stakes
 * summary; one retry on transient failure is enough before falling back
 * to the error UI.
 *
 * When `payerId` is `null` (the peek isn't open), the query is disabled
 * and the hook returns the idle React-Query state — no fetch.
 */
export function usePayerSummary(payerId: string | null) {
  return useQuery<PayerSummary>({
    queryKey: ["payer-summary", payerId],
    queryFn: () => api.getPayerSummary(payerId as string),
    enabled: payerId !== null,
    staleTime: 60 * 1000,
    retry: 1,
  });
}