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

/**
 * The state-segmented earnings list. The **state filter + page are part of the query key** so each tab and
 * each page caches independently — switching back to a viewed tab is a cache hit with no network. `state`
 * omitted = the unfiltered ("all") tab. `keepPreviousData` holds the prior page/tab visible while the next
 * loads, so paging/tabbing never flashes an empty list.
 */
export function useNurseEarnings(state: EarningsState | undefined, page: number) {
  return useQuery({
    queryKey: payoutKeys.earningsList(state ?? 'all', page),
    queryFn: () => payoutsApi.getNurseEarnings({ state, page, pageSize: PAYOUTS_PAGE_SIZE }),
    staleTime: EARNINGS_LIST_STALE_TIME,
    gcTime: PAYOUTS_GC_TIME,
    placeholderData: keepPreviousData,
  });
}
