import { Race } from '@core/racing/domain/entities/Race';
import { RaceRegistration } from '@core/racing/domain/entities/RaceRegistration';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
import type { RaceRegistrationRepository } from '../../domain/repositories/RaceRegistrationRepository';
import type { RaceRepository } from '../../domain/repositories/RaceRepository';
import {
    GetRaceRegistrationsUseCase,
    type GetRaceRegistrationsErrorCode,
    type GetRaceRegistrationsInput,
} from './GetRaceRegistrationsUseCase';

describe('GetRaceRegistrationsUseCase', () => {
  let useCase: GetRaceRegistrationsUseCase;
  let raceRepository: { findById: Mock };
  let registrationRepository: { findByRaceId: Mock };

  beforeEach(() => {
    raceRepository = { findById: vi.fn() };
    registrationRepository = { findByRaceId: vi.fn() };
    useCase = new GetRaceRegistrationsUseCase(
      raceRepository as unknown as RaceRepository,
      registrationRepository as unknown as RaceRegistrationRepository
    );
  });

  it('should return race and registrations on success', async () => {
    const input: GetRaceRegistrationsInput = { raceId: 'race-1' };

    const race = Race.create({
      id: input.raceId,
      leagueId: 'league-1',
      scheduledAt: new Date(),
      track: 'Track',
      car: 'Car',
    });

    const registrations = [
      RaceRegistration.create({ raceId: input.raceId, driverId: 'driver-1' }),
      RaceRegistration.create({ raceId: input.raceId, driverId: 'driver-2' }),
    ];

    raceRepository.findById.mockResolvedValue(race);
    registrationRepository.findByRaceId.mockResolvedValue(registrations);

    const result = await useCase.execute(input);

    expect(result.isOk()).toBe(true);
    const presented = result.unwrap();

    expect(presented.race).toEqual(race);
    expect(presented.registrations).toHaveLength(2);
    expect(presented.registrations[0]!.registration).toEqual(registrations[0]);
    expect(presented.registrations[1]!.registration).toEqual(registrations[1]);
  });

  it('should return RACE_NOT_FOUND error when race does not exist', async () => {
    const input: GetRaceRegistrationsInput = { raceId: 'non-existent-race' };

    raceRepository.findById.mockResolvedValue(null);

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<
      GetRaceRegistrationsErrorCode,
      { message: string }
    >;

    expect(err.code).toBe('RACE_NOT_FOUND');
    expect(err.details?.message).toBe('Race not found');
  });

  it('should return REPOSITORY_ERROR when repository throws', async () => {
    const input: GetRaceRegistrationsInput = { raceId: 'race-1' };

    raceRepository.findById.mockRejectedValue(new Error('DB failure'));

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<
      GetRaceRegistrationsErrorCode,
      { message: string }
    >;

    expect(err.code).toBe('REPOSITORY_ERROR');
    expect(err.details?.message).toBe('DB failure');
  });
});
