{% extends "base.html" %}

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

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

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

    .forum-stats {
        color: var(--text-secondary);
        font-size: var(--font-size-sm);
    }

    /* Filters bar */
    .filters-bar {
        display: flex;
        gap: var(--spacing-md);
        margin-bottom: var(--spacing-lg);
        flex-wrap: wrap;
        align-items: center;
    }

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

    .filter-label {
        font-size: var(--font-size-sm);
        color: var(--text-secondary);
        font-weight: 500;
    }

    .filter-btn {
        padding: var(--spacing-sm) var(--spacing-md);
        border-radius: var(--radius);
        border: 1px solid var(--border);
        background: var(--surface);
        text-decoration: none;
        color: var(--text-secondary);
        font-size: var(--font-size-sm);
        transition: var(--transition);
    }

    .filter-btn:hover {
        background: var(--background);
        color: var(--text-primary);
    }

    .filter-btn.active {
        background: var(--primary);
        border-color: var(--primary);
        color: white;
    }

    .topics-list {
        display: flex;
        flex-direction: column;
        gap: var(--spacing-md);
    }

    .topic-card {
        background: var(--surface);
        border-radius: var(--radius-lg);
        padding: var(--spacing-lg);
        box-shadow: var(--shadow);
        transition: var(--transition);
        display: grid;
        grid-template-columns: 1fr auto;
        gap: var(--spacing-lg);
        align-items: center;
    }

    .topic-card:hover {
        box-shadow: var(--shadow-md);
        transform: translateY(-2px);
    }

    .topic-card.pinned {
        border-left: 4px solid var(--primary);
        background: linear-gradient(135deg, #eff6ff, var(--surface));
    }

    .topic-card.locked {
        opacity: 0.7;
    }

    .topic-main {
        min-width: 0;
    }

    .topic-title {
        font-size: var(--font-size-lg);
        font-weight: 600;
        color: var(--text-primary);
        text-decoration: none;
        display: flex;
        align-items: center;
        gap: var(--spacing-sm);
        flex-wrap: wrap;
    }

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

    .topic-badge {
        font-size: var(--font-size-xs);
        padding: 2px 8px;
        border-radius: var(--radius-sm);
        font-weight: 500;
        white-space: nowrap;
    }

    .badge-pinned {
        background: var(--primary);
        color: white;
    }

    .badge-locked {
        background: var(--secondary);
        color: white;
    }

    /* Category badges */
    .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;
    }

    .badge-test {
        background: #f3f4f6;
        color: #6b7280;
        border-color: #d1d5db;
    }

    .topic-card.test-topic {
        opacity: 0.6;
        border-left: 4px solid #9ca3af;
    }

    .toggle-test-btn {
        background: #fef3c7;
        border-color: #fcd34d;
        color: #92400e;
    }

    .toggle-test-btn:hover {
        background: #fde68a;
    }

    .toggle-test-btn.active {
        background: var(--primary);
        border-color: var(--primary);
        color: white;
    }

    /* Status badges */
    .badge-status {
        font-size: var(--font-size-xs);
        padding: 2px 6px;
        border-radius: var(--radius-sm);
    }

    .badge-new {
        background: #f3f4f6;
        color: #374151;
    }

    .badge-in_progress {
        background: #dbeafe;
        color: #1e40af;
    }

    .badge-resolved {
        background: #dcfce7;
        color: #166534;
    }

    .badge-rejected {
        background: #fee2e2;
        color: #991b1b;
    }

    /* AI generated indicator */
    .ai-indicator {
        display: inline-flex;
        align-items: center;
        gap: 2px;
        padding: 2px 6px;
        background: linear-gradient(135deg, #e0e7ff, #c7d2fe);
        border-radius: var(--radius-sm);
        font-size: 10px;
        font-weight: 500;
        color: #4338ca;
        cursor: help;
    }

    .ai-indicator svg {
        width: 12px;
        height: 12px;
    }

    .topic-meta {
        display: flex;
        gap: var(--spacing-lg);
        margin-top: var(--spacing-sm);
        font-size: var(--font-size-sm);
        color: var(--text-secondary);
    }

    .topic-meta span {
        display: flex;
        align-items: center;
        gap: var(--spacing-xs);
    }

    .topic-stats {
        display: flex;
        flex-direction: column;
        align-items: flex-end;
        gap: var(--spacing-xs);
        text-align: right;
    }

    .stat-item {
        display: flex;
        align-items: center;
        gap: var(--spacing-xs);
        font-size: var(--font-size-sm);
        color: var(--text-secondary);
    }

    .stat-item strong {
        color: var(--text-primary);
    }

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

    .empty-state h3 {
        margin-bottom: var(--spacing-md);
        color: var(--text-primary);
    }

    .pagination {
        display: flex;
        justify-content: center;
        gap: var(--spacing-sm);
        margin-top: var(--spacing-xl);
    }

    .pagination a, .pagination span {
        padding: var(--spacing-sm) var(--spacing-md);
        border-radius: var(--radius);
        text-decoration: none;
        font-weight: 500;
    }

    .pagination a {
        background: var(--surface);
        color: var(--text-primary);
        border: 1px solid var(--border);
    }

    .pagination a:hover {
        background: var(--background);
        border-color: var(--primary);
    }

    .pagination span.current {
        background: var(--primary);
        color: white;
    }

    @media (max-width: 768px) {
        .forum-header {
            flex-direction: column;
            align-items: flex-start;
            gap: var(--spacing-md);
        }

        .filters-bar {
            flex-direction: column;
            align-items: stretch;
            gap: var(--spacing-sm);
        }

        .filter-group {
            flex-wrap: wrap;
        }

        .filter-btn {
            padding: var(--spacing-xs) var(--spacing-sm);
            font-size: var(--font-size-xs);
        }

        .topic-card {
            grid-template-columns: 1fr;
        }

        .topic-stats {
            flex-direction: row;
            align-items: center;
        }
    }
</style>
{% endblock %}

{% block content %}
<div class="forum-header">
    <div>
        <h1>Forum Norda Biznes</h1>
        <p class="forum-stats">{{ total_topics }} tematów</p>
    </div>
    <a href="{{ url_for('forum_new_topic') }}" class="btn btn-primary">
        + Nowy temat
    </a>
</div>

<!-- Filters - Uproszczone (ukryte kategorie i statusy) -->
<div class="filters-bar">
    <!-- Filtry kategorii i statusów ukryte - forum w fazie upraszczania -->
    <!-- Oryginalny kod zachowany w komentarzu na wypadek przywrócenia:
    <div class="filter-group">
        <span class="filter-label">Kategoria:</span>
        <a href="{{ url_for('forum_index', status=status_filter) }}" class="filter-btn active">Wszystkie</a>
    </div>
    -->
    <div class="filter-group">
        <span class="filter-label" style="color: var(--text-secondary);">💬 Uwagi rozwojowe</span>
        <span style="color: var(--text-muted); font-size: var(--font-size-sm); margin-left: var(--spacing-sm);">Zapraszamy do podzielenia się swoimi opiniami</span>
    </div>
    <div class="filter-group" style="margin-left: auto;">
        <button type="button" id="toggleTestBtn" class="filter-btn toggle-test-btn" onclick="toggleTestTopics()">
            <span class="hide-label">🙈 Ukryj testowe</span>
            <span class="show-label" style="display:none;">👁 Pokaż testowe</span>
        </button>
    </div>
</div>

{% if topics %}
<div class="topics-list">
    {% for topic in topics %}
    <article class="topic-card {% if topic.is_pinned %}pinned{% endif %} {% if topic.is_locked %}locked{% endif %} {% if topic.category == 'test' %}test-topic{% endif %}">
        <div class="topic-main">
            <a href="{{ url_for('forum_topic', topic_id=topic.id) }}" class="topic-title">
                {% if topic.is_pinned %}
                <span class="topic-badge badge-pinned">Przypięty</span>
                {% endif %}
                {% if topic.is_locked %}
                <span class="topic-badge badge-locked">Zamknięty</span>
                {% endif %}
                <span class="topic-badge badge-category badge-{{ topic.category or 'question' }}">
                    {{ category_labels.get(topic.category, 'Pytanie') }}
                </span>
                <span class="topic-badge badge-status badge-{{ topic.status or 'new' }}">
                    {{ status_labels.get(topic.status, 'Nowy') }}
                </span>
                {{ topic.title }}
            </a>
            <div class="topic-meta">
                <span>
                    <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                        <path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path>
                        <circle cx="12" cy="7" r="4"></circle>
                    </svg>
                    {{ topic.author.name or topic.author.email.split('@')[0] }}
                    {% if topic.is_ai_generated %}
                    <span class="ai-indicator" title="Wygenerowano przez AI">
                        <svg viewBox="0 0 24 24" fill="currentColor"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-1 17.93c-3.95-.49-7-3.85-7-7.93 0-.62.08-1.21.21-1.79L9 15v1c0 1.1.9 2 2 2v1.93zm6.9-2.54c-.26-.81-1-1.39-1.9-1.39h-1v-3c0-.55-.45-1-1-1H8v-2h2c.55 0 1-.45 1-1V7h2c1.1 0 2-.9 2-2v-.41c2.93 1.19 5 4.06 5 7.41 0 2.08-.8 3.97-2.1 5.39z"/></svg>
                        AI
                    </span>
                    {% endif %}
                </span>
                <span>
                    <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                        <circle cx="12" cy="12" r="10"></circle>
                        <polyline points="12 6 12 12 16 14"></polyline>
                    </svg>
                    {{ topic.created_at.strftime('%d.%m.%Y %H:%M') }}
                </span>
            </div>
        </div>
        <div class="topic-stats">
            <div class="stat-item">
                <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                    <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path>
                </svg>
                <strong>{{ topic.reply_count }}</strong> odpowiedzi
            </div>
            <div class="stat-item">
                <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                    <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path>
                    <circle cx="12" cy="12" r="3"></circle>
                </svg>
                <strong>{{ topic.views_count }}</strong> wyświetleń
            </div>
        </div>
    </article>
    {% endfor %}
</div>

{% if total_pages > 1 %}
<nav class="pagination">
    {% if page > 1 %}
    <a href="{{ url_for('forum_index', page=page-1, category=category_filter, status=status_filter) }}">&laquo; Poprzednia</a>
    {% endif %}

    {% for p in range(1, total_pages + 1) %}
        {% if p == page %}
        <span class="current">{{ p }}</span>
        {% elif p <= 3 or p > total_pages - 3 or (p >= page - 1 and p <= page + 1) %}
        <a href="{{ url_for('forum_index', page=p, category=category_filter, status=status_filter) }}">{{ p }}</a>
        {% elif p == 4 or p == total_pages - 3 %}
        <span>...</span>
        {% endif %}
    {% endfor %}

    {% if page < total_pages %}
    <a href="{{ url_for('forum_index', page=page+1, category=category_filter, status=status_filter) }}">Nastepna &raquo;</a>
    {% endif %}
</nav>
{% endif %}

{% else %}
<div class="empty-state">
    <svg width="64" height="64" fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24" style="margin: 0 auto var(--spacing-md); display: block; opacity: 0.5;">
        <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path>
    </svg>
    <h3>Brak tematów</h3>
    <p>Bądź pierwszy! Utwórz nowy temat dyskusji.</p>
    <a href="{{ url_for('forum_new_topic') }}" class="btn btn-primary mt-2">
        + Utwórz pierwszy temat
    </a>
</div>
{% endif %}
{% endblock %}

{% block extra_js %}
function setTestTopicsVisibility(hidden) {
    const btn = document.getElementById('toggleTestBtn');
    const hideLabel = btn.querySelector('.hide-label');
    const showLabel = btn.querySelector('.show-label');
    const testTopics = document.querySelectorAll('.test-topic');

    if (hidden) {
        testTopics.forEach(t => t.style.display = 'none');
        hideLabel.style.display = 'none';
        showLabel.style.display = '';
        btn.classList.add('active');
    } else {
        testTopics.forEach(t => t.style.display = '');
        hideLabel.style.display = '';
        showLabel.style.display = 'none';
        btn.classList.remove('active');
    }
    localStorage.setItem('hideTestTopics', hidden ? 'true' : 'false');

    // Update topic count
    const visibleTopics = document.querySelectorAll('.topic-card:not([style*="display: none"])').length;
    const statsEl = document.querySelector('.forum-stats');
    if (statsEl) {
        statsEl.textContent = visibleTopics + ' tematów';
    }
}

function toggleTestTopics() {
    const isCurrentlyHidden = localStorage.getItem('hideTestTopics') === 'true';
    setTestTopicsVisibility(!isCurrentlyHidden);
}

// Apply saved preference on page load
(function() {
    if (localStorage.getItem('hideTestTopics') === 'true') {
        setTestTopicsVisibility(true);
    }
})();
{% endblock %}
