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


    friendlyName: 'Find',


    description: 'Find Commande.',


    inputs: {
        etatNote: {
            type: 'string',
            required: true
        },
        codMoy: {
            type: 'string',
            required: true
        },

        codeEcole: {
            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: {
        encryptionError: {
            statusCode: 500
        },
        notFound: {
            statusCode: 404
        },
    },


    fn: async function (inputs, exits) {

        // 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 note = await ProfNote.find({ codMoy: inputs.codMoy, etatNote: inputs.etatNote, codeEcole: inputs.codeEcole, anneeScolaire: inputs.anneeScolaire }).then(_.first)
        if (!note) {
            return exits.notFound({ message: "Note déja synchronisé" })
        }
        let enseignant = await Enseignants.find({ codeEnseignant: note.codeEnseignant }).then(_.first)


        let detailsNotes = await ProfNoteDetails.find({ codMoy: note.codMoy, codeEcole: inputs.codeEcole, anneeScolaire: inputs.anneeScolaire })
        /*/console.log('detailsNote', detailsNote)
        .then(detailsNotes => {
            return Promise.all(detailsNotes.map(async detail => {
                
            }))

        })*/

        let detailsCount = await ProfNoteDetails.count({ codMoy: note.codMoy, codeEcole: inputs.codeEcole, anneeScolaire: inputs.anneeScolaire })

        note.dateEvaluation = moment(note.dateEvaluation, 'YYYY-MM-DD').format('DD/MM/YYYY')
        note.qui = enseignant.nom
        note.detailsCount = detailsCount
        note.detailsNotes = detailsNotes

        //console.log("ens", note.qui)
        return exits.success(note);

    }

    // All done. 

};
