import { RacesApiClient } from '../../api/races/RacesApiClient';
import { RaceResultsDetailPresenter } from '../../presenters/RaceResultsDetailPresenter';
import { RaceWithSOFPresenter } from '../../presenters/RaceWithSOFPresenter';
import type { RaceWithSOFViewModel } from '../../presenters/RaceWithSOFPresenter';
import { ImportRaceResultsPresenter } from '../../presenters/ImportRaceResultsPresenter';
import type { ImportRaceResultsSummaryViewModel } from '../../presenters/ImportRaceResultsPresenter';
import type { RaceResultsDetailViewModel } from '../../view-models/RaceResultsDetailViewModel';
import type { ImportRaceResultsInputDto } from '../../dtos';

/**
 * Race Results Service
 *
 * Orchestrates race results operations including viewing, importing, and SOF calculations.
 * All dependencies are injected via constructor.
 */
export class RaceResultsService {
  constructor(
    private readonly apiClient: RacesApiClient,
    private readonly resultsDetailPresenter: RaceResultsDetailPresenter,
    private readonly sofPresenter: RaceWithSOFPresenter,
    private readonly importPresenter: ImportRaceResultsPresenter
  ) {}

  /**
   * Get race results detail with presentation transformation
   */
  async getResultsDetail(raceId: string, currentUserId?: string): Promise<RaceResultsDetailViewModel> {
    const dto = await this.apiClient.getResultsDetail(raceId);
    return this.resultsDetailPresenter.present(dto, currentUserId);
  }

  /**
   * Get race with strength of field calculation
   */
  async getWithSOF(raceId: string): Promise<RaceWithSOFViewModel> {
    const dto = await this.apiClient.getWithSOF(raceId);
    return this.sofPresenter.present(dto);
  }

  /**
   * Import race results and get summary
   */
  async importResults(raceId: string, input: ImportRaceResultsInputDto): Promise<ImportRaceResultsSummaryViewModel> {
    const dto = await this.apiClient.importResults(raceId, input);
    return this.importPresenter.present(dto);
  }
}