import { useQuery } from '@tanstack/react-query';
import { bookingsApi } from '../apis';
import { bookingKeys } from '../keys';
import { CARE_INSTRUCTIONS_STALE_TIME } from '../constants';

/**
 * The gated stage-2 care-instructions read (`bookings/care_instructions/{id}`) — the two-stage clinical
 * disclosure boundary. **`enabled` is the hard UI gate:** the caller passes `enabled` only when the
 * booking is `confirmed`+ **and** the viewer is the assigned nurse (or admin). When disabled the query
 * **never fires** — the client must not even request instructions it has no right to (a 403/404 from the
 * server is a defect path, not the design). Always reads with the `nurse` viewer role.
 */
export function useCareInstructions(bookingId: number | undefined, options: { enabled: boolean }) {
  return useQuery({
    queryKey: bookingKeys.careInstructions(bookingId ?? -1),
    queryFn: () => bookingsApi.getCareInstructions(bookingId as number, 'nurse'),
    enabled: options.enabled && bookingId != null && bookingId > 0,
    staleTime: CARE_INSTRUCTIONS_STALE_TIME,
  });
}
