import type { GetTeamsLeaderboardResult } from '@core/racing/application/use-cases/GetTeamsLeaderboardUseCase';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { GetTeamsLeaderboardOutputDTO } from '../dtos/GetTeamsLeaderboardOutputDTO';

export class TeamsLeaderboardPresenter implements UseCaseOutputPort<GetTeamsLeaderboardResult> {
  private result: GetTeamsLeaderboardOutputDTO | null = null;

  reset() {
    this.result = null;
  }

  present(result: GetTeamsLeaderboardResult): void {
    this.result = {
      teams: result.items.map(item => ({
        id: item.team.id,
        name: item.team.name.toString(),
        tag: item.team.tag.toString(),
        logoUrl: null, // MediaResolver would be needed here
        memberCount: item.memberCount,
        rating: item.rating,
        totalWins: item.totalWins,
        totalRaces: item.totalRaces,
        performanceLevel: item.performanceLevel,
        isRecruiting: item.isRecruiting,
        createdAt: item.createdAt.toISOString(),
        description: item.team.description?.toString() || '',
      })),
      recruitingCount: result.recruitingCount,
      groupsBySkillLevel: {
        beginner: result.groupsBySkillLevel.beginner.map(item => ({
          id: item.team.id,
          name: item.team.name.toString(),
          tag: item.team.tag.toString(),
          logoUrl: null,
          memberCount: item.memberCount,
          rating: item.rating,
          totalWins: item.totalWins,
          totalRaces: item.totalRaces,
          performanceLevel: item.performanceLevel,
          isRecruiting: item.isRecruiting,
          createdAt: item.createdAt.toISOString(),
          description: item.team.description?.toString() || '',
        })),
        intermediate: result.groupsBySkillLevel.intermediate.map(item => ({
          id: item.team.id,
          name: item.team.name.toString(),
          tag: item.team.tag.toString(),
          logoUrl: null,
          memberCount: item.memberCount,
          rating: item.rating,
          totalWins: item.totalWins,
          totalRaces: item.totalRaces,
          performanceLevel: item.performanceLevel,
          isRecruiting: item.isRecruiting,
          createdAt: item.createdAt.toISOString(),
          description: item.team.description?.toString() || '',
        })),
        advanced: result.groupsBySkillLevel.advanced.map(item => ({
          id: item.team.id,
          name: item.team.name.toString(),
          tag: item.team.tag.toString(),
          logoUrl: null,
          memberCount: item.memberCount,
          rating: item.rating,
          totalWins: item.totalWins,
          totalRaces: item.totalRaces,
          performanceLevel: item.performanceLevel,
          isRecruiting: item.isRecruiting,
          createdAt: item.createdAt.toISOString(),
          description: item.team.description?.toString() || '',
        })),
        pro: result.groupsBySkillLevel.pro.map(item => ({
          id: item.team.id,
          name: item.team.name.toString(),
          tag: item.team.tag.toString(),
          logoUrl: null,
          memberCount: item.memberCount,
          rating: item.rating,
          totalWins: item.totalWins,
          totalRaces: item.totalRaces,
          performanceLevel: item.performanceLevel,
          isRecruiting: item.isRecruiting,
          createdAt: item.createdAt.toISOString(),
          description: item.team.description?.toString() || '',
        })),
      },
      topTeams: result.topItems.map(item => ({
        id: item.team.id,
        name: item.team.name.toString(),
        tag: item.team.tag.toString(),
        logoUrl: null,
        memberCount: item.memberCount,
        rating: item.rating,
        totalWins: item.totalWins,
        totalRaces: item.totalRaces,
        performanceLevel: item.performanceLevel,
        isRecruiting: item.isRecruiting,
        createdAt: item.createdAt.toISOString(),
        description: item.team.description?.toString() || '',
      })),
    };
  }

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