import { useQuery } from '@tanstack/react-query';
import { refundsApi } from '../apis';
import { refundKeys } from '../keys';
import { ADMIN_REFUND_PREVIEW_STALE_TIME } from '../constants';

/**
 * The admin refund decomposition preview for a booking — the server's fee/payout split, applied percentage,
 * channel, ETA, and the `willCreateClawback` warning — disclosed **before** the admin initiates. Keyed by the
 * booking **and** the linking ticket (a refund is always previewed in a ticket's context). Short `staleTime`
 * (the split forks on the booking's live payout state); enabled only when a booking id is present. The
 * client renders the served numbers verbatim — it never recomputes the split.
 */
export function useRefundPreview(bookingId: number | null, ticketId: number | null = null) {
  return useQuery({
    queryKey: refundKeys.adminPreview(bookingId ?? -1, ticketId),
    queryFn: () => refundsApi.getRefundPreview(bookingId as number, ticketId),
    enabled: bookingId != null && bookingId > 0,
    staleTime: ADMIN_REFUND_PREVIEW_STALE_TIME,
  });
}
