import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { CancelRaceUseCase } from './CancelRaceUseCase';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { Logger } from '@core/shared/application';
import { Race } from '../../domain/entities/Race';
import { SessionType } from '../../domain/value-objects/SessionType';

describe('CancelRaceUseCase', () => {
  let useCase: CancelRaceUseCase;
  let raceRepository: {
    findById: Mock;
    update: Mock;
  };
  let logger: {
    debug: Mock;
    warn: Mock;
    info: Mock;
    error: Mock;
  };

  beforeEach(() => {
    raceRepository = {
      findById: vi.fn(),
      update: vi.fn(),
    };
    logger = {
      debug: vi.fn(),
      warn: vi.fn(),
      info: vi.fn(),
      error: vi.fn(),
    };
    useCase = new CancelRaceUseCase(raceRepository as unknown as IRaceRepository, logger as unknown as Logger);
  });

  it('should cancel race successfully', async () => {
    const raceId = 'race-1';
    const race = Race.create({
      id: raceId,
      leagueId: 'league-1',
      scheduledAt: new Date(),
      track: 'Track 1',
      car: 'Car 1',
      sessionType: SessionType.main(),
      status: 'scheduled',
    });

    raceRepository.findById.mockResolvedValue(race);

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

    expect(result.isOk()).toBe(true);
    expect(raceRepository.findById).toHaveBeenCalledWith(raceId);
    expect(raceRepository.update).toHaveBeenCalledWith(expect.objectContaining({ id: raceId, status: 'cancelled' }));
  });

  it('should return error if race not found', async () => {
    const raceId = 'race-1';
    raceRepository.findById.mockResolvedValue(null);

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

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr().code).toBe('RACE_NOT_FOUND');
  });

  it('should return domain error if race is already cancelled', async () => {
    const raceId = 'race-1';
    const race = Race.create({
      id: raceId,
      leagueId: 'league-1',
      scheduledAt: new Date(),
      track: 'Track 1',
      car: 'Car 1',
      sessionType: SessionType.main(),
      status: 'cancelled',
    });

    raceRepository.findById.mockResolvedValue(race);

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

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr().code).toBe('RACE_ALREADY_CANCELLED');
  });

  it('should return domain error if race is completed', async () => {
    const raceId = 'race-1';
    const race = Race.create({
      id: raceId,
      leagueId: 'league-1',
      scheduledAt: new Date(),
      track: 'Track 1',
      car: 'Car 1',
      sessionType: SessionType.main(),
      status: 'completed',
    });

    raceRepository.findById.mockResolvedValue(race);

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

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr().code).toBe('CANNOT_CANCEL_COMPLETED_RACE');
  });
});