module.exports = {
    friendlyName: "Find User Activity Logs",

    description: "Récupère tous les logs d'activité d'un utilisateur (connexion, récupération de mot de passe, etc.)",

    inputs: {
        username: {
            type: "string",
            required: true
        },
        limit: {
            type: "string",
        },
        skip: {
            type: "string",
        }
    },

    exits: {
        notFound: {
            statusCode: 404
        }
    },

    fn: async function (inputs, exits) {
        // Vérification de l'utilisateur
        const { id, user } = this.req.token;
        let username = inputs.username || (user && user.login);
        if (!username) {
            return exits.notFound({ message: "Utilisateur non trouvé ou non authentifié." });
        }

        // Pagination
        this.req.query.limit = this.req.query.limit || "1000";
        this.req.query.skip = this.req.query.skip || "0";

        // Recherche des logs d'activité
        let cond = {
            username: username,
            action: { nin: ['API_ACCESS', 'FIRST CONNEXION', 'VERIFY_USER'] }
        };

        let options = {
            ...this.req.query,
            fixCond: cond,
            sort: [{ date: 'DESC' }, { heure: 'DESC' }]
        };

        let logs = await sails.helpers.paginator(ActivityLogs, cond, options);
        console.log('logs', logs)
        // All done.
        return exits.success(logs);
    },
}; 