import { Result } from '@/lib/contracts/Result';
import { PageQuery } from '@/lib/contracts/page-queries/PageQuery';
import { PresentationError, mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';
import { TeamService } from '@/lib/services/teams/TeamService';
import { TeamSummaryViewModel } from '@/lib/view-models/TeamSummaryViewModel';

export interface TeamLeaderboardPageData {
  teams: TeamSummaryViewModel[];
}

export class TeamLeaderboardPageQuery implements PageQuery<TeamLeaderboardPageData, void> {
  async execute(): Promise<Result<TeamLeaderboardPageData, PresentationError>> {
    try {
      const service = new TeamService();
      const result = await service.getAllTeams();
      
      if (result.isErr()) {
        return Result.err(mapToPresentationError(result.getError()));
      }

      const teams = result.unwrap().map((t: any) => new TeamSummaryViewModel(t));

      return Result.ok({ teams });
    } catch (error) {
      return Result.err('unknown');
    }
  }
}
