import { BaseApiClient } from '../base/BaseApiClient';
import type { RaceStatsDTO } from '../../types/generated/RaceStatsDTO';
import type { RacesPageDataRaceDTO } from '../../types/generated/RacesPageDataRaceDTO';
import type { RaceResultsDetailDTO } from '../../types/generated/RaceResultsDetailDTO';
import type { RaceWithSOFDTO } from '../../types/generated/RaceWithSOFDTO';
import type { RegisterForRaceParamsDTO } from '../../types/generated/RegisterForRaceParamsDTO';
import type { ImportRaceResultsDTO } from '../../types/generated/ImportRaceResultsDTO';
import type { WithdrawFromRaceParamsDTO } from '../../types/generated/WithdrawFromRaceParamsDTO';
import type { RaceDetailRaceDTO } from '../../types/generated/RaceDetailRaceDTO';
import type { RaceDetailLeagueDTO } from '../../types/generated/RaceDetailLeagueDTO';
import type { RaceDetailEntryDTO } from '../../types/generated/RaceDetailEntryDTO';
import type { RaceDetailRegistrationDTO } from '../../types/generated/RaceDetailRegistrationDTO';
import type { RaceDetailUserResultDTO } from '../../types/generated/RaceDetailUserResultDTO';
import type { FileProtestCommandDTO } from '../../types/generated/FileProtestCommandDTO';

// Define missing types
export type RacesPageDataDTO = { races: RacesPageDataRaceDTO[] };
export type RaceDetailDTO = {
  race: RaceDetailRaceDTO | null;
  league: RaceDetailLeagueDTO | null;
  entryList: RaceDetailEntryDTO[];
  registration: RaceDetailRegistrationDTO;
  userResult: RaceDetailUserResultDTO | null;
  error?: string;
};
export type ImportRaceResultsSummaryDTO = {
  success: boolean;
  raceId: string;
  driversProcessed: number;
  resultsRecorded: number;
  errors?: string[];
};

/**
 * Races API Client
 *
 * Handles all race-related API operations.
 */
export class RacesApiClient extends BaseApiClient {
  /** Get total number of races */
  getTotal(): Promise<RaceStatsDTO> {
    return this.get<RaceStatsDTO>('/races/total-races');
  }

  /** Get races page data */
  getPageData(leagueId?: string): Promise<RacesPageDataDTO> {
    const query = leagueId ? `?leagueId=${encodeURIComponent(leagueId)}` : '';
    return this.get<RacesPageDataDTO>(`/races/page-data${query}`);
  }

  /** Get race detail */
  getDetail(raceId: string, driverId: string): Promise<RaceDetailDTO> {
    return this.get<RaceDetailDTO>(`/races/${raceId}?driverId=${driverId}`);
  }

  /** Get race results detail */
  getResultsDetail(raceId: string): Promise<RaceResultsDetailDTO> {
    return this.get<RaceResultsDetailDTO>(`/races/${raceId}/results`);
  }

  /** Get race with strength of field */
  getWithSOF(raceId: string): Promise<RaceWithSOFDTO> {
    return this.get<RaceWithSOFDTO>(`/races/${raceId}/sof`);
  }

  /** Register for race */
  register(raceId: string, input: RegisterForRaceParamsDTO): Promise<void> {
    return this.post<void>(`/races/${raceId}/register`, input);
  }

  /** Import race results */
  importResults(raceId: string, input: ImportRaceResultsDTO): Promise<ImportRaceResultsSummaryDTO> {
    return this.post<ImportRaceResultsSummaryDTO>(`/races/${raceId}/import-results`, input);
  }

  /** Withdraw from race */
  withdraw(raceId: string, input: WithdrawFromRaceParamsDTO): Promise<void> {
    return this.post<void>(`/races/${raceId}/withdraw`, input);
  }

  /** Cancel race */
  cancel(raceId: string): Promise<void> {
    return this.post<void>(`/races/${raceId}/cancel`, {});
  }

  /** Complete race */
  complete(raceId: string): Promise<void> {
    return this.post<void>(`/races/${raceId}/complete`, {});
  }

  /** Re-open race */
  reopen(raceId: string): Promise<void> {
    return this.post<void>(`/races/${raceId}/reopen`, {});
  }

  /** File a protest */
  fileProtest(input: FileProtestCommandDTO): Promise<void> {
    return this.post<void>('/races/protests/file', input);
  }
}
