import { useMutation, useQueryClient } from '@tanstack/react-query';
import { refundsApi } from '../apis';
import { invalidateAfterRefundRejection } from '../invalidations';

/**
 * Reject a refund with a required reason (moves it to terminal `rejected`). Returns `void`, so on success we
 * invalidate the by-refund read + the admin previews via the shared helper. Domain 4xx (`400` missing reason,
 * `404` not found, `409` already terminal) surface via `mutation.error`.
 */
export function useRejectRefund() {
  const queryClient = useQueryClient();
  return useMutation<void, unknown, { refundId: number; reason: string }>({
    mutationFn: ({ refundId, reason }) => refundsApi.rejectRefund(refundId, reason),
    onSuccess: (_void, { refundId }) => invalidateAfterRefundRejection(queryClient, refundId),
  });
}
