import { rollInitiativeCheck } from "./rolls.mjs"

/**
 * Custom Combat class for Oath Hammer.
 * Initiative = opposed Leadership check (DV=0).
 * The number of successes becomes the combatant's initiative score.
 */
export default class OathHammerCombat extends Combat {
  /**
   * Override Foundry's rollInitiative to use Leadership skill checks.
   * For characters: Leadership (Fate) opposed check, successes = score.
   * For NPCs: Fate rank + initiative bonus pool.
   *
   * @param {string|string[]} ids   Combatant IDs to roll for
   * @param {object}          options
   */
  async rollInitiative(ids, { formula = null, updateTurn = true, messageOptions = {} } = {}) {
    const combatantIds = typeof ids === "string" ? [ids] : ids
    const updates = []

    for (const id of combatantIds) {
      const combatant = this.combatants.get(id)
      if (!combatant?.isOwner) continue
      const actor = combatant.actor
      if (!actor) continue

      const { successes } = await rollInitiativeCheck(actor)
      updates.push({ _id: id, initiative: successes })
    }

    if (!updates.length) return this
    await this.updateEmbeddedDocuments("Combatant", updates)
    if (updateTurn && this.turn !== null) await this.update({ turn: 0 })
    return this
  }
}
