/**
 * Chroniques de l'Étrange — Système FoundryVTT
 *
 * Chroniques de l'Étrange est un jeu de rôle édité par Antre-Monde Éditions.
 * Ce système FoundryVTT est une implémentation indépendante et n'est pas
 * affilié à Antre-Monde Éditions,
 * mais a été réalisé avec l'autorisation d'Antre-Monde Éditions.
 *
 * @author    LeRatierBretonnien
 * @copyright 2024–2026 LeRatierBretonnien
 * @license   CC BY-NC-SA 4.0 – https://creativecommons.org/licenses/by-nc-sa/4.0/
 */

export default class NpcDataModel extends foundry.abstract.TypeDataModel {
  static defineSchema() {
    const { fields } = foundry.data
    const numberField = (initial = 0, extra = {}) => new fields.NumberField({ required: true, nullable: false, integer: true, initial, ...extra })
    const stringField = (initial = "") => new fields.StringField({ required: true, nullable: false, initial })
    const boolField = (initial = false) => new fields.BooleanField({ required: true, initial })
    const htmlField = (initial = "") => new fields.HTMLField({ required: true, nullable: false, initial, textSearch: true })

    const aptitudeField = () =>
      new fields.SchemaField({
        value: numberField(0, { min: 0 }),
        speciality: stringField(""),
      })

    const trackedField = () =>
      new fields.SchemaField({
        value: numberField(0, { min: 0 }),
        calcul: numberField(0, { min: 0 }),
        note: stringField(""),
      })

    return {
      type: stringField(""),
      threat: numberField(0, { min: 0, max: 4 }), // profane(0) | apprentice(1) | initiate(2) | accomplished(3) | renowned(4)
      nuisance: numberField(0, { min: 0, max: 5 }), // figurant(0) | minion(1) | adversary(2) | ally(3) | boss(4) | divinity(5)
      initiative: numberField(1, { min: 0 }),
      anti_initiative: numberField(24, { min: 0 }),
      aptitudes: new fields.SchemaField({
        physical: aptitudeField(),
        martial: aptitudeField(),
        mental: aptitudeField(),
        social: aptitudeField(),
        spiritual: aptitudeField(),
      }),
      vitality: trackedField(),
      hei: trackedField(),
      description: htmlField(""),
      prefs: new fields.SchemaField({
        typeofthrow: new fields.SchemaField({
          check: boolField(false),
          choice: stringField("0"),
        }),
      }),
    }
  }

  prepareDerivedData() {
    this.anti_initiative = 25 - (this.initiative ?? 0)
    this.vitality.calcul = (this.aptitudes.physical.value ?? 0) * 4
    this.hei.calcul = (this.aptitudes.spiritual.value ?? 0) * 4
  }
}
