{% 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>

        <div class="card mb-4">
            <div class="card-header">
                <h4 class="mb-0">{% translate "Recorded outcome" %}</h4>
            </div>
            <div class="card-body">
                <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-0">
                    <tbody>
                        <tr>
                            <th scope="row">{% translate "Member ID" %}</th>
                            <td>{{ finding.member_id }}</td>
                        </tr>
                        <tr>
                            <th scope="row">{% translate "Username" %}</th>
                            <td>{{ finding.member_username }}</td>
                        </tr>
                        <tr>
                            <th scope="row">{% translate "Category" %}</th>
                            <td><span class="badge {{ category_badge.css }}">{{ category_badge.label }}</span></td>
                        </tr>
                        <tr>
                            <th scope="row">{% translate "State" %}</th>
                            <td><span class="badge {{ state_badge.css }}">{{ state_badge.label }}</span></td>
                        </tr>
                        <tr>
                            <th scope="row">{% translate "Action" %}</th>
                            <td><span class="badge {{ action_badge.css }}">{{ action_badge.label }}</span></td>
                        </tr>
                        <tr>
                            <th scope="row">{% translate "AA user" %}</th>
                            <td>{{ finding.aa_user|default:"&mdash;" }}</td>
                        </tr>
                        <tr>
                            <th scope="row">{% translate "Kicked" %}</th>
                            <td>
                                {% include "aa_discord_audit/_flag_badge.html" with badge=kicked_badge only %}
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">{% translate "Skip reason" %}</th>
                            <td>{{ finding.skipped_reason|default:"&mdash;" }}</td>
                        </tr>
                        <tr>
                            <th scope="row">{% translate "Error" %}</th>
                            <td>
                                {% if error_redacted %}
                                    <pre class="mb-0">{{ error_redacted }}</pre>
                                {% else %}
                                    &mdash;
                                {% endif %}
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">{% 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>
            </div>
        </div>

        <div class="card mb-4">
            <div class="card-header">
                <h4 class="mb-0">{% translate "Current operator-config inputs" %}</h4>
            </div>
            <div class="card-body">
                <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>
                <p class="text-muted small">{% blocktranslate with ts=as_of %}Current operator-config inputs as of {{ ts }}{% endblocktranslate %}</p>
                <table class="table table-bordered w-auto mb-0">
                    <tbody>
                        <tr>
                            <th scope="row">{% 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 scope="row">{% 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 scope="row">{% 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 scope="row">{% 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>
            </div>
        </div>

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