{% extends "base.html" %}

{% block title %}Analityka Forum - Norda Biznes Partner{% endblock %}

{% block extra_css %}
<style>
    .admin-header {
        margin-bottom: var(--spacing-xl);
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        gap: var(--spacing-md);
    }

    .admin-header h1 {
        font-size: var(--font-size-3xl);
        color: var(--text-primary);
    }

    .header-actions {
        display: flex;
        gap: var(--spacing-md);
    }

    .stats-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
        gap: var(--spacing-lg);
        margin-bottom: var(--spacing-2xl);
    }

    .stat-card {
        background: var(--surface);
        padding: var(--spacing-lg);
        border-radius: var(--radius-lg);
        box-shadow: var(--shadow);
        text-align: center;
    }

    .stat-card.highlight {
        border-left: 4px solid var(--primary);
    }

    .stat-value {
        font-size: var(--font-size-3xl);
        font-weight: 700;
        color: var(--primary);
    }

    .stat-label {
        color: var(--text-secondary);
        font-size: var(--font-size-sm);
        margin-top: var(--spacing-xs);
    }

    .stat-sublabel {
        color: var(--text-muted);
        font-size: var(--font-size-xs);
        margin-top: var(--spacing-xs);
    }

    .stat-card.warning .stat-value { color: var(--warning); }
    .stat-card.success .stat-value { color: var(--success); }
    .stat-card.info .stat-value { color: var(--info); }

    .section {
        background: var(--surface);
        padding: var(--spacing-xl);
        border-radius: var(--radius-lg);
        box-shadow: var(--shadow);
        margin-bottom: var(--spacing-xl);
    }

    .section h2 {
        font-size: var(--font-size-xl);
        margin-bottom: var(--spacing-lg);
        color: var(--text-primary);
        border-bottom: 2px solid var(--border);
        padding-bottom: var(--spacing-sm);
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .section h2 .badge {
        font-size: var(--font-size-sm);
        font-weight: normal;
    }

    .data-table {
        width: 100%;
        border-collapse: collapse;
    }

    .data-table th,
    .data-table td {
        padding: var(--spacing-md);
        text-align: left;
        border-bottom: 1px solid var(--border);
    }

    .data-table th {
        font-weight: 600;
        color: var(--text-secondary);
        font-size: var(--font-size-sm);
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    .data-table tr:hover {
        background: var(--background);
    }

    .topic-link {
        color: var(--text-primary);
        text-decoration: none;
        font-weight: 500;
    }

    .topic-link:hover {
        color: var(--primary);
    }

    .badge {
        display: inline-block;
        padding: 2px 8px;
        border-radius: var(--radius-sm);
        font-size: var(--font-size-xs);
        font-weight: 500;
    }

    .badge-category {
        border: 1px solid;
    }

    .badge-feature_request { background: #dbeafe; color: #1e40af; border-color: #93c5fd; }
    .badge-bug { background: #fee2e2; color: #991b1b; border-color: #fca5a5; }
    .badge-question { background: #dcfce7; color: #166534; border-color: #86efac; }
    .badge-announcement { background: #fef3c7; color: #92400e; border-color: #fcd34d; }

    .user-rank {
        display: flex;
        align-items: center;
        gap: var(--spacing-sm);
    }

    .rank-position {
        width: 28px;
        height: 28px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: bold;
        font-size: var(--font-size-sm);
    }

    .rank-1 { background: #fef3c7; color: #92400e; }
    .rank-2 { background: #e5e7eb; color: #374151; }
    .rank-3 { background: #fde68a; color: #78350f; }
    .rank-default { background: var(--background); color: var(--text-secondary); }

    .user-avatar {
        width: 32px;
        height: 32px;
        border-radius: 50%;
        background: var(--primary);
        color: white;
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: 600;
        font-size: var(--font-size-sm);
    }

    .user-info {
        display: flex;
        flex-direction: column;
    }

    .user-name {
        font-weight: 500;
        color: var(--text-primary);
    }

    .user-email {
        font-size: var(--font-size-xs);
        color: var(--text-muted);
    }

    .chart-container {
        position: relative;
        height: 300px;
        margin-top: var(--spacing-lg);
    }

    .empty-state {
        text-align: center;
        padding: var(--spacing-2xl);
        color: var(--text-secondary);
    }

    .empty-state svg {
        width: 48px;
        height: 48px;
        margin-bottom: var(--spacing-md);
        opacity: 0.5;
    }

    .activity-indicator {
        display: flex;
        align-items: center;
        gap: var(--spacing-xs);
    }

    .activity-indicator .icon {
        width: 8px;
        height: 8px;
        border-radius: 50%;
    }

    .activity-indicator .icon.topics { background: var(--primary); }
    .activity-indicator .icon.replies { background: var(--success); }

    /* Date range picker */
    .date-range {
        display: flex;
        align-items: center;
        gap: var(--spacing-sm);
        background: var(--background);
        padding: var(--spacing-sm) var(--spacing-md);
        border-radius: var(--radius);
        font-size: var(--font-size-sm);
    }

    .date-range input {
        border: 1px solid var(--border);
        border-radius: var(--radius-sm);
        padding: var(--spacing-xs) var(--spacing-sm);
        font-size: var(--font-size-sm);
    }

    /* Stats comparison */
    .stat-compare {
        font-size: var(--font-size-xs);
        margin-top: var(--spacing-xs);
    }

    .stat-compare.up { color: var(--success); }
    .stat-compare.down { color: var(--error); }

    /* Responsive */
    @media (max-width: 768px) {
        .admin-header {
            flex-direction: column;
            align-items: flex-start;
        }

        .header-actions {
            width: 100%;
            flex-wrap: wrap;
        }

        .stats-grid {
            grid-template-columns: repeat(2, 1fr);
        }

        .data-table {
            font-size: var(--font-size-sm);
        }
    }
</style>
{% endblock %}

{% block content %}
<div class="admin-header">
    <div>
        <h1>Analityka Forum</h1>
        <p class="text-muted">Statystyki, trendy i aktywnosc uzytkownikow</p>
    </div>
    <div class="header-actions">
        <a href="{{ url_for('forum.admin_forum') }}" class="btn btn-outline">
            <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M10 19l-7-7m0 0l7-7m-7 7h18"/></svg>
            Moderacja
        </a>
        <a href="{{ url_for('forum.admin_forum_export_activity') }}?start_date={{ start_date }}&end_date={{ end_date }}" class="btn btn-primary">
            <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/></svg>
            Eksportuj CSV
        </a>
    </div>
</div>

<!-- Date Range Filter -->
<div class="section" style="padding: var(--spacing-md) var(--spacing-lg);">
    <form method="GET" style="display: flex; align-items: center; gap: var(--spacing-lg); flex-wrap: wrap;">
        <div class="date-range">
            <label>Od:</label>
            <input type="date" name="start_date" value="{{ start_date }}" onchange="this.form.submit()">
        </div>
        <div class="date-range">
            <label>Do:</label>
            <input type="date" name="end_date" value="{{ end_date }}" onchange="this.form.submit()">
        </div>
        <div style="display: flex; gap: var(--spacing-sm);">
            <button type="button" class="btn btn-sm btn-outline" onclick="setDateRange(7)">7 dni</button>
            <button type="button" class="btn btn-sm btn-outline" onclick="setDateRange(30)">30 dni</button>
            <button type="button" class="btn btn-sm btn-outline" onclick="setDateRange(90)">90 dni</button>
        </div>
    </form>
</div>

<!-- Overview Stats -->
<div class="stats-grid">
    <div class="stat-card highlight">
        <div class="stat-value">{{ stats.total_topics }}</div>
        <div class="stat-label">Tematow lacznie</div>
        <div class="stat-sublabel">+{{ stats.topics_this_month }} w tym miesiacu</div>
    </div>
    <div class="stat-card highlight">
        <div class="stat-value">{{ stats.total_replies }}</div>
        <div class="stat-label">Odpowiedzi lacznie</div>
        <div class="stat-sublabel">+{{ stats.replies_this_month }} w tym miesiacu</div>
    </div>
    <div class="stat-card info">
        <div class="stat-value">{{ stats.active_users_7d }}</div>
        <div class="stat-label">Aktywni (7 dni)</div>
    </div>
    <div class="stat-card">
        <div class="stat-value">{{ stats.avg_response_time }}</div>
        <div class="stat-label">Sredni czas odpowiedzi</div>
        <div class="stat-sublabel">do pierwszej odpowiedzi</div>
    </div>
    <div class="stat-card {% if stats.unanswered_topics > 0 %}warning{% else %}success{% endif %}">
        <div class="stat-value">{{ stats.unanswered_topics }}</div>
        <div class="stat-label">Bez odpowiedzi</div>
    </div>
    <div class="stat-card success">
        <div class="stat-value">{{ stats.resolved_topics }}</div>
        <div class="stat-label">Rozwiazanych</div>
    </div>
</div>

<!-- Activity Chart -->
<div class="section">
    <h2>
        Aktywnosc w czasie
        <div style="display: flex; gap: var(--spacing-md);">
            <span class="activity-indicator"><span class="icon topics"></span> Tematy</span>
            <span class="activity-indicator"><span class="icon replies"></span> Odpowiedzi</span>
        </div>
    </h2>
    <div class="chart-container">
        <canvas id="activityChart"></canvas>
    </div>
</div>

<!-- Topics Without Replies -->
<div class="section">
    <h2>
        Tematy bez odpowiedzi
        <span class="badge {% if unanswered_topics|length > 0 %}badge-bug{% else %}badge-question{% endif %}">{{ unanswered_topics|length }}</span>
    </h2>
    {% if unanswered_topics %}
    <table class="data-table">
        <thead>
            <tr>
                <th>Tytul</th>
                <th>Kategoria</th>
                <th>Autor</th>
                <th>Data</th>
                <th>Oczekuje</th>
            </tr>
        </thead>
        <tbody>
            {% for topic in unanswered_topics %}
            <tr>
                <td>
                    <a href="{{ url_for('forum_topic', topic_id=topic.id) }}" class="topic-link">
                        {{ topic.title }}
                    </a>
                </td>
                <td>
                    <span class="badge badge-category badge-{{ topic.category or 'question' }}">
                        {{ category_labels.get(topic.category, 'Pytanie') }}
                    </span>
                </td>
                <td>{{ topic.author.name or topic.author.email.split('@')[0] }}</td>
                <td>{{ topic.created_at.strftime('%d.%m.%Y %H:%M') }}</td>
                <td>{{ topic.days_waiting }} dni</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    {% else %}
    <div class="empty-state">
        <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
        <p>Wszystkie tematy maja odpowiedzi!</p>
    </div>
    {% endif %}
</div>

<!-- User Rankings -->
<div class="section">
    <h2>Ranking uzytkownikow</h2>
    {% if user_rankings %}
    <table class="data-table">
        <thead>
            <tr>
                <th>#</th>
                <th>Uzytkownik</th>
                <th>Tematy</th>
                <th>Odpowiedzi</th>
                <th>Rozwiazania</th>
                <th>Reakcje</th>
                <th>Suma</th>
            </tr>
        </thead>
        <tbody>
            {% for user in user_rankings %}
            <tr>
                <td>
                    <span class="rank-position {% if loop.index <= 3 %}rank-{{ loop.index }}{% else %}rank-default{% endif %}">
                        {{ loop.index }}
                    </span>
                </td>
                <td>
                    <div class="user-rank">
                        <div class="user-avatar">{{ (user.name or user.email)[0]|upper }}</div>
                        <div class="user-info">
                            <span class="user-name">{{ user.name or user.email.split('@')[0] }}</span>
                            <span class="user-email">{{ user.email }}</span>
                        </div>
                    </div>
                </td>
                <td>{{ user.topic_count }}</td>
                <td>{{ user.reply_count }}</td>
                <td>{{ user.solution_count }}</td>
                <td>{{ user.reaction_count }}</td>
                <td><strong>{{ user.total_score }}</strong></td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    {% else %}
    <div class="empty-state">
        <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"/></svg>
        <p>Brak danych o aktywnosci uzytkownikow</p>
    </div>
    {% endif %}
</div>

<!-- Category Stats -->
<div class="section">
    <h2>Statystyki kategorii</h2>
    <div class="stats-grid" style="margin-bottom: 0;">
        {% for cat, count in category_stats.items() %}
        <div class="stat-card">
            <div class="stat-value" style="font-size: var(--font-size-2xl);">{{ count }}</div>
            <div class="stat-label">
                <span class="badge badge-category badge-{{ cat }}">{{ category_labels.get(cat, cat) }}</span>
            </div>
        </div>
        {% endfor %}
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
{% endblock %}

{% block extra_js %}
    const chartData = {{ chart_data|tojson|safe }};

    // Activity Chart
    const ctx = document.getElementById('activityChart').getContext('2d');
    new Chart(ctx, {
        type: 'line',
        data: {
            labels: chartData.labels,
            datasets: [
                {
                    label: 'Tematy',
                    data: chartData.topics,
                    borderColor: 'rgb(79, 70, 229)',
                    backgroundColor: 'rgba(79, 70, 229, 0.1)',
                    fill: true,
                    tension: 0.4
                },
                {
                    label: 'Odpowiedzi',
                    data: chartData.replies,
                    borderColor: 'rgb(34, 197, 94)',
                    backgroundColor: 'rgba(34, 197, 94, 0.1)',
                    fill: true,
                    tension: 0.4
                }
            ]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
                legend: {
                    display: false
                },
                tooltip: {
                    mode: 'index',
                    intersect: false
                }
            },
            scales: {
                x: {
                    grid: {
                        display: false
                    }
                },
                y: {
                    beginAtZero: true,
                    ticks: {
                        stepSize: 1
                    }
                }
            }
        }
    });

    // Date range helpers
    function setDateRange(days) {
        const end = new Date();
        const start = new Date();
        start.setDate(start.getDate() - days);

        document.querySelector('input[name="start_date"]').value = formatDate(start);
        document.querySelector('input[name="end_date"]').value = formatDate(end);
        document.querySelector('form').submit();
    }

    function formatDate(date) {
        return date.toISOString().split('T')[0];
    }
{% endblock %}
