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

/**
 * Server-aggregated KPI totals for the Remittances page tiles.
 * Returns ``{count, total_paid, total_adjustments}`` summed over the
 * full filtered remittance population — NOT a page-limited sample.
 *
 * The page's KPI tiles consume this hook instead of summing
 * ``items.reduce(...)`` over the current page (25 rows) + live-tail
 * delta, which silently understates the true population in the same
 * shape that ``59c3275`` retired for the Dashboard and that
 * ``d81b6ed`` retired for the count tile.
 *
 * The optional ``params`` object lets the page pass through the same
 * filter chips it uses for the list endpoint (``status``, ``payer``,
 * ``date_from``, ``date_to``) so the summary and the row list always
 * agree on which rows they're describing.
 */
export function useRemittanceSummary(params: ListRemittancesParams = {}) {
  return useQuery<RemittanceSummary>({
    queryKey: ["remittances", "summary", params],
    queryFn: () => api.listRemittanceSummary(params),
    enabled: api.isConfigured,
  });
}
