import { describe, it, expect } from 'vitest';
import { WalletViewModel } from './WalletViewModel';
import { WalletTransactionViewModel } from './WalletTransactionViewModel';

const createWalletDto = (overrides: Partial<any> = {}): any => ({
  id: 'wallet-1',
  leagueId: 'league-1',
  balance: 100.5,
  totalRevenue: 1000,
  totalPlatformFees: 50,
  totalWithdrawn: 200,
  createdAt: '2024-01-01T00:00:00Z',
  currency: 'EUR',
  transactions: [],
  ...overrides,
});

const createTransactionDto = (overrides: Partial<any> = {}): any => ({
  id: 'tx-1',
  type: 'sponsorship',
  description: 'Test',
  amount: 10,
  fee: 1,
  netAmount: 9,
  date: new Date('2024-01-01T00:00:00Z'),
  status: 'completed',
  reference: 'ref',
  ...overrides,
});

describe('WalletViewModel', () => {
  it('maps wallet DTO fields and wraps transactions into view models', () => {
    const dto = createWalletDto({
      transactions: [createTransactionDto({ id: 'tx-1' }), createTransactionDto({ id: 'tx-2', amount: -5 })],
    });

    const vm = new WalletViewModel(dto);

    expect(vm.id).toBe('wallet-1');
    expect(vm.leagueId).toBe('league-1');
    expect(vm.balance).toBe(100.5);
    expect(vm.transactions).toHaveLength(2);
    expect(vm.transactions[0]).toBeInstanceOf(WalletTransactionViewModel);
  });

  it('formats balance with currency and 2 decimals', () => {
    const vm = new WalletViewModel(createWalletDto({ balance: 250, currency: 'USD' }));

    expect(vm.formattedBalance).toBe('USD 250.00');
  });

  it('derives balanceColor based on sign of balance', () => {
    const positive = new WalletViewModel(createWalletDto({ balance: 10 }));
    const zero = new WalletViewModel(createWalletDto({ balance: 0 }));
    const negative = new WalletViewModel(createWalletDto({ balance: -5 }));

    expect(positive.balanceColor).toBe('green');
    expect(zero.balanceColor).toBe('green');
    expect(negative.balanceColor).toBe('red');
  });

  it('exposes recentTransactions and totalTransactions helpers', () => {
    const transactions = [
      createTransactionDto({ id: 'tx-1' }),
      createTransactionDto({ id: 'tx-2' }),
      createTransactionDto({ id: 'tx-3' }),
      createTransactionDto({ id: 'tx-4' }),
      createTransactionDto({ id: 'tx-5' }),
      createTransactionDto({ id: 'tx-6' }),
    ];

    const vm = new WalletViewModel(createWalletDto({ transactions }));

    expect(vm.totalTransactions).toBe(6);
    expect(vm.recentTransactions).toHaveLength(5);
    expect(vm.recentTransactions[0]).toBeInstanceOf(WalletTransactionViewModel);
  });
});
