// ── Live metrics via /api/metrics (served by Node.js app layer) ───────

function setBar(barId, valId, pct, label) {
  const bar = document.getElementById(barId);
  const val = document.getElementById(valId);
  if (bar) {
    bar.style.width = Math.min(pct, 100) + "%";
  }
  if (val && label !== undefined) {
    val.textContent = label;
  }
}

async function updateMetrics() {
  try {
    const res = await fetch("/api/metrics");
    const data = await res.json();

    setBar("cpu-bar", "cpu-val", data.cpu, data.cpu + "%");
    setBar("ram-bar", "ram-val", data.memPct, data.memUsedGB + "G");
    setBar("gpu-bar", "gpu-val", 5, "5%"); // GPU live data future enhancement
    setBar("disk-bar", "disk-val", 5, "5%");

    const uptimeEl = document.getElementById("uptime");
    if (uptimeEl) {
      uptimeEl.textContent = data.uptime;
    }
  } catch {
    // Server not ready yet — no-op
  }
}

function updateUptime() {
  // Static uptime from system — weeks:days:hours
  const el = document.getElementById("uptime");
  if (el) {
    el.textContent = "2w 3d 21h";
  }
}

function updateSubagents() {
  const el = document.getElementById("subagent-count");
  if (el) {
    el.textContent = "0 active";
  }
}

// Initial + interval updates
void updateMetrics();
updateUptime();
updateSubagents();

setInterval(updateMetrics, 2000);
