import { RequestAvatarGenerationOutputDTO } from '@/lib/types/generated/RequestAvatarGenerationOutputDTO';

/**
 * Request Avatar Generation View Model
 *
 * Represents the result of an avatar generation request
 */
export class RequestAvatarGenerationViewModel {
  success: boolean;
  requestId?: string;
  avatarUrls?: string[];
  errorMessage?: string;

  constructor(
    dto:
      | RequestAvatarGenerationOutputDTO
      | {
          success: boolean;
          requestId?: string;
          avatarUrls?: string[];
          errorMessage?: string;
          avatarUrl?: string;
          error?: string;
        },
  ) {
    this.success = dto.success;

    if ('requestId' in dto && dto.requestId !== undefined) this.requestId = dto.requestId;

    if ('avatarUrls' in dto && dto.avatarUrls !== undefined) {
      this.avatarUrls = dto.avatarUrls;
    } else if ('avatarUrl' in dto && dto.avatarUrl !== undefined) {
      this.avatarUrls = [dto.avatarUrl];
    }

    if ('errorMessage' in dto && dto.errorMessage !== undefined) {
      this.errorMessage = dto.errorMessage;
    } else if ('error' in dto && dto.error !== undefined) {
      this.errorMessage = dto.error;
    }
  }

  /** UI-specific: Whether generation was successful */
  get isSuccessful(): boolean {
    return this.success;
  }

  /** UI-specific: Whether there was an error */
  get hasError(): boolean {
    return !!this.errorMessage;
  }

  /** UI-specific: Get first avatar URL */
  get firstAvatarUrl(): string | undefined {
    return this.avatarUrls?.[0];
  }

  get avatarUrl(): string | undefined {
    return this.firstAvatarUrl;
  }

  get error(): string | undefined {
    return this.errorMessage;
  }
}