import { SYSTEM } from "../config/system.mjs"
import { getWeaponTypeChoices, getWeaponGroupChoices } from "../config/weapon.mjs"

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

    // Prism RPG weapon properties
    schema.weaponType = new fields.StringField({
      required: true,
      initial: "light",
      choices: () => getWeaponTypeChoices()
    })

    schema.weaponGroup = new fields.StringField({
      required: true,
      initial: "longsword",
      choices: () => getWeaponGroupChoices()
    })

    // APC (Action Point Cost) - determined by weapon type
    schema.apc = new fields.NumberField({
      ...requiredInteger,
      required: true,
      initial: 1,
      min: 0,
      max: 3
    })

    // Damage dice (e.g., "1d8", "2d6", "1d12")
    schema.damage = new fields.StringField({
      required: true,
      initial: "1d8"
    })

    // Damage type(s)
    schema.damageType = new fields.SchemaField({
      piercing: new fields.BooleanField({ initial: false }),
      bludgeoning: new fields.BooleanField({ initial: false }),
      slashing: new fields.BooleanField({ initial: false })
    })

    // Group passive (inherited from weapon group)
    schema.groupPassive = new fields.StringField({
      required: true,
      initial: ""
    })

    schema.groupPassiveDescription = new fields.HTMLField({
      required: true,
      initial: ""
    })

    // Weapon-specific passive abilities
    schema.passives = new fields.ArrayField(new fields.SchemaField({
      name: new fields.StringField({
        required: true,
        initial: ""
      }),
      description: new fields.HTMLField({
        required: true,
        initial: ""
      })
    }), {
      required: true,
      initial: []
    })

    // Maneuver(s) available with this weapon
    schema.maneuvers = new fields.ArrayField(new fields.SchemaField({
      name: new fields.StringField({
        required: true,
        initial: ""
      }),
      description: new fields.HTMLField({
        required: true,
        initial: ""
      })
    }), {
      required: true,
      initial: []
    })

    // Augment effects (for equipment progression)
    schema.augment = new fields.StringField({
      required: true,
      initial: ""
    })

    schema.augmentDescription = new fields.HTMLField({
      required: true,
      initial: ""
    })

    // Range properties
    schema.shortRange = new fields.NumberField({
      ...requiredInteger,
      initial: 0,
      min: 0
    })

    schema.longRange = new fields.NumberField({
      ...requiredInteger,
      initial: 0,
      min: 0
    })

    schema.reloadAPC = new fields.NumberField({
      ...requiredInteger,
      initial: 0,
      min: 0
    })

    // Bonuses (from magical enhancements, etc.)
    schema.bonuses = new fields.SchemaField({
      attackBonus: new fields.NumberField({ ...requiredInteger, required: true, initial: 0, min: 0 }),
      damageBonus: new fields.NumberField({ ...requiredInteger, required: true, initial: 0, min: 0 }),
      defenseBonus: new fields.NumberField({ ...requiredInteger, required: true, initial: 0, min: 0 })
    })

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

    return schema
  }

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

  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 weapon: Invalid money type "${data.money}", defaulting to coppercoin`)
        data.money = "coppercoin"
      }
    }

    return super.migrateData(data)
  }
}
