import { RacesApiClient } from '@/lib/api/races/RacesApiClient';
import { Result } from '@/lib/contracts/Result';
import { DomainError, Service } from '@/lib/contracts/services/Service';
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
import { ConsoleErrorReporter } from '@/lib/infrastructure/logging/ConsoleErrorReporter';
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
import { ApiError } from '@/lib/api/base/ApiError';
import type { RacesPageDataDTO } from '@/lib/types/generated/RacesPageDataDTO';
import type { RaceDetailDTO } from '@/lib/api/races/RacesApiClient';
import type { RaceResultsDetailDTO } from '@/lib/types/generated/RaceResultsDetailDTO';
import type { RaceWithSOFDTO } from '@/lib/types/generated/RaceWithSOFDTO';

/**
 * Races Service
 * 
 * Orchestration service for race-related operations.
 * Returns raw API DTOs. No ViewModels or UX logic.
 */
export class RacesService implements Service {
  private apiClient: RacesApiClient;

  constructor() {
    // Service creates its own dependencies
    const baseUrl = getWebsiteApiBaseUrl();
    const logger = new ConsoleLogger();
    const errorReporter = new ConsoleErrorReporter();
    
    this.apiClient = new RacesApiClient(baseUrl, errorReporter, logger);
  }

  /**
   * Get races page data
   * Returns races for the main races page
   */
  async getRacesPageData(): Promise<Result<RacesPageDataDTO, DomainError>> {
    try {
      const data = await this.apiClient.getPageData();
      return Result.ok(data);
    } catch (error) {
      return Result.err(this.mapError(error, 'Failed to fetch races page data'));
    }
  }

  /**
   * Get race detail
   * Returns detailed information for a specific race
   */
  async getRaceDetail(raceId: string, driverId: string): Promise<Result<RaceDetailDTO, DomainError>> {
    try {
      const data = await this.apiClient.getDetail(raceId, driverId);
      return Result.ok(data);
    } catch (error) {
      return Result.err(this.mapError(error, 'Failed to fetch race detail'));
    }
  }

  /**
   * Get race results detail
   * Returns results for a specific race
   */
  async getRaceResultsDetail(raceId: string): Promise<Result<RaceResultsDetailDTO, DomainError>> {
    try {
      const data = await this.apiClient.getResultsDetail(raceId);
      return Result.ok(data);
    } catch (error) {
      return Result.err(this.mapError(error, 'Failed to fetch race results'));
    }
  }

  /**
   * Get race with strength of field
   * Returns race data with SOF calculation
   */
  async getRaceWithSOF(raceId: string): Promise<Result<RaceWithSOFDTO, DomainError>> {
    try {
      const data = await this.apiClient.getWithSOF(raceId);
      return Result.ok(data);
    } catch (error) {
      return Result.err(this.mapError(error, 'Failed to fetch race SOF'));
    }
  }

  /**
   * Get all races for the all races page
   * Returns all races with pagination support
   */
  async getAllRacesPageData(): Promise<Result<RacesPageDataDTO, DomainError>> {
    try {
      const data = await this.apiClient.getPageData();
      return Result.ok(data);
    } catch (error) {
      return Result.err(this.mapError(error, 'Failed to fetch all races'));
    }
  }

  private mapError(error: unknown, defaultMessage: string): DomainError {
    if (error instanceof ApiError) {
      return {
        type: this.mapApiErrorType(error.type),
        message: error.message
      };
    }
    return {
      type: 'unknown',
      message: defaultMessage
    };
  }

  private mapApiErrorType(apiErrorType: string): DomainError['type'] {
    switch (apiErrorType) {
      case 'NOT_FOUND':
        return 'notFound';
      case 'AUTH_ERROR':
        return 'unauthorized';
      case 'VALIDATION_ERROR':
        return 'validation';
      case 'SERVER_ERROR':
        return 'serverError';
      case 'NETWORK_ERROR':
        return 'networkError';
      default:
        return 'unknown';
    }
  }
}
