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

export interface GameKeyProps {
  value: string;
}

export class GameKey implements IValueObject<GameKeyProps> {
  readonly value: string;

  private constructor(value: string) {
    this.value = value;
  }

  static create(value: string): GameKey {
    if (!value || value.trim().length === 0) {
      throw new IdentityDomainValidationError('GameKey cannot be empty');
    }

    const trimmed = value.trim();
    // Game keys should be lowercase alphanumeric with optional underscores/hyphens
    const gameKeyRegex = /^[a-z0-9][a-z0-9_-]*$/;
    if (!gameKeyRegex.test(trimmed)) {
      throw new IdentityDomainValidationError(
        `Invalid game key: ${value}. Must be lowercase alphanumeric with optional underscores/hyphens`
      );
    }

    return new GameKey(trimmed);
  }

  get props(): GameKeyProps {
    return { value: this.value };
  }

  equals(other: IValueObject<GameKeyProps>): boolean {
    return this.value === other.props.value;
  }

  toString(): string {
    return this.value;
  }
}