import type { IValueObject } from '@core/shared/domain';
import { IdentityDomainValidationError } from '../errors/IdentityDomainError';
import { GameKey } from './GameKey';

export interface ExternalRatingProps {
  gameKey: GameKey;
  type: string;
  value: number;
}

export class ExternalRating implements IValueObject<ExternalRatingProps> {
  readonly gameKey: GameKey;
  readonly type: string;
  readonly value: number;

  private constructor(gameKey: GameKey, type: string, value: number) {
    this.gameKey = gameKey;
    this.type = type;
    this.value = value;
  }

  static create(gameKey: GameKey, type: string, value: number): ExternalRating {
    if (!type || type.trim().length === 0) {
      throw new IdentityDomainValidationError('External rating type cannot be empty');
    }

    if (typeof value !== 'number' || isNaN(value)) {
      throw new IdentityDomainValidationError('External rating value must be a valid number');
    }

    const trimmedType = type.trim();
    return new ExternalRating(gameKey, trimmedType, value);
  }

  get props(): ExternalRatingProps {
    return {
      gameKey: this.gameKey,
      type: this.type,
      value: this.value,
    };
  }

  equals(other: IValueObject<ExternalRatingProps>): boolean {
    return (
      this.gameKey.equals(other.props.gameKey) &&
      this.type === other.props.type &&
      this.value === other.props.value
    );
  }

  toString(): string {
    return `${this.gameKey.toString()}:${this.type}=${this.value}`;
  }
}