import { Result } from '@/lib/contracts/Result';
import { PageQuery } from '@/lib/contracts/page-queries/PageQuery';
import { PresentationError, mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';
import { TeamService } from '@/lib/services/teams/TeamService';
import type { TeamsViewData } from '@/lib/view-data/TeamsViewData';
import { TeamsViewDataBuilder } from '@/lib/builders/view-data/TeamsViewDataBuilder';
import type { TeamListItemDTO } from '@/lib/types/generated/TeamListItemDTO';

export interface TeamsPageDto {
  teams: TeamListItemDTO[];
}

/**
 * TeamsPageQuery - Server-side composition for teams list page
 * Manual wiring only; no ContainerManager; no PageDataFetcher
 */
export class TeamsPageQuery implements PageQuery<TeamsViewData, void> {
  async execute(): Promise<Result<TeamsViewData, PresentationError>> {
    try {
      // Manual dependency creation
      const service = new TeamService();
      
      // Fetch teams
      const result = await service.getAllTeams();
      
      if (result.isErr()) {
        return Result.err(mapToPresentationError(result.getError()));
      }

      const teams = result.unwrap();
      
      // Transform to ViewData using builder
      const viewData = TeamsViewDataBuilder.build({ teams });

      return Result.ok(viewData);
    } catch (error) {
      return Result.err('unknown');
    }
  }
}
