/**
 * useDeposit Hook
 * TanStack Query hook for deposit operations
 * Epic: OQI-004 Cuentas de Inversion
 */

import { useMutation, useQueryClient } from '@tanstack/react-query';
import { createDeposit, type Transaction } from '../../../services/investment.service';
import { investmentAccountKeys } from './useInvestmentAccounts';

// ============================================================================
// Types
// ============================================================================

export interface DepositParams {
  accountId: string;
  amount: number;
}

// ============================================================================
// Hook
// ============================================================================

/**
 * Hook to create a deposit to an investment account
 * Invalidates account queries on success
 */
export function useDeposit() {
  const queryClient = useQueryClient();

  return useMutation<Transaction, Error, DepositParams>({
    mutationFn: ({ accountId, amount }) => createDeposit(accountId, amount),
    onSuccess: (_, { accountId }) => {
      // Invalidate accounts list, summary, and the specific account detail
      queryClient.invalidateQueries({ queryKey: investmentAccountKeys.lists() });
      queryClient.invalidateQueries({ queryKey: investmentAccountKeys.summary() });
      queryClient.invalidateQueries({
        queryKey: investmentAccountKeys.detail(accountId),
      });
    },
  });
}

export default useDeposit;
