/**
 * GET /v1/debug/memory/stats
 * Obtiene estadísticas históricas de memoria
 */

const memoryMonitor = require('../../../system/memory-monitor.slm');
const logger = require('../../../system/logger.slm');

module.exports = async (req, res, next, slmapi) => {
    try {
        const stats = memoryMonitor.getMemoryStats();
        const leakDetection = memoryMonitor.detectMemoryLeak();

        logger.info('Memory stats requested', {
            type: 'DEBUG_MEMORY_STATS',
            ip: req.ip,
            measurements: stats.measurements,
            leakDetected: !!leakDetection
        });

        return res.status(200).json({
            error: false,
            data: {
                ...stats,
                leakDetection
            }
        });

    } catch (error) {
        logger.error('Error getting memory stats', {
            type: 'DEBUG_MEMORY_STATS_ERROR',
            error: error.message
        });

        return res.status(500).json({
            error: true,
            message: 'Error al obtener estadísticas de memoria',
            details: error.message
        });
    }
};
