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


    friendlyName: 'Create',


    description: 'Create Enseignant!',


    inputs: {

        codeEnseignant: {
            type: 'string',
            required: true,
        },
        nom: {
            type: 'string',
            required: true,
        },
        fonction: {
            type: 'string',
        },
        sexe: {
            type: 'string',
        },
        telephone: {
            type: 'string',
        },

        adresse: {
            type: 'string',

        },
        ville: {
            type: 'string',

        },
        pays: {
            type: 'string',

        },


        codeEcole: {
            type: 'string',
            required: true,

        },

        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 enseignantCreate;
        let enseignant = await Enseignants.find({
            nom: inputs.nom,
            codeEnseignant: inputs.codeEnseignant,
            codeEcole: inputs.codeEcole,
        }).then(_.first)
        if (enseignant) {

            return exits.notFound({ message: "Cet enseignant existe déja, veuillez le modifier!" })
        } else {
            enseignantCreate = await Enseignants.create(inputs).fetch();
        }
        // All done.
        return exits.success(enseignantCreate);

    }


};
