import { describe, it, expect, vi, Mocked } from 'vitest';
import { WalletService } from './WalletService';
import { PaymentsApiClient } from '../../api/payments/PaymentsApiClient';
import { WalletViewModel } from '../../view-models';

describe('WalletService', () => {
  let mockApiClient: Mocked<PaymentsApiClient>;
  let service: WalletService;

  beforeEach(() => {
    mockApiClient = {
      getWallet: vi.fn(),
    } as Mocked<PaymentsApiClient>;

    service = new WalletService(mockApiClient);
  });

  describe('getWallet', () => {
    it('should call apiClient.getWallet and return WalletViewModel', async () => {
      const mockResponse = {
        wallet: {
          id: 'wallet-1',
          leagueId: 'league-1',
          balance: 1000,
          totalRevenue: 1500,
          totalPlatformFees: 100,
          totalWithdrawn: 400,
          createdAt: '2023-01-01T00:00:00Z',
          currency: 'EUR',
        },
        transactions: [
          {
            id: 'tx-1',
            walletId: 'wallet-1',
            amount: 500,
            description: 'Deposit',
            createdAt: '2023-01-01T00:00:00Z',
            type: 'deposit' as const,
          },
        ],
      };

      mockApiClient.getWallet.mockResolvedValue(mockResponse);

      const result = await service.getWallet('league-1');

      expect(mockApiClient.getWallet).toHaveBeenCalledWith({ leagueId: 'league-1' });
      expect(result).toBeInstanceOf(WalletViewModel);
      expect(result.id).toBe('wallet-1');
      expect(result.balance).toBe(1000);
      expect(result.transactions).toHaveLength(1);
      expect(result.transactions[0].id).toBe('tx-1');
    });

    it('should handle wallet without transactions', async () => {
      const mockResponse = {
        wallet: {
          id: 'wallet-1',
          leagueId: 'league-1',
          balance: 1000,
          totalRevenue: 1500,
          totalPlatformFees: 100,
          totalWithdrawn: 400,
          createdAt: '2023-01-01T00:00:00Z',
          currency: 'EUR',
        },
        transactions: [],
      };

      mockApiClient.getWallet.mockResolvedValue(mockResponse);

      const result = await service.getWallet('league-1');

      expect(result.transactions).toHaveLength(0);
    });
  });
});