/**
 * GetSponsorLogoPageQuery
 *
 * Server-side composition for sponsor 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 { SponsorLogoViewDataBuilder } from '@/lib/builders/view-data/SponsorLogoViewDataBuilder';
import { SponsorLogoViewData } from '@/lib/view-data/SponsorLogoViewData';
import { PresentationError, mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';

export class GetSponsorLogoPageQuery implements PageQuery<SponsorLogoViewData, { sponsorId: string }> {
  async execute(params: { sponsorId: string }): Promise<Result<SponsorLogoViewData, PresentationError>> {
    try {
      const service = new MediaService();
      const apiDtoResult = await service.getSponsorLogo(params.sponsorId);

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

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

  static async execute(params: { sponsorId: string }): Promise<Result<SponsorLogoViewData, PresentationError>> {
    const query = new GetSponsorLogoPageQuery();
    return query.execute(params);
  }
}