import {
  ITeamJoinRequestsPresenter,
  TeamJoinRequestsResultDTO,
  TeamJoinRequestsViewModel,
  TeamJoinRequestViewModel,
} from '@core/racing/application/presenters/ITeamJoinRequestsPresenter';

export class TeamJoinRequestsPresenter implements ITeamJoinRequestsPresenter {
  private result: TeamJoinRequestsViewModel | null = null;

  reset() {
    this.result = null;
  }

  present(dto: TeamJoinRequestsResultDTO) {
    const { requests, driverNames, avatarUrls } = dto;

    const requestViewModels: TeamJoinRequestViewModel[] = requests.map((request) => ({
      requestId: request.id,
      driverId: request.driverId,
      driverName: driverNames[request.driverId] || 'Unknown',
      teamId: request.teamId,
      status: 'pending' as const,
      requestedAt: request.requestedAt.toISOString(),
      avatarUrl: avatarUrls[request.driverId] || '',
    }));

    this.result = {
      requests: requestViewModels,
      pendingCount: requestViewModels.length,
      totalCount: requestViewModels.length,
    };
  }

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

  get viewModel(): TeamJoinRequestsViewModel {
    if (!this.result) throw new Error('Presenter not presented');
    return this.result;
  }
}