import { SYSTEM } from "../config/system.mjs"

export default class OathHammerAmmunition extends foundry.abstract.TypeDataModel {
  static defineSchema() {
    const fields = foundry.data.fields
    const requiredInteger = { required: true, nullable: false, integer: true }
    const schema = {}

    schema.description = new fields.HTMLField({ required: true, textSearch: true })

    // Ammo type determines special effect (p.88):
    //   bodkin     → −1 to target's armor roll
    //   envenomed  → poison damage
    //   incendiary → flaming damage
    schema.ammoType = new fields.StringField({ required: true, initial: "standard", choices: SYSTEM.AMMO_TYPE_CHOICES })

    // Quantity of individual arrows/bolts in this stack
    schema.quantity = new fields.NumberField({ ...requiredInteger, initial: 20, min: 0 })

    schema.rarity = new fields.StringField({ required: true, initial: "common", choices: SYSTEM.RARITY_CHOICES })

    schema.cost = new fields.NumberField({ required: true, nullable: false, initial: 0, min: 0 })
    schema.currency = new fields.StringField({ required: true, initial: "sp", choices: SYSTEM.CURRENCY_CHOICES })

    return schema
  }

  static migrateData(source) {
    if (typeof source.rarity === "number") {
      const map = { 0: "common", 1: "uncommon", 2: "rare", 3: "very-rare", 4: "legendary", 5: "legendary", 6: "legendary" }
      source.rarity = map[source.rarity] ?? "common"
    }
    return super.migrateData(source)
  }

  static LOCALIZATION_PREFIXES = ["OATHHAMMER.Ammunition"]
}
