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

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

  beforeEach(() => {
    protestRepository = { findByRaceId: vi.fn() };
    driverRepository = { findById: vi.fn() };
    useCase = new GetRaceProtestsUseCase(
      protestRepository as unknown as IProtestRepository,
      driverRepository as unknown as IDriverRepository,
    );
  });

  it('should return protests with driver map', async () => {
    const raceId = 'race-1';
    const protests = [
      {
        id: 'protest-1',
        raceId,
        protestingDriverId: 'driver-1',
        accusedDriverId: 'driver-2',
        reviewedBy: 'driver-3',
        filedAt: new Date(),
        comment: 'Comment',
        status: 'pending' as const,
      },
    ];
    const drivers = [
      { id: 'driver-1', name: 'Driver 1' },
      { id: 'driver-2', name: 'Driver 2' },
      { id: 'driver-3', name: 'Driver 3' },
    ];

    protestRepository.findByRaceId.mockResolvedValue(protests);
    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.protests).toEqual(protests);
    expect(dto.driverMap.get('driver-1')).toBe('Driver 1');
    expect(dto.driverMap.get('driver-2')).toBe('Driver 2');
    expect(dto.driverMap.get('driver-3')).toBe('Driver 3');
  });

  it('should return empty when no protests', async () => {
    protestRepository.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.protests).toEqual([]);
    expect(dto.driverMap.size).toBe(0);
  });
});