import { useMutation, useQueryClient } from '@tanstack/react-query';
import { bookingRequestsApi } from '../apis';
import { bookingRequestKeys } from '../keys';

/**
 * Nurse accept — opens the 30-minute payment window server-side. On success it invalidates every inbox
 * list (so the request leaves the pending inbox immediately) and the request detail (so the customer's
 * polled C5 reflects the accept). A stale accept (past deadline / not pending) returns `409` via
 * `mutation.error` — the caller surfaces it and refetches.
 */
export function useAcceptBookingRequest() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => bookingRequestsApi.accept(id),
    onSuccess: (dto) => {
      queryClient.setQueryData(bookingRequestKeys.detail(dto.id), dto);
      queryClient.invalidateQueries({ queryKey: bookingRequestKeys.lists() });
      queryClient.invalidateQueries({ queryKey: bookingRequestKeys.detail(dto.id) });
    },
  });
}
