{% extends "base.html" %}
{% block title %}Dashboard — Mneme HQ{% endblock %}
{% block content %}

{% if batches is not none %}
<div class="dashboard-header">
    <h1>Mneme HQ Benchmark Dashboard</h1>
    <p>Select a batch to view results.</p>
    {% if batches %}
    <ul class="batch-list">
        {% for b in batches %}
        <li><a href="{{ url_for('dashboard.index', batch_id=b) }}">{{ b }}</a></li>
        {% endfor %}
    </ul>
    {% else %}
    <p>No benchmark batches found. Run <code>flask seed-demo</code> to generate demo data.</p>
    {% endif %}
</div>

{% elif report %}
<div class="dashboard-header">
    <h1>{{ report.batch_id }}</h1>
    <p class="meta">Protocol {{ report.protocol_version }} | Model: {{ report.model }} | Temp: {{ report.temperature }}</p>
    <p class="meta">Scored: {{ report.scored_runs }}/{{ report.total_runs }} valid runs ({{ report.excluded_runs }} excluded)</p>
</div>

<!-- Primary Verdict -->
<div class="verdict-card verdict-{{ report.closeness.verdict | lower | replace(' ', '-') }}">
    <h2>Primary: Closeness to User Thinking</h2>
    <div class="verdict-label">{{ report.closeness.verdict }}</div>
    <div class="verdict-stats">
        <span>Win rate: {{ report.closeness.wins }}/{{ report.closeness.total }} = {{ "%.1f" | format(report.closeness.win_rate * 100) }}%</span>
        <span>Avg delta: {{ "%+.2f" | format(report.closeness.avg_delta) }}</span>
        <span>Ties: {{ report.closeness.ties }}/{{ report.closeness.total }}</span>
    </div>
    <div class="bar-container">
        <div class="bar bar-win" style="width: {{ (report.closeness.wins / report.closeness.total * 100) if report.closeness.total > 0 else 0 }}%"></div>
    </div>
</div>

<!-- Secondary Metrics -->
{% for dim_name, dim_key in [("Usefulness", "usefulness"), ("Distinctiveness", "distinctiveness")] %}
{% set v = report[dim_key] %}
<div class="metric-card">
    <h3>Secondary: {{ dim_name }}</h3>
    <span>Win rate: {{ v.wins }}/{{ v.total }} = {{ "%.1f" | format(v.win_rate * 100) }}%</span>
    <span>Avg delta: {{ "%+.2f" | format(v.avg_delta) }}</span>
    <div class="bar-container">
        <div class="bar bar-secondary" style="width: {{ (v.wins / v.total * 100) if v.total > 0 else 0 }}%"></div>
    </div>
</div>
{% endfor %}

<!-- Per-User Breakdown -->
<div class="section">
    <h2>Per-User Breakdown (Closeness)</h2>
    <table>
        <thead>
            <tr><th>User</th><th>Wins</th><th>Losses</th><th>Ties</th><th>Win%</th><th>Avg Delta</th><th>Pattern</th></tr>
        </thead>
        <tbody>
            {% for uid, s in report.per_user.items() %}
            <tr>
                <td><code>{{ uid[:8] }}</code></td>
                <td>{{ s.wins }}</td>
                <td>{{ s.losses }}</td>
                <td>{{ s.ties }}</td>
                <td>{{ "%.0f" | format(s.win_rate * 100) }}%</td>
                <td>{{ "%+.1f" | format(s.avg_delta) }}</td>
                <td><span class="pattern pattern-{{ s.pattern }}">{{ s.pattern }}</span></td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>

<!-- Per-Category Breakdown -->
<div class="section">
    <h2>Per-Category Breakdown (Closeness)</h2>
    <table>
        <thead>
            <tr><th>Category</th><th>Wins</th><th>Total</th><th>Win%</th><th>Avg Delta</th></tr>
        </thead>
        <tbody>
            {% for cat, s in report.per_category.items() %}
            <tr>
                <td>{{ cat }}</td>
                <td>{{ s.wins }}</td>
                <td>{{ s.total }}</td>
                <td>{{ "%.0f" | format(s.win_rate * 100) }}%</td>
                <td>{{ "%+.1f" | format(s.avg_delta) }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>

<!-- Consistency -->
<div class="section">
    <h2>Scoring Consistency</h2>
    <p>Agreement rate: {{ "%.0f" | format(report.consistency.agreement_rate * 100) }}%</p>
    {% if report.consistency.concern %}
    <p class="warning">Above 25% disagreement threshold — review scoring quality.</p>
    {% endif %}
    {% if report.consistency.disagreements %}
    <p>{{ report.consistency.disagreements | length }} disagreement(s) detected.</p>
    {% endif %}
</div>

<div class="section">
    <a href="{{ url_for('dashboard.index') }}" class="btn">Back to Batches</a>
</div>

{% endif %}
{% endblock %}
