export class MyriadesActor extends Actor {
  prepareDerivedData() {
    super.prepareDerivedData();

    // Apply effects to characteristics and secondary stats
    this._applyEffects();

    const { health, magic } = this.system.resources ?? {};
    if (health) health.value = Math.clamp(health.value, health.min, health.max);
    if (magic) magic.value = Math.clamp(magic.value, magic.min, magic.max);
  }

  _applyEffects() {
    const characteristics = this.system.characteristics ?? {};
    const secondaryModifiers = this.system.secondaryModifiers ?? {};

    // Build effect modifiers map: target -> { add: 0, multiply: 1, override: null }
    const modifiers = {
      puissance: { add: 0, multiply: 1, override: null },
      coordination: { add: 0, multiply: 1, override: null },
      ingeniosite: { add: 0, multiply: 1, override: null },
      perspicacite: { add: 0, multiply: 1, override: null },
      healthMax: { add: 0, multiply: 1, override: null },
      magicMax: { add: 0, multiply: 1, override: null },
      inventoryCapacity: { add: 0, multiply: 1, override: null }
    };

    // Iterate enabled effects and accumulate modifiers
    const effectItems = this.items.filter(item => item.type === "effect" && item.system.enabled);
    for (const effect of effectItems) {
      const effectModifiers = Array.isArray(effect.system.modifiers) ? effect.system.modifiers : [];
      for (const mod of effectModifiers) {
        const target = mod.target ?? "puissance";
        if (!modifiers[target]) continue;

        const value = mod.perGrade && mod.grade ? Number(mod.value ?? 0) * Number(mod.grade ?? 1) : Number(mod.value ?? 0);
        const mode = mod.mode ?? "add";

        if (mode === "add") {
          modifiers[target].add += value;
        } else if (mode === "multiply") {
          modifiers[target].multiply *= (1 + value);
        } else if (mode === "override") {
          modifiers[target].override = value;
        }
      }
    }

    // Apply modifiers to characteristics
    for (const key of ["puissance", "coordination", "ingeniosite", "perspicacite"]) {
      const mod = modifiers[key];
      let value = characteristics[key] ?? 50;

      // Apply multiply first, then add, then override
      if (mod.multiply !== 1) value *= mod.multiply;
      if (mod.add !== 0) value += mod.add;
      if (mod.override !== null) value = mod.override;

      characteristics[key] = Math.max(0, Math.floor(value));
    }

    // Apply modifiers to secondary stats
    const secondaryKeys = { healthMax: "health", magicMax: "magic", inventoryCapacity: "inventory" };
    for (const [effectTarget, modKey] of Object.entries(secondaryKeys)) {
      const mod = modifiers[effectTarget];
      let value = secondaryModifiers[modKey] ?? 0;

      if (mod.multiply !== 1) value *= mod.multiply;
      if (mod.add !== 0) value += mod.add;
      if (mod.override !== null) value = mod.override;

      secondaryModifiers[modKey] = Math.floor(value);
    }
  }
}

export class MyriadesItem extends Item {}
