/**
 * useInvestmentAccounts Hook
 * TanStack Query hook for fetching investment accounts
 * Epic: OQI-004 Cuentas de Inversion
 */

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import {
  getUserAccounts,
  getAccountSummary,
  getAccountById,
  createAccount,
  closeAccount,
  type InvestmentAccount,
  type AccountSummary,
  type AccountDetail,
} from '../../../services/investment.service';

// ============================================================================
// Query Keys
// ============================================================================

export const investmentAccountKeys = {
  all: ['investment-accounts'] as const,
  lists: () => [...investmentAccountKeys.all, 'list'] as const,
  summary: () => [...investmentAccountKeys.all, 'summary'] as const,
  detail: (id: string) => [...investmentAccountKeys.all, 'detail', id] as const,
};

// ============================================================================
// Hooks
// ============================================================================

/**
 * Hook to fetch all user investment accounts
 */
export function useInvestmentAccounts() {
  return useQuery<InvestmentAccount[], Error>({
    queryKey: investmentAccountKeys.lists(),
    queryFn: getUserAccounts,
    staleTime: 30 * 1000, // 30 seconds
  });
}

/**
 * Hook to fetch account summary (total balance, earnings, etc.)
 */
export function useAccountSummary() {
  return useQuery<AccountSummary, Error>({
    queryKey: investmentAccountKeys.summary(),
    queryFn: getAccountSummary,
    staleTime: 30 * 1000,
  });
}

/**
 * Hook to fetch a single account by ID
 */
export function useAccountDetail(accountId: string) {
  return useQuery<AccountDetail, Error>({
    queryKey: investmentAccountKeys.detail(accountId),
    queryFn: () => getAccountById(accountId),
    enabled: !!accountId,
    staleTime: 30 * 1000,
  });
}

/**
 * Hook to create a new investment account
 */
export function useCreateAccount() {
  const queryClient = useQueryClient();

  return useMutation<
    InvestmentAccount,
    Error,
    { productId: string; initialDeposit: number }
  >({
    mutationFn: ({ productId, initialDeposit }) =>
      createAccount(productId, initialDeposit),
    onSuccess: () => {
      // Invalidate accounts list and summary
      queryClient.invalidateQueries({ queryKey: investmentAccountKeys.lists() });
      queryClient.invalidateQueries({ queryKey: investmentAccountKeys.summary() });
    },
  });
}

/**
 * Hook to close an investment account
 */
export function useCloseAccount() {
  const queryClient = useQueryClient();

  return useMutation<void, Error, string>({
    mutationFn: closeAccount,
    onSuccess: (_, accountId) => {
      // Invalidate accounts list, summary, and detail
      queryClient.invalidateQueries({ queryKey: investmentAccountKeys.lists() });
      queryClient.invalidateQueries({ queryKey: investmentAccountKeys.summary() });
      queryClient.invalidateQueries({
        queryKey: investmentAccountKeys.detail(accountId),
      });
    },
  });
}

export default useInvestmentAccounts;
