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


    friendlyName: 'Create',


    description: 'Create Attribution note moyenne!',


    inputs: {
        codeMatiere: {
            type: 'string',
            required: true
        },

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

        libelleCourt: {
            type: 'string',
        },

        libelleLong: {
            type: 'string',
        },
        typeNote: {
            type: 'string',
        },

        note: {
            type: 'string',
        },
        moyenne: {
            type: 'string',
        },
        moyenneCoef: {
            type: 'string',
        },

        totalMoyenne: {
            type: 'string',
        },

        rang: {
            type: 'string',
        },

        forteMoyenne: {
            type: 'string',
        },
        faibleMoyenne: {
            type: 'string',
        },

        moyenneClasse: {
            type: 'string',
        },

        appreciationProf: {
            type: 'string',
        },

        anneeScolaire: {
            type: 'string',
        },

        coef: {
            type: 'string',
        },

        trimSem: {
            type: 'string',
        },

        codMoy: {
            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 attributionNoteMoyenneCreate;
        let attributionNoteMoyenne = await AttributionNoteMoyenne.find({ codeMatiere: inputs.codeMatiere, codeEcole: inputs.codeEcole, codeClasse: inputs.codeClasse, matricule: inputs.matricule, typeNote: inputs.typeNote, anneeScolaire: inputs.anneeScolaire, trimSem: inputs.trimSem, codMoy: inputs.codMoy }).then(_.first)
        if (attributionNoteMoyenne) {

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

    }


};
