const ActivityLog = require('../models/ActivityLog');

const activityLogController = {
    /**
     * Get all logs
     * GET /api/activity-logs
     */
    getAll: async (req, res) => {
        try {
            const page = parseInt(req.query.page) || 1;
            const limit = parseInt(req.query.limit) || 20;
            const searchEmail = req.query.search || '';
            const logType = req.query.logType || '';

            const result = await ActivityLog.getAll(page, limit, searchEmail, logType);

            return res.status(200).json({
                status: true,
                message: 'Logs retrieved successfully',
                data: result.data,
                pagination: result.pagination
            });
        } catch (error) {
            console.error('Get logs error:', error);
            return res.status(500).json({ status: false, message: 'Internal server error', error: error.message });
        }
    },

    /**
     * Get dashboard logs
     * GET /api/activity-logs/dashboard
     */
    getDashboardLogs: async (req, res) => {
        try {
            const limit = parseInt(req.query.limit) || 20;
            const logs = await ActivityLog.getDashboardLogs(limit);

            return res.status(200).json({
                status: true,
                message: 'Dashboard logs retrieved successfully',
                data: logs
            });
        } catch (error) {
            console.error('Get dashboard logs error:', error);
            return res.status(500).json({ status: false, message: 'Internal server error', error: error.message });
        }
    },

    /**
     * Get log types
     * GET /api/activity-logs/types
     */
    getTypes: async (req, res) => {
        try {
            const types = await ActivityLog.getLogTypes();
            return res.status(200).json({
                status: true,
                data: types
            });
        } catch (error) {
            return res.status(500).json({ status: false, message: 'Internal server error' });
        }
    }
};

module.exports = activityLogController;
