/**
 * Health value object — non-negative.
 *
 * Invariants enforced at construction (create) and on every operation.
 * Level-capped gains belong to later stories (healing, leveling).
 */
export class Health {
  #value: number;

  private constructor(n: number) {
    this.#value = n;
  }

  static create(n: number): Health {
    if (n < 0) {
      throw new Error(`Health cannot be negative, got ${n}`);
    }
    return new Health(n);
  }

  get value(): number {
    return this.#value;
  }

  /** Subtract damage — never goes below 0. */
  sub(amount: number): Health {
    return Health.create(Math.max(0, this.#value - amount));
  }

  /** Add healing — capped at the given maximum. */
  add(amount: number, max: number): Health {
    return Health.create(Math.min(this.#value + amount, max));
  }
}
