import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { GetRacePenaltiesUseCase } from './GetRacePenaltiesUseCase';
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';

describe('GetRacePenaltiesUseCase', () => {
  let useCase: GetRacePenaltiesUseCase;
  let penaltyRepository: { findByRaceId: Mock };
  let driverRepository: { findById: Mock };

  beforeEach(() => {
    penaltyRepository = { findByRaceId: vi.fn() };
    driverRepository = { findById: vi.fn() };
    useCase = new GetRacePenaltiesUseCase(
      penaltyRepository as unknown as IPenaltyRepository,
      driverRepository as unknown as IDriverRepository,
    );
  });

  it('should return penalties with driver map', async () => {
    const raceId = 'race-1';
    const penalties = [
      {
        id: 'penalty-1',
        raceId,
        driverId: 'driver-1',
        issuedBy: 'driver-2',
        type: 'time' as const,
        value: 10,
        reason: 'Reason 1',
        status: 'applied' as const,
        issuedAt: new Date(),
      },
    ];
    const drivers = [
      { id: 'driver-1', name: 'Driver 1' },
      { id: 'driver-2', name: 'Driver 2' },
    ];

    penaltyRepository.findByRaceId.mockResolvedValue(penalties);
    driverRepository.findById.mockImplementation((id) => Promise.resolve(drivers.find(d => d.id === id)));

    const result = await useCase.execute({ raceId });

    expect(result.isOk()).toBe(true);
    const dto = result.unwrap();
    expect(dto.penalties).toEqual(penalties);
    expect(dto.driverMap.get('driver-1')).toBe('Driver 1');
    expect(dto.driverMap.get('driver-2')).toBe('Driver 2');
  });

  it('should return empty when no penalties', async () => {
    penaltyRepository.findByRaceId.mockResolvedValue([]);
    driverRepository.findById.mockResolvedValue(null);

    const result = await useCase.execute({ raceId: 'race-1' });

    expect(result.isOk()).toBe(true);
    const dto = result.unwrap();
    expect(dto.penalties).toEqual([]);
    expect(dto.driverMap.size).toBe(0);
  });
});