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

    schema.etape = new fields.StringField({required: true, initial: "pouponniere"});
    schema.accessible = new fields.SchemaField(
      Object.values((game.system.tedeum.config.origineSociale)).reduce((obj, spec) => {
        obj[spec.id] = new fields.SchemaField({
          isaccessible: new fields.BooleanField({initial: false}),
        });
        return obj;
      }, {})
    );
    
    schema.genre = new fields.StringField({required: true, initial: "Homme", choices: ["masculin", "mixte", "Homme", "Femme", "Mixte"]});

    schema.nbChoixCarac = new fields.NumberField({ ...requiredInteger, initial: 1, min: 1 });
    schema.caracteristiques = new fields.SchemaField(Array.fromRange(3, 1).reduce((caracs, i) => {
      caracs[`carac${i}`] = new fields.SchemaField({
        caracId: new fields.StringField({ required: true, blank: false, initial: "entregent" })
      });
      return caracs;
    }, {}));

    schema.competences = new fields.SchemaField(Array.fromRange(10, 1).reduce((comps, i) => {
      comps[`comp${i}`] = new fields.SchemaField({
        compName: new fields.StringField({ required: true, blank: true, initial: "" }),
        toSelect: new fields.BooleanField({ initial: false })
      });
      return comps;
    }, {}));
    
    schema.hasCompetencesOpt = new fields.BooleanField({initial: false})
    schema.competencesOptNumber = new fields.NumberField({ ...requiredInteger, initial: 1, min:0 })
    schema.competencesOpt = new fields.SchemaField(Array.fromRange(14, 1).reduce((comps, i) => {
      comps[`comp${i}`] = new fields.SchemaField({
        compName: new fields.StringField({ required: true, blank: true, initial: "" })
      });
      return comps;
    }, {}));

    schema.questionnaire = new fields.SchemaField(Array.fromRange(8, 1).reduce((questions, i) => {
      questions[`question${i}`] = new fields.SchemaField({
        question: new fields.StringField({ required: true, blank: true, initial: "" }),
        reponses: new fields.SchemaField( Array.fromRange(6, 1).reduce((resp, i) => {
          resp[`reponse${i}`] = new fields.SchemaField({
            num: new fields.NumberField({ ...requiredInteger, initial: i, min:i }),
            reponse: new fields.StringField({ required: true, blank: true, initial: "" }),
            compName: new fields.StringField({ required: true, blank: true, initial: "" }),
            toSelect: new fields.BooleanField({ initial: false }),
            compList: new fields.SchemaField(Array.fromRange(10, 1).reduce((comps, i) => {
              comps[`comp${i}`] = new fields.SchemaField({
                compName: new fields.StringField({ required: true, blank: true, initial: "" }),
              });
              return comps;
            }, {}))
          });
          return resp;
        }, {}))
      });
      return questions;
    }, {}));
    
    schema.debouches = new fields.SchemaField(Array.fromRange(24, 1).reduce((debouches, i) => {
      debouches[`debouche${i}`] = new fields.SchemaField({
        debouche: new fields.StringField({ required: true, blank: true, initial: "" })
      })
      return debouches;
    }, {}));

    schema.cagnotteMultiplier = new fields.NumberField({ ...requiredDouble, initial: 1.0, min: 0 });
    schema.cagnotteDivider = new fields.NumberField({ ...requiredDouble, initial: 1.0, min: 0 });
 
    schema.description = new fields.HTMLField({ required: true, blank: true });
    schema.trousseau = new fields.StringField({ required: true, blank: true, initial: "" });

    return schema;
  }
}
