import { DriversApiClient } from '@/lib/api/drivers/DriversApiClient';
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
import { isProductionEnvironment } from '@/lib/config/env';
import { Result } from '@/lib/contracts/Result';
import type { DomainError, Service } from '@/lib/contracts/services/Service';
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
import type { DriverDTO } from '@/lib/types/generated/DriverDTO';

export class DriverProfileUpdateService implements Service {
  private readonly apiClient: DriversApiClient;

  constructor() {
    const baseUrl = getWebsiteApiBaseUrl();
    const logger = new ConsoleLogger();
    const errorReporter = new EnhancedErrorReporter(logger, {
      showUserNotifications: true,
      logToConsole: true,
      reportToExternal: isProductionEnvironment(),
    });

    this.apiClient = new DriversApiClient(baseUrl, errorReporter, logger);
  }

  async updateProfile(
    updates: { bio?: string; country?: string },
  ): Promise<Result<DriverDTO, DomainError>> {
    try {
      const dto = await this.apiClient.updateProfile(updates);
      return Result.ok(dto);
    } catch {
      return Result.err({ type: 'unknown', message: 'DRIVER_PROFILE_UPDATE_FAILED' });
    }
  }
}
