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

/**
 * Resolve the cancellation policy for a booking **before** the customer confirms — the tier (by lead time),
 * its refund % + fee %, the concrete refund-vs-fee amounts, and the per-session refundable/locked
 * breakdown. Short `staleTime` because the tier depends on `now` vs the booking start (a stale preview
 * could under/over-state the fee); enabled only when a booking id is present.
 */
export function useCancellationPolicyPreview(bookingId: number | undefined, options?: { enabled?: boolean }) {
  return useQuery({
    queryKey: refundKeys.policyPreview(bookingId ?? -1),
    queryFn: () => refundsApi.resolveCancellationPolicy(bookingId as number),
    enabled: (options?.enabled ?? true) && bookingId != null && bookingId > 0,
    staleTime: POLICY_PREVIEW_STALE_TIME,
  });
}
