/* ===================================================
   JAESWIFT.XYZ — Top Processes Panel
   Live-updating process table from /api/processes
   =================================================== */

(function () {
    'use strict';

    var tbody = document.getElementById('procTableBody');
    if (!tbody) return;

    function cpuClass(val) {
        if (val >= 50) return 'proc-cpu-high';
        if (val >= 15) return 'proc-cpu-med';
        return 'proc-cpu-low';
    }

    function fetchProcesses() {
        fetch('/api/processes')
            .then(function (r) { return r.json(); })
            .then(function (procs) {
                if (!procs || !procs.length) return;

                var html = '';
                for (var i = 0; i < procs.length; i++) {
                    var p = procs[i];
                    var cls = cpuClass(p.cpu);
                    html += '<tr>'
                        + '<td>' + p.pid + '</td>'
                        + '<td>' + p.name + '</td>'
                        + '<td class="' + cls + '">' + p.cpu.toFixed(1) + '%</td>'
                        + '<td>' + p.mem.toFixed(1) + '%</td>'
                        + '</tr>';
                }
                tbody.innerHTML = html;
            })
            .catch(function () {
                // Silently fail, keep old data
            });
    }

    // Initial fetch
    setTimeout(fetchProcesses, 1000);

    // Poll every 5 seconds
    setInterval(fetchProcesses, 5000);

})();
