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_STALE_TIME,
} from '../constants';
import type { BookingRequestStatus } from '../types';

/**
 * The customer's own booking-requests inbox (`list?role=customer`), optionally filtered by status. C5 is
 * keyed by a single request id, so this list is not required by a phase-7 screen — it exists so f8's
 * "my bookings" surface (and any future customer request list) reads the same cached domain, and so a
 * create/cancel invalidation has a list to refresh.
 */
export function useCustomerRequests(status?: BookingRequestStatus, page = 1) {
  const isAuthenticated = useIsAuthenticated();
  const params = { role: 'customer' as const, status, page, pageSize: BOOKING_REQUEST_PAGE_SIZE };
  return useQuery({
    queryKey: bookingRequestKeys.list(params),
    queryFn: () => bookingRequestsApi.list(params),
    enabled: isAuthenticated,
    staleTime: BOOKING_REQUEST_STALE_TIME,
    gcTime: BOOKING_REQUEST_GC_TIME,
  });
}
