import { useMutation, useQueryClient } from '@tanstack/react-query';
import { payoutsApi } from '../apis';
import { payoutKeys } from '../keys';

/**
 * Record a manually reconciled bank transfer reference on a payout (REQ-036 — b13 has `mark_failed` but no
 * record-reference route). On success we invalidate the owning batch's detail so the reference renders on the
 * row without a manual refresh.
 */
export function useRecordTransferReference() {
  const queryClient = useQueryClient();
  return useMutation<void, unknown, { payoutId: number; reference: string; batchId: number }>({
    mutationFn: ({ payoutId, reference }) => payoutsApi.recordTransferReference(payoutId, reference),
    onSuccess: (_data, { batchId }) => {
      queryClient.invalidateQueries({ queryKey: [...payoutKeys.adminBatchDetails(), batchId] });
    },
  });
}
