{% extends "allianceauth/base-bs5.html" %}
{% load i18n %}

{% block page_title %}{% translate "Discord Audit Run" %} #{{ run.pk }}{% endblock %}
{% block header_nav_brand %}{% translate "Discord Audit" %}{% endblock %}

{% block content %}
{% include "aa_discord_audit/_subnav.html" with active="runs" %}

<nav aria-label="{% translate 'breadcrumb' %}" class="mb-3">
    <ol class="breadcrumb">
        <li class="breadcrumb-item">
            <a href="{% url 'aa_discord_audit:run_list' %}">{% translate "Discord Audit" %}</a>
        </li>
        <li class="breadcrumb-item active" aria-current="page">{% blocktranslate with run_id=run.pk %}Run #{{ run_id }}{% endblocktranslate %}</li>
    </ol>
</nav>

<div class="mb-4">
    <h2>{% translate "AuditRun" %} #{{ run.pk }}</h2>

    <table class="table table-bordered w-auto">
        <tbody>
            <tr><th>{% translate "State" %}</th><td>{{ run.state }}</td></tr>
            <tr><th>{% translate "Triggered by" %}</th><td>{{ run.triggered_by }}</td></tr>
            <tr><th>{% translate "Triggered by user" %}</th><td>{{ run.triggered_by_user|default:"&mdash;" }}</td></tr>
            <tr><th>{% translate "Started" %}</th><td>{{ run.started_at|date:"Y-m-d H:i:s" }}</td></tr>
            <tr><th>{% translate "Finished" %}</th><td>{% if run.finished_at %}{{ run.finished_at|date:"Y-m-d H:i:s" }}{% else %}&mdash;{% endif %}</td></tr>
            <tr><th>{% translate "Members scanned" %}</th><td>{{ run.members_scanned }}</td></tr>
            <tr><th>{% translate "Findings" %}</th><td>{{ run.findings_count }}</td></tr>
            <tr><th>{% translate "Actions applied" %}</th><td>{{ run.actions_applied }}</td></tr>
            <tr><th>{% translate "Actions skipped" %}</th><td>{{ run.actions_skipped }}</td></tr>
            <tr><th>{% translate "Dry run" %}</th><td>{{ run.dry_run|yesno:_("yes,no") }}</td></tr>
            <tr><th>{% translate "First-run lock" %}</th><td>{{ run.first_run_lock_was_active|yesno:_("yes,no") }}</td></tr>
            <tr><th>{% translate "Confirmation bypassed" %}</th><td>{{ run.confirmation_bypassed|yesno:_("yes,no") }}</td></tr>
            {% if run.error_message %}
                <tr><th>{% translate "Error" %}</th><td><pre class="mb-0">{{ run.error_message }}</pre></td></tr>
            {% endif %}
        </tbody>
    </table>
</div>

<div class="d-flex align-items-center gap-2 mb-2">
    <h3 class="mb-0">{% translate "Findings" %}</h3>
    {% if can_view_findings %}
        <a href="{% url 'aa_discord_audit:findings_csv' pk=run.pk %}"
           class="btn btn-outline-secondary btn-sm">
            {% translate "Download CSV" %}
        </a>
    {% endif %}
</div>
{% if not can_view_findings %}
    <div class="alert alert-warning">
        {% translate "You lack the view_auditfinding permission; per-finding details are hidden." %}
    </div>
{% else %}
    <form method="get" class="mb-3 d-flex align-items-center gap-2 flex-wrap">
        <label class="form-label mb-0">{% translate "Category" %}</label>
        <select name="category" class="form-select w-auto">
            <option value="">{% translate "(any)" %}</option>
            {% for c in categories %}
                <option value="{{ c.value }}"{% if c.value == selected_filters.category %} selected{% endif %}>{{ c.value }}</option>
            {% endfor %}
        </select>

        <label class="form-label mb-0">{% translate "State" %}</label>
        <select name="state" class="form-select w-auto">
            <option value="">{% translate "(any)" %}</option>
            {% for s in finding_states %}
                <option value="{{ s.value }}"{% if s.value == selected_filters.state %} selected{% endif %}>{{ s.value }}</option>
            {% endfor %}
        </select>

        <label class="form-label mb-0">{% translate "Action" %}</label>
        <select name="action" class="form-select w-auto">
            <option value="">{% translate "(any)" %}</option>
            {% for a in actions %}
                <option value="{{ a.value }}"{% if a.value == selected_filters.action %} selected{% endif %}>{{ a.value }}</option>
            {% endfor %}
        </select>

        <button type="submit" class="btn btn-primary">{% translate "Apply" %}</button>
    </form>

    <div class="table-responsive">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>{% translate "Member ID" %}</th>
                    <th>{% translate "Username" %}</th>
                    <th>{% translate "Category" %}</th>
                    <th>{% translate "State" %}</th>
                    <th>{% translate "Action" %}</th>
                    <th>{% translate "Kicked" %}</th>
                    <th>{% translate "Skip reason" %}</th>
                    <th>{% translate "Error" %}</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                {% for f in page_obj %}
                    <tr>
                        <td>{{ f.member_id }}</td>
                        <td>{{ f.member_username }}</td>
                        <td>{{ f.category }}</td>
                        <td>{{ f.state }}</td>
                        <td>{{ f.action }}</td>
                        <td>{{ f.kicked|yesno:_("yes,no") }}</td>
                        <td>{{ f.skipped_reason|default:"&mdash;" }}</td>
                        <td>{% if f.error_message %}{{ f.error_message|truncatechars:80 }}{% else %}&mdash;{% endif %}</td>
                        <td>
                            <a href="{% url 'aa_discord_audit:finding_explain' pk=f.pk %}"
                               class="btn btn-outline-secondary btn-sm">
                                {% translate "Explain" %}
                            </a>
                        </td>
                    </tr>
                {% empty %}
                    <tr><td colspan="9" class="text-center text-muted">{% translate "No findings to display." %}</td></tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    {% if page_obj.has_other_pages %}
        <nav aria-label="{% translate 'pagination' %}">
            <ul class="pagination">
                {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% for k, v in selected_filters.items %}{% if v %}&{{ k }}={{ v }}{% endif %}{% endfor %}">&laquo;</a>
                    </li>
                {% endif %}
                <li class="page-item active" aria-current="page">
                    <span class="page-link">{{ page_obj.number }} / {{ page_obj.paginator.num_pages }}</span>
                </li>
                {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.next_page_number }}{% for k, v in selected_filters.items %}{% if v %}&{{ k }}={{ v }}{% endif %}{% endfor %}">&raquo;</a>
                    </li>
                {% endif %}
            </ul>
        </nav>
    {% endif %}
{% endif %}
{% endblock %}
