/**
 * GET /v1/debug/memory
 * Obtiene métricas actuales de memoria
 */

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

module.exports = async (req, res, next, slmapi) => {
    try {
        const metrics = memoryMonitor.getMemoryMetrics();
        const status = memoryMonitor.getMemoryStatus();

        logger.info('Memory metrics requested', {
            type: 'DEBUG_MEMORY',
            ip: req.ip,
            heapPercentage: metrics.heapPercentage,
            status
        });

        return res.status(200).json({
            error: false,
            data: {
                ...metrics,
                status,
                formatted: memoryMonitor.formatMetrics(metrics),
                isAlert: memoryMonitor.isMemoryAlert(),
                isCritical: memoryMonitor.isMemoryCritical()
            }
        });

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

        return res.status(500).json({
            error: true,
            message: 'Error al obtener métricas de memoria',
            details: error.message
        });
    }
};
