/**
 * GetLeagueCoverPageQuery
 *
 * Server-side composition for league cover media route.
 */

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

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

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

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

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