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

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

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

  it('should return already registered error', async () => {
    registrationRepository.isRegistered.mockResolvedValue(true);

    const result = await useCase.execute({ raceId: 'race-1', leagueId: 'league-1', driverId: 'driver-1' });

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({
      code: 'ALREADY_REGISTERED',
      details: { message: 'Already registered for this race' },
    });
  });

  it('should return not active member error', async () => {
    registrationRepository.isRegistered.mockResolvedValue(false);
    membershipRepository.getMembership.mockResolvedValue(null);

    const result = await useCase.execute({ raceId: 'race-1', leagueId: 'league-1', driverId: 'driver-1' });

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({
      code: 'NOT_ACTIVE_MEMBER',
      details: { message: 'Must be an active league member to register for races' },
    });
  });

  it('should return not active member error for inactive membership', async () => {
    registrationRepository.isRegistered.mockResolvedValue(false);
    membershipRepository.getMembership.mockResolvedValue({ status: 'inactive' });

    const result = await useCase.execute({ raceId: 'race-1', leagueId: 'league-1', driverId: 'driver-1' });

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({
      code: 'NOT_ACTIVE_MEMBER',
      details: { message: 'Must be an active league member to register for races' },
    });
  });

  it('should register successfully', async () => {
    registrationRepository.isRegistered.mockResolvedValue(false);
    membershipRepository.getMembership.mockResolvedValue({ status: 'active' });
    registrationRepository.register.mockResolvedValue(undefined);

    const result = await useCase.execute({ raceId: 'race-1', leagueId: 'league-1', driverId: 'driver-1' });

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toBeUndefined();
    expect(registrationRepository.register).toHaveBeenCalledWith(
      expect.objectContaining({
        raceId: 'race-1',
        driverId: 'driver-1',
      }),
    );
  });
});