{% import "macros.html" as m %}
{% if data.total == 0 %}
  {{ m.empty('∅', 'No past runs',
     'Once tests run, their records (result, effective config, lease audit) are stored locally and listed here.') }}
{% else %}
  <p class="lead pw-lede">Every run that happened — local or CI — reconstructed from its run-record: the exact
    <b>effective config</b> that produced the result and the full <b>lease audit</b> (including any
    double-launch the authority absorbed).</p>
  <div class="pw-filters">
    <span class="pw-filters__lbl mono muted">result</span>
    {% for f in data.filters %}
    <a class="pw-fpill {% if data.active.result == f %}pw-fpill--on{% endif %}"
       href="/p/{{ project.slug }}/history?result={{ f }}&test={{ data.active.test }}&origin={{ data.active.origin }}">{{ f }}</a>
    {% endfor %}
  </div>
  <form class="pw-filterform" method="get" action="/p/{{ project.slug }}/history">
    <input type="hidden" name="result" value="{{ data.active.result }}">
    <label class="pw-filterform__l mono muted">test
      <select name="test" class="pw-fselect">
        <option value="all"{% if data.active.test == 'all' %} selected{% endif %}>all</option>
        {% for t in data.tests %}<option value="{{ t }}"{% if data.active.test == t %} selected{% endif %}>{{ t }}</option>{% endfor %}
      </select></label>
    <label class="pw-filterform__l mono muted">origin
      <select name="origin" class="pw-fselect">
        <option value="all"{% if data.active.origin == 'all' %} selected{% endif %}>all</option>
        {% for o in data.origins %}<option value="{{ o }}"{% if data.active.origin == o %} selected{% endif %}>{{ o }}</option>{% endfor %}
      </select></label>
    <button class="button button-sm button-secondary" type="submit">Filter</button>
    {% if data.active.result != 'all' or data.active.test != 'all' or data.active.origin != 'all' %}<a class="pw-fclear mono muted" href="/p/{{ project.slug }}/history">clear</a>{% endif %}
  </form>
  {% if not data.rows %}
    {{ m.empty('∅', 'No matching runs', 'No runs match this filter (result / test / origin). Clear it to see all runs.') }}
  {% else %}
  <div class="card pw-panel">
  <table class="pw-grid">
    <thead><tr><th>Test</th><th>Result</th><th>Duration</th><th>Origin</th><th>Effective config</th><th>Started</th></tr></thead>
    <tbody>
    {% for r in data.rows %}
    <tr>
      <td class="mono">{{ r.test_id }}</td>
      <td>{{ m.status_chip(r.outcome) }}</td>
      <td class="mono muted">{{ r.duration_s }}s</td>
      <td>{{ m.origin_badge(r.origin) }}</td>
      <td class="mono muted">{{ r.config_str }}{% if r.coalesced %} · <span class="pw-absorbed">absorbed 1 double-launch</span>{% endif %}</td>
      <td class="mono muted">{{ r.started_hhmmss }}</td>
    </tr>
    <tr class="pw-detailrow"><td colspan="6">
      <details class="pw-detailwrap">
        <summary class="muted mono">run detail · lease audit</summary>
        <div class="detail">
          <div><span class="detail__key">run_id</span> {{ r.run_id }}</div>
          <div><span class="detail__key">effective config</span> {{ r.effective_config | tojson }}</div>
          {% if r.config_digest %}<div><span class="detail__key">config digest</span> {{ r.config_digest }}</div>{% endif %}
          {% if r.lease %}<div><span class="detail__key">lease</span> heartbeats {{ r.lease.heartbeats }} · reclaimed {{ r.lease.reclaimed }}</div>{% endif %}
          {% if r.audit %}<div class="pw-audit-list">{% for a in r.audit %}<div>· {{ a.ts[11:19] if a.ts|length >= 19 else a.ts }} <b>{{ a.event }}</b>{% if a.origin %} ({{ a.origin }}){% endif %}{% if a.other_run %} ↔ {{ a.other_run }}{% endif %}</div>{% endfor %}</div>{% endif %}
        </div>
      </details>
    </td></tr>
    {% endfor %}
    </tbody>
  </table>
  </div>
  {% endif %}
{% endif %}
