import { useQuery } from '@tanstack/react-query';
import { paymentApi } from '../apis';
import { paymentKeys } from '../keys';
import { CHECKOUT_SUMMARY_STALE_TIME } from '../constants';

/**
 * The C6 payload — badge status, nurse/service mini-summary, and the served, reconciling money rows
 * (service + commission + VAT = total). Short `staleTime`: the price/deadline can change between visits,
 * and after a capture the summary's `requestStatus` must flip promptly (it is also invalidated by
 * `invalidateAfterPaymentSuccess`). Enabled only when an id is present.
 */
export function useCheckoutSummary(bookingRequestId: number | undefined) {
  return useQuery({
    queryKey: paymentKeys.summary(bookingRequestId ?? -1),
    queryFn: () => paymentApi.getCheckoutSummary(bookingRequestId as number),
    enabled: bookingRequestId != null && bookingRequestId > 0,
    staleTime: CHECKOUT_SUMMARY_STALE_TIME,
  });
}
