import type { MediaApiClient } from '../../api/media/MediaApiClient';
import type { AvatarPresenter } from '../../presenters/AvatarPresenter';
import type {
  RequestAvatarGenerationInputDto,
  UpdateAvatarInputDto
} from '../../dtos';
import type {
  RequestAvatarGenerationViewModel,
  AvatarViewModel,
  UpdateAvatarViewModel
} from '../../view-models';

/**
 * Avatar Service
 *
 * Orchestrates avatar operations by coordinating API calls and presentation logic.
 * All dependencies are injected via constructor.
 */
export class AvatarService {
  constructor(
    private readonly apiClient: MediaApiClient,
    private readonly presenter: AvatarPresenter
  ) {}

  /**
   * Request avatar generation with presentation transformation
   */
  async requestAvatarGeneration(input: RequestAvatarGenerationInputDto): Promise<RequestAvatarGenerationViewModel> {
    try {
      const dto = await this.apiClient.requestAvatarGeneration(input);
      return this.presenter.presentRequestGeneration(dto);
    } catch (error) {
      throw error;
    }
  }

  /**
   * Get avatar for driver with presentation transformation
   */
  async getAvatar(driverId: string): Promise<AvatarViewModel> {
    try {
      const dto = await this.apiClient.getAvatar(driverId);
      return this.presenter.presentAvatar(dto);
    } catch (error) {
      throw error;
    }
  }

  /**
   * Update avatar for driver with presentation transformation
   */
  async updateAvatar(input: UpdateAvatarInputDto): Promise<UpdateAvatarViewModel> {
    try {
      const dto = await this.apiClient.updateAvatar(input);
      return this.presenter.presentUpdate(dto);
    } catch (error) {
      throw error;
    }
  }
}