/**
 * Data model pour les compétences MournbladeCYD2
 * Prédilections enrichies (schema Hawkmoon CYD2)
 */
export default class CompetenceDataModel extends foundry.abstract.TypeDataModel {
  static defineSchema() {
    const fields = foundry.data.fields;
    return {
      description: new fields.HTMLField({ initial: "" }),
      niveau: new fields.NumberField({ initial: 0, integer: true }),
      attribut1: new fields.StringField({ initial: "" }),
      attribut2: new fields.StringField({ initial: "" }),
      attribut3: new fields.StringField({ initial: "" }),
      doublebonus: new fields.BooleanField({ initial: false }),
      predilections: new fields.ArrayField(
        new fields.SchemaField({
          id: new fields.StringField({ initial: "" }),
          name: new fields.StringField({ initial: "" }),
          description: new fields.StringField({ initial: "" }),
          acquise: new fields.BooleanField({ initial: false }),
          maitrise: new fields.BooleanField({ initial: false }),
          used: new fields.BooleanField({ initial: false })
        }),
        { initial: [] }
      )
    };
  }

  static migrateData(source) {
    if (Array.isArray(source.predilections)) {
      source.predilections = source.predilections.map(pred => {
        if (typeof pred === "string") return { id: "", name: pred, description: "", acquise: false, maitrise: false, used: false };
        if (!("acquise" in pred)) pred.acquise = false;
        if (!("maitrise" in pred)) pred.maitrise = false;
        if (!("id" in pred)) pred.id = "";
        if (!("description" in pred)) pred.description = "";
        return pred;
      });
    }
    return super.migrateData(source);
  }
}
