{% extends 'generic/examcollection_base.html' %}

{% block content %}

<h1 class="mb-4">Exam Collections</h1>

<form method="get" class="mb-3" id="archive-filter-form">
    <div class="form-check form-switch">
        <input class="form-check-input" type="checkbox" id="showArchived" name="show_archived" value="1" onchange="this.form.submit()"
               {% if request.GET.show_archived %}checked{% endif %}>
        <label class="form-check-label" for="showArchived">Show archived collections</label>
    </div>
</form>

{% if object_list %}
<div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 g-3">
    {% for collection in object_list %}
    <div class="col">
        <div class="card h-100 shadow-sm">
            <div class="card-body d-flex flex-column">
                <div class="mb-2">
                    <a class="stretched-link h5 mb-0 d-block" href="{% url 'generic:examcollection_detail' collection.pk %}">{{ collection.name }}</a>
                    {% if collection.date %}
                        <div class="text-muted small">Date: {{ collection.date }}</div>
                    {% endif %}
                </div>

                <div class="mb-2">
                    <small class="text-muted">Authors: {{ collection.get_authors }}</small>
                </div>

                <div class="mb-3">
                    {% comment %} Show archive state and counts per exam type {% endcomment %}
                    {% if collection.archive %}
                        <span class="badge bg-secondary" title="Archived">Archived</span>
                    {% endif %}

                    {# Use annotated counts provided by the view to avoid per-card COUNT queries #}
                    <span class="badge bg-info text-dark ms-1" title="Total exams">{{ collection.total_count }} exams</span>
                    {# Per-type badges (only show non-zero) #}
                    {% with a=collection.anatomy_count l=collection.longs_count p=collection.physics_count r=collection.rapids_count s=collection.sbas_count sh=collection.shorts_count %}
                        {% if a %}<span class="badge bg-primary ms-1">Anatomy {{ a }}</span>{% endif %}
                        {% if l %}<span class="badge bg-primary ms-1">Longs {{ l }}</span>{% endif %}
                        {% if p %}<span class="badge bg-primary ms-1">Physics {{ p }}</span>{% endif %}
                        {% if r %}<span class="badge bg-primary ms-1">Rapids {{ r }}</span>{% endif %}
                        {% if s %}<span class="badge bg-primary ms-1">SBAs {{ s }}</span>{% endif %}
                        {% if sh %}<span class="badge bg-primary ms-1">Shorts {{ sh }}</span>{% endif %}
                    {% endwith %}
                </div>

                <div class="mt-auto d-flex gap-2">
                    <a class="btn btn-sm btn-outline-primary" href="{% url 'generic:examcollection_detail' collection.pk %}">View</a>
                    {% if can_edit %}
                        <a class="btn btn-sm btn-outline-secondary" href="{% url 'generic:examcollection_edit' collection.pk %}">Edit</a>
                        <a class="btn btn-sm btn-outline-warning" href="{% url 'generic:examcollection_clone' collection.pk %}">Clone</a>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
    {% endfor %}
</div>
{% else %}
    <div class="alert alert-info">No exam collections yet.</div>
{% endif %}


{% endblock %}

{% block js %}

{% endblock %}
