import { useQuery } from '@tanstack/react-query';
import { bookingsApi } from '../apis';
import { bookingKeys } from '../keys';
import { BOOKING_LIST_STALE_TIME, BOOKINGS_PAGE_SIZE } from '../constants';
import type { BookingListParams, BookingListRole, BookingStatus } from '../types';

/**
 * The role-scoped "My bookings" list (`bookings/list`). The customer رزروها tab reads `role='customer'`;
 * the nurse reads `role='nurse'`. The role + status filter are part of the query key so each scope is a
 * distinct cache entry. A conversion/transition invalidates `bookingKeys.lists()`.
 */
export function useBookingList(
  role: BookingListRole,
  options?: { status?: BookingStatus; page?: number; pageSize?: number },
) {
  const params: BookingListParams = {
    role,
    status: options?.status,
    page: options?.page ?? 1,
    pageSize: options?.pageSize ?? BOOKINGS_PAGE_SIZE,
  };
  return useQuery({
    queryKey: bookingKeys.list(params),
    queryFn: () => bookingsApi.listBookings(params),
    staleTime: BOOKING_LIST_STALE_TIME,
  });
}
