import { PageQuery } from '@/lib/contracts/page-queries/PageQuery';
import { Result } from '@/lib/contracts/Result';
import { LeagueSponsorshipsService } from '@/lib/services/leagues/LeagueSponsorshipsService';
import { LeagueSponsorshipsViewDataBuilder } from '@/lib/builders/view-data/LeagueSponsorshipsViewDataBuilder';
import { LeagueSponsorshipsViewData } from '@/lib/view-data/leagues/LeagueSponsorshipsViewData';
import { type PresentationError, mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';

export class LeagueSponsorshipsPageQuery implements PageQuery<LeagueSponsorshipsViewData, string, PresentationError> {
  async execute(leagueId: string): Promise<Result<LeagueSponsorshipsViewData, PresentationError>> {
    const service = new LeagueSponsorshipsService();
    const result = await service.getSponsorshipsData(leagueId);

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

    const viewData = LeagueSponsorshipsViewDataBuilder.build(result.unwrap());
    return Result.ok(viewData);
  }

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