import { injectable, unmanaged } from 'inversify';
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 { RaceResultsDetailViewModel } from '@/lib/view-models/RaceResultsDetailViewModel';
import { RaceWithSOFViewModel } from '@/lib/view-models/RaceWithSOFViewModel';
import { ImportRaceResultsSummaryViewModel } from '@/lib/view-models/ImportRaceResultsSummaryViewModel';

/**
 * Race Results Service
 * 
 * Orchestration service for race results operations.
 * Returns raw API DTOs. No ViewModels or UX logic.
 */
@injectable()
export class RaceResultsService implements Service {
  private apiClient: RacesApiClient;

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

  async getResultsDetail(raceId: string, currentUserId?: string): Promise<any> {
    const res = await this.getRaceResultsDetail(raceId);
    if (res.isErr()) throw new Error((res as any).error.message);
    const data = (res as any).value;
    return new RaceResultsDetailViewModel(data, (currentUserId === undefined || currentUserId === null) ? '' : currentUserId);
  }

  async importResults(raceId: string, input: any): Promise<any> {
    const res = await this.apiClient.importResults(raceId, input);
    return new ImportRaceResultsSummaryViewModel(res);
  }

  /**
   * Get race results detail
   * Returns results for a specific race
   */
  async getRaceResultsDetail(raceId: string): Promise<Result<unknown, DomainError>> {
    try {
      const data = await this.apiClient.getResultsDetail(raceId);
      return Result.ok(data);
    } catch (error: unknown) {
      if (error instanceof ApiError) {
        return Result.err({
          type: this.mapApiErrorType(error.type),
          message: error.message
        });
      }
      return Result.err({
        type: 'unknown',
        message: (error as Error).message || 'Failed to fetch race results'
      });
    }
  }

  /**
   * Get race with strength of field
   * Returns race data with SOF calculation
   */
  async getWithSOF(raceId: string): Promise<any> {
    try {
      const data = await this.apiClient.getWithSOF(raceId);
      return new RaceWithSOFViewModel(data);
    } catch (error: unknown) {
      throw error;
    }
  }

  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';
    }
  }
}
