import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
import {
  ReopenRaceUseCase,
  type ReopenRaceInput,
  type ReopenRaceResult,
  type ReopenRaceErrorCode,
} from './ReopenRaceUseCase';
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';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';

describe('ReopenRaceUseCase', () => {
  let raceRepository: {
    findById: Mock;
    update: Mock;
  };
  let logger: {
    debug: Mock;
    warn: Mock;
    info: Mock;
    error: Mock;
  };
  let output: UseCaseOutputPort<ReopenRaceResult> & { present: Mock };

  let useCase: ReopenRaceUseCase;

  beforeEach(() => {
    raceRepository = {
      findById: vi.fn(),
      update: vi.fn(),
    };

    logger = {
      debug: vi.fn(),
      warn: vi.fn(),
      info: vi.fn(),
      error: vi.fn(),
    };

    output = {
      present: vi.fn(),
    } as unknown as UseCaseOutputPort<ReopenRaceResult> & { present: Mock };

    useCase = new ReopenRaceUseCase(
      raceRepository as unknown as IRaceRepository,
      logger as unknown as Logger,
      output,
    );
  });

  it('returns RACE_NOT_FOUND when race does not exist', async () => {
    const input: ReopenRaceInput = { raceId: 'race-404', reopenedById: 'admin-1' };

    raceRepository.findById.mockResolvedValue(null);

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<
      ReopenRaceErrorCode,
      { message: string }
    >;
    expect(err.code).toBe('RACE_NOT_FOUND');
    expect(err.details.message).toContain('race-404');
    expect(output.present).not.toHaveBeenCalled();
  });

  it('reopens a completed race, persists, and presents the result', async () => {
    const race = Race.create({
      id: 'race-1',
      leagueId: 'league-1',
      scheduledAt: new Date('2025-01-01T00:00:00.000Z'),
      track: 'Track 1',
      car: 'Car 1',
      sessionType: SessionType.main(),
      status: 'completed',
    });

    raceRepository.findById.mockResolvedValue(race);
    raceRepository.update.mockResolvedValue(race.reopen());

    const input: ReopenRaceInput = { raceId: 'race-1', reopenedById: 'admin-1' };
    const result = await useCase.execute(input);

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

    expect(raceRepository.update).toHaveBeenCalledTimes(1);
    const updatedRace = (raceRepository.update as Mock).mock.calls[0]?.[0] as Race;
    expect(updatedRace.id).toBe('race-1');
    expect(updatedRace.status.toString()).toBe('scheduled');

    expect(output.present).toHaveBeenCalledTimes(1);
    const presented = (output.present as Mock).mock.calls[0]?.[0] as ReopenRaceResult;
    expect(presented.race.id).toBe('race-1');
    expect(presented.race.status.toString()).toBe('scheduled');

    expect(logger.info).toHaveBeenCalled();
  });

  it('returns INVALID_RACE_STATE when race is already scheduled', async () => {
    const race = Race.create({
      id: 'race-1',
      leagueId: 'league-1',
      scheduledAt: new Date('2025-01-01T00:00:00.000Z'),
      track: 'Track 1',
      car: 'Car 1',
      sessionType: SessionType.main(),
      status: 'scheduled',
    });

    raceRepository.findById.mockResolvedValue(race);

    const input: ReopenRaceInput = { raceId: 'race-1', reopenedById: 'admin-1' };
    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<
      ReopenRaceErrorCode,
      { message: string }
    >;
    expect(err.code).toBe('INVALID_RACE_STATE');
    expect(err.details.message).toContain('already scheduled');
    expect(output.present).not.toHaveBeenCalled();
  });

  it('returns INVALID_RACE_STATE when race is running', async () => {
    const race = Race.create({
      id: 'race-1',
      leagueId: 'league-1',
      scheduledAt: new Date('2025-01-01T00:00:00.000Z'),
      track: 'Track 1',
      car: 'Car 1',
      sessionType: SessionType.main(),
      status: 'running',
    });

    raceRepository.findById.mockResolvedValue(race);

    const input: ReopenRaceInput = { raceId: 'race-1', reopenedById: 'admin-1' };
    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<
      ReopenRaceErrorCode,
      { message: string }
    >;
    expect(err.code).toBe('INVALID_RACE_STATE');
    expect(err.details.message).toContain('running race');
    expect(output.present).not.toHaveBeenCalled();
  });

  it('returns REPOSITORY_ERROR when repository throws unexpected error', async () => {
    raceRepository.findById.mockRejectedValue(new Error('DB error'));

    const input: ReopenRaceInput = { raceId: 'race-1', reopenedById: 'admin-1' };
    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<
      ReopenRaceErrorCode,
      { message: string }
    >;
    expect(err.code).toBe('REPOSITORY_ERROR');
    expect(err.details.message).toBe('DB error');
    expect(output.present).not.toHaveBeenCalled();
  });
});