import { keepPreviousData, useQuery } from '@tanstack/react-query';
import { payoutsApi } from '../apis';
import { payoutKeys } from '../keys';
import { ADMIN_BATCHES_STALE_TIME, PAYOUTS_GC_TIME, PAYOUTS_PAGE_SIZE } from '../constants';
import type { PayoutBatchFilters } from '../types';

/**
 * The admin reconciliation list of payout batches (newest first). The **filter + page params are part of the
 * query key** so each status filter / page caches independently; `keepPreviousData` avoids an empty flash
 * while paging or switching filters. Volatile relative to the nurse read — a short `staleTime`, refreshed by
 * explicit invalidation from the batch mutations.
 */
export function usePayoutBatches(filters: PayoutBatchFilters, page: number) {
  const params = { page, pageSize: PAYOUTS_PAGE_SIZE };
  return useQuery({
    queryKey: payoutKeys.adminBatches(filters, params),
    queryFn: () => payoutsApi.listPayoutBatches(filters, params),
    staleTime: ADMIN_BATCHES_STALE_TIME,
    gcTime: PAYOUTS_GC_TIME,
    placeholderData: keepPreviousData,
  });
}
