{% extends "base.html" %}
{% block title %}Dashboard - MoralStack{% endblock %}
{% block content %}
<div class="page-header">
    <h1>Dashboard</h1>
    <p class="subtitle">Recent benchmark runs</p>
</div>

<div class="card filters-card">
    <form method="get" action="/runs" class="runs-filters-form">
        <div class="runs-filter-group">
            <label for="domain">Domain</label>
            <select id="domain" name="domain">
                <option value="">All domains</option>
                {% for d in available_domains %}
                <option value="{{ d }}" {% if d == domain %}selected{% endif %}>{{ d }}</option>
                {% endfor %}
            </select>
        </div>
        <div class="runs-filter-group runs-filter-text">
            <label for="search_text">Question/Response contains</label>
            <input id="search_text" name="search_text" type="text" value="{{ search_text }}"
                   placeholder="Search in prompt or final response"/>
        </div>
        <div class="runs-filter-actions">
            <button type="submit" class="btn btn-sm">Search</button>
            <a href="/runs" class="btn btn-sm btn-outline">Reset</a>
        </div>
    </form>
</div>

<div class="card filters-card" style="margin-top:0.5rem;">
    <form method="get" action="/conversations" class="runs-filters-form">
        <div class="runs-filter-group runs-filter-text">
            <label for="conv_q">Open conversation by ID</label>
            <input id="conv_q" name="q" type="text" placeholder="conversation_id"/>
        </div>
        <div class="runs-filter-actions">
            <button type="submit" class="btn btn-sm">Open</button>
        </div>
    </form>
</div>

<div class="runs-summary">
    Showing {{ runs|length }} of {{ total_runs }} runs · page {{ page }} / {{ total_pages }}
</div>

{% if runs %}
<div class="card">
    <table class="table">
        <thead>
        <tr>
            <th>Run ID</th>
            <th>Type</th>
            <th>Status</th>
            <th>Started</th>
            <th>Ended</th>
            <th class="col-actions">Actions</th>
        </tr>
        </thead>
        <tbody>
        {% for r in runs %}
        <tr>
            <td class="mono"><a href="/runs/{{ r.run_id }}">{{ r.run_id[:12] }}</a></td>
            <td>
                {% if r.run_type == 'benchmark' %}
                <span class="badge badge-benchmark">BENCHMARK</span>
                {% elif r.run_type == 'single' %}
                <span class="badge badge-type">SINGLE</span>
                {% else %}
                <span class="badge badge-type">{{ r.run_type or '—' }}</span>
                {% endif %}
            </td>
            <td>
                {% if r.status == 'completed' %}
                <span class="badge badge-ok">{{ r.status }}</span>
                {% elif r.status == 'running' %}
                <span class="badge badge-running">{{ r.status }}</span>
                {% elif r.status == 'failed' %}
                <span class="badge badge-fail">{{ r.status }}</span>
                {% else %}
                <span class="badge">{{ r.status or '—' }}</span>
                {% endif %}
            </td>
            <td class="ts">{{ r.started_at | fmtdate }}</td>
            <td class="ts">{{ r.ended_at | fmtdate }}</td>
            <td class="col-actions">
                <a href="/runs/{{ r.run_id }}" class="btn btn-sm">View</a>
                <a href="/runs/{{ r.run_id }}/export_benchmark.md" download class="btn btn-sm btn-outline">Export</a>
                <button class="btn btn-sm btn-danger btn-delete" data-run-id="{{ r.run_id }}">Delete</button>
            </td>
        </tr>
        {% endfor %}
        </tbody>
    </table>
</div>
<div class="runs-pagination">
    {% if page > 1 %}
    <a class="btn btn-sm btn-outline"
       href="/runs?page={{ page - 1 }}&domain={{ domain|urlencode }}&search_text={{ search_text|urlencode }}">
        Previous
    </a>
    {% endif %}
    <span class="runs-page-label">Page {{ page }} of {{ total_pages }}</span>
    {% if page < total_pages %}
    <a class="btn btn-sm btn-outline"
       href="/runs?page={{ page + 1 }}&domain={{ domain|urlencode }}&search_text={{ search_text|urlencode }}">
        Next
    </a>
    {% endif %}
</div>
{% else %}
<div class="empty-state">
    <p>No runs found.</p>
    <p class="muted">Run a benchmark first, then visit this page.</p>
</div>
{% endif %}
{% endblock %}
