import { keepPreviousData, useQuery } from '@tanstack/react-query';
import { payoutsApi } from '../apis';
import { payoutKeys } from '../keys';
import { ADMIN_BATCH_DETAIL_STALE_TIME, PAYOUTS_GC_TIME } from '../constants';

/**
 * One batch expanded — its header + paginated per-payout rows (status, net, masked IBAN, transfer reference,
 * booking links). Disabled until a batch is selected (`batchId` present); each page keys separately and
 * `keepPreviousData` holds the prior page while the next loads. Invalidated by retry / record-reference.
 */
export function usePayoutBatchDetail(batchId: number | null, page: number) {
  return useQuery({
    queryKey: payoutKeys.adminBatchDetail(batchId ?? -1, page),
    queryFn: () => payoutsApi.getPayoutBatchDetail(batchId as number, page),
    enabled: batchId != null && batchId > 0,
    staleTime: ADMIN_BATCH_DETAIL_STALE_TIME,
    gcTime: PAYOUTS_GC_TIME,
    placeholderData: keepPreviousData,
  });
}
