/* system-pulse.jsx — multi-track agent activity timeline (last 60 seconds) */
function SystemPulse() {
  const [tick, setTick] = React.useState(0);
  const [hovered, setHovered] = React.useState(null);
  React.useEffect(() => {
    return window.NTData.subscribe((evt) => {
      if (evt.type === "pulse") setTick(t => t + 1);
    });
  }, []);

  const pulse = window.NTData.getPulse();
  const agents = window.NTData.AGENTS;
  const W = 60; // 60 seconds visible
  const lastValues = agents.map(a => pulse[a.id][pulse[a.id].length - 1]);
  const allEvents = agents.reduce((sum, a, i) => sum + pulse[a.id].filter(v => v > 0.4).length, 0);

  return (
    <div className="syspulse">
      <style>{`
        .syspulse {
          display: grid;
          grid-template-columns: 220px 1fr 180px;
          gap: var(--pad-3);
          padding: var(--pad-2) var(--pad-3);
          background: var(--bg-elev);
          border-bottom: 1px solid var(--border);
          align-items: center;
          height: 86px;
          flex-shrink: 0;
        }
        .syspulse__title {
          display: flex; flex-direction: column; gap: 2px;
        }
        .syspulse__eyebrow {
          font-family: var(--font-mono); font-size: 9px;
          color: var(--accent); letter-spacing: 0.18em; text-transform: uppercase;
        }
        .syspulse__heading {
          font-family: var(--font-display); font-size: 14px;
          color: var(--fg); font-weight: 600; letter-spacing: -0.01em;
        }
        .syspulse__sub {
          font-family: var(--font-mono); font-size: 9.5px;
          color: var(--fg-muted); letter-spacing: 0.04em;
        }
        .syspulse__tracks {
          position: relative;
          display: grid;
          grid-template-columns: 56px 1fr;
          row-gap: 1px;
          column-gap: 8px;
          align-items: center;
          height: 100%;
          padding: 4px 0;
        }
        .syspulse__agent-label {
          font-family: var(--font-mono); font-size: 9px;
          letter-spacing: 0.12em; text-transform: uppercase;
          font-weight: 600;
          text-align: right;
        }
        .syspulse__track {
          position: relative; height: 12px;
          background: linear-gradient(to right,
            transparent 0%,
            color-mix(in srgb, var(--border) 60%, transparent) 20%,
            color-mix(in srgb, var(--border) 60%, transparent) 100%);
          border-radius: 1px;
          overflow: hidden;
        }
        .syspulse__bars {
          display: flex; gap: 1px; height: 100%;
          align-items: flex-end;
          position: absolute; inset: 0;
        }
        .syspulse__bar {
          flex: 1; min-width: 0;
          border-radius: 0.5px;
          transition: height .3s ease, opacity .3s ease;
        }
        .syspulse__scrubber {
          position: absolute;
          right: 0;
          top: -2px; bottom: -2px;
          width: 1px;
          background: var(--accent);
          opacity: 0.5;
          box-shadow: 0 0 6px var(--accent);
        }
        .syspulse__legend {
          display: flex; flex-direction: column; gap: 4px;
          font-family: var(--font-mono); font-size: 9px;
          color: var(--fg-muted);
        }
        .syspulse__legend-row {
          display: flex; align-items: center; justify-content: space-between;
          padding: 2px 6px;
          border-radius: 2px;
        }
        .syspulse__legend-row:hover { background: var(--bg-subtle); }
        .syspulse__legend-label {
          letter-spacing: 0.1em; text-transform: uppercase;
        }
        .syspulse__legend-val {
          font-weight: 600; color: var(--fg-2);
        }
        .syspulse__axis {
          position: absolute; bottom: -2px; left: 64px; right: 0;
          height: 8px;
          display: flex; justify-content: space-between;
          font-family: var(--font-mono); font-size: 8px;
          color: var(--fg-faint); letter-spacing: 0.06em;
          pointer-events: none;
        }
        .syspulse__axis span:nth-child(2) { text-align: center; }
        .syspulse__axis span:nth-child(3) { text-align: right; }
      `}</style>

      <div className="syspulse__title">
        <span className="syspulse__eyebrow">System Pulse</span>
        <span className="syspulse__heading">Agent activity · last 60s</span>
        <span className="syspulse__sub">{allEvents} bursts · {agents.length} agents online</span>
      </div>

      <div className="syspulse__tracks">
        {agents.map((a, i) => {
          const data = pulse[a.id];
          return (
            <React.Fragment key={a.id}>
              <div className="syspulse__agent-label" style={{ color: a.color }}>{a.id}</div>
              <div className="syspulse__track"
                   onMouseEnter={() => setHovered(a.id)}
                   onMouseLeave={() => setHovered(null)}>
                <div className="syspulse__bars">
                  {data.map((v, j) => (
                    <div
                      key={j}
                      className="syspulse__bar"
                      style={{
                        height: `${Math.max(8, v * 100)}%`,
                        background: a.color,
                        opacity: 0.25 + v * 0.7,
                      }}
                    />
                  ))}
                </div>
                {hovered === a.id && (
                  <div className="syspulse__scrubber"></div>
                )}
              </div>
            </React.Fragment>
          );
        })}
        <div></div>
        <div className="syspulse__axis">
          <span>-60s</span>
          <span>-30s</span>
          <span>NOW</span>
        </div>
      </div>

      <div className="syspulse__legend">
        {agents.map((a, i) => (
          <div className="syspulse__legend-row" key={a.id}>
            <span className="syspulse__legend-label" style={{ color: a.color }}>● {a.id}</span>
            <span className="syspulse__legend-val">
              {(lastValues[i] * 100).toFixed(0)}<span className="fg-dim">%</span>
            </span>
          </div>
        ))}
      </div>
    </div>
  );
}

window.SystemPulse = SystemPulse;
