import { useQuery } from '@tanstack/react-query';
import { useIsAuthenticated } from '@/hooks';
import { bookingRequestsApi } from '../apis';
import { bookingRequestKeys } from '../keys';
import {
  BOOKING_REQUEST_GC_TIME,
  BOOKING_REQUEST_PAGE_SIZE,
  BOOKING_REQUEST_POLL_MS,
  BOOKING_REQUEST_STALE_TIME,
} from '../constants';
import type { BookingRequestStatus } from '../types';

/**
 * The nurse incoming-requests inbox (`list?role=nurse`), defaulting to `pending_nurse_response`. Lightly
 * polls so newly-arrived requests appear without a manual refresh; every accept/reject invalidates this
 * list so an actioned request leaves the pending inbox immediately.
 */
export function useNurseRequestInbox(status: BookingRequestStatus | undefined = 'pending_nurse_response', page = 1) {
  const isAuthenticated = useIsAuthenticated();
  const params = { role: 'nurse' as const, status, page, pageSize: BOOKING_REQUEST_PAGE_SIZE };
  return useQuery({
    queryKey: bookingRequestKeys.nurseInbox(params),
    queryFn: () => bookingRequestsApi.list(params),
    enabled: isAuthenticated,
    staleTime: BOOKING_REQUEST_STALE_TIME,
    gcTime: BOOKING_REQUEST_GC_TIME,
    refetchInterval: BOOKING_REQUEST_POLL_MS,
  });
}
