{% import "macros.html" as m %}
{% if not data.catalog %}
  {{ m.empty('∅', 'No catalog',
     'This project hasn’t generated a test catalog yet. Run `testctl catalog` in the project root
      to publish the gated, freshness-checked test tree.') }}
{% elif not data.catalog.categories %}
  {{ m.empty('∅', 'No tests collected',
     'The catalog is present but empty — `pytest --collect-only` found no tests. Add tests, then
      re-run `testctl catalog`.') }}
{% else %}
  <p class="lead pw-lede">The real test tree, generated from the project’s runners and freshness-gated —
    what you see is provably the actual suite. Configs are hidden by default; expand to view or edit
    (edits ride with <i>this run</i> only, never changing committed defaults).</p>

  {% if data.staleness and data.staleness.stale %}
  <div class="ribbon ribbon--warning pw-ribbon">⚠ <span>This catalog was generated at
    <span class="mono">{{ data.staleness.generated_commit }}</span>, but the repo is now at
    <span class="mono">{{ data.staleness.current_commit }}</span> — the test list <b>may be out of date</b>.
    Run <span class="mono">testctl catalog</span> to refresh.</span></div>
  {% endif %}

  {% for cat in data.catalog.categories %}
  <details class="pw-cat" open>
    <summary class="pw-cat__h">{{ cat.label }}</summary>
    {% for group in cat.groups %}
    <div class="pw-grp__h mono">{{ group.label }}</div>
    {% for test in group.tests %}
    {% set running = test.id in data.running_ids %}
    <form class="pw-trowform" method="post" action="/p/{{ project.slug }}/run">
      <div class="pw-trow">
        <span class="pw-trow__id mono">{{ test.id }}</span>
        {{ m.status_chip(test.last_status, running=running) }}
        <span class="pw-trow__t">{{ test.label }}</span>
        <span class="pw-runner mono">{{ test.runner }}</span>
        <span class="pw-tags">
          {% if test.independent %}<span class="pill">independent</span>{% endif %}
          {% for tag in test.tags %}<span class="pill">{{ tag }}</span>{% endfor %}
        </span>
        <input type="hidden" name="test_id" value="{{ test.id }}">
        <input type="hidden" name="independent" value="{{ 'true' if test.independent else 'false' }}">
        <button class="button button-accent button-sm" type="submit" {% if broker == 'down' %}disabled{% endif %}>{{ 'Running…' if running else 'Run' }}</button>
      </div>
      {% if test.config_schema %}
      <details class="pw-cfgwrap">
        <summary class="config-toggle">config · {{ test.config_schema|length }} field(s)</summary>
        <div class="config-panel">
          <div class="config-panel__head">⚙ config · {{ test.id }} · per-run overlay</div>
          {% for field in test.config_schema %}
          {% set val = test.config.get(field.name, field.default) %}
          <div class="config-panel__row">
            <label>{{ field.name }}<span>{{ field.desc or '' }}{% if field.min is not none or field.max is not none %} · range {{ field.min if field.min is not none else '−∞' }}…{{ field.max if field.max is not none else '∞' }}{% endif %} · default {{ field.default }}</span></label>
            {% if field.type == 'bool' %}
            <select class="input input-sm" name="cfg.{{ field.name }}"><option value="true" {% if val %}selected{% endif %}>true</option><option value="false" {% if not val %}selected{% endif %}>false</option></select>
            {% elif field.type == 'enum' %}
            <select class="input input-sm" name="cfg.{{ field.name }}">{% for opt in field.options %}<option {% if opt == val %}selected{% endif %}>{{ opt }}</option>{% endfor %}</select>
            {% else %}
            <input class="input input-sm" name="cfg.{{ field.name }}" value="{{ val }}">
            {% endif %}
          </div>
          {% endfor %}
          <div class="config-panel__foot">
            <span class="config-panel__note">overlay rides with this run only · committed defaults stay read-only</span>
            <span class="pw-spacer"></span>
            <span class="pw-cfg-effective mono muted" hidden>effective: <span class="pw-cfg-eff"></span></span>
            <button type="reset" class="button button-sm button-secondary pw-cfg-reset">Reset to defaults</button>
          </div>
        </div>
      </details>
      {% endif %}
    </form>
    {% endfor %}
    {% endfor %}
  </details>
  {% endfor %}
{% endif %}
