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

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

    // Ordered list of member entries — position = marching order
    schema.memberRefs = new fields.ArrayField(
      new fields.SchemaField({
        id:           new fields.StringField({ required: true, nullable: false, blank: false }),
        carriesLight: new fields.BooleanField({ initial: false }),
      })
    )

    schema.treasury = new fields.SchemaField({
      gp: new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 }),
      sp: new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 }),
      cp: new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 }),
    })

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

    return schema
  }

  static migrateData(source) {
    if (Array.isArray(source.memberRefs)) {
      source.memberRefs = source.memberRefs.map(r =>
        typeof r === "string" ? { id: r, carriesLight: false } : r
      )
    }
    return super.migrateData(source)
  }

  static LOCALIZATION_PREFIXES = ["OATHHAMMER.Party"]
}
