import { describe, it, expect, vi, beforeEach } from 'vitest';
import {
  WithdrawFromRaceUseCase,
  type WithdrawFromRaceInput,
  type WithdrawFromRaceResult,
  type WithdrawFromRaceErrorCode,
} from './WithdrawFromRaceUseCase';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { IRaceRegistrationRepository } from '../../domain/repositories/IRaceRegistrationRepository';
import type { Logger } from '@core/shared/application/Logger';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';

describe('WithdrawFromRaceUseCase', () => {
  let raceRepository: { findById: ReturnType<typeof vi.fn> };
  let registrationRepository: { isRegistered: ReturnType<typeof vi.fn>; withdraw: ReturnType<typeof vi.fn> };
  let logger: Logger;
  let output: UseCaseOutputPort<WithdrawFromRaceResult> & { present: ReturnType<typeof vi.fn> };

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

    registrationRepository = {
      isRegistered: vi.fn(),
      withdraw: vi.fn(),
    };

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

    output = { present: vi.fn() } as unknown as UseCaseOutputPort<WithdrawFromRaceResult> & {
      present: ReturnType<typeof vi.fn>;
    };
  });

  const createUseCase = () =>
    new WithdrawFromRaceUseCase(
      raceRepository as unknown as IRaceRepository,
      registrationRepository as unknown as IRaceRegistrationRepository,
      logger,
      output,
    );

  it('withdraws from race successfully', async () => {
    const race = {
      id: 'race-1',
      isUpcoming: vi.fn().mockReturnValue(true),
    };

    raceRepository.findById.mockResolvedValue(
      race as unknown as Awaited<ReturnType<IRaceRepository['findById']>>,
    );
    registrationRepository.isRegistered.mockResolvedValue(true);
    registrationRepository.withdraw.mockResolvedValue(undefined);

    const useCase = createUseCase();

    const input: WithdrawFromRaceInput = {
      raceId: 'race-1',
      driverId: 'driver-1',
    };

    const result = await useCase.execute(input);

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toBeUndefined();
    expect(output.present).toHaveBeenCalledTimes(1);
    expect(output.present).toHaveBeenCalledWith({
      raceId: 'race-1',
      driverId: 'driver-1',
      status: 'withdrawn',
    });
    expect(registrationRepository.withdraw).toHaveBeenCalledWith('race-1', 'driver-1');
  });

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

    const useCase = createUseCase();

    const input: WithdrawFromRaceInput = {
      raceId: 'race-unknown',
      driverId: 'driver-1',
    };

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<WithdrawFromRaceErrorCode, { message: string }>;
    expect(error.code).toBe('RACE_NOT_FOUND');
    expect(error.details?.message).toContain('Race race-unknown not found');
    expect(output.present).not.toHaveBeenCalled();
  });

  it('returns error when registration is not found', async () => {
    const race = {
      id: 'race-1',
      isUpcoming: vi.fn().mockReturnValue(true),
    };

    raceRepository.findById.mockResolvedValue(
      race as unknown as Awaited<ReturnType<IRaceRepository['findById']>>,
    );
    registrationRepository.isRegistered.mockResolvedValue(false);

    const useCase = createUseCase();

    const input: WithdrawFromRaceInput = {
      raceId: 'race-1',
      driverId: 'driver-unknown',
    };

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<WithdrawFromRaceErrorCode, { message: string }>;
    expect(error.code).toBe('REGISTRATION_NOT_FOUND');
    expect(error.details?.message).toContain('Driver driver-unknown is not registered for race race-1');
    expect(output.present).not.toHaveBeenCalled();
  });

  it('returns error when withdrawal is not allowed', async () => {
    const race = {
      id: 'race-1',
      isUpcoming: vi.fn().mockReturnValue(false),
    };

    raceRepository.findById.mockResolvedValue(
      race as unknown as Awaited<ReturnType<IRaceRepository['findById']>>,
    );
    registrationRepository.isRegistered.mockResolvedValue(true);

    const useCase = createUseCase();

    const input: WithdrawFromRaceInput = {
      raceId: 'race-1',
      driverId: 'driver-1',
    };

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<WithdrawFromRaceErrorCode, { message: string }>;
    expect(error.code).toBe('WITHDRAWAL_NOT_ALLOWED');
    expect(error.details?.message).toContain('Withdrawal is not allowed for this race');
    expect(output.present).not.toHaveBeenCalled();
  });

  it('wraps repository errors and logs them', async () => {
    const race = {
      id: 'race-1',
      isUpcoming: vi.fn().mockReturnValue(true),
    };

    raceRepository.findById.mockResolvedValue(
      race as unknown as Awaited<ReturnType<IRaceRepository['findById']>>,
    );
    registrationRepository.isRegistered.mockResolvedValue(true);
    registrationRepository.withdraw.mockRejectedValue(new Error('DB failure'));

    const useCase = createUseCase();

    const input: WithdrawFromRaceInput = {
      raceId: 'race-1',
      driverId: 'driver-1',
    };

    const result = await useCase.execute(input);

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