{% extends "base.html" %}
{% block content %}
<h2>Build from JSON: {{ cfg_name }}</h2>
<p class="muted" style="max-width: 70ch;">This page shows the results of a non-interactive build from the selected JSON configuration.</p>
{% if commander %}
<div class="muted">Commander: <strong data-card-name="{{ commander }}">{{ commander }}</strong>{% if tag_mode %} · Combine: <code>{{ tag_mode }}</code>{% endif %}{% if use_owned_only %} · Owned-only{% endif %}</div>
{% endif %}

<div class="two-col two-col-left-rail">
  <aside class="card-preview">
    {% if commander %}
    {# Strip synergy annotation for Scryfall search and image fuzzy param #}
    {% set commander_base = (commander.split(' - Synergy (')[0] if ' - Synergy (' in commander else commander) %}
    <a href="https://scryfall.com/search?q={{ commander_base|urlencode }}" target="_blank" rel="noopener">
  <img src="https://api.scryfall.com/cards/named?fuzzy={{ commander_base|urlencode }}&format=image&version=normal" alt="{{ commander }} card image" width="320" data-card-name="{{ commander_base }}" />
    </a>
    {% endif %}
    <div style="margin-top:.75rem; display:flex; gap:.35rem; flex-wrap:wrap;">
      {% if ok and csv_path %}
      <form action="/files" method="get" target="_blank" style="display:inline; margin:0;">
        <input type="hidden" name="path" value="{{ csv_path }}" />
        <button type="submit">Download CSV</button>
      </form>
      {% endif %}
      {% if ok and txt_path %}
      <form action="/files" method="get" target="_blank" style="display:inline; margin:0;">
        <input type="hidden" name="path" value="{{ txt_path }}" />
        <button type="submit">Download TXT</button>
      </form>
      {% endif %}
      <form method="get" action="/configs" style="display:inline; margin:0;">
        <button type="submit">Back to Build from JSON</button>
      </form>
    </div>
  </aside>
  <div class="grow">
    {% if not ok %}
      <div class="error">Build failed: {{ error }}</div>
    {% else %}
      <div class="notice">Build completed{% if commander %} — <strong>{{ commander }}</strong>{% endif %}</div>
      
      <!-- Include/Exclude Summary Panel (M3: Include/Exclude Summary Panel) -->
      {% include "partials/include_exclude_summary.html" %}

      {% if summary %}
  {{ render_cached('partials/deck_summary.html', cfg_name, request=request, summary=summary, game_changers=game_changers, owned_set=owned_set, combos=combos, synergies=synergies, versions=versions) | safe }}
      {% endif %}
    {% endif %}

    {% if show_logs %}
      <details style="margin-top:1rem;">
        <summary>Show logs</summary>
        <pre style="margin-top:.5rem; white-space:pre-wrap; background:#0f1115; border:1px solid var(--border); padding:1rem; border-radius:8px; max-height:40vh; overflow:auto;">{{ log }}</pre>
      </details>
    {% endif %}

    
  </div>
</div>
{% endblock %}
