import { useMutation, useQueryClient } from '@tanstack/react-query';
import { nurseBankApi } from '../apis';
import { bankKeys } from '../keys';
import type { AddBankAccountInput } from '../types';

/**
 * Submits an IBAN + account-holder name; the server kicks off the ownership inquiry and
 * returns the account (pending in the mock, resolved on the real path). Invalidates the list
 * so the pending state — and its later transition — surfaces on the next read/poll. Domain
 * 400s (invalid/duplicate IBAN, no nurse profile) surface via `mutation.error`.
 */
export function useAddNurseBankAccount() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (input: AddBankAccountInput) => nurseBankApi.add(input),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: bankKeys.list() });
    },
  });
}
