import type { GetLeagueStandingsResult } from '@core/racing/application/use-cases/GetLeagueStandingsUseCase';
import { LeagueStandingsDTO } from '../dtos/LeagueStandingsDTO';
import type { Presenter } from '../../../shared/presentation/Presenter';

export class LeagueStandingsPresenter implements Presenter<GetLeagueStandingsResult, LeagueStandingsDTO> {
  private result: LeagueStandingsDTO | null = null;

  reset() {
    this.result = null;
  }

  present(dto: GetLeagueStandingsResult) {
    const standings = dto.standings.map(standing => ({
      driverId: standing.driverId,
      driver: {
        id: standing.driver.id,
        iracingId: standing.driver.iracingId.toString(),
        name: standing.driver.name.toString(),
        country: standing.driver.country.toString(),
        ...(standing.driver.bio ? { bio: standing.driver.bio.toString() } : {}),
        joinedAt: standing.driver.joinedAt.toString(),
      },
      points: standing.points,
      position: standing.rank,
      // TODO: GetLeagueStandings currently does not provide these metrics.
      // Keep them stable in the API contract for now.
      wins: 0,
      podiums: 0,
      races: 0,
    }));
    this.result = { standings };
  }

  getResponseModel(): LeagueStandingsDTO {
    if (!this.result) throw new Error('Presenter not presented');
    return this.result;
  }
}
