
let moment = require('moment');

module.exports = {


    friendlyName: 'Login',


    description: 'Login auth.',


    inputs: {
        username: {
            type: 'string',
            require: true
        },

    },


    exits: {
        notFound: {
            description: 'User not found',
            statusCode: 404
        },
        encryptionError: {
            statusCode: 500
        }
    },


    fn: async function ({ username }, exits) {
        let ouestDate = moment.utc(new Date()).utcOffset('+01:00');
        let date = ouestDate.format('YYYY-MM-DD');
        let heure = ouestDate.format('HH:mm:ss');
        try {
            if (username === '') {
                await ActivityLogs.create({
                    date,
                    heure,
                    username,
                    action: "VERIFY_USER",
                    status: "failed",
                    errorMessage: "Champs manquants",
                    device: "Web"
                });
                return exits.notFound('Veuillez renseigner tous les champs')
            }
            let user;
            user = await Utilisateurs.find({ login: username, desactiverCompte: false },).then(_.first)
            if (!user) {
                await ActivityLogs.create({
                    date,
                    heure,
                    username,
                    action: "VERIFY_USER",
                    status: "failed",
                    errorMessage: "Identifiant incorrect",
                    device: "Web"
                });
                return exits.notFound('Identifiant incorrect')
            }
            let resp = {};
            if (user.isUpdate === false) {
                resp.isUpdate = false
                resp.email = !!user.email
                await ActivityLogs.create({
                    date,
                    heure,
                    username: user.login,
                    action: "VERIFY_USER",
                    status: "success",
                    errorMessage: null,
                    device: "Web"
                });
                return exits.success(resp)
            }
            else if (user.isUpdate === true) {
                resp.isUpdate = true
                resp.email = !!user.email
                await ActivityLogs.create({
                    date,
                    heure,
                    username: user.login,
                    action: "VERIFY_USER",
                    status: "success",
                    errorMessage: null,
                    device: "Web"
                });
                return exits.success(resp)
            }
        } catch (err) {
            await ActivityLogs.create({
                date,
                heure,
                username,
                action: "VERIFY_USER",
                status: "failed",
                errorMessage: (typeof err === 'string' ? err : (err && err.message) || 'Erreur inconnue'),
                device: "Web"
            });
            return exits.error('Erreur lors de la vérification utilisateur.');
        }
    }


};
