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

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

  reset(): void {
    this.viewModel = null;
  }

  present(input: TeamJoinRequestsResultDTO): void {
    const requestItems: TeamJoinRequestViewModel[] = input.requests.map((request) => ({
      requestId: request.id,
      driverId: request.driverId,
      driverName: input.driverNames[request.driverId] ?? 'Unknown Driver',
      teamId: request.teamId,
      status: 'pending',
      requestedAt: request.requestedAt.toISOString(),
      avatarUrl: input.avatarUrls[request.driverId] ?? '',
    }));

    const pendingCount = requestItems.filter((r) => r.status === 'pending').length;

    this.viewModel = {
      requests: requestItems,
      pendingCount,
      totalCount: requestItems.length,
    };
  }

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