import type { Transaction, Wallet } from '@core/payments/domain/entities/Wallet';
import { TransactionType } from '@core/payments/domain/entities/Wallet';
import type { Logger } from '@core/shared/domain/Logger';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { InMemoryTransactionRepository, InMemoryWalletRepository } from './InMemoryWalletRepository';

describe('InMemoryWalletRepository', () => {
  let walletRepo: InMemoryWalletRepository;
  let txRepo: InMemoryTransactionRepository;
  let logger: Logger;

  beforeEach(() => {
    logger = {
      debug: vi.fn(),
      info: vi.fn(),
      warn: vi.fn(),
      error: vi.fn(),
    } as unknown as Logger;

    walletRepo = new InMemoryWalletRepository(logger);
    txRepo = new InMemoryTransactionRepository(logger);
  });

  it('creates and finds wallets', async () => {
    const wallet: Wallet = {
      id: 'wallet-1',
      leagueId: 'league-1',
      balance: 0,
      totalRevenue: 0,
      totalPlatformFees: 0,
      totalWithdrawn: 0,
      currency: 'USD',
      createdAt: new Date('2025-01-01T00:00:00.000Z'),
    };

    await walletRepo.create(wallet);

    expect((await walletRepo.findById('wallet-1'))?.id).toBe('wallet-1');
    expect((await walletRepo.findByLeagueId('league-1'))?.id).toBe('wallet-1');

    const updated = await walletRepo.update({ ...wallet, balance: 10 });
    expect(updated.balance).toBe(10);
  });

  it('creates and queries transactions', async () => {
    const tx: Transaction = {
      id: 'tx-1',
      walletId: 'wallet-2',
      type: TransactionType.DEPOSIT,
      amount: 25,
      description: 'Test deposit',
      createdAt: new Date('2025-01-02T00:00:00.000Z'),
    };

    await txRepo.create(tx);

    expect((await txRepo.findById('tx-1'))?.id).toBe('tx-1');
    expect((await txRepo.findByWalletId('wallet-2')).map(t => t.id)).toContain('tx-1');
  });
});
