'use client';

import { usePageData } from '@/lib/page/usePageData';
import { useInject } from '@/lib/di/hooks/useInject';
import { LEAGUE_WALLET_SERVICE_TOKEN } from '@/lib/di/tokens';
import { LeagueWalletViewModel } from '@/lib/view-models/LeagueWalletViewModel';
import { WalletTransactionViewModel } from '@/lib/view-models/WalletTransactionViewModel';
import { useLeagueWalletWithdrawalWithBlockers } from './useLeagueWalletWithdrawalWithBlockers';

export function useLeagueWalletPageData(leagueId: string) {
  const leagueWalletService = useInject(LEAGUE_WALLET_SERVICE_TOKEN);

  const queryResult = usePageData({
    queryKey: ['leagueWallet', leagueId],
    queryFn: async () => {
      const dto = await leagueWalletService.getWalletForLeague(leagueId);
      // Transform DTO to ViewModel at client boundary
      const transactions = dto.transactions.map(t => new WalletTransactionViewModel({
        id: t.id,
        type: t.type as any,
        description: t.description,
        amount: t.amount,
        fee: 0,
        netAmount: t.amount,
        date: new globalThis.Date(t.createdAt),
        status: t.status,
      }));
      return new LeagueWalletViewModel({
        balance: dto.balance,
        currency: dto.currency,
        totalRevenue: dto.balance, // Fallback
        totalFees: 0,
        totalWithdrawals: 0,
        pendingPayouts: 0,
        transactions,
        canWithdraw: true,
        withdrawalBlockReason: undefined,
      });
    },
    enabled: !!leagueId,
  });

  return queryResult;
}

/**
 * @deprecated Use useLeagueWalletWithdrawalWithBlockers instead
 * This wrapper maintains backward compatibility while using the new blocker-aware hook
 */
export function useLeagueWalletWithdrawal(leagueId: string, data: LeagueWalletViewModel | null, refetch: () => void) {
  return useLeagueWalletWithdrawalWithBlockers(leagueId, data, refetch);
}