/**
 * GetCategoryIconPageQuery
 *
 * Server-side composition for category icon media route.
 */

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

export class GetCategoryIconPageQuery implements PageQuery<CategoryIconViewData, { categoryId: string }> {
  async execute(params: { categoryId: string }): Promise<Result<CategoryIconViewData, PresentationError>> {
    try {
      const service = new MediaService();
      const apiDtoResult = await service.getCategoryIcon(params.categoryId);

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

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

  static async execute(params: { categoryId: string }): Promise<Result<CategoryIconViewData, PresentationError>> {
    const query = new GetCategoryIconPageQuery();
    return query.execute(params);
  }
}