import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { AsyncUseCase } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { GetLeagueScheduleResultDTO } from '../dto/GetLeagueScheduleResultDTO';

export interface GetLeagueScheduleUseCaseParams {
  leagueId: string;
}

export class GetLeagueScheduleUseCase implements AsyncUseCase<GetLeagueScheduleUseCaseParams, GetLeagueScheduleResultDTO, 'NO_ERROR'> {
  constructor(private readonly raceRepository: IRaceRepository) {}

  async execute(params: GetLeagueScheduleUseCaseParams): Promise<Result<GetLeagueScheduleResultDTO, ApplicationErrorCode<'NO_ERROR'>>> {
    const races = await this.raceRepository.findByLeagueId(params.leagueId);
    return Result.ok({
      races: races.map(race => ({
        id: race.id,
        name: `${race.track} - ${race.car}`,
        scheduledAt: race.scheduledAt,
      })),
    });
  }
}