{% extends "base.html" %}
{% block banner_subtitle %}Finished Decks{% endblock %}
{% block content %}
<h2>Finished Deck</h2>
{% if display_name %}
  <div><strong>{{ display_name }}</strong></div>
{% endif %}
{% set hover_tags_source = deck_theme_tags if deck_theme_tags else (tags if tags else commander_combined_tags) %}
{% set hover_tags_joined = hover_tags_source|join(', ') %}
<div class="muted">Commander:
  <strong class="commander-hover"
    data-card-name="{{ commander }}"
    data-original-name="{{ commander }}"
    data-role="{{ commander_role_label }}"
      {% if hover_tags_joined %}data-tags="{{ hover_tags_joined }}"{% endif %}
    {% if commander_tag_slugs %}data-tags-slug="{{ commander_tag_slugs|join(' ') }}"{% endif %}
    {% if commander_overlap_tags %}data-overlaps="{{ commander_overlap_tags|join(', ') }}"{% endif %}
    {% if commander_reason_text %}data-reasons="{{ commander_reason_text|e }}"{% endif %}>{{ commander }}</strong>
  {% if tags and tags|length %} • Themes: {{ tags|join(', ') }}{% endif %}
</div>
<div class="muted">This view mirrors the end-of-build summary. Use the buttons to download the CSV/TXT exports.</div>

<div class="two-col two-col-left-rail" style="margin-top:.75rem;">
  <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) %}
    <div class="commander-card"
      tabindex="0"
      style="display:inline-block; cursor:pointer;"
      data-card-name="{{ commander_base }}"
      data-original-name="{{ commander }}"
      data-role="{{ commander_role_label }}"
    {% if hover_tags_joined %}data-tags="{{ hover_tags_joined }}"{% endif %}
      {% if commander_tag_slugs %}data-tags-slug="{{ commander_tag_slugs|join(' ') }}"{% endif %}
      {% if commander_overlap_tags %}data-overlaps="{{ commander_overlap_tags|join(', ') }}"{% endif %}
      {% if commander_reason_text %}data-reasons="{{ commander_reason_text|e }}"{% endif %}>
      <img src="https://api.scryfall.com/cards/named?fuzzy={{ commander_base|urlencode }}&format=image&version=normal"
           alt="{{ commander }} card image"
           data-card-name="{{ commander_base }}"
           data-original-name="{{ commander }}"
           data-role="{{ commander_role_label }}"
    {% if hover_tags_joined %}data-tags="{{ hover_tags_joined }}"{% endif %}
       {% if commander_tag_slugs %}data-tags-slug="{{ commander_tag_slugs|join(' ') }}"{% endif %}
       {% if commander_overlap_tags %}data-overlaps="{{ commander_overlap_tags|join(', ') }}"{% endif %}
     {% if commander_reason_text %}data-reasons="{{ commander_reason_text|e }}"{% endif %}
           width="320" />
    </div>
    <div class="muted" style="margin-top:.25rem;">Commander: <span data-card-name="{{ commander }}"
    data-original-name="{{ commander }}"
    data-role="{{ commander_role_label }}"
  {% if hover_tags_joined %}data-tags="{{ hover_tags_joined }}"{% endif %}
      {% if commander_tag_slugs %}data-tags-slug="{{ commander_tag_slugs|join(' ') }}"{% endif %}
      {% if commander_overlap_tags %}data-overlaps="{{ commander_overlap_tags|join(', ') }}"{% endif %}
    {% if commander_reason_text %}data-reasons="{{ commander_reason_text|e }}"{% endif %}>{{ commander }}</span></div>
    {% endif %}
    <div style="margin-top:.75rem; display:flex; gap:.35rem; flex-wrap:wrap;">
      {% if 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 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 %}
      <a href="/decks/compare?A={{ name|urlencode }}" class="btn" role="button" title="Compare this deck with another">Compare…</a>
      <form method="get" action="/decks" style="display:inline; margin:0;">
        <button type="submit">Back to Finished Decks</button>
      </form>
    </div>
  </aside>
  <div class="grow">
    {% if summary %}
      {% if owned_set %}
        {% set ns = namespace(owned=0, total=0) %}
        {% set tb = summary.type_breakdown %}
        {% if tb and tb.cards %}
          {% for t, clist in tb.cards.items() %}
            {% for c in clist %}
              {% set cnt = c.count if c.count else 1 %}
              {% set ns.total = ns.total + cnt %}
              {% if c.name and (c.name|lower in owned_set) %}
                {% set ns.owned = ns.owned + cnt %}
              {% endif %}
            {% endfor %}
          {% endfor %}
        {% endif %}
        {% set not_owned = (ns.total - ns.owned) %}
        {% set pct = ( (ns.owned * 100.0 / (ns.total or 1)) | round(1) ) %}
        <div class="panel" style="margin-bottom:.75rem;">
          <div style="display:flex; gap:1rem; align-items:center; flex-wrap:wrap;">
            <div><strong>Ownership</strong></div>
            <div class="muted">Owned: {{ ns.owned }} • Not owned: {{ not_owned }} • Total: {{ ns.total }} ({{ pct }}%)</div>
          </div>
        </div>
      {% endif %}
  {{ render_cached('partials/deck_summary.html', name, request=request, summary=summary, game_changers=game_changers, owned_set=owned_set, combos=combos, synergies=synergies, versions=versions) | safe }}
    {% else %}
      <div class="muted">No summary available.</div>
    {% endif %}
  </div>
</div>
{% endblock %}
