import { useMutation } from '@tanstack/react-query';
import { paymentApi } from '../apis';
import type { InitiatePaymentInput } from '../types';

/**
 * Start a card payment for an accepted request. The caller owns the **per-attempt idempotency key**
 * (generated once per attempt and reused across retries of that attempt — see the C6 page), because only
 * the UI knows when a *new* attempt starts (after a failed/cancelled outcome). On success the caller
 * navigates to `redirectUrl`. A `409` here means "already paid / already in progress / window lapsed" —
 * the page converges (routes to the return surface to read the outcome) instead of toasting; other
 * domain 4xx render inline via `mutation.error` (the fetch layer owns 401/403/5xx toasts).
 */
export function useInitiatePayment() {
  return useMutation({
    mutationFn: (input: InitiatePaymentInput) => paymentApi.initiatePayment(input),
  });
}
