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

export default class OathHammerTrait 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 })

    // class-trait (class-exclusive, acquirable) or special-trait (starting class trait)
    schema.traitType = new fields.StringField({
      required: true, initial: "special-trait", choices: SYSTEM.TRAIT_TYPE_CHOICES
    })

    // Which class or lineage this trait belongs to (e.g. "Berserker", "Wood Elf")
    schema.source = new fields.StringField({ required: true, nullable: false, initial: "" })

    // When uses reset: none = passive (always on), encounter, day
    schema.usagePeriod = new fields.StringField({
      required: true, initial: "none", choices: SYSTEM.TRAIT_USAGE_PERIOD
    })

    // Maximum uses per period. 0 = passive / unlimited.
    schema.maxUses = new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 })

    return schema
  }

  static LOCALIZATION_PREFIXES = ["OATHHAMMER.Trait"]

  static migrateData(source) {
    // Migrate old abilityType field → traitType
    if (source.abilityType !== undefined && source.traitType === undefined) {
      const map = { "class-ability": "class-trait" }
      source.traitType = map[source.abilityType] ?? "special-trait"
    }
    // Migrate old traitType values
    if (source.traitType === "class-ability") source.traitType = "class-trait"
    return super.migrateData(source)
  }
}
