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

{% block page_title %}{% translate "Discord Audit Finding" %} #{{ finding.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">
            <a href="{% url 'aa_discord_audit:run_detail' pk=run.pk %}">{% blocktranslate with run_id=run.pk %}Run #{{ run_id }}{% endblocktranslate %}</a>
        </li>
        <li class="breadcrumb-item active" aria-current="page">{% blocktranslate with finding_id=finding.pk %}Finding #{{ finding_id }}{% endblocktranslate %}</li>
    </ol>
</nav>

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

    <h4>{% translate "Recorded outcome" %}</h4>
    <p class="text-muted small">
        {% translate "Frozen at the time of the audit run; classify() ran against the live Discord member and the operator-config tables in effect when the run started." %}
    </p>
    <table class="table table-bordered w-auto mb-4">
        <tbody>
            <tr><th>{% translate "Member ID" %}</th><td>{{ finding.member_id }}</td></tr>
            <tr><th>{% translate "Username" %}</th><td>{{ finding.member_username }}</td></tr>
            <tr><th>{% translate "Category" %}</th><td>{{ finding.category }}</td></tr>
            <tr><th>{% translate "State" %}</th><td>{{ finding.state }}</td></tr>
            <tr><th>{% translate "Action" %}</th><td>{{ finding.action }}</td></tr>
            <tr><th>{% translate "AA user" %}</th><td>{{ finding.aa_user|default:"&mdash;" }}</td></tr>
            <tr><th>{% translate "Kicked" %}</th><td>{{ finding.kicked|yesno:_("yes,no") }}</td></tr>
            <tr><th>{% translate "Skip reason" %}</th><td>{{ finding.skipped_reason|default:"&mdash;" }}</td></tr>
            {% if finding.error_message %}
                <tr><th>{% translate "Error" %}</th><td><pre class="mb-0">{{ finding.error_message }}</pre></td></tr>
            {% endif %}
            <tr>
                <th>{% translate "Roles removed" %}</th>
                <td>
                    {% if finding.role_names_removed %}
                        <ul class="mb-0">
                            {% for name in finding.role_names_removed %}
                                <li><code>{{ name }}</code></li>
                            {% endfor %}
                        </ul>
                        <small class="text-muted">{% translate "Role IDs:" %} <code>{{ finding.role_ids_removed|join:", " }}</code></small>
                    {% else %}
                        &mdash;
                    {% endif %}
                </td>
            </tr>
        </tbody>
    </table>

    <h4>{% translate "Current operator-config inputs" %}</h4>
    <p class="text-muted small">
        {% translate "Live state of the tables classify() consults. Diverges from the recorded category when the operator changed config between the run and now — useful when investigating 'why was this flagged?' after a follow-up edit." %}
    </p>
    <table class="table table-bordered w-auto">
        <tbody>
            <tr>
                <th>{% translate "In BotAccountUid" %}</th>
                <td>
                    {% if in_bot_uids %}
                        <span class="badge bg-warning text-dark">{% translate "yes" %}</span>
                    {% else %}
                        <span class="text-muted">{% translate "no" %}</span>
                    {% endif %}
                </td>
            </tr>
            <tr>
                <th>{% translate "ProtectedDiscordMember" %}</th>
                <td>
                    {% if is_protected %}
                        <span class="badge bg-success">{% translate "protected" %}</span>
                    {% else %}
                        <span class="text-muted">{% translate "not protected" %}</span>
                    {% endif %}
                </td>
            </tr>
            <tr>
                <th>{% translate "Has DiscordUser link" %}</th>
                <td>
                    {% if has_discord_user %}
                        <span class="badge bg-info text-dark">{% translate "yes" %}</span>
                        {% if discord_user_user_id %}
                            <small class="text-muted">{% translate "AA user pk:" %} {{ discord_user_user_id }}</small>
                        {% endif %}
                    {% else %}
                        <span class="text-muted">{% translate "no" %}</span>
                    {% endif %}
                </td>
            </tr>
            <tr>
                <th>{% translate "Has DiscordIdentity" %}</th>
                <td>
                    {% if has_identity %}
                        <span class="badge bg-info text-dark">{% translate "yes" %}</span>
                        {% if identity.last_seen_in_guild_at %}
                            <small class="text-muted">{% translate "last seen:" %} {{ identity.last_seen_in_guild_at|date:"Y-m-d H:i:s" }}</small>
                        {% endif %}
                    {% else %}
                        <span class="text-muted">{% translate "no" %}</span>
                    {% endif %}
                </td>
            </tr>
        </tbody>
    </table>

    <details class="mt-3">
        <summary class="text-muted">{% translate "Recorded run policy snapshot" %}</summary>
        <pre class="mt-2 p-2 bg-light"><code>{{ run.policy_snapshot|stringformat:"s" }}</code></pre>
    </details>
</div>
{% endblock %}
