import { useQuery } from '@tanstack/react-query';
import { bookingsApi } from '../apis';
import { bookingKeys } from '../keys';
import { BOOKING_DETAIL_GC_TIME, BOOKING_DETAIL_STALE_TIME } from '../constants';
import type { BookingViewerRole } from '../types';

/**
 * The booking header + money summary + embedded sessions + timeline status. `viewerRole` drives the
 * mock's address masking (the real server infers it from auth). A modest `staleTime` keeps the timeline
 * fresh across status transitions; EVV mutations **invalidate** this key so the timeline and session rows
 * reflect server truth immediately. Enabled only when an id is present.
 */
export function useBookingDetail(id: number | undefined, viewerRole: BookingViewerRole) {
  return useQuery({
    queryKey: bookingKeys.bookingDetail(id ?? -1),
    queryFn: () => bookingsApi.getBookingDetail(id as number, viewerRole),
    enabled: id != null && id > 0,
    staleTime: BOOKING_DETAIL_STALE_TIME,
    gcTime: BOOKING_DETAIL_GC_TIME,
  });
}
