{# Shared project header strip + sync-action affordances + the not-IN_SYNC banner.
   Used by project.html (lenses), feedback.html, and review_pack.html so the sync
   truth + actions read identically on every project sub-page. `active_lens` keys the
   Check/Force-sync forms' lens; `sync_result` (optional) is the just-taken action. #}
{% import "macros.html" as m %}
<header class="fw-phead">
  <div class="fw-phead__id">
    <div class="fw-phead__name">{{ meta.name }}</div>
    <div class="fw-phead__kind mono">{{ meta.kind }}</div>
  </div>
  <div class="fw-phead__meta mono">
    {% if meta.wired %}
    <span>generated@ <b>{{ meta.source_short }}</b></span>
    <span>HEAD@ {{ meta.head_short }}</span>
    <span>at {{ meta.generated_at or '—' }}</span>
    {% else %}
    <span>not wired into the federation</span>
    {% endif %}
  </div>
  {{ m.sync_chip(meta.sync_state) }}
  <span class="fw-phead__sp"></span>
  {# Sync actions (spec §5.5): Check sync is safe (deep, non-mutating); Force-sync is
     the affirmative mutation behind a confirm disclosure. Compose feedback opens the
     one sanctioned write (§5.4, §5.6). An unwired/unreadable project can't be
     deep-checked — offer nothing false, say why (honest-absence). #}
  <div class="fw-phead__act">
    {% if meta.wired %}
    <a class="button button-secondary button-sm" href="/p/{{ meta.slug }}/feedback">Compose feedback</a>
    <form class="fw-actform" method="post" action="/p/{{ meta.slug }}/check?lens={{ active_lens }}">
      <button class="button button-secondary button-sm" type="submit"
              data-transient="CHECKING" data-busy="Checking…">Check sync</button>
    </form>
    <details class="fw-confirm">
      <summary class="button button-sm fw-act--gold">Force-sync</summary>
      <div class="fw-confirm__pop">
        <p class="fw-confirm__warn">Rebuilds <span class="mono">generated/</span> in the working
          tree — uncommitted; you review + commit project-side.</p>
        <form method="post" action="/p/{{ meta.slug }}/force-sync?lens={{ active_lens }}">
          <button class="button button-accent button-sm" type="submit"
                  data-transient="SYNCING" data-busy="Force-syncing…">Force-sync now</button>
        </form>
      </div>
    </details>
    {% else %}
    <span class="fw-phead__noact mono" title="Unwired / unreadable projects can't be deep-checked">{{ meta.sync_state }} — can't check</span>
    {% endif %}
  </div>
</header>

{% if sync_result %}
{# The result of the action just taken — kept in the readout so it prints. #}
<div class="fw-resultbar {{ 'fw-resultbar--ok' if sync_result.ok else 'fw-resultbar--warn' }}">
  <span class="fw-resultbar__icon">{{ '✓' if sync_result.ok else '!' }}</span>
  <span class="mono">{{ sync_result.text }}</span>
</div>
{% endif %}

{% if not meta.wired %}
  {# Project-level honest-absence: requested but not discoverable / unreadable. #}
  <div class="fw-phead-gap">
    {{ m.absence({
      'state': meta.sync_state,
      'title': "This project isn't wired yet" if meta.sync_state == 'UNKNOWN' else "This project's resources can't be read",
      'reason': meta.sync_detail,
      'action': 'Add a fed-resources/ tree and run the fedres generator; it then appears in the federation.' if meta.sync_state == 'UNKNOWN' else 'Regenerate on a supported schema (0.1), or upgrade the reader.',
      'errors': [],
    }) }}
  </div>
{% else %}
  {# Not-IN_SYNC banner — one line: exactly what's behind + the single action that
     fixes it (spec §5.4, §5.7). #}
  {% if meta.sync_state == 'STALE' %}
  <div class="ribbon ribbon--warning fw-ribbon">
    <span><b>{{ meta.name }}</b> is behind — {{ meta.sync_detail }}. Force-sync to rebuild
    <span class="mono">generated/</span> from current source.</span>
  </div>
  {% elif meta.sync_state == 'TAMPERED' %}
  <div class="ribbon ribbon--danger fw-ribbon">
    <span><b>{{ meta.name }}</b> — {{ meta.sync_detail }}. Force-sync to regenerate from source.</span>
  </div>
  {% elif meta.sync_state in ('UNREADABLE', 'ERROR') %}
  <div class="ribbon ribbon--danger fw-ribbon"><span>{{ meta.sync_detail }}</span></div>
  {% endif %}
{% endif %}
