/**
 * GetLeagueLogoPageQuery
 *
 * Server-side composition for league logo media route.
 */

import { PageQuery } from '@/lib/contracts/page-queries/PageQuery';
import { Result } from '@/lib/contracts/Result';
import { MediaService } from '@/lib/services/media/MediaService';
import { LeagueLogoViewDataBuilder } from '@/lib/builders/view-data/LeagueLogoViewDataBuilder';
import { LeagueLogoViewData } from '@/lib/view-data/LeagueLogoViewData';
import { PresentationError, mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';

export class GetLeagueLogoPageQuery implements PageQuery<LeagueLogoViewData, { leagueId: string }> {
  async execute(params: { leagueId: string }): Promise<Result<LeagueLogoViewData, PresentationError>> {
    try {
      const service = new MediaService();
      const apiDtoResult = await service.getLeagueLogo(params.leagueId);

      if (apiDtoResult.isErr()) {
        return Result.err(mapToPresentationError(apiDtoResult.getError()));
      }

      const output = LeagueLogoViewDataBuilder.build(apiDtoResult.unwrap());
      return Result.ok(output);
    } catch (err) {
      console.error('GetLeagueLogoPageQuery failed:', err);
      return Result.err('serverError');
    }
  }

  static async execute(params: { leagueId: string }): Promise<Result<LeagueLogoViewData, PresentationError>> {
    const query = new GetLeagueLogoPageQuery();
    return query.execute(params);
  }
}