import { useQuery } from '@tanstack/react-query';
import { bnplApi } from '../apis';
import { bnplKeys } from '../keys';
import { BNPL_SCHEDULE_STALE_TIME } from '../constants';
import type { ProviderCode } from '../types';

/**
 * D4 — the served repayment schedule (down-payment + N installments, amounts + due dates) for the chosen
 * plan. **Amounts are server-served** (the client never derives a schedule amount); only Shamsi date
 * display is a client concern. Enabled once provider + plan are chosen.
 */
export function useBnplSchedule(
  bookingRequestId: number | undefined,
  providerCode: ProviderCode | undefined,
  planId: string | undefined,
) {
  const enabled = bookingRequestId != null && bookingRequestId > 0 && providerCode != null && planId != null;
  return useQuery({
    queryKey: bnplKeys.schedule(bookingRequestId ?? -1, providerCode ?? '', planId ?? ''),
    queryFn: () =>
      bnplApi.getBnplSchedule({
        bookingRequestId: bookingRequestId as number,
        providerCode: providerCode as ProviderCode,
        planId: planId as string,
      }),
    enabled,
    staleTime: BNPL_SCHEDULE_STALE_TIME,
  });
}
