/**
 * Item.js
 * Represents a piece of equipment or consumable.
 */
export class Item {
  /**
   * @param {Object} def - The JSON definition of the item.
   */
  constructor(def) {
    this.id = def.id;
    this.name = def.name;
    this.type = def.type; // WEAPON, ARMOR, UTILITY, RELIC
    this.rarity = def.rarity || "COMMON";
    this.tags = def.tags || [];

    // Base Stats (e.g. { attack: 5, defense: 2 })
    this.stats = def.stats || {};

    // Passive Effects (Event Listeners)
    // e.g. { trigger: "ON_HIT", action: "APPLY_STATUS", ... }
    this.passives = def.passive_effects || [];

    // Active Ability (Grants a new action button)
    // e.g. { ability_id: "SKILL_FIREBALL" }
    this.activeAbility = def.active_ability || null;

    // Requirements to equip
    this.requirements = def.requirements || {};
  }

  /**
   * Returns true if the unit meets the requirements to equip this.
   */
  canEquip(unit) {
    if (this.requirements.class_lock) {
      // Check if unit's active class is in the allowed list
      if (!this.requirements.class_lock.includes(unit.activeClassId)) {
        return false;
      }
    }

    if (this.requirements.min_stat) {
      for (const [stat, value] of Object.entries(this.requirements.min_stat)) {
        if (unit.baseStats[stat] < value) return false;
      }
    }

    return true;
  }

  getStat(statName) {
    return this.stats[statName] || 0;
  }
}
