{% extends 'admin/base.html' %}
{% block title %}{{ t('audit_log.title') }}{% endblock %}
{% block content %}

<div class="page-header page-header--with-action">
    <div class="page-header__content">
        <h2>🛡️ {{ t('audit_log.title') }}</h2>
    </div>
    {# height-keeper: the sticky bar is button-high on every page
       (posts-list standard); this page has no header action #}
    <span class="btn header-spacer" aria-hidden="true">&nbsp;</span>
</div>
<div class="page-help">{{ t('audit_log.subtitle') }}</div>

<!-- Filter -->
<div class="filter-bar">
    <form method="GET">
        <select name="action" onchange="this.form.submit()">
            <option value="">{{ t('audit_log.all_actions') }}</option>
            {% for action in action_list %}
            <option value="{{ action }}" {% if action_filter == action %}selected{% endif %}>{{ t('audit_log.action_' + action) }}</option>
            {% endfor %}
        </select>
        <select name="actor" onchange="this.form.submit()">
            <option value="">{{ t('audit_log.actor_all') }}</option>
            <option value="human" {% if actor_filter == 'human' %}selected{% endif %}>{{ t('audit_log.actor_human') }}</option>
            <option value="agent" {% if actor_filter == 'agent' %}selected{% endif %}>{{ t('audit_log.actor_agent') }}</option>
        </select>
    </form>
    {% if action_filter or actor_filter %}
    <a href="{{ url_for('admin_audit_log') }}">{{ t('audit_log.clear_filter') }}</a>
    {% endif %}
    <div class="flex-1"></div>
    <span class="text-sm text-muted">{{ logs.total }} {{ t('audit_log.entries') }}</span>
</div>

{% if logs.items %}
<table class="log-table">
    <thead>
        <tr>
            <th>{{ t('audit_log.time') }}</th>
            <th>{{ t('audit_log.user') }}</th>
            <th>{{ t('audit_log.action') }}</th>
            <th>{{ t('audit_log.details') }}</th>
        </tr>
    </thead>
    <tbody>
        {% for log in logs.items %}
        <tr>
            <td class="time-cell">{{ log.created_at|format_datetime }}</td>
            <td>{{ log.username or '—' }}{% if log.via_agent %} <span class="badge badge-actor-agent" title="{{ t('audit_log.via_agent_hint') }}">🤖 {{ t('audit_log.via_agent') }}</span>{% endif %}</td>
            <td><span class="badge badge-{{ log.action }}">{{ t('audit_log.action_' + log.action) }}</span></td>
            <td>{{ (log.details|translate_detail) or '—' }}{% if log.ip_address and log.action in SECURITY_ACTIONS %} <span class="ip-inline" title="{{ t('audit_log.ip') }}">{{ log.ip_address }}</span>{% endif %}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>

<div class="pagination-footer">
{% if logs.pages > 1 %}
<div class="pagination">
    {% if logs.has_prev %}
    <a href="{{ url_for('admin_audit_log', page=logs.prev_num, action=action_filter, actor=actor_filter, per_page=per_page) }}">{{ t('pagination.prev') }}</a>
    {% endif %}
    {% for p in logs.iter_pages(left_edge=1, right_edge=1, left_current=2, right_current=2) %}
        {% if p %}
            {% if p == logs.page %}
                <span class="current">{{ p }}</span>
            {% else %}
                <a href="{{ url_for('admin_audit_log', page=p, action=action_filter, actor=actor_filter, per_page=per_page) }}">{{ p }}</a>
            {% endif %}
        {% else %}
            <span>…</span>
        {% endif %}
    {% endfor %}
    {% if logs.has_next %}
    <a href="{{ url_for('admin_audit_log', page=logs.next_num, action=action_filter, actor=actor_filter, per_page=per_page) }}">{{ t('pagination.next') }}</a>
    {% endif %}
</div>
{% endif %}
    <div class="per-page-selector">
        <span>{{ t('pagination.per_page') }}</span>
        <select onchange="var u=new URLSearchParams(location.search);u.set('per_page',this.value);u.delete('page');location.search=u">
            {% for pp in [20, 50, 100] %}
            <option value="{{ pp }}" {% if per_page == pp %}selected{% endif %}>{{ pp }}</option>
            {% endfor %}
        </select>
        <span>{{ t('pagination.per_page_suffix') }}</span>
    </div>
</div>

{% else %}
<div class="empty-state">
    <p class="empty-state-icon">📋</p>
    <h3>{{ t('audit_log.no_entries') }}</h3>
    <p>{{ t('audit_log.no_entries_text') }}</p>
</div>
{% endif %}

{% endblock %}
