{% extends "base.html" %}
{% block title %}Pulse — psyc admin{% endblock %}
{% block content %}
<section class="panel">
  <div class="panel-head">
    <h1>Pulse — autonomous heartbeat</h1>
    <span class="count">{{ pipelines|length }} pipeline{{ '' if pipelines|length == 1 else 's' }}</span>
  </div>
  <p class="page-intro">Cron-style scheduler that drives every psyc line on a cadence without human input. Each pipeline has an autonomy mode and a cadence in seconds. The kill switch halts everything instantly — it overrides cadence, mode, and the enabled flag.</p>
  <p class="back"><a href="/admin">← back to admin</a></p>

  {% if flash %}
  <div class="verdict verdict-clean">{{ flash }}</div>
  {% endif %}
</section>

<section class="panel">
  <div class="panel-head">
    <h2>Global kill switch</h2>
    <span class="count">{{ 'ARMED' if kill_switch else 'OFF' }}</span>
  </div>
  {% if kill_switch %}
  <div class="verdict" style="background:rgba(248,113,113,0.12); border:1px solid rgba(248,113,113,0.45); color:#fca5a5; padding:14px 18px; border-radius:6px; font-weight:700; letter-spacing:0.04em;">
    ✗ KILL SWITCH ARMED — every pipeline is paused. tick() returns "skipped" for everything. Run-now is also blocked. Toggle off to resume.
  </div>
  <form method="post" action="/admin/pulse/kill" style="margin-top:14px;">
    <button type="submit" class="btn btn-approve">Disarm kill switch</button>
  </form>
  {% else %}
  <div class="verdict verdict-clean">✓ Pulse is live — the background loop ticks every {{ tick_interval }}s.</div>
  <form method="post" action="/admin/pulse/kill" style="margin-top:14px;"
        onsubmit="return confirm('Arm the kill switch? Every pipeline halts immediately.');">
    <button type="submit" class="btn btn-reject">Arm kill switch</button>
  </form>
  {% endif %}
</section>

<section class="panel">
  <div class="panel-head">
    <h2>Pipelines</h2>
    <span class="count">{{ pipelines|selectattr('enabled')|list|length }} enabled</span>
  </div>
  <p class="page-intro">Mode: <code>auto-execute</code> fires the line, <code>auto-propose</code> stages proposals for human approval, <code>manual</code> runs only when you press “Run now”. Cadence is the gap between ticks; the loop wakes up every {{ tick_interval }}s.</p>

  <table class="ledger">
    <thead>
      <tr>
        <th style="width:24%;">Pipeline</th>
        <th>Mode · cadence · enabled</th>
        <th style="width:11%;">Last fired</th>
        <th style="width:11%;">Next fire</th>
        <th>Last result</th>
        <th style="width:1%;"></th>
      </tr>
    </thead>
    <tbody>
    {% for p in pipelines %}
      <tr class="ledger-row">
        <td>
          <strong>{{ p.title }}</strong>
          <div class="lg-sub"><code>{{ p.name }}</code> · {{ p.description }}</div>
        </td>
        <td>
          <form method="post" action="/admin/pulse/{{ p.name }}/update" class="queue-action" style="display:flex; gap:8px; align-items:center; flex-wrap:wrap;">
            <select name="mode" style="background:var(--panel-2); color:var(--text); border:1px solid var(--border); border-radius:4px; padding:4px 6px;">
              <option value="auto-execute" {% if p.mode.value == 'auto-execute' %}selected{% endif %}>auto-execute</option>
              <option value="auto-propose" {% if p.mode.value == 'auto-propose' %}selected{% endif %}>auto-propose</option>
              <option value="manual"       {% if p.mode.value == 'manual' %}selected{% endif %}>manual</option>
            </select>
            <input type="number" name="cadence_seconds" value="{{ p.cadence_seconds }}" min="1" step="1"
                   style="background:var(--panel-2); color:var(--text); border:1px solid var(--border); border-radius:4px; padding:4px 6px; width:84px;" title="cadence in seconds">
            <label style="display:inline-flex; align-items:center; gap:4px; font-size:12px;">
              <input type="checkbox" name="enabled" value="1" {% if p.enabled %}checked{% endif %}> enabled
            </label>
            <button type="submit" class="btn">save</button>
          </form>
        </td>
        <td class="lg-ts">
          {% if p.last_fired %}<span title="{{ p.last_fired.isoformat() }}">{{ relative(p.last_fired) }}</span>{% else %}—{% endif %}
        </td>
        <td class="lg-ts">
          {% if p.next_fire %}<span title="{{ p.next_fire.isoformat() }}">{{ relative(p.next_fire) }}</span>{% else %}—{% endif %}
        </td>
        <td class="lg-sub" style="max-width:0;">
          {% if p.last_outcome == 'ok' %}<span style="color: var(--green);">✓</span>
          {% elif p.last_outcome == 'err' %}<span style="color: var(--red);">✗</span>
          {% elif p.last_outcome == 'skipped' %}<span style="color: var(--muted);">⊘</span>
          {% endif %}
          {{ (p.last_result or '—')[:60] }}{% if (p.last_result or '')|length > 60 %}…{% endif %}
        </td>
        <td>
          <form method="post" action="/admin/pulse/{{ p.name }}/run" class="queue-action">
            <button type="submit" class="btn btn-enforce" title="Fire now, regardless of cadence">Run now</button>
          </form>
        </td>
      </tr>
    {% endfor %}
    </tbody>
  </table>
</section>
{% endblock %}
