import { SYSTEM } from "../config/system.mjs"
export default class PrismRPGArmor extends foundry.abstract.TypeDataModel {
  static defineSchema() {
    const fields = foundry.data.fields
    const schema = {}
    const requiredInteger = { required: true, nullable: false, integer: true }

    schema.description = new fields.HTMLField({ required: true, textSearch: true })
    schema.armorType = new fields.StringField({ required: true, initial: "light", choices: SYSTEM.ARMOR_TYPE })

    // MRR - Movement Rating Reduction (reduced by Vigor sub-attribute)
    schema.mrr = new fields.NumberField({ ...requiredInteger, required: true, initial: 0, min: 0, label: "Movement Rating Reduction" })

    // Armor Passive - the passive ability of this armor piece
    schema.passive = new fields.StringField({ required: false, initial: "", label: "Armor Passive" })
    schema.passiveDescription = new fields.HTMLField({ required: false, textSearch: true, label: "Passive Description" })

    // Armor Augment - the Withstand action effect (requires augment from class feature)
    schema.augment = new fields.StringField({ required: false, initial: "", label: "Armor Augment" })
    schema.augmentDescription = new fields.HTMLField({ required: false, textSearch: true, label: "Augment Description" })

    schema.quantity = new fields.NumberField({ ...requiredInteger, initial: 1, min: 1 })
    schema.encLoad = new fields.NumberField({ required: true, initial: 0, min: 0 })
    schema.equipped = new fields.BooleanField({ required: true, initial: false })
    schema.isHelmet = new fields.BooleanField({ required: true, initial: false })

    schema.cost = new fields.NumberField({ required: true, initial: 0, min: 0 })
    schema.money = new fields.StringField({ required: true, initial: "coppercoin", choices: SYSTEM.MONEY })
    schema.containerId = new fields.StringField({ required: false, initial: "", nullable: false })

    return schema
  }

  /** @override */
  static LOCALIZATION_PREFIXES = ["PRISMRPG.Armor"]

  static migrateData(data) {
    // Migrate old money types to new ones
    if (data?.money) {
      const moneyMigration = {
        "tinbit": "coppercoin",
        "copper": "coppercoin",
        "silver": "silvercoin",
        "gold": "goldcoin",
        "platinum": "note"
      }

      if (moneyMigration[data.money]) {
        data.money = moneyMigration[data.money]
      }

      // If still invalid, default to coppercoin
      if (!SYSTEM.MONEY[data.money]) {
        console.warn(`Prism RPG | Migrate armor: Invalid money type "${data.money}", defaulting to coppercoin`)
        data.money = "coppercoin"
      }
    }

    return super.migrateData(data)
  }

}
