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


    friendlyName: 'Create',


    description: 'Create Emplois du Temps!',


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

        },

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

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

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

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

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

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

        },
        anneeScolaire: {
            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 emploisCreate;
        let emplois = await EmploisDuTemps.find({
            codeMatiere: inputs.codeMatiere,
            codeEnseignant: inputs.codeEnseignant,
            codeEcole: inputs.codeEcole,
            codeClasse: inputs.codeClasse,
            nomJour: inputs.nomJour,
            heureDebut: inputs.heureDebut,
            heureFin: inputs.heureFin,
            anneeScolaire: inputs.anneeScolaire
        }).then(_.first)
        if (emplois) {

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

    }


};
