import { MediaApiClient } from '@/lib/api/media/MediaApiClient';
import { DriversApiClient } from '@/lib/api/drivers/DriversApiClient';
import { RequestAvatarGenerationInputDTO } from '@/lib/types/generated/RequestAvatarGenerationInputDTO';
import { CompleteOnboardingInputDTO } from '@/lib/types/generated/CompleteOnboardingInputDTO';
import { RequestAvatarGenerationOutputDTO } from '@/lib/types/generated/RequestAvatarGenerationOutputDTO';
import { ValidateFaceInputDTO } from '@/lib/types/generated/ValidateFaceInputDTO';
import { ValidateFaceOutputDTO } from '@/lib/types/generated/ValidateFaceOutputDTO';
import { RequestAvatarGenerationViewModel } from '@/lib/view-models/RequestAvatarGenerationViewModel';
import { CompleteOnboardingViewModel } from '@/lib/view-models/CompleteOnboardingViewModel';
import { AvatarGenerationViewModel } from '@/lib/view-models/AvatarGenerationViewModel';

/**
 * Onboarding Service
 *
 * Handles the complete onboarding flow including avatar generation and profile creation.
 */
export class OnboardingService {
  constructor(
    private readonly mediaApiClient: MediaApiClient,
    private readonly driversApiClient: DriversApiClient
  ) {}

  /**
   * Validate face photo using the API
   */
  async validateFacePhoto(photoData: string): Promise<{ isValid: boolean; errorMessage?: string }> {
    const input: ValidateFaceInputDTO = { imageData: photoData };
    const dto: ValidateFaceOutputDTO = await this.mediaApiClient.validateFacePhoto(input);
    return { isValid: dto.isValid, errorMessage: dto.errorMessage };
  }

  /**
   * Generate avatars based on face photo and suit color
   * This method wraps the API call and returns a ViewModel
   */
  async generateAvatars(
    userId: string,
    facePhotoData: string,
    suitColor: string
  ): Promise<AvatarGenerationViewModel> {
    const input: RequestAvatarGenerationInputDTO = {
      userId,
      facePhotoData,
      suitColor,
    };

    const dto: RequestAvatarGenerationOutputDTO = await this.mediaApiClient.requestAvatarGeneration(input);
    return new AvatarGenerationViewModel(dto);
  }

  /**
   * Complete onboarding process
   */
  async completeOnboarding(
    input: CompleteOnboardingInputDTO
  ): Promise<CompleteOnboardingViewModel> {
    const dto = await this.driversApiClient.completeOnboarding(input);
    return new CompleteOnboardingViewModel(dto);
  }
}