/**
 * Value Object: InstallId
 * Represents a unique installation identifier for a device/app instance.
 * Used to track anonymous users before they authenticate.
 * Max 128 characters, must be non-empty.
 */
export class InstallId {
  private static readonly MAX_LENGTH = 128;
  private static readonly MIN_LENGTH = 1;

  private constructor(private readonly value: string) {}

  static create(id: string): InstallId {
    if (!id || id.trim().length === 0) {
      throw new Error('Install ID cannot be empty');
    }
    if (id.length < InstallId.MIN_LENGTH) {
      throw new Error(`Install ID must be at least ${InstallId.MIN_LENGTH} character(s)`);
    }
    if (id.length > InstallId.MAX_LENGTH) {
      throw new Error(`Install ID must be at most ${InstallId.MAX_LENGTH} characters, got ${id.length}`);
    }
    return new InstallId(id);
  }

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

  equals(other: InstallId): boolean {
    return this.value === other.value;
  }

  /** Returns a masked version for logging (shows first 8 + last 4 chars) */
  toMasked(): string {
    if (this.value.length <= 12) {
      return this.value;
    }
    return `${this.value.slice(0, 8)}...${this.value.slice(-4)}`;
  }
}
