import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import { AllLeaguesWithCapacityDTO, LeagueWithCapacityDTO } from '../dtos/AllLeaguesWithCapacityDTO';
import type { GetAllLeaguesWithCapacityResult } from '@core/racing/application/use-cases/GetAllLeaguesWithCapacityUseCase';

export class AllLeaguesWithCapacityPresenter implements UseCaseOutputPort<GetAllLeaguesWithCapacityResult> {
  private result: AllLeaguesWithCapacityDTO | null = null;

  present(result: GetAllLeaguesWithCapacityResult): void {
    const leagues: LeagueWithCapacityDTO[] = result.leagues.map(league => ({
      id: league.league.id.toString(),
      name: league.league.name.toString(),
      description: league.league.description?.toString() || '',
      ownerId: league.league.ownerId.toString(),
      settings: { maxDrivers: league.maxDrivers },
      createdAt: league.league.createdAt.toDate().toISOString(),
      usedSlots: league.currentDrivers,
      socialLinks: league.league.socialLinks || {},
    }));
    this.result = {
      leagues,
      totalCount: leagues.length,
    };
  }

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