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

export class LeagueWalletPageQuery implements PageQuery<LeagueWalletViewData, string, PresentationError> {
  async execute(leagueId: string): Promise<Result<LeagueWalletViewData, PresentationError>> {
    const service = new LeagueWalletService();
    const result = await service.getWalletData(leagueId);

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

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

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