const modelLog = require('../models/log.model');

class LogService {
    async createLog({ userId, action, content, ipAddress, userAgent }) {
        try {
            await modelLog.create({
                userId,
                action,
                content,
                ipAddress,
                userAgent,
            });
        } catch (error) {
            console.error('Error creating log:', error);
            // Do not throw error to avoid blocking main flow
        }
    }

    async getLogsByUser(userId, limit = 20) {
        return await modelLog.find({ userId }).sort({ createdAt: -1 }).limit(limit);
    }
}

module.exports = new LogService();
