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

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

{% block extra_javascript %}
    {% include "aa_discord_audit/_datatables_init.html" %}
{% 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>

    <div class="card mb-3">
        <div class="card-header d-flex align-items-center gap-2">
            <strong>{% translate "Audit invocations" %}</strong>
            <span class="badge bg-secondary">{{ total_count }}</span>
        </div>
        <div class="card-body">
            <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 for="inv-accepted" class="form-label mb-0">{% translate "Accepted" %}</label>
                <select id="inv-accepted" 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 for="inv-refusal" class="form-label mb-0">{% translate "Refusal reason" %}</label>
                <select id="inv-refusal" 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.label }}</option>
                    {% endfor %}
                </select>

                <label for="inv-trigger" class="form-label mb-0">{% translate "Triggered by" %}</label>
                <select id="inv-trigger" 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.label }}</option>
                    {% endfor %}
                </select>

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

            <div class="table-responsive">
                <table class="table table-striped"
                       data-audit-table
                       data-order='[[1, "desc"]]'
                       data-paging="{{ client_mode|yesno:'true,false' }}">
                    <thead>
                        <tr>
                            <th scope="col">{% translate "ID" %}</th>
                            <th scope="col">{% translate "Started at" %}</th>
                            <th scope="col">{% translate "Triggered by" %}</th>
                            <th scope="col">{% translate "User" %}</th>
                            <th scope="col">{% translate "Accepted" %}</th>
                            <th scope="col">{% translate "Refusal reason" %}</th>
                            <th scope="col">{% translate "Argv" %}</th>
                            <th scope="col">{% translate "Run" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for row in rows %}
                            <tr>
                                <td>{{ row.obj.pk }}</td>
                                <td data-order="{{ row.obj.started_at|date:'U' }}" title="{{ row.started_abs }}">{{ row.started_rel }}</td>
                                <td>
                                    <span class="badge {{ row.trigger_badge.css }}">{{ row.trigger_badge.label }}</span>
                                </td>
                                <td>{{ row.obj.triggered_by_user|default:"&mdash;" }}</td>
                                <td>
                                    {% if row.obj.accepted %}
                                        <span class="badge bg-success">{% translate "yes" %}</span>
                                    {% else %}
                                        <span class="badge bg-warning text-dark">{% translate "no" %}</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if row.refusal_badge %}
                                        <span class="badge {{ row.refusal_badge.css }}">{{ row.refusal_badge.label }}</span>
                                    {% else %}
                                        &mdash;
                                    {% endif %}
                                </td>
                                <td>
                                    <details>
                                        <summary class="small text-muted">{% translate "show" %}</summary>
                                        <code>{{ row.argv_text }}</code>
                                    </details>
                                </td>
                                <td>
                                    {% if row.obj.audit_run_id %}
                                        <a href="{% url 'aa_discord_audit:run_detail' pk=row.obj.audit_run_id %}">#{{ row.obj.audit_run_id }}</a>
                                    {% elif row.obj.audit_run_pk %}
                                        #{{ row.obj.audit_run_pk }} <small class="text-muted">{% translate "(pruned)" %}</small>
                                    {% else %}
                                        &mdash;
                                    {% endif %}
                                    {% if row.run_state_badge %}
                                        <span class="badge {{ row.run_state_badge.css }}">{{ row.run_state_badge.label }}</span>
                                    {% endif %}
                                </td>
                            </tr>
                        {% empty %}
                            <tr>
                                <td colspan="8" class="text-center text-muted py-4">{% translate "No invocations to display." %}</td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            {% if not client_mode and page_obj and page_obj.number == 1 %}
                <p class="text-muted small">
                    {% translate "Showing the first page — narrow your filters to search across all results." %}
                </p>
                {% 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 %}
            {% endif %}
        </div>
    </div>
{% endblock %}
