import { injectable, unmanaged } from 'inversify';
import { DriversApiClient } from '@/lib/api/drivers/DriversApiClient';
import { DriverRegistrationStatusViewModel } from '@/lib/view-models/DriverRegistrationStatusViewModel';
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
import { Service } from '@/lib/contracts/services/Service';

@injectable()
export class DriverRegistrationService implements Service {
  private readonly apiClient: DriversApiClient;

  constructor(@unmanaged() apiClient?: DriversApiClient) {
    if (apiClient) {
      this.apiClient = apiClient;
    } else {
      const baseUrl = getWebsiteApiBaseUrl();
      const logger = new ConsoleLogger();
      const errorReporter = new EnhancedErrorReporter(logger);
      this.apiClient = new DriversApiClient(baseUrl, errorReporter, logger);
    }
  }

  async getDriverRegistrationStatus(driverId: string, raceId: string): Promise<DriverRegistrationStatusViewModel> {
    const data = await this.apiClient.getRegistrationStatus(driverId, raceId);
    return new DriverRegistrationStatusViewModel(data);
  }
}
