/**
 * Onboarding Service
 *
 * Orchestrates onboarding operations.
 * Uses DriversApiClient for onboarding-related API calls.
 *
 * @server-safe
 */

import { DriversApiClient } from '@/lib/api/drivers/DriversApiClient';
import { Result } from '@/lib/contracts/Result';
import { DomainError, Service } from '@/lib/contracts/services/Service';
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
import { getWebsiteServerEnv } from '@/lib/config/env';
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
import { CompleteOnboardingInputDTO } from '@/lib/types/generated/CompleteOnboardingInputDTO';
import { CompleteOnboardingOutputDTO } from '@/lib/types/generated/CompleteOnboardingOutputDTO';
import { RequestAvatarGenerationInputDTO } from '@/lib/types/generated/RequestAvatarGenerationInputDTO';
import { RequestAvatarGenerationOutputDTO } from '@/lib/types/generated/RequestAvatarGenerationOutputDTO';

export class OnboardingService implements Service {
  private apiClient: DriversApiClient;

  constructor() {
    const baseUrl = getWebsiteApiBaseUrl();
    const logger = new ConsoleLogger();
    const { NODE_ENV } = getWebsiteServerEnv();
    const errorReporter = new EnhancedErrorReporter(logger, {
      showUserNotifications: false,
      logToConsole: true,
      reportToExternal: NODE_ENV === 'production',
    });
    this.apiClient = new DriversApiClient(baseUrl, errorReporter, logger);
  }

  async completeOnboarding(input: CompleteOnboardingInputDTO): Promise<Result<CompleteOnboardingOutputDTO, DomainError>> {
    try {
      const result = await this.apiClient.completeOnboarding(input);
      return Result.ok(result);
    } catch (error) {
      const errorMessage = error instanceof Error ? error.message : 'Failed to complete onboarding';
      return Result.err({ type: 'unknown', message: errorMessage });
    }
  }

  async checkCurrentDriver(): Promise<Result<unknown, DomainError>> {
    try {
      const result = await this.apiClient.getCurrent();
      return Result.ok(result);
    } catch (error) {
      const errorMessage = error instanceof Error ? error.message : 'Failed to check driver status';
      return Result.err({ type: 'unknown', message: errorMessage });
    }
  }

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  async generateAvatars(input: RequestAvatarGenerationInputDTO): Promise<Result<RequestAvatarGenerationOutputDTO, DomainError>> {
    // Endpoint not implemented yet - return NotImplemented error
    return Result.err({
      type: 'notImplemented',
      message: 'Avatar generation endpoint is not implemented yet'
    });
  }
}