import { Result } from '@/lib/contracts/Result';
import { PageQuery } from '@/lib/contracts/page-queries/PageQuery';
import { PresentationError, mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';
import { RacesViewData } from '@/lib/view-data/RacesViewData';
import { RacesService } from '@/lib/services/races/RacesService';
import { RacesViewDataBuilder } from '@/lib/builders/view-data/RacesViewDataBuilder';

/**
 * Races All Page Query
 * 
 * Fetches all races data for the all races page.
 * Returns Result<RacesViewData, PresentationError>
 */
export class RacesAllPageQuery implements PageQuery<RacesViewData, void> {
  async execute(): Promise<Result<RacesViewData, PresentationError>> {
    // Manual wiring: Service creates its own dependencies
    const service = new RacesService();
    
    // Get all races data
    const result = await service.getAllRacesPageData();
    
    if (result.isErr()) {
      return Result.err(mapToPresentationError(result.getError()));
    }
    
    // Transform to ViewData using builder
    const viewData = RacesViewDataBuilder.build(result.unwrap());
    return Result.ok(viewData);
  }

  // Static method to avoid object construction in server code
  static async execute(): Promise<Result<RacesViewData, PresentationError>> {
    const query = new RacesAllPageQuery();
    return await query.execute();
  }
}