/**
 * Data model pour les compétences
 */
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({
          name: new fields.StringField({ initial: "" }),
          used: new fields.BooleanField({ initial: false })
        }),
        { initial: [] }
      )
    };
  }

  static migrateData(source) {
    if (Array.isArray(source.predilections)) {
      source.predilections = source.predilections.map(pred =>
        typeof pred === "string" ? { name: pred, used: false } : pred
      );
    }
    return super.migrateData(source);
  }
}