import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { GetLeagueScheduleUseCase } from './GetLeagueScheduleUseCase';
import { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import { Race } from '../../domain/entities/Race';

describe('GetLeagueScheduleUseCase', () => {
  let useCase: GetLeagueScheduleUseCase;
  let raceRepository: {
    findByLeagueId: Mock;
  };

  beforeEach(() => {
    raceRepository = {
      findByLeagueId: vi.fn(),
    };
    useCase = new GetLeagueScheduleUseCase(
      raceRepository as unknown as IRaceRepository,
    );
  });

  it('should return league schedule', async () => {
    const leagueId = 'league-1';
    const race = Race.create({
      id: 'race-1',
      leagueId,
      scheduledAt: new Date('2023-01-01T10:00:00Z'),
      track: 'Track 1',
      car: 'Car 1',
    });

    raceRepository.findByLeagueId.mockResolvedValue([race]);

    const result = await useCase.execute({ leagueId });

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toEqual({
      races: [
        {
          id: 'race-1',
          name: 'Track 1 - Car 1',
          scheduledAt: new Date('2023-01-01T10:00:00Z'),
        },
      ],
    });
  });

  it('should return empty schedule when no races', async () => {
    const leagueId = 'league-1';

    raceRepository.findByLeagueId.mockResolvedValue([]);

    const result = await useCase.execute({ leagueId });

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toEqual({
      races: [],
    });
  });
});