import { injectable, unmanaged } from 'inversify';
import { MediaApiClient } from '@/lib/api/media/MediaApiClient';
import { RequestAvatarGenerationViewModel } from '@/lib/view-models/RequestAvatarGenerationViewModel';
import { AvatarViewModel } from '@/lib/view-models/AvatarViewModel';
import { UpdateAvatarViewModel } from '@/lib/view-models/UpdateAvatarViewModel';
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 AvatarService implements Service {
  private readonly apiClient: MediaApiClient;

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

  async requestAvatarGeneration(input: { userId: string; facePhotoData: string; suitColor: 'red' | 'blue' | 'green' | 'yellow' | 'black' | 'white' }): Promise<RequestAvatarGenerationViewModel> {
    const data = await this.apiClient.requestAvatarGeneration(input);
    return new RequestAvatarGenerationViewModel(data);
  }

  async getAvatar(driverId: string): Promise<AvatarViewModel> {
    const data = await this.apiClient.getAvatar(driverId);
    return new AvatarViewModel({ ...data, driverId });
  }

  async updateAvatar(input: { driverId: string; avatarUrl: string }): Promise<UpdateAvatarViewModel> {
    const data = await this.apiClient.updateAvatar(input);
    return new UpdateAvatarViewModel(data);
  }
}
