{% extends "base.html" %}

{% block title %}Decision Traces &mdash; AtlasBridge{% endblock %}

{% block content %}
<h1>Decision Traces</h1>

{% if not trace_available %}
<div class="notice">
    <h3>No trace data</h3>
    <p>The decision trace file does not exist yet. Enable autopilot and run a session to generate trace data:</p>
    <pre class="code-hint">atlasbridge autopilot enable
atlasbridge run claude</pre>
</div>
{% else %}

<form class="filter-bar" method="get" action="/traces">
    <select name="action_type" class="filter-select">
        <option value="">All actions</option>
        <option value="auto_respond" {% if filter_action_type == 'auto_respond' %}selected{% endif %}>auto_respond</option>
        <option value="escalate" {% if filter_action_type == 'escalate' %}selected{% endif %}>escalate</option>
        <option value="require_human" {% if filter_action_type == 'require_human' %}selected{% endif %}>require_human</option>
        <option value="block" {% if filter_action_type == 'block' %}selected{% endif %}>block</option>
    </select>
    <select name="confidence" class="filter-select">
        <option value="">All confidences</option>
        <option value="high" {% if filter_confidence == 'high' %}selected{% endif %}>High</option>
        <option value="medium" {% if filter_confidence == 'medium' %}selected{% endif %}>Medium</option>
        <option value="low" {% if filter_confidence == 'low' %}selected{% endif %}>Low</option>
    </select>
    <button type="submit" class="filter-btn">Filter</button>
    {% if filter_action_type or filter_confidence %}
    <a href="/traces" class="filter-clear">Clear</a>
    {% endif %}
</form>

{% if entries %}
<p class="result-count">Showing {{ entries|length }} of {{ total }} entries (page {{ page }}/{{ total_pages }})</p>
<div class="table-responsive">
<table>
    <thead>
        <tr>
            <th>Timestamp</th>
            <th>Action</th>
            <th>Confidence</th>
            <th>Rule ID</th>
            <th>Session</th>
        </tr>
    </thead>
    <tbody>
        {% for e in entries %}
        <tr>
            <td title="{{ e.get('timestamp', '') }}">{{ e.get('timestamp', '&mdash;')|timeago }}</td>
            <td>{{ e.get('action_type', '&mdash;') }}</td>
            <td>{{ e.get('confidence', '&mdash;') }}</td>
            <td>{{ e.get('rule_id', '&mdash;') }}</td>
            <td>
                {% if e.get('session_id') %}
                <a href="/sessions/{{ e.session_id }}">{{ e.session_id[:12] }}&hellip;</a>
                {% else %}
                &mdash;
                {% endif %}
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>
</div>

<div class="pagination">
    {% if page > 1 %}
    <a href="/traces?page={{ page - 1 }}{% if filter_action_type %}&amp;action_type={{ filter_action_type }}{% endif %}{% if filter_confidence %}&amp;confidence={{ filter_confidence }}{% endif %}">&larr; Previous</a>
    {% endif %}
    <span>Page {{ page }} of {{ total_pages }}</span>
    {% if page < total_pages %}
    <a href="/traces?page={{ page + 1 }}{% if filter_action_type %}&amp;action_type={{ filter_action_type }}{% endif %}{% if filter_confidence %}&amp;confidence={{ filter_confidence }}{% endif %}">Next &rarr;</a>
    {% endif %}
</div>
{% else %}
<p class="empty">No trace entries found{% if filter_action_type or filter_confidence %} matching the current filters. <a href="/traces">Clear filters</a>{% endif %}.</p>
{% endif %}

{% endif %}
{% endblock %}
