<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Auto Improver Dashboard</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: #0f1117;
            color: #e1e4e8;
            min-height: 100vh;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        header {
            background: linear-gradient(135deg, #1a1d2e 0%, #2a2d3e 100%);
            padding: 24px 32px;
            border-bottom: 1px solid #2d3148;
            margin-bottom: 24px;
        }

        header h1 {
            font-size: 24px;
            font-weight: 600;
            color: #fff;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        header h1::before {
            content: "🔍";
        }

        header p {
            color: #8b8fa3;
            margin-top: 4px;
            font-size: 14px;
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 16px;
            margin-bottom: 24px;
        }

        .stat-card {
            background: #1a1d2e;
            border: 1px solid #2d3148;
            border-radius: 12px;
            padding: 20px;
            transition: transform 0.2s, border-color 0.2s;
        }

        .stat-card:hover {
            transform: translateY(-2px);
            border-color: #4a9eff;
        }

        .stat-card .label {
            font-size: 12px;
            color: #8b8fa3;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 8px;
        }

        .stat-card .value {
            font-size: 28px;
            font-weight: 700;
            color: #fff;
        }

        .stat-card .value.good { color: #3fb950; }
        .stat-card .value.warning { color: #d29922; }
        .stat-card .value.danger { color: #f85149; }

        .section {
            background: #1a1d2e;
            border: 1px solid #2d3148;
            border-radius: 12px;
            margin-bottom: 24px;
            overflow: hidden;
        }

        .section-header {
            padding: 16px 20px;
            border-bottom: 1px solid #2d3148;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .section-header h2 {
            font-size: 16px;
            font-weight: 600;
            color: #fff;
        }

        .section-body {
            padding: 0;
        }

        .analysis-item {
            padding: 16px 20px;
            border-bottom: 1px solid #2d3148;
            transition: background 0.2s;
            cursor: pointer;
        }

        .analysis-item:last-child {
            border-bottom: none;
        }

        .analysis-item:hover {
            background: #22253a;
        }

        .analysis-item .top-row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 8px;
        }

        .analysis-item .session-info {
            font-size: 14px;
            color: #c9d1d9;
        }

        .analysis-item .session-info strong {
            color: #4a9eff;
        }

        .analysis-item .timestamp {
            font-size: 12px;
            color: #8b8fa3;
        }

        .analysis-item .summary {
            font-size: 13px;
            color: #8b8fa3;
            margin-bottom: 10px;
            line-height: 1.5;
        }

        .score-badge {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            min-width: 48px;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 13px;
            font-weight: 600;
        }

        .score-badge.good {
            background: rgba(63, 185, 80, 0.15);
            color: #3fb950;
        }

        .score-badge.warning {
            background: rgba(210, 153, 34, 0.15);
            color: #d29922;
        }

        .score-badge.danger {
            background: rgba(248, 81, 73, 0.15);
            color: #f85149;
        }

        .issue-tags {
            display: flex;
            gap: 6px;
            flex-wrap: wrap;
        }

        .issue-tag {
            font-size: 11px;
            padding: 2px 8px;
            border-radius: 4px;
            font-weight: 500;
        }

        .issue-tag.low {
            background: rgba(139, 143, 163, 0.15);
            color: #8b8fa3;
        }

        .issue-tag.medium {
            background: rgba(210, 153, 34, 0.15);
            color: #d29922;
        }

        .issue-tag.high {
            background: rgba(248, 81, 73, 0.15);
            color: #f85149;
        }

        .issue-tag.critical {
            background: rgba(248, 81, 73, 0.25);
            color: #ff6b6b;
        }

        .empty-state {
            padding: 48px 20px;
            text-align: center;
            color: #8b8fa3;
        }

        .empty-state .icon {
            font-size: 48px;
            margin-bottom: 12px;
        }

        .empty-state p {
            font-size: 14px;
        }

        .detail-panel {
            display: none;
            position: fixed;
            top: 0;
            right: 0;
            width: 500px;
            max-width: 100vw;
            height: 100vh;
            background: #1a1d2e;
            border-left: 1px solid #2d3148;
            overflow-y: auto;
            z-index: 100;
            box-shadow: -4px 0 20px rgba(0,0,0,0.3);
        }

        .detail-panel.open {
            display: block;
        }

        .detail-panel .panel-header {
            padding: 20px;
            border-bottom: 1px solid #2d3148;
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: sticky;
            top: 0;
            background: #1a1d2e;
            z-index: 1;
        }

        .detail-panel .panel-header h3 {
            font-size: 16px;
            color: #fff;
        }

        .detail-panel .close-btn {
            background: none;
            border: none;
            color: #8b8fa3;
            font-size: 20px;
            cursor: pointer;
            padding: 4px 8px;
            border-radius: 4px;
        }

        .detail-panel .close-btn:hover {
            background: #2d3148;
            color: #fff;
        }

        .detail-panel .panel-body {
            padding: 20px;
        }

        .detail-panel .score-display {
            text-align: center;
            padding: 20px;
            margin-bottom: 20px;
        }

        .detail-panel .score-circle {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            font-size: 32px;
            font-weight: 700;
            margin-bottom: 8px;
        }

        .detail-panel .score-circle.good {
            background: rgba(63, 185, 80, 0.15);
            color: #3fb950;
            border: 3px solid #3fb950;
        }

        .detail-panel .score-circle.warning {
            background: rgba(210, 153, 34, 0.15);
            color: #d29922;
            border: 3px solid #d29922;
        }

        .detail-panel .score-circle.danger {
            background: rgba(248, 81, 73, 0.15);
            color: #f85149;
            border: 3px solid #f85149;
        }

        .issue-detail {
            background: #22253a;
            border-radius: 8px;
            padding: 16px;
            margin-bottom: 12px;
            border-left: 3px solid;
        }

        .issue-detail.high {
            border-left-color: #f85149;
        }

        .issue-detail.medium {
            border-left-color: #d29922;
        }

        .issue-detail.low {
            border-left-color: #8b8fa3;
        }

        .issue-detail .issue-type {
            font-size: 12px;
            color: #8b8fa3;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 4px;
        }

        .issue-detail .issue-message {
            font-size: 14px;
            color: #c9d1d9;
            margin-bottom: 8px;
            line-height: 1.5;
        }

        .issue-detail .issue-suggestion {
            font-size: 13px;
            color: #4a9eff;
            line-height: 1.5;
        }

        .suggestion-section {
            margin-top: 20px;
        }

        .suggestion-section h4 {
            font-size: 14px;
            color: #fff;
            margin-bottom: 12px;
        }

        .suggestion-item {
            background: #22253a;
            border-radius: 8px;
            padding: 12px 16px;
            margin-bottom: 8px;
        }

        .suggestion-item .cat-name {
            font-size: 12px;
            color: #4a9eff;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 4px;
        }

        .suggestion-item .cat-suggestion {
            font-size: 13px;
            color: #c9d1d9;
            line-height: 1.5;
        }

        .overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100vw;
            height: 100vh;
            background: rgba(0,0,0,0.5);
            z-index: 99;
        }

        .overlay.open {
            display: block;
        }

        .config-info {
            background: #22253a;
            border-radius: 8px;
            padding: 16px;
            margin-bottom: 20px;
        }

        .config-info h4 {
            font-size: 14px;
            color: #fff;
            margin-bottom: 8px;
        }

        .config-info p {
            font-size: 13px;
            color: #8b8fa3;
            line-height: 1.6;
        }

        .config-info code {
            background: #1a1d2e;
            padding: 2px 6px;
            border-radius: 4px;
            font-size: 12px;
            color: #d29922;
        }

        @media (max-width: 768px) {
            .detail-panel {
                width: 100vw;
            }
            .stats-grid {
                grid-template-columns: repeat(2, 1fr);
            }
        }
    </style>
</head>
<body>
    <header>
        <div class="container">
            <h1>Auto Improver Dashboard</h1>
            <p>Analyze agent conversations, detect issues, and get improvement suggestions</p>
        </div>
    </header>

    <div class="container">
        {% if analyses %}
        <!-- Stats Overview -->
        <div class="stats-grid">
            <div class="stat-card">
                <div class="label">Total Analyses</div>
                <div class="value">{{ analyses|length }}</div>
            </div>
            <div class="stat-card">
                <div class="label">Average Score</div>
                <div class="value {% if (analyses|map(attribute='score')|list|sum / analyses|length) >= 80 %}good{% elif (analyses|map(attribute='score')|list|sum / analyses|length) >= 50 %}warning{% else %}danger{% endif %}">
                    {{ "%.0f"|format(analyses|map(attribute='score')|list|sum / analyses|length) }}
                </div>
            </div>
            <div class="stat-card">
                <div class="label">Total Issues Found</div>
                <div class="value {% if analyses|map(attribute='issues')|list|sum(length=0) > 10 %}danger{% elif analyses|map(attribute='issues')|list|sum(length=0) > 5 %}warning{% else %}good{% endif %}">
                    {{ analyses|map(attribute='issues')|list|sum(length=0) }}
                </div>
            </div>
            <div class="stat-card">
                <div class="label">Sessions Analyzed</div>
                <div class="value">{{ analyses|map(attribute='session_id')|list|unique|length }}</div>
            </div>
        </div>

        <!-- Recent Analyses -->
        <div class="section">
            <div class="section-header">
                <h2>Recent Analyses</h2>
            </div>
            <div class="section-body">
                {% for analysis in analyses %}
                <div class="analysis-item" onclick="showDetail({{ loop.index0 }})">
                    <div class="top-row">
                        <div class="session-info">
                            Session: <strong>{{ analysis.session_id[:12] }}{% if analysis.session_id|length > 12 %}...{% endif %}</strong> | Agent: <strong>{{ analysis.agent_id[:12] }}{% if analysis.agent_id|length > 12 %}...{% endif %}</strong>
                        </div>
                        <span class="score-badge {% if analysis.score >= 80 %}good{% elif analysis.score >= 50 %}warning{% else %}danger{% endif %}">
                            {{ analysis.score }}/100
                        </span>
                    </div>
                    <div class="summary">{{ analysis.summary }}</div>
                    <div class="issue-tags">
                        {% for issue in analysis.issues[:5] %}
                        <span class="issue-tag {{ issue.severity }}">{{ issue.severity }}</span>
                        {% endfor %}
                        {% if analysis.issues|length > 5 %}
                        <span class="issue-tag">+{{ analysis.issues|length - 5 }} more</span>
                        {% endif %}
                    </div>
                </div>
                {% endfor %}
            </div>
        </div>

        <!-- Config Info -->
        <div class="section">
            <div class="section-header">
                <h2>How It Works</h2>
            </div>
            <div class="section-body">
                <div class="config-info">
                    <h4>Auto Analysis</h4>
                    <p>Enable <code>AUTO_ANALYSIS_ENABLED</code> in plugin settings to automatically analyze conversations after each turn or final answer. The plugin checks for:</p>
                    <p>
                        - Long responses (>500 words)<br>
                        - Short responses (<20 words)<br>
                        - Repetitive content<br>
                        - Inefficient tool usage<br>
                        - Missing key information<br>
                        - Tone and structure issues
                    </p>
                </div>
                <div class="config-info">
                    <h4>Manual Analysis</h4>
                    <p>Use the API to trigger analysis on any session:</p>
                    <p><code>POST /api/auto_improver/analyze</code> with <code>{"session_id": "...", "agent_id": "..."}</code></p>
                </div>
            </div>
        </div>
        {% else %}
        <!-- Empty State -->
        <div class="section">
            <div class="empty-state">
                <div class="icon">📊</div>
                <h3>No Analyses Yet</h3>
                <p style="margin-top: 8px;">Enable auto analysis in plugin settings or trigger a manual analysis to get started.</p>
                <p style="margin-top: 16px; font-size: 12px; color: #6b6f83;">
                    API Endpoint: <code style="background: #22253a; padding: 2px 6px; border-radius: 4px;">POST /api/auto_improver/analyze</code>
                </p>
            </div>
        </div>
        {% endif %}
    </div>

    <!-- Detail Panel Overlay -->
    <div class="overlay" id="overlay" onclick="closeDetail()"></div>

    <!-- Detail Panel -->
    <div class="detail-panel" id="detailPanel">
        <div class="panel-header">
            <h3>Analysis Details</h3>
            <button class="close-btn" onclick="closeDetail()">&times;</button>
        </div>
        <div class="panel-body" id="panelBody">
            <!-- Content will be injected by JS -->
        </div>
    </div>

    <script>
        const ANALYSES_DATA = {{ analyses | tojson }};

        function scoreClass(score) {
            return score >= 80 ? 'good' : score >= 50 ? 'warning' : 'danger';
        }

        function escHtml(str) {
            return String(str)
                .replace(/&/g, '&amp;')
                .replace(/</g, '&lt;')
                .replace(/>/g, '&gt;')
                .replace(/"/g, '&quot;');
        }

        function showDetail(index) {
            const analysis = ANALYSES_DATA[index];
            if (!analysis) return;

            const sc = scoreClass(analysis.score);
            const issues = analysis.issues || [];
            const suggestions = analysis.suggestions || [];

            let html = `
                <div class="score-display">
                    <div class="score-circle ${sc}">${analysis.score}</div>
                    <div style="color: #8b8fa3; font-size: 14px;">Overall Score</div>
                </div>
                <div style="margin-bottom: 16px;">
                    <div style="font-size: 12px; color: #8b8fa3; margin-bottom: 4px;">SESSION</div>
                    <div style="font-size: 13px; color: #c9d1d9; word-break: break-all;">${escHtml(analysis.session_id)}</div>
                </div>
                <div style="margin-bottom: 16px;">
                    <div style="font-size: 12px; color: #8b8fa3; margin-bottom: 4px;">AGENT</div>
                    <div style="font-size: 13px; color: #c9d1d9;">${escHtml(analysis.agent_id || '—')}</div>
                </div>
                <div style="margin-bottom: 20px;">
                    <div style="font-size: 12px; color: #8b8fa3; margin-bottom: 4px;">SUMMARY</div>
                    <div style="font-size: 14px; color: #c9d1d9; line-height: 1.6;">${escHtml(analysis.summary)}</div>
                </div>
            `;

            if (issues.length > 0) {
                html += `<h4 style="font-size: 14px; color: #fff; margin-bottom: 12px;">Issues Found (${issues.length})</h4>`;
                issues.forEach(issue => {
                    const sev = issue.severity || 'low';
                    html += `
                        <div class="issue-detail ${sev}">
                            <div class="issue-type">${escHtml(issue.type || sev)} · ${escHtml(sev)}</div>
                            <div class="issue-message">${escHtml(issue.message || '')}</div>
                            ${issue.suggestion ? `<div class="issue-suggestion">💡 ${escHtml(issue.suggestion)}</div>` : ''}
                        </div>
                    `;
                });
            }

            if (suggestions.length > 0) {
                html += `<div class="suggestion-section"><h4>Suggestions by Category</h4>`;
                suggestions.forEach(s => {
                    (s.suggestions || []).forEach(text => {
                        html += `
                            <div class="suggestion-item">
                                <div class="cat-name">${escHtml(s.category)}</div>
                                <div class="cat-suggestion">${escHtml(text)}</div>
                            </div>
                        `;
                    });
                });
                html += `</div>`;
            }

            document.getElementById('panelBody').innerHTML = html;
            document.getElementById('detailPanel').classList.add('open');
            document.getElementById('overlay').classList.add('open');
        }

        function closeDetail() {
            document.getElementById('detailPanel').classList.remove('open');
            document.getElementById('overlay').classList.remove('open');
        }

        document.addEventListener('keydown', (e) => {
            if (e.key === 'Escape') closeDetail();
        });
    </script>
</body>
</html>
