{% extends "base.html" %}

{% block title %}{{ category.name }} - Forum{% endblock %}

{% block content %}

<!-- Category Header -->
<section class="relative py-20 overflow-hidden">
    <div class="absolute inset-0 dot-grid opacity-30 pointer-events-none"></div>
    <div class="absolute top-0 left-1/2 -translate-x-1/2 w-[500px] h-[300px] pointer-events-none"
         style="background: radial-gradient(ellipse, rgba(92,184,44,0.09) 0%, transparent 70%); filter: blur(40px);"></div>

    <div class="relative max-w-5xl mx-auto px-4 sm:px-6 lg:px-8">
        <div class="mb-5">
            <a href="{{ url_for('forum_index') }}" class="inline-flex items-center gap-2 text-sm font-medium text-primary-400 hover:text-primary-300 transition-colors">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16l-4-4m0 0l4-4m-4 4h18"/>
                </svg>
                Back to Forum
            </a>
        </div>
        <div class="badge badge-primary mb-4">Category</div>
        <h1 class="text-4xl md:text-5xl font-extrabold mb-3" style="font-family: 'Syne', sans-serif; color: #f1f0ff;">
            {{ category.name }}
        </h1>
        <p class="text-base" style="color: #8b8ca8;">{{ category.description }}</p>
    </div>
</section>

<!-- Topics -->
<section class="py-10 pb-24" style="background: #0a0a14;">
    <div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8">
        <div class="flex justify-between items-center mb-6">
            <div class="text-xs font-semibold tracking-widest uppercase" style="color: #6b6b8a; font-family: 'Syne', sans-serif;">
                {{ topics.total if topics else 0 }} Topics
            </div>
            <button onclick="document.getElementById('new-topic-modal').classList.remove('hidden')"
                    class="btn btn-primary text-sm">
                <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
                </svg>
                New Topic
            </button>
        </div>

        <div id="topics-list">
            {% include "forum/partials/topic_list.html" %}
        </div>

        <!-- Pagination -->
        {% if topics and topics.pages > 1 %}
        <div class="flex justify-center mt-10 gap-2">
            {% if topics.has_prev %}
            <a href="{{ url_for('forum_category', slug=category.slug, page=topics.prev_num) }}" class="btn btn-outline">
                &larr; Previous
            </a>
            {% endif %}
            <span class="btn btn-secondary">Page {{ topics.page }} of {{ topics.pages }}</span>
            {% if topics.has_next %}
            <a href="{{ url_for('forum_category', slug=category.slug, page=topics.next_num) }}" class="btn btn-outline">
                Next &rarr;
            </a>
            {% endif %}
        </div>
        {% endif %}
    </div>
</section>

<!-- New Topic Modal -->
<div id="new-topic-modal" class="fixed inset-0 hidden z-50 flex items-center justify-center p-4"
     style="background: rgba(0,0,0,0.7); backdrop-filter: blur(8px);">
    <div class="w-full max-w-2xl max-h-[90vh] overflow-y-auto rounded-2xl"
         style="background: #0f0f1a; border: 1px solid rgba(92,184,44,0.3); box-shadow: 0 0 60px rgba(92,184,44,0.15);">
        <div class="flex justify-between items-center px-7 py-5" style="border-bottom: 1px solid rgba(255,255,255,0.06);">
            <h3 class="text-lg font-bold" style="font-family: 'Syne', sans-serif; color: #f1f0ff;">Create New Topic</h3>
            <button onclick="document.getElementById('new-topic-modal').classList.add('hidden')"
                    class="w-8 h-8 rounded-lg flex items-center justify-center text-gray-500 hover:text-white hover:bg-white/10 transition-all">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
                </svg>
            </button>
        </div>
        <form hx-post="{{ url_for('forum_create_topic', category_slug=category.slug) }}"
              hx-target="#topics-list"
              hx-swap="innerHTML"
              hx-on::after-request="if(event.detail.successful) document.getElementById('new-topic-modal').classList.add('hidden')"
              class="p-7 space-y-5">
            <div>
                <label for="title" class="label">Topic Title</label>
                <input type="text" id="title" name="title" required class="input" placeholder="Enter a descriptive title">
            </div>
            <div>
                <label for="content" class="label">Content</label>
                <textarea id="content" name="content" rows="6" required class="input resize-none" placeholder="Share your thoughts, ask a question…"></textarea>
            </div>
            <div class="flex justify-end gap-3 pt-2">
                <button type="button" onclick="document.getElementById('new-topic-modal').classList.add('hidden')"
                        class="btn btn-secondary">Cancel</button>
                <button type="submit" class="btn btn-primary">Create Topic</button>
            </div>
        </form>
    </div>
</div>

{% endblock %}

{% block scripts %}
<script>
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape') document.getElementById('new-topic-modal').classList.add('hidden');
    });
    document.getElementById('new-topic-modal').addEventListener('click', function(e) {
        if (e.target === this) this.classList.add('hidden');
    });
</script>
{% endblock %}
