{% extends 'base.html' %}

{% block css %}
    {{ block.super }}
    <style>
        .log-row {
            border-bottom: 1px solid var(--bs-border-color);
        }
        .log-message {
            font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
            font-size: 0.9rem;
            white-space: pre-wrap;
            word-break: break-word;
        }
        .log-trace {
            margin: 0;
            font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
            font-size: 0.82rem;
            line-height: 1.35;
            white-space: pre-wrap;
            word-break: break-word;
            max-height: 24rem;
            overflow-y: auto;
            background: rgba(0, 0, 0, 0.15);
            border: 1px solid var(--bs-border-color);
            border-radius: 0.375rem;
            padding: 0.75rem;
        }
        .logs-filter-group {
            border: 1px solid var(--bs-border-color);
            border-radius: 0.5rem;
            padding: 0.75rem;
            background: rgba(255, 255, 255, 0.02);
        }
        .logs-filter-group legend {
            width: auto;
            padding: 0 0.35rem;
            margin: 0;
            font-size: 0.8rem;
            color: var(--bs-secondary-color);
            text-transform: uppercase;
            letter-spacing: 0.04em;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="container-fluid py-4">
        <div class="d-flex justify-content-between align-items-start gap-3 mb-4">
            <div>
                <h2 class="mb-1">Production Logs</h2>
                <p class="text-muted small mb-0">
                    Real-time log monitoring from <code>{{ log_file_path }}</code>
                </p>
            </div>
            <div class="text-end d-flex flex-column align-items-end gap-2">
                <span class="badge bg-secondary rounded-pill px-3 py-2">
                    {% if show_all %}
                        Viewing all {{ total_entries }} entries
                    {% else %}
                        Showing {{ entries|length }}/{{ total_entries }} entries
                    {% endif %}
                </span>
                <a href="{{ raw_url }}" class="btn btn-sm btn-primary" target="_blank" rel="noopener">
                    <i class="bi bi-file-earmark-text me-1"></i> Raw file
                </a>
            </div>
        </div>

        {% if action_message %}
            <div class="alert alert-info">{{ action_message }}</div>
        {% endif %}

        <div class="card border-0 shadow-sm mb-4">
            <div class="card-body">
                <form method="get" class="row g-3">
                    <div class="col-12 col-lg-5">
                        <label for="id_logs_query" class="form-label small text-muted mb-1">Keyword</label>
                        <input type="search"
                               id="id_logs_query"
                               name="q"
                               class="form-control"
                               placeholder="Search logs by keyword (error, warning, etc)…"
                               value="{{ search_query }}">
                    </div>
                    <div class="col-12 col-lg-3">
                        <label for="id_logs_level" class="form-label small text-muted mb-1">Level</label>
                        <select id="id_logs_level" name="level" class="form-select">
                            <option value="">All levels</option>
                            {% for level in available_levels %}
                                <option value="{{ level }}" {% if level_filter == level %}selected{% endif %}>{{ level }}</option>
                            {% endfor %}
                        </select>
                    </div>

                    <div class="col-12 col-lg-4">
                        <fieldset class="logs-filter-group">
                            <legend>Date Range</legend>
                            <div class="row g-2">
                                <div class="col-12 col-sm-6">
                                    <label for="id_logs_date_from" class="form-label small mb-1">From date</label>
                                    <input id="id_logs_date_from" type="date" name="date_from" class="form-control" value="{{ date_from }}" aria-label="Date from">
                                </div>
                                <div class="col-12 col-sm-6">
                                    <label for="id_logs_date_to" class="form-label small mb-1">To date</label>
                                    <input id="id_logs_date_to" type="date" name="date_to" class="form-control" value="{{ date_to }}" aria-label="Date to">
                                </div>
                            </div>
                        </fieldset>
                    </div>

                    <div class="col-12 col-lg-4">
                        <fieldset class="logs-filter-group">
                            <legend>Time Range</legend>
                            <div class="row g-2">
                                <div class="col-12 col-sm-6">
                                    <label for="id_logs_time_from" class="form-label small mb-1">From time</label>
                                    <input id="id_logs_time_from" type="time" name="time_from" class="form-control" value="{{ time_from }}" aria-label="Time from">
                                </div>
                                <div class="col-12 col-sm-6">
                                    <label for="id_logs_time_to" class="form-label small mb-1">To time</label>
                                    <input id="id_logs_time_to" type="time" name="time_to" class="form-control" value="{{ time_to }}" aria-label="Time to">
                                </div>
                            </div>
                        </fieldset>
                    </div>

                    <div class="col-12 col-lg-4 d-flex align-items-center">
                        <div class="form-check mt-1">
                            <input class="form-check-input" type="checkbox" value="1" id="include_noise" name="include_noise" {% if include_noise %}checked{% endif %}>
                            <label class="form-check-label small" for="include_noise">
                                Include template debug noise
                            </label>
                        </div>
                    </div>

                    <div class="col-12 col-md-auto d-flex flex-wrap gap-2">
                        <button type="submit" class="btn btn-primary">Search</button>
                        {% if search_query or level_filter %}
                            <a href="{% url 'logs_view' %}" class="btn btn-outline-secondary">Clear</a>
                        {% endif %}
                    </div>
                    <div class="col-12 col-md-auto">
                        {% if show_all %}
                            <a href="{% url 'logs_view' %}?q={{ search_query|urlencode }}&level={{ level_filter|urlencode }}{% if date_from %}&date_from={{ date_from|urlencode }}{% endif %}{% if date_to %}&date_to={{ date_to|urlencode }}{% endif %}{% if time_from %}&time_from={{ time_from|urlencode }}{% endif %}{% if time_to %}&time_to={{ time_to|urlencode }}{% endif %}{% if include_noise %}&include_noise=1{% endif %}" class="btn btn-outline-secondary">Show recent only</a>
                        {% else %}
                            <a href="{% url 'logs_view' %}?q={{ search_query|urlencode }}&level={{ level_filter|urlencode }}&all=1{% if date_from %}&date_from={{ date_from|urlencode }}{% endif %}{% if date_to %}&date_to={{ date_to|urlencode }}{% endif %}{% if time_from %}&time_from={{ time_from|urlencode }}{% endif %}{% if time_to %}&time_to={{ time_to|urlencode }}{% endif %}{% if include_noise %}&include_noise=1{% endif %}" class="btn btn-outline-info">View all logs</a>
                        {% endif %}
                    </div>
                    <div class="col-12 col-md-auto">
                        <button type="submit" form="clear-log-form" class="btn btn-outline-danger" onclick="return confirm('Clear the log file?');">Reset log file</button>
                    </div>
                    <div class="col-12 col-md-auto">
                        <a href="{{ raw_url }}" class="btn btn-outline-dark" target="_blank" rel="noopener">View raw file</a>
                    </div>
                </form>
                <form id="clear-log-form" method="post" class="d-none">
                    {% csrf_token %}
                    <input type="hidden" name="action" value="clear">
                </form>
            </div>
        </div>

        {% if entries %}
            <div class="card border-0 shadow-sm">
                <div class="card-body p-0" style="max-height: 78vh; overflow-y: auto;">
                    {% for entry in entries %}
                        <div class="p-3 log-row">
                            <div class="d-flex flex-wrap align-items-center gap-2 mb-2">
                                {% if entry.level == 'ERROR' or entry.level == 'CRITICAL' %}
                                    <span class="badge bg-danger">{{ entry.level }}</span>
                                {% elif entry.level == 'WARNING' or entry.level == 'WARN' %}
                                    <span class="badge bg-warning text-dark">{{ entry.level }}</span>
                                {% elif entry.level == 'DEBUG' %}
                                    <span class="badge bg-secondary">{{ entry.level }}</span>
                                {% elif entry.level == 'INFO' %}
                                    <span class="badge bg-info text-dark">{{ entry.level }}</span>
                                {% else %}
                                    <span class="badge bg-dark">{{ entry.level|default:'LOG' }}</span>
                                {% endif %}
                                {% if entry.timestamp %}
                                    <span class="text-muted small">{{ entry.timestamp }}</span>
                                {% endif %}
                                {% if entry.logger %}
                                    <span class="text-muted small">{{ entry.logger }}{% if entry.source_line %}:{{ entry.source_line }}{% endif %}</span>
                                {% endif %}
                            </div>

                            <div class="log-message">{{ entry.message }}</div>

                            {% if entry.details %}
                                <details class="mt-2">
                                    <summary class="small text-muted">Traceback / Details ({{ entry.details|length }} lines)</summary>
                                    <pre class="log-trace mt-2">{% for detail_line in entry.details %}
{{ detail_line }}
{% endfor %}</pre>
                                </details>
                            {% endif %}
                        </div>
                    {% endfor %}
                </div>
            </div>

            <div class="alert alert-secondary mt-3 small">
                <i class="bi bi-info-circle"></i>
                {% if show_all %}
                    Displaying all {{ total_entries }} log entries (newest first).
                {% else %}
                    Displaying most recent 500 entries (newest first).
                    {% if total_entries > 500 %}
                        <a href="{% url 'logs_view' %}?q={{ search_query|urlencode }}&level={{ level_filter|urlencode }}&all=1{% if include_noise %}&include_noise=1{% endif %}" class="alert-link">View all {{ total_entries }} entries</a>.
                    {% endif %}
                {% endif %}
                {% if search_query %}
                    Search found {{ entries|length }} matching entr{{ entries|length|pluralize:"y,ies" }}.
                {% endif %}
                {% if noise_suppressed and not include_noise %}
                    Hidden {{ noise_suppressed }} noisy template debug entr{{ noise_suppressed|pluralize:"y,ies" }}.
                    <a href="{% url 'logs_view' %}?q={{ search_query|urlencode }}&level={{ level_filter|urlencode }}{% if show_all %}&all=1{% endif %}{% if date_from %}&date_from={{ date_from|urlencode }}{% endif %}{% if date_to %}&date_to={{ date_to|urlencode }}{% endif %}{% if time_from %}&time_from={{ time_from|urlencode }}{% endif %}{% if time_to %}&time_to={{ time_to|urlencode }}{% endif %}&include_noise=1" class="alert-link">Show them</a>.
                {% endif %}
            </div>
        {% else %}
            <div class="alert alert-info">
                No log entries found.
                {% if search_query %}
                    Try a different search term.
                {% else %}
                    The log file may be empty or not yet created.
                {% endif %}
            </div>
        {% endif %}
    </div>
{% endblock %}
