import { GetLeagueStandingsUseCase, LeagueStandingsViewModel, StandingItemViewModel } from './GetLeagueStandingsUseCase';
import { ILeagueStandingsRepository, RawStanding } from '../ports/ILeagueStandingsRepository';

export class GetLeagueStandingsUseCaseImpl implements GetLeagueStandingsUseCase {
  constructor(private repository: ILeagueStandingsRepository) {}

  async execute(leagueId: string): Promise<LeagueStandingsViewModel> {
    const rawStandings = await this.repository.getLeagueStandings(leagueId);

    const standingItems: StandingItemViewModel[] = rawStandings.map((standing: RawStanding) => {
      return {
        id: standing.id,
        leagueId: standing.leagueId,
        seasonId: standing.seasonId ?? '',
        driverId: standing.driverId,
        position: standing.position,
        points: standing.points,
        wins: standing.wins,
        podiums: standing.podiums ?? 0,
        racesCompleted: standing.racesCompleted,
      };
    });

    return {
      leagueId: leagueId,
      standings: standingItems,
    };
  }
}
