import { Result } from '@/lib/contracts/Result';
import { PageQuery } from '@/lib/contracts/page-queries/PageQuery';
import { SponsorService } from '@/lib/services/sponsors/SponsorService';
import { SponsorDashboardViewDataBuilder } from '@/lib/builders/view-data/SponsorDashboardViewDataBuilder';
import type { SponsorDashboardViewData } from '@/lib/view-data/SponsorDashboardViewData';
import { type PresentationError, mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';

/**
 * Sponsor Dashboard Page Query
 *
 * Composes data for the sponsor dashboard page.
 * Maps domain errors to presentation errors.
 */
export class SponsorDashboardPageQuery implements PageQuery<SponsorDashboardViewData, string, PresentationError> {
  async execute(sponsorId: string): Promise<Result<SponsorDashboardViewData, PresentationError>> {
    const service = new SponsorService();

    const dashboardResult = await service.getSponsorDashboard(sponsorId);
    if (dashboardResult.isErr()) {
      return Result.err(mapToPresentationError(dashboardResult.getError()));
    }

    const dto = dashboardResult.unwrap();
    const viewData = SponsorDashboardViewDataBuilder.build(dto);

    return Result.ok(viewData);
  }

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