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

export class LeagueSchedulePageQuery implements PageQuery<LeagueScheduleViewData, string, PresentationError> {
  async execute(leagueId: string): Promise<Result<LeagueScheduleViewData, PresentationError>> {
    const service = new LeagueScheduleService();
    const result = await service.getScheduleData(leagueId);

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

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

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