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

describe('GetRaceResultsDetailUseCase', () => {
  let useCase: GetRaceResultsDetailUseCase;
  let raceRepository: { findById: Mock };
  let leagueRepository: { findById: Mock };
  let resultRepository: { findByRaceId: Mock };
  let driverRepository: { findAll: Mock };
  let penaltyRepository: { findByRaceId: Mock };

  beforeEach(() => {
    raceRepository = { findById: vi.fn() };
    leagueRepository = { findById: vi.fn() };
    resultRepository = { findByRaceId: vi.fn() };
    driverRepository = { findAll: vi.fn() };
    penaltyRepository = { findByRaceId: vi.fn() };
    useCase = new GetRaceResultsDetailUseCase(
      raceRepository as unknown as IRaceRepository,
      leagueRepository as unknown as ILeagueRepository,
      resultRepository as unknown as IResultRepository,
      driverRepository as unknown as IDriverRepository,
      penaltyRepository as unknown as IPenaltyRepository,
    );
  });

  it('should return race results detail when race exists', async () => {
    const raceId = 'race-1';
    const race = {
      id: raceId,
      leagueId: 'league-1',
      track: 'Track 1',
      scheduledAt: new Date('2023-01-01T10:00:00Z'),
      status: 'completed' as const,
    };
    const league = {
      id: 'league-1',
      name: 'League 1',
      settings: { pointsSystem: 'f1-2024' },
    };
    const results = [
      { driverId: 'driver-1', position: 1, fastestLap: 120 },
      { driverId: 'driver-2', position: 2, fastestLap: 125 },
    ];
    const drivers = [
      { id: 'driver-1', name: 'Driver 1' },
      { id: 'driver-2', name: 'Driver 2' },
    ];
    const penalties = [
      { driverId: 'driver-1', type: 'time' as const, value: 10 },
    ];

    raceRepository.findById.mockResolvedValue(race);
    leagueRepository.findById.mockResolvedValue(league);
    resultRepository.findByRaceId.mockResolvedValue(results);
    driverRepository.findAll.mockResolvedValue(drivers);
    penaltyRepository.findByRaceId.mockResolvedValue(penalties);

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

    expect(result.isOk()).toBe(true);
    const viewModel = result.unwrap();
    expect(viewModel.race).toEqual({
      id: raceId,
      leagueId: 'league-1',
      track: 'Track 1',
      scheduledAt: new Date('2023-01-01T10:00:00Z'),
      status: 'completed',
    });
    expect(viewModel.league).toEqual({ id: 'league-1', name: 'League 1' });
    expect(viewModel.results).toEqual(results);
    expect(viewModel.drivers).toEqual(drivers);
    expect(viewModel.penalties).toEqual([{ driverId: 'driver-1', type: 'time', value: 10 }]);
    expect(viewModel.pointsSystem).toBeDefined();
    expect(viewModel.fastestLapTime).toBe(120);
    expect(viewModel.currentDriverId).toBe('driver-1');
  });

  it('should return error when race not found', async () => {
    raceRepository.findById.mockResolvedValue(null);

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

    expect(result.isErr()).toBe(true);
    expect(result.error).toEqual({ code: 'RACE_NOT_FOUND' });
  });
});