import { IGetLeagueJoinRequestsPresenter, GetLeagueJoinRequestsResultDTO, GetLeagueJoinRequestsViewModel } from '@core/racing/application/presenters/IGetLeagueJoinRequestsPresenter';

export class LeagueJoinRequestsPresenter implements IGetLeagueJoinRequestsPresenter {
  private result: GetLeagueJoinRequestsViewModel | null = null;

  reset() {
    this.result = null;
  }

  present(dto: GetLeagueJoinRequestsResultDTO) {
    const driverMap = new Map(dto.drivers.map(d => [d.id, d]));
    const joinRequests = dto.joinRequests.map(request => ({
      id: request.id,
      leagueId: request.leagueId,
      driverId: request.driverId,
      requestedAt: request.requestedAt,
      message: request.message,
      driver: driverMap.get(request.driverId) || null,
    }));
    this.result = { joinRequests };
  }

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