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

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 RaceRepository,
      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, cancelledById: 'admin-1' });

    expect(result.isOk()).toBe(true);
    const presented = result.unwrap();
    expect(raceRepository.findById).toHaveBeenCalledWith(raceId);
    expect(raceRepository.update).toHaveBeenCalledTimes(1);
    const updatedRace = (raceRepository.update as Mock).mock.calls[0]?.[0] as Race;
    expect(updatedRace.id).toBe(raceId);
    expect(updatedRace.status.toString()).toBe('cancelled');

    expect(presented.race.id).toBe(raceId);
    expect(presented.race.status.toString()).toBe('cancelled');
  });

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

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

    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, cancelledById: 'admin-1' });

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr();
    expect(err.code).toBe('NOT_AUTHORIZED');
    if ('details' in err && err.details && typeof err.details === 'object' && 'message' in err.details) {
      expect(err.details.message).toContain('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, cancelledById: 'admin-1' });

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr();
    expect(err.code).toBe('NOT_AUTHORIZED');
    if ('details' in err && err.details && typeof err.details === 'object' && 'message' in err.details) {
      expect(err.details.message).toContain('completed race');
    }
  });
});
