{% extends "base.html" %}
{% import "macros.html" as m %}
{% block title %}Federation overview — mgf-fed-web{% endblock %}

{% block main %}
<div class="fw-view-h">
  <h1 class="fw-view-h__t">Federation overview</h1>
  <span class="fw-view-h__s mono">{{ chrome.projects|length }} project{{ '' if chrome.projects|length == 1 else 's' }} · combined</span>
</div>
<p class="lead fw-lede">
  {{ summary }} The overview opens on the sync matrix — every project × its state,
  <span class="mono">source@commit</span> vs <span class="mono">HEAD@commit</span>. States are read
  <span class="mono">{{ check_mode }}</span>{% if check_mode == 'shallow' %} (manifest + HEAD) on load —
  run <b>Check all</b> for the authoritative deep verdict{% else %} (regenerate → diff) — authoritative{% endif %};
  a project with no resources reads <span class="mono">UNKNOWN</span>, never blank.
</p>

<div class="lens-tabs fw-lensbar">
  <span class="lens-tab lens-tab--active">Sync</span>
</div>

<section class="card fw-panel">
  <header class="fw-panel__head">
    <h2 class="card__title fw-panel__title">Sync matrix</h2>
    <span class="fw-spacer"></span>
    <span class="fw-panel__note mono">{{ m.derived() }} aggregate = worst project state</span>
  </header>
  <div class="table-wrap">
    <table class="table fw-matrix">
      <thead>
        <tr>
          <th>Project</th>
          <th>State</th>
          <th>Generated from → HEAD</th>
          <th>Checked</th>
          <th class="fw-matrix__acth">Actions</th>
        </tr>
      </thead>
      <tbody>
        {% for p in matrix %}
        <tr>
          <td>
            <a class="fw-matrix__name" href="/p/{{ p.slug }}">
              <b>{{ p.name }}</b><span class="mono">{{ p.kind }}</span>
            </a>
          </td>
          <td>{{ m.sync_chip(p.sync_state) }}</td>
          <td>
            {% if p.source_commit %}
            <span class="fw-commits mono">
              <b>{{ p.source_short }}</b><span class="fw-arrow">→</span>{% if p.behind %}<span class="fw-behind">{{ p.head_short }}</span>{% else %}{{ p.head_short }}{% endif %}
            </span>{% if p.behind %} <span class="muted mono">(behind)</span>{% endif %}
            {% else %}<span class="muted">—</span>{% endif %}
            <div class="fw-resultline mono {% if p.sync_state == 'IN_SYNC' %}fw-resultline--ok{% endif %}">{{ p.sync_detail }}</div>
          </td>
          <td><span class="mono muted">{{ chrome.check_mode }} · {{ chrome.last_checked }}</span></td>
          {# Per-row actions (spec §5.3): Check is safe; Force-sync confirms, then routes
             to the project so its settled state + result line are shown in full. #}
          <td class="fw-matrix__act">
            {% if p.sync_state in ('UNKNOWN', 'UNREADABLE') %}
            <span class="muted mono">can't check</span>
            {% else %}
            <form class="fw-actform" method="post" action="/p/{{ p.slug }}/check">
              <button class="button button-secondary button-sm" type="submit"
                      data-transient="CHECKING" data-busy="Checking…">Check</button>
            </form>
            <form class="fw-actform" method="post" action="/p/{{ p.slug }}/force-sync"
                  onsubmit="return confirm('Force-sync rebuilds {{ p.slug }}/fed-resources/generated/ in the working tree (uncommitted). Continue?');">
              <button class="button button-sm fw-act--gold" type="submit"
                      data-transient="SYNCING" data-busy="Syncing…">Force-sync</button>
            </form>
            {% endif %}
          </td>
        </tr>
        {% else %}
        <tr><td colspan="5">{{ m.empty_inline("No projects discovered", "No sibling project carries a fed-resources/generated/manifest.json yet.") }}</td></tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
</section>
{% endblock %}
