import { PageQuery } from '@/lib/contracts/page-queries/PageQuery';
import { Result } from '@/lib/contracts/Result';
import { TeamRankingsViewDataBuilder } from '@/lib/builders/view-data/TeamRankingsViewDataBuilder';
import type { TeamRankingsViewData } from '@/lib/view-data/TeamRankingsViewData';
import { TeamRankingsService } from '@/lib/services/leaderboards/TeamRankingsService';
import { mapToPresentationError, type PresentationError } from '@/lib/contracts/page-queries/PresentationError';

/**
 * Team Rankings page query
 * Returns Result<TeamRankingsViewData, PresentationError>
 */
export class TeamRankingsPageQuery implements PageQuery<TeamRankingsViewData, void, PresentationError> {
  async execute(): Promise<Result<TeamRankingsViewData, PresentationError>> {
    const service = new TeamRankingsService();
    
    const serviceResult = await service.getTeamRankings();
    
    if (serviceResult.isErr()) {
      return Result.err(mapToPresentationError(serviceResult.getError()));
    }

    const apiDto = serviceResult.unwrap();
    const viewData = TeamRankingsViewDataBuilder.build(apiDto);
    return Result.ok(viewData);
  }

  static async execute(): Promise<Result<TeamRankingsViewData, PresentationError>> {
    const query = new TeamRankingsPageQuery();
    return query.execute();
  }
}
