import { keepPreviousData, useQuery } from '@tanstack/react-query';
import { verificationApi } from '../apis';
import { verificationKeys } from '../keys';
import { ADMIN_QUEUE_PAGE_SIZE, ADMIN_QUEUE_STALE_TIME } from '../constants';
import type { AdminVerificationQueueFilters } from '../types';

/**
 * The admin review queue (one item per nurse), filtered by `status` and paginated. `filters` + `params`
 * are part of the query key, so switching the status filter or paging reuses cached pages; `keepPreviousData`
 * avoids an empty flash while the next page loads. A decision (`useDecideStep` / approve / reject) invalidates
 * the queue so the desk re-renders without a manual refetch.
 */
export function useVerificationQueue(filters: AdminVerificationQueueFilters, page: number) {
  const params = { page, pageSize: ADMIN_QUEUE_PAGE_SIZE };
  return useQuery({
    queryKey: verificationKeys.adminQueue(filters, params),
    queryFn: () => verificationApi.listVerificationQueue(filters, params),
    staleTime: ADMIN_QUEUE_STALE_TIME,
    placeholderData: keepPreviousData,
  });
}
