{% extends "base.html" %}
{% block extra_head %}
<link rel="stylesheet" href="{{ static_url('/static/css/sidebar.css') }}">
<link rel="stylesheet" href="{{ static_url('/static/css/feed.css') }}">
{% endblock %}
{% block content %}
<h1 class="sr-only">Opinion Wars</h1>
<div class="feed-layout">
    <aside class="sidebar-card" role="complementary" aria-label="Battle filters">
        {% set _action = "/battles" %}{% set _placeholder = "Search battles..." %}{% set _hidden = {"filter": current_filter} %}{% include "_sidebar_search.html" %}

        <div class="sidebar-heading">Filters</div>
        <div class="sidebar-nav">
            <a href="/battles?filter=active" class="sidebar-link {% if current_filter == 'active' %}active{% endif %}">
                <span class="icon">&#x2694;&#xFE0E;</span> Active <span class="war-filter-count">{{ counts.get('active', 0) }}</span>
            </a>
            <a href="/battles?filter=ended" class="sidebar-link {% if current_filter == 'ended' %}active{% endif %}">
                <span class="icon">&#x1F3C6;</span> Ended <span class="war-filter-count">{{ counts.get('ended', 0) }}</span>
            </a>
            {% if user %}
            <a href="/battles?filter=mine" class="sidebar-link {% if current_filter == 'mine' %}active{% endif %}">
                <span class="icon">&#x1F6E1;&#xFE0E;</span> Mine <span class="war-filter-count">{{ counts.get('mine', 0) }}</span>
            </a>
            {% endif %}
        </div>

        {% if not user %}
        <div class="sidebar-section">
            <a href="/auth/login" class="sidebar-link"><span class="icon">🔑</span> Log in to fight</a>
        </div>
        {% endif %}
    </aside>

    <div class="feed-main">
        <div class="feed-posts" role="feed" aria-label="Opinion Wars">
            {% for battle in battles %}
            <article class="post-card war-listing-item">
                <div class="war-listing-head">
                    <a href="{{ battle.post_url }}" class="post-title-link"><h3 class="post-title">{{ render_title(battle.post_title) if battle.post_title else 'View post' }}</h3></a>
                    <span class="war-listing-meta">by {% set _user = battle.author %}{% include "_user_link.html" %} &middot; {{ dt_ago(battle.created_at) }}</span>
                </div>
                {% set _war = battle %}{% include "_opinion_war.html" %}
            </article>
            {% else %}
            <div class="empty-state">No battles here yet. Start an Opinion War from the Create New Post form.</div>
            {% endfor %}
        </div>

        {% set pagination_query = "filter=" ~ current_filter ~ "&" ~ ("search=" ~ search ~ "&" if search else "") %}
        {% include "_pagination.html" %}
    </div>
</div>
{% endblock %}
