/**
 * GetTeamLogoPageQuery
 *
 * Server-side composition for team 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 { TeamLogoViewDataBuilder } from '@/lib/builders/view-data/TeamLogoViewDataBuilder';
import { TeamLogoViewData } from '@/lib/view-data/TeamLogoViewData';
import { PresentationError, mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';

export class GetTeamLogoPageQuery implements PageQuery<TeamLogoViewData, { teamId: string }> {
  async execute(params: { teamId: string }): Promise<Result<TeamLogoViewData, PresentationError>> {
    try {
      const service = new MediaService();
      const apiDtoResult = await service.getTeamLogo(params.teamId);

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

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

  static async execute(params: { teamId: string }): Promise<Result<TeamLogoViewData, PresentationError>> {
    const query = new GetTeamLogoPageQuery();
    return query.execute(params);
  }
}