import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { GetRacesPageDataUseCase } from './GetRacesPageDataUseCase';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';

describe('GetRacesPageDataUseCase', () => {
  let useCase: GetRacesPageDataUseCase;
  let raceRepository: { findAll: Mock };
  let leagueRepository: { findAll: Mock };

  beforeEach(() => {
    raceRepository = { findAll: vi.fn() };
    leagueRepository = { findAll: vi.fn() };
    useCase = new GetRacesPageDataUseCase(
      raceRepository as unknown as IRaceRepository,
      leagueRepository as unknown as ILeagueRepository,
    );
  });

  it('should return races page data', async () => {
    const races = [
      {
        id: 'race-1',
        track: 'Track 1',
        car: 'Car 1',
        scheduledAt: new Date('2023-01-01T10:00:00Z'),
        status: 'scheduled' as const,
        leagueId: 'league-1',
        strengthOfField: 1500,
        isUpcoming: () => true,
        isLive: () => false,
        isPast: () => false,
      },
      {
        id: 'race-2',
        track: 'Track 2',
        car: 'Car 2',
        scheduledAt: new Date('2023-01-02T10:00:00Z'),
        status: 'completed' as const,
        leagueId: 'league-1',
        strengthOfField: 1600,
        isUpcoming: () => false,
        isLive: () => false,
        isPast: () => true,
      },
    ];
    const leagues = [
      { id: 'league-1', name: 'League 1' },
    ];

    raceRepository.findAll.mockResolvedValue(races);
    leagueRepository.findAll.mockResolvedValue(leagues);

    const result = await useCase.execute();

    expect(result.isOk()).toBe(true);
    const dto = result.unwrap();
    expect(dto.races).toHaveLength(2);
    expect(dto.races[0]).toEqual({
      id: 'race-1',
      track: 'Track 1',
      car: 'Car 1',
      scheduledAt: '2023-01-01T10:00:00.000Z',
      status: 'scheduled',
      leagueId: 'league-1',
      leagueName: 'League 1',
      strengthOfField: 1500,
      isUpcoming: true,
      isLive: false,
      isPast: false,
    });
  });

});