import { LeaguesApiClient } from './leagues/LeaguesApiClient';
import { RacesApiClient } from './races/RacesApiClient';
import { DriversApiClient } from './drivers/DriversApiClient';
import { TeamsApiClient } from './teams/TeamsApiClient';
import { SponsorsApiClient } from './sponsors/SponsorsApiClient';
import { MediaApiClient } from './media/MediaApiClient';
import { AnalyticsApiClient } from './analytics/AnalyticsApiClient';
import { AuthApiClient } from './auth/AuthApiClient';
import { PaymentsApiClient } from './payments/PaymentsApiClient';
import { DashboardApiClient } from './dashboard/DashboardApiClient';
import { PenaltiesApiClient } from './penalties/PenaltiesApiClient';
import { ProtestsApiClient } from './protests/ProtestsApiClient';
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
import { ConsoleErrorReporter } from '@/lib/infrastructure/logging/ConsoleErrorReporter';

/**
 * Main API Client
 *
 * Orchestrates all domain-specific API clients with consistent configuration.
 */
export class ApiClient {
  public readonly leagues: LeaguesApiClient;
  public readonly races: RacesApiClient;
  public readonly drivers: DriversApiClient;
  public readonly teams: TeamsApiClient;
  public readonly sponsors: SponsorsApiClient;
  public readonly media: MediaApiClient;
  public readonly analytics: AnalyticsApiClient;
  public readonly auth: AuthApiClient;
  public readonly payments: PaymentsApiClient;
  public readonly dashboard: DashboardApiClient;
  public readonly penalties: PenaltiesApiClient;
  public readonly protests: ProtestsApiClient;

  constructor(baseUrl: string) {
    const logger = new ConsoleLogger();
    const errorReporter = new ConsoleErrorReporter();

    this.leagues = new LeaguesApiClient(baseUrl, errorReporter, logger);
    this.races = new RacesApiClient(baseUrl, errorReporter, logger);
    this.drivers = new DriversApiClient(baseUrl, errorReporter, logger);
    this.teams = new TeamsApiClient(baseUrl, errorReporter, logger);
    this.sponsors = new SponsorsApiClient(baseUrl, errorReporter, logger);
    this.media = new MediaApiClient(baseUrl, errorReporter, logger);
    this.analytics = new AnalyticsApiClient(baseUrl, errorReporter, logger);
    this.auth = new AuthApiClient(baseUrl, errorReporter, logger);
    this.payments = new PaymentsApiClient(baseUrl, errorReporter, logger);
    this.dashboard = new DashboardApiClient(baseUrl, errorReporter, logger);
    this.penalties = new PenaltiesApiClient(baseUrl, errorReporter, logger);
    this.protests = new ProtestsApiClient(baseUrl, errorReporter, logger);
  }
}

// ============================================================================
// Singleton Instance
// ============================================================================

const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3001';

export const api = new ApiClient(API_BASE_URL);