require('dotenv').config();
module.exports = {


    friendlyName: 'Create',


    description: 'Create Classe!',


    inputs: {
        codeClasse: {
            type: 'string',
            required: true
        },
        codeEcole: {
            type: 'string',
            required: true
        },

        libelleClasse: {
            type: 'string',
            required: true
        },

        effectif: {
            type: 'string',
        },
        effectifHomme: {
            type: 'string',
        },
        effectifFemme: {
            type: 'string',
        },


        niveau: {
            type: 'string',
        },

        ordre: {
            type: 'string',
        },


        logiUsername: {
            type: 'string',
            require: true

        },
        key: {
            type: 'string',
            require: true

        },
        logiPassword: {
            type: 'string',
            require: true

        },


    },


    exits: {
        notFound: {
            statusCode: 404
        },
    },


    fn: async function (inputs, exits) {

        //console.log("INPUTS 1", inputs)

        // TOKEN VERIFY
        let user = await Utilisateurs.find({ login: inputs.logiUsername, codeUser: inputs.logiPassword }).then(_.first)
        if (!user) {
            return exits.notFound('Utilisateur non trouvé')
        }
        console.log("INPUTS 2", inputs)

        let keyVerify = process.env.KEYVERIFY
        //
        if (keyVerify !== inputs.key) {
            return exits.notFound({ message: "Token non valide" })
        }


        ///////////////////
        let classeCreate;
        let classe = await Classe.find({ codeClasse: inputs.codeClasse, codeEcole: inputs.codeEcole, systemSco: inputs.systemSco }).then(_.first)
        if (classe) {

            return exits.notFound({ message: "Cette Classe existe déja, veuillez la modifier!" })
        } else {
            classeCreate = await Classe.create(inputs).fetch();
        }
        // All done.
        return exits.success(classeCreate);

    }


};
