import type { QueryClient } from '@tanstack/react-query';
import { invalidateAfterPaymentSuccess } from '@/services/payment/invalidations';
import { bnplKeys } from './keys';

/**
 * The cache transition a settled BNPL order causes: the request flipped `converted`, a confirmed booking
 * now exists, and a new provider-reported installment plan is live. A settled BNPL order is, to Balinyaar,
 * a card payment net-of-fee — so it invalidates exactly the same booking/request/checkout keys the card
 * capture does (reusing `invalidateAfterPaymentSuccess`), **plus** the BNPL order + the Wallet installment
 * status (so D5 reflects the new plan) — never a blanket refetch. Shared by `useAcceptBnplSchedule` and the
 * return-page settle poll.
 */
export function invalidateAfterBnplSettlement(
  queryClient: QueryClient,
  bookingRequestId: number,
  bookingId: number | null,
): void {
  invalidateAfterPaymentSuccess(queryClient, bookingRequestId, bookingId);
  queryClient.invalidateQueries({ queryKey: bnplKeys.order(bookingRequestId) });
  queryClient.invalidateQueries({ queryKey: bnplKeys.walletInstallments() });
}
