<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>🤖 Intelectra AI Control Center</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: #fff;
            min-height: 100vh;
        }
        
        .header {
            background: rgba(0,0,0,0.2);
            padding: 1rem;
            text-align: center;
            backdrop-filter: blur(10px);
        }
        
        .container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 2rem;
        }
        
        .grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 2rem;
            margin-bottom: 2rem;
        }
        
        .card {
            background: rgba(255,255,255,0.1);
            backdrop-filter: blur(10px);
            border-radius: 15px;
            padding: 1.5rem;
            border: 1px solid rgba(255,255,255,0.2);
            box-shadow: 0 8px 32px rgba(31, 38, 135, 0.37);
        }
        
        .card h3 {
            margin-bottom: 1rem;
            font-size: 1.2rem;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }
        
        .ai-stat {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0.5rem 0;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }
        
        .ai-stat:last-child {
            border-bottom: none;
        }
        
        .ai-name {
            font-weight: bold;
            color: #4ade80;
        }
        
        .ai-count {
            background: rgba(255,255,255,0.2);
            padding: 0.2rem 0.8rem;
            border-radius: 20px;
            font-size: 0.9rem;
        }
        
        .article-item {
            background: rgba(0,0,0,0.1);
            padding: 1rem;
            border-radius: 10px;
            margin-bottom: 0.5rem;
        }
        
        .article-id {
            font-weight: bold;
            color: #fbbf24;
        }
        
        .chart-container {
            height: 200px;
            background: rgba(255,255,255,0.05);
            border-radius: 10px;
            padding: 1rem;
            display: flex;
            align-items: end;
            justify-content: space-around;
        }
        
        .chart-bar {
            background: linear-gradient(to top, #4ade80, #22d3ee);
            border-radius: 4px 4px 0 0;
            min-width: 20px;
            position: relative;
            transition: all 0.3s ease;
        }
        
        .chart-bar:hover {
            transform: scale(1.1);
        }
        
        .chart-bar::after {
            content: attr(data-value);
            position: absolute;
            top: -25px;
            left: 50%;
            transform: translateX(-50%);
            font-size: 0.8rem;
            font-weight: bold;
        }
        
        .nav-menu {
            background: rgba(255,255,255,0.1);
            padding: 1rem;
            border-radius: 10px;
            margin-bottom: 2rem;
            text-align: center;
        }
        
        .nav-menu a {
            color: #fff;
            text-decoration: none;
            padding: 0.5rem 1rem;
            margin: 0 0.5rem;
            border-radius: 20px;
            background: rgba(255,255,255,0.1);
            transition: all 0.3s ease;
        }
        
        .nav-menu a:hover {
            background: rgba(255,255,255,0.2);
            transform: translateY(-2px);
        }
        
        .status-indicator {
            display: inline-block;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            margin-right: 0.5rem;
        }
        
        .status-online { background: #4ade80; }
        .status-offline { background: #ef4444; }
        
        .live-counter {
            font-size: 2rem;
            font-weight: bold;
            text-align: center;
            margin: 1rem 0;
            color: #4ade80;
        }
        
        @keyframes pulse {
            0% { opacity: 1; }
            50% { opacity: 0.5; }
            100% { opacity: 1; }
        }
        
        .pulse {
            animation: pulse 2s infinite;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>🤖 Intelectra AI Control Center</h1>
        <p>KI-Tracking & Schema Management Dashboard</p>
    </div>
    
    <div class="container">
        <div class="nav-menu">
            <a href="/">📊 Dashboard</a>
            <a href="/faq-management">🗄️ FAQ Management</a>
            <a href="/add-problem">➕ Problem hinzufügen</a>
            <a href="/schema-preview/12345">👁️ Schema Preview</a>
        </div>
        
        <div class="grid">
            <!-- Live AI Activity -->
            <div class="card">
                <h3>🔥 Live AI Activity</h3>
                <div class="live-counter pulse" id="live-counter">0</div>
                <p style="text-align: center; opacity: 0.8;">KI-Besuche heute</p>
                <div class="status-indicator status-online"></div>
                <span>Tracking aktiv</span>
            </div>
            
            <!-- AI Visits -->
            <div class="card">
                <h3>🤖 KI-Besucher (7 Tage)</h3>
                {% if stats.ai_visits %}
                    {% for ai in stats.ai_visits %}
                    <div class="ai-stat">
                        <span class="ai-name">{{ ai.ai_name }}</span>
                        <div>
                            <span class="ai-count">{{ ai.visit_count }} Besuche</span>
                            <small style="opacity: 0.7; margin-left: 0.5rem;">
                                {{ ai.unique_visitors }} Unique
                            </small>
                        </div>
                    </div>
                    {% endfor %}
                {% else %}
                    <p style="opacity: 0.7;">Noch keine KI-Besuche erkannt</p>
                {% endif %}
            </div>
            
            <!-- Top Articles -->
            <div class="card">
                <h3>📈 Top KI-Artikel</h3>
                {% if stats.top_articles %}
                    {% for article in stats.top_articles %}
                    <div class="article-item">
                        <div class="article-id">Artikel #{{ article.article_id }}</div>
                        <div style="font-size: 0.9rem; opacity: 0.8;">
                            {{ article.visit_count }} Besuche von: {{ article.visiting_ais }}
                        </div>
                    </div>
                    {% endfor %}
                {% else %}
                    <p style="opacity: 0.7;">Noch keine Artikel-Besuche</p>
                {% endif %}
            </div>
            
            <!-- Hourly Distribution -->
            <div class="card">
                <h3>⏰ Tagesverteilung</h3>
                <div class="chart-container">
                    {% if stats.hourly_distribution %}
                        {% for hour_data in stats.hourly_distribution %}
                            {% set max_visits = stats.hourly_distribution|map(attribute='visits')|max %}
                            {% set bar_height = (hour_data.visits / max_visits * 150)|round|int %}
                            <div class="chart-bar" 
                                 style="height: {{ bar_height }}px" 
                                 data-value="{{ hour_data.visits }}"
                                 title="{{ hour_data.hour }}:00 - {{ hour_data.visits }} Besuche">
                            </div>
                        {% endfor %}
                    {% else %}
                        <p style="opacity: 0.7; align-self: center;">Noch keine Daten</p>
                    {% endif %}
                </div>
            </div>
            
            <!-- System Status -->
            <div class="card">
                <h3>⚙️ System Status</h3>
                <div class="ai-stat">
                    <span>AI Schema Generator</span>
                    <span class="ai-count">✅ Aktiv</span>
                </div>
                <div class="ai-stat">
                    <span>Problem Database</span>
                    <span class="ai-count">✅ Online</span>
                </div>
                <div class="ai-stat">
                    <span>KI Tracking</span>
                    <span class="ai-count">✅ Läuft</span>
                </div>
                <div class="ai-stat">
                    <span>Flask Dashboard</span>
                    <span class="ai-count">🟡 Dev Mode</span>
                </div>
            </div>
            
            <!-- Quick Actions -->
            <div class="card">
                <h3>⚡ Quick Actions</h3>
                <div style="display: flex; flex-direction: column; gap: 0.5rem;">
                    <button onclick="refreshStats()" style="background: rgba(255,255,255,0.2); border: none; color: #fff; padding: 0.8rem; border-radius: 8px; cursor: pointer;">
                        🔄 Stats aktualisieren
                    </button>
                    <button onclick="exportData()" style="background: rgba(255,255,255,0.2); border: none; color: #fff; padding: 0.8rem; border-radius: 8px; cursor: pointer;">
                        📥 Daten exportieren
                    </button>
                    <button onclick="clearCache()" style="background: rgba(255,255,255,0.2); border: none; color: #fff; padding: 0.8rem; border-radius: 8px; cursor: pointer;">
                        🗑️ Cache leeren
                    </button>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        // Live Updates alle 30 Sekunden
        setInterval(updateLiveStats, 30000);
        
        function updateLiveStats() {
            fetch('/api/ai-visits')
                .then(response => response.json())
                .then(data => {
                    // Live Counter updaten
                    const totalToday = data.ai_visits.reduce((sum, ai) => sum + ai.visit_count, 0);
                    document.getElementById('live-counter').textContent = totalToday;
                })
                .catch(error => console.error('Live update failed:', error));
        }
        
        function refreshStats() {
            window.location.reload();
        }
        
        function exportData() {
            window.open('/api/export-stats', '_blank');
        }
        
        function clearCache() {
            if (confirm('Cache wirklich leeren?')) {
                fetch('/api/clear-cache', { method: 'POST' })
                    .then(() => alert('Cache geleert!'));
            }
        }
        
        // Initial load
        updateLiveStats();
    </script>
</body>
</html>