{% extends 'layouts/admin.html.twig' %}

{% block title %}{{ source.label }} — Logs — Admin{% endblock %}

{% block admin_content %}
    <section class="page-header log-view-header">
        <div>
            <h1>{{ source.label }}</h1>
            <p class="muted"><code title="{{ source.path }}">{{ source.path_short }}</code></p>
        </div>
        <div class="log-view-actions">
            <a class="btn btn-small" href="/admin/logs">All sources</a>
            <a class="btn btn-small" href="{{ refresh_url }}">Refresh</a>
            {% if live %}
                <a class="btn btn-small" href="{{ live_stop_url }}">Stop live</a>
            {% else %}
                <a class="btn btn-small" href="{{ live_start_url }}">Live · 30s</a>
            {% endif %}
        </div>
    </section>

    {% if rotated %}
        <p class="admin-hint admin-hint--info">Log rotated — showing latest entries.</p>
    {% endif %}
    {% if scan_budget_exhausted %}
        <p class="admin-hint admin-hint--info">Showing matches within the scan window; older matches may exist — use Load older.</p>
    {% endif %}
    {% if matches_exhausted and filters is not empty %}
        <p class="admin-hint admin-hint--info">All matching entries in the scanned range are shown.</p>
    {% endif %}

    <form class="log-filter-form" id="log-viewer-filter-form" method="get" action="/admin/logs/view">
        <input type="hidden" name="source" value="{{ source.id }}">
        {% if live %}<input type="hidden" name="live" value="1">{% endif %}

        <div class="log-filter-grid{% if source.format == 'json_lines' %} log-filter-grid--json{% endif %}">
            {% if source.format == 'json_lines' %}
                <label class="log-filter-field log-filter-field--event">
                    <span class="log-filter-label">Event</span>
                    <select name="event">
                        <option value="">All events</option>
                        {% for event in security_event_types %}
                            <option value="{{ event }}"{% if filters.event|default('') == event %} selected{% endif %}>{{ event }}</option>
                        {% endfor %}
                    </select>
                </label>
                <label class="log-filter-field log-filter-field--ip">
                    <span class="log-filter-label">IP</span>
                    <input type="text" name="ip" value="{{ filters.ip|default('') }}" maxlength="45" placeholder="203.0.113.5">
                </label>
                <label class="log-filter-field log-filter-field--user">
                    <span class="log-filter-label">Username</span>
                    <input type="text" name="username" value="{{ filters.username|default('') }}" maxlength="32">
                </label>
                <label class="log-filter-field log-filter-field--time">
                    <span class="log-filter-label" title="ISO-8601 UTC">Since</span>
                    <input type="text" name="since" value="{{ filters.since|default('') }}" placeholder="2026-07-12T00:00:00Z" spellcheck="false">
                </label>
                <label class="log-filter-field log-filter-field--time">
                    <span class="log-filter-label" title="ISO-8601 UTC">Until</span>
                    <input type="text" name="until" value="{{ filters.until|default('') }}" placeholder="2026-07-12T23:59:59Z" spellcheck="false">
                </label>
            {% else %}
                <label class="log-filter-field log-filter-field--search">
                    <span class="log-filter-label">Search</span>
                    <input type="search" name="q" value="{{ filters.q|default('') }}" maxlength="200" placeholder="Substring filter">
                </label>
            {% endif %}
        </div>
    </form>

    <div class="log-filter-actions">
        <button type="submit" class="btn btn-small btn-primary" form="log-viewer-filter-form">Apply filters</button>
        <a class="btn btn-small" href="/admin/logs/view?source={{ source.id|url_encode }}{% if live %}&amp;live=1{% endif %}">Clear</a>
    </div>

    <div id="log-viewer-output" class="log-viewer-output" data-format="{{ source.format }}">
        {% if source.status == 'missing' %}
            <p class="muted">No log file yet — events will appear after the first write.</p>
        {% elseif lines is empty %}
            <p class="muted">No lines matched the current filters.</p>
        {% elseif source.format == 'json_lines' %}
            <div class="log-code-view">
                <table class="log-code-table">
                    <thead>
                        <tr>
                            <th class="log-col-n" scope="col">#</th>
                            <th class="log-col-ts" scope="col">Time</th>
                            <th class="log-col-event" scope="col">Event</th>
                            <th class="log-col-ip" scope="col">IP</th>
                            <th class="log-col-user" scope="col">User</th>
                        </tr>
                    </thead>
                    <tbody id="log-viewer-lines">
                        {% for index, line in lines %}
                            {% set row = parsed_lines[index] %}
                            <tr class="log-code-row">
                                <td class="log-col-n">{{ index + 1 }}</td>
                                <td class="log-col-ts">{% if row and row.ts %}{{ row.ts|format_datetime }}{% else %}—{% endif %}</td>
                                <td class="log-col-event">{% if row and row.parse_error %}<span class="log-parse-error">parse error</span>{% else %}{{ row.event|default('—') }}{% endif %}</td>
                                <td class="log-col-ip">{{ row.ip|default('—') }}</td>
                                <td class="log-col-user">{{ row.username|default('—') }}</td>
                            </tr>
                            <tr class="log-code-line-row{% if not row or row.meta is null %} log-code-entry-end{% endif %}">
                                <td colspan="5" class="log-code-subrow-cell">
                                    <span class="log-code-subrow-label">Line</span>
                                    <pre class="log-code-line">{{ line }}</pre>
                                </td>
                            </tr>
                            {% if row and row.meta is not null %}
                                <tr class="log-code-details-row log-code-entry-end">
                                    <td colspan="5" class="log-code-subrow-cell">
                                        <span class="log-code-subrow-label">Details</span>
                                        <pre class="log-code-details">{{ row.meta|json_encode(constant('JSON_UNESCAPED_SLASHES')) }}</pre>
                                    </td>
                                </tr>
                            {% endif %}
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        {% else %}
            <div class="log-code-view log-code-view--text" id="log-viewer-lines">
                {% for index, line in lines %}
                    <div class="log-code-text-row">
                        <span class="log-code-gutter" aria-hidden="true">{{ index + 1 }}</span>
                        <pre class="log-code-line">{{ line }}</pre>
                    </div>
                {% endfor %}
            </div>
        {% endif %}
    </div>

    {% if load_older_url %}
        <p class="log-pagination"><a href="{{ load_older_url }}">Load older</a></p>
    {% endif %}

    {% if live %}
        <div id="admin-logs-live-root" data-feed-url="{{ feed_url }}" data-interval="30000" hidden></div>
    {% endif %}
{% endblock %}

{% block scripts %}
    {% if live %}
        <script src="/assets/js/admin-logs-live.js?v={{ asset_version }}" defer nonce="{{ csp_nonce }}"></script>
    {% endif %}
{% endblock %}