import { GetLeagueJoinRequestsResult } from '@core/racing/application/use-cases/GetLeagueJoinRequestsUseCase';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import { LeagueJoinRequestWithDriverDTO } from '../dtos/LeagueJoinRequestWithDriverDTO';

export interface LeagueJoinRequestsViewModel {
  joinRequests: LeagueJoinRequestWithDriverDTO[];
}

export class LeagueJoinRequestsPresenter implements UseCaseOutputPort<GetLeagueJoinRequestsResult> {
  private result: LeagueJoinRequestsViewModel | null = null;

  reset() {
    this.result = null;
  }

  present(result: GetLeagueJoinRequestsResult) {
    const joinRequests: LeagueJoinRequestWithDriverDTO[] = result.joinRequests.map(item => ({
      id: item.id,
      leagueId: item.leagueId,
      driverId: item.driverId,
      requestedAt: item.requestedAt,
      ...(item.message ? { message: item.message } : {}),
      driver: {
        id: item.driver.id,
        name: item.driver.name.toString(),
      },
    }));
    this.result = {
      joinRequests,
    };
  }

  getViewModel(): LeagueJoinRequestsViewModel | null {
    return this.result;
  }
}