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

{% block page_title %}{% translate "Discord Audit Invocations" %}{% endblock %}
{% block header_nav_brand %}{% translate "Discord Audit" %}{% endblock %}

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

<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">{% translate "Invocations" %}</li>
    </ol>
</nav>

<h2>{% translate "Audit invocations" %}</h2>
<p class="text-muted small">
    {% translate "Every CLI / beat / manual trigger of the audit, including refused attempts. Refused rows do NOT count toward the per-user rate limit." %}
</p>

<form method="get" class="mb-3 d-flex align-items-center gap-2 flex-wrap">
    <label class="form-label mb-0">{% translate "Accepted" %}</label>
    <select name="accepted" class="form-select w-auto">
        <option value="">{% translate "(any)" %}</option>
        <option value="true"{% if selected_accepted == "true" %} selected{% endif %}>{% translate "yes" %}</option>
        <option value="false"{% if selected_accepted == "false" %} selected{% endif %}>{% translate "no" %}</option>
    </select>

    <label class="form-label mb-0">{% translate "Refusal reason" %}</label>
    <select name="refusal_reason" class="form-select w-auto">
        <option value="">{% translate "(any)" %}</option>
        {% for r in refusal_reasons %}
            <option value="{{ r.value }}"{% if r.value == selected_refusal %} selected{% endif %}>{{ r.value }}</option>
        {% endfor %}
    </select>

    <label class="form-label mb-0">{% translate "Triggered by" %}</label>
    <select name="triggered_by" class="form-select w-auto">
        <option value="">{% translate "(any)" %}</option>
        {% for t in triggers %}
            <option value="{{ t.value }}"{% if t.value == selected_triggered_by %} selected{% endif %}>{{ t.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 "ID" %}</th>
                <th>{% translate "Started at" %}</th>
                <th>{% translate "Triggered by" %}</th>
                <th>{% translate "User" %}</th>
                <th>{% translate "Accepted" %}</th>
                <th>{% translate "Refusal reason" %}</th>
                <th>{% translate "Argv" %}</th>
                <th>{% translate "Run" %}</th>
            </tr>
        </thead>
        <tbody>
            {% for inv in page_obj %}
                <tr>
                    <td>{{ inv.pk }}</td>
                    <td>{{ inv.started_at|date:"Y-m-d H:i:s" }}</td>
                    <td>{{ inv.triggered_by }}</td>
                    <td>{{ inv.triggered_by_user|default:"&mdash;" }}</td>
                    <td>
                        {% if inv.accepted %}
                            <span class="badge bg-success">{% translate "yes" %}</span>
                        {% else %}
                            <span class="badge bg-warning text-dark">{% translate "no" %}</span>
                        {% endif %}
                    </td>
                    <td>{{ inv.refusal_reason|default:"&mdash;" }}</td>
                    <td><code>{{ inv.cli_argv|join:" "|truncatechars:60 }}</code></td>
                    <td>
                        {% if inv.audit_run_id %}
                            <a href="{% url 'aa_discord_audit:run_detail' pk=inv.audit_run_id %}">#{{ inv.audit_run_id }}</a>
                        {% elif inv.audit_run_pk %}
                            #{{ inv.audit_run_pk }} <small class="text-muted">{% translate "(pruned)" %}</small>
                        {% else %}
                            &mdash;
                        {% endif %}
                    </td>
                </tr>
            {% empty %}
                <tr><td colspan="8" class="text-center text-muted">{% translate "No invocations 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 }}{% if selected_accepted %}&accepted={{ selected_accepted }}{% endif %}{% if selected_refusal %}&refusal_reason={{ selected_refusal }}{% endif %}{% if selected_triggered_by %}&triggered_by={{ selected_triggered_by }}{% endif %}">&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 }}{% if selected_accepted %}&accepted={{ selected_accepted }}{% endif %}{% if selected_refusal %}&refusal_reason={{ selected_refusal }}{% endif %}{% if selected_triggered_by %}&triggered_by={{ selected_triggered_by }}{% endif %}">&raquo;</a>
                </li>
            {% endif %}
        </ul>
    </nav>
{% endif %}
{% endblock %}
