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

export class LeagueSettingsPageQuery implements PageQuery<LeagueSettingsViewData, string, PresentationError> {
  async execute(leagueId: string): Promise<Result<LeagueSettingsViewData, PresentationError>> {
    const service = new LeagueSettingsService();
    const result = await service.getSettingsData(leagueId);

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

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

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