{% extends "base.html" %}
{% import "macros.html" as m %}
{% block title %}{{ project.name }} — ProofWarden{% endblock %}

{% block head_extra %}
{# A live run streams. JS-off: a 3s full-page refresh keeps the lease + console
   current (read-only poll; print/other views untouched). JS-on: live-console.js
   removes that meta tag and streams the log over SSE (real-time, Pause, per-run). #}
{% if known and broker == 'up' and view == 'running' and data.cards %}<meta http-equiv="refresh" content="3"><script defer src="/static/live-console.js"></script>{% endif %}
{# Catalog config panels get a live effective-config preview (PE — form works JS-off). #}
{% if known and view == 'catalog' and data.catalog %}<script defer src="/static/catalog-config.js"></script>{% endif %}
{% endblock %}

{% block main %}
<header class="pw-phead">
  <div>
    <div class="pw-phead__name">{{ project.name }}</div>
    <div class="pw-phead__kind mono">{{ project.kind or ('unknown project' if not known else 'test project') }}</div>
  </div>
  <span class="pw-phead__sp"></span>
  {% if broker == 'down' %}
  <span class="service-status service-status--down">warden down</span>
  {% else %}
  <span class="service-status service-status--up">warden up</span>
  {% endif %}
</header>

{% if not known %}
  {{ m.empty('?', 'Unknown project',
     'No project “' ~ project.slug ~ '” is wired into this console. Point MGF_TEST_WEB_ROOTS at its
      directory — it needs a fed-resources/ tree (where the warden socket + catalog live).') }}
{% else %}
  {% if broker == 'down' and view != 'history' %}
  <div class="ribbon ribbon--danger pw-ribbon">⛔ <span><b>{{ project.name }}</b>'s test authority is
    unreachable — <b>no runs can start</b> (fail-closed). Start the warden
    (<span class="mono">testctl serve</span>) to run or manage tests.</span></div>
  {% endif %}

  {% if outcome %}
  {% set tone = {'started': 'info', 'coalesced': 'info', 'queued': 'warning', 'refused': 'danger'}.get(outcome.status, 'info') %}
  <div class="ribbon ribbon--{{ tone }} pw-ribbon">
    {% if outcome.status == 'started' %}▶ <span>Run <b>started</b> · {{ outcome.test_id }} →
      <span class="mono">{{ outcome.run_id }}</span> · {{ outcome.config_str }}. The warden is the executor — streaming below.</span>
    {% elif outcome.status == 'coalesced' %}⇄ <span><b>Single-flight</b>: {{ outcome.test_id }} is already
      running — attached as a watcher to <span class="mono">{{ outcome.run_id }}</span>; <b>did not start a second run</b>.</span>
    {% elif outcome.status == 'queued' %}⏳ <span><b>Serialized</b>: {{ outcome.test_id }} queued
      (position {{ outcome.position }}) — it starts when the lease frees. The console submitted the request; the warden decides.</span>
    {% else %}⛔ <span>{{ outcome.note }}.</span>
    {% endif %}
  </div>
  {% endif %}

  <nav class="tabs pw-tabs" aria-label="Views">
    {% for v in views %}
    <a class="tab {% if v == view %}tab--active{% endif %}" href="/p/{{ project.slug }}/{{ v }}">{{ view_labels[v] }}{% if v == 'running' %}{% if counts.running %} · ● live{% elif counts.queued %} · {{ counts.queued }} queued{% endif %}{% elif v == 'history' %} · {{ counts.history }}{% endif %}</a>
    {% endfor %}
  </nav>

  {% include "_" + view + ".html" %}
{% endif %}
{% endblock %}
