import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { IsDriverRegisteredForRaceUseCase } from './IsDriverRegisteredForRaceUseCase';
import { IRaceRegistrationRepository } from '../../domain/repositories/IRaceRegistrationRepository';
import type { Logger } from '@core/shared/application';

describe('IsDriverRegisteredForRaceUseCase', () => {
  let useCase: IsDriverRegisteredForRaceUseCase;
  let registrationRepository: {
    isRegistered: Mock;
  };
  let logger: {
    debug: Mock;
    info: Mock;
    warn: Mock;
    error: Mock;
  };

  beforeEach(() => {
    registrationRepository = {
      isRegistered: vi.fn(),
    };
    logger = {
      debug: vi.fn(),
      info: vi.fn(),
      warn: vi.fn(),
      error: vi.fn(),
    };
    useCase = new IsDriverRegisteredForRaceUseCase(
      registrationRepository as unknown as IRaceRegistrationRepository,
      logger as unknown as Logger,
    );
  });

  it('should return true when driver is registered', async () => {
    const params = { raceId: 'race-1', driverId: 'driver-1' };

    registrationRepository.isRegistered.mockResolvedValue(true);

    const result = await useCase.execute(params);

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

  it('should return false when driver is not registered', async () => {
    const params = { raceId: 'race-1', driverId: 'driver-1' };

    registrationRepository.isRegistered.mockResolvedValue(false);

    const result = await useCase.execute(params);

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toBe(false);
  });

  it('should return error on repository failure', async () => {
    const params = { raceId: 'race-1', driverId: 'driver-1' };
    const error = new Error('Repository error');

    registrationRepository.isRegistered.mockRejectedValue(error);

    const result = await useCase.execute(params);

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({
      code: 'REPOSITORY_ERROR',
      details: { message: 'Repository error' },
    });
  });
});