import { describe, it, expect, beforeEach, vi, type Mock } from 'vitest';
import {
  GetRaceResultsDetailUseCase,
  type GetRaceResultsDetailInput,
  type GetRaceResultsDetailResult,
  type GetRaceResultsDetailErrorCode,
} 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';
import type { UseCaseOutputPort } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';

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 };
  let output: UseCaseOutputPort<GetRaceResultsDetailResult> & { present: Mock };

  beforeEach(() => {
    raceRepository = { findById: vi.fn() };
    leagueRepository = { findById: vi.fn() };
    resultRepository = { findByRaceId: vi.fn() };
    driverRepository = { findAll: vi.fn() };
    penaltyRepository = { findByRaceId: vi.fn() };
    output = { present: vi.fn() } as unknown as UseCaseOutputPort<GetRaceResultsDetailResult> & {
      present: Mock;
    };

    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,
      output,
    );
  });

  it('presents race results detail when race exists', async () => {
    const input: GetRaceResultsDetailInput = { raceId: 'race-1' };

    const race = {
      id: 'race-1',
      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 = [
      {
        id: 'res-1',
        raceId: 'race-1',
        driverId: 'driver-1',
        position: { toNumber: () => 1 },
        fastestLap: { toNumber: () => 120 },
        incidents: { toNumber: () => 0 },
        startPosition: { toNumber: () => 1 },
      },
      {
        id: 'res-2',
        raceId: 'race-1',
        driverId: 'driver-2',
        position: { toNumber: () => 2 },
        fastestLap: { toNumber: () => 125 },
        incidents: { toNumber: () => 1 },
        startPosition: { toNumber: () => 2 },
      },
    ];

    const drivers = [
      { id: 'driver-1', name: 'Driver 1' },
      { id: 'driver-2', name: 'Driver 2' },
    ];

    const penalties = [
      {
        id: 'pen-1',
        leagueId: 'league-1',
        raceId: 'race-1',
        driverId: 'driver-1',
        type: 'time_penalty',
        value: 5,
        reason: 'cut track',
        protestId: undefined,
        issuedBy: 'steward-1',
        status: 'pending',
        issuedAt: new Date(),
        appliedAt: undefined,
        notes: undefined,
      },
    ];

    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(input);

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toBeUndefined();

    expect(output.present).toHaveBeenCalledTimes(1);
    const presented = (output.present as Mock).mock.calls[0]![0] as GetRaceResultsDetailResult;

    expect(presented.race).toEqual(race);
    expect(presented.league).toEqual(league);
    expect(presented.results).toEqual(results);
    expect(presented.drivers).toEqual(drivers);
    expect(presented.penalties).toEqual(penalties);
    expect(presented.pointsSystem).toBeDefined();
    expect(presented.fastestLapTime).toBe(120);
    expect(presented.currentDriverId).toBe('driver-1');
  });

  it('returns error when race not found and does not present data', async () => {
    const input: GetRaceResultsDetailInput = { raceId: 'race-1' };

    raceRepository.findById.mockResolvedValue(null);

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<
      GetRaceResultsDetailErrorCode,
      { message: string }
    >;

    expect(error.code).toBe('RACE_NOT_FOUND');
    expect(error.details.message).toBe('Race not found');
    expect(output.present).not.toHaveBeenCalled();
  });

  it('returns repository error when an unexpected error occurs', async () => {
    const input: GetRaceResultsDetailInput = { raceId: 'race-1' };

    raceRepository.findById.mockRejectedValue(new Error('Database failure'));

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<
      GetRaceResultsDetailErrorCode,
      { message: string }
    >;

    expect(error.code).toBe('REPOSITORY_ERROR');
    expect(error.details.message).toBe('Database failure');
    expect(output.present).not.toHaveBeenCalled();
  });
});
