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

/**
 * LeagueRosterAdminPageQuery
 * 
 * Fetches league roster admin data (members and join requests).
 */
export class LeagueRosterAdminPageQuery implements PageQuery<unknown, string, PresentationError> {
  async execute(leagueId: string): Promise<Result<unknown, PresentationError>> {
    const service = new LeagueService();
    const result = await service.getRosterAdminData(leagueId);

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

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

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