{% extends "base.html" %}
{% block title %}Coleções - Viral Social Pro{% endblock %}
{% block nav_collections %}active{% endblock %}

{% block content %}
<div class="p-6 space-y-6" x-data="collectionsApp()">
    <!-- Header -->
    <div class="gradient-bg rounded-2xl p-8 text-white flex items-center justify-between">
        <div>
            <h1 class="text-3xl font-bold mb-2">Coleções</h1>
            <p class="text-indigo-200">Organize seus vídeos salvos em coleções temáticas</p>
        </div>
        <button @click="showCreate = true"
                class="px-6 py-3 bg-white/20 hover:bg-white/30 text-white rounded-xl font-medium transition backdrop-blur">
            + Nova Coleção
        </button>
    </div>

    <!-- Create Collection Modal -->
    <div x-show="showCreate" x-cloak class="fixed inset-0 z-50 flex items-center justify-center bg-black/50" @click.self="showCreate = false">
        <div class="bg-white rounded-2xl p-6 w-full max-w-md shadow-xl">
            <h2 class="text-xl font-bold text-gray-900 mb-4">Nova Coleção</h2>
            <div class="space-y-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Nome</label>
                    <input type="text" x-model="newCollection.name" placeholder="Ex: Referências de Trends..."
                           class="w-full px-4 py-2.5 border border-gray-200 rounded-lg focus:ring-2 focus:ring-brand-500 outline-none">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Descrição</label>
                    <textarea x-model="newCollection.description" rows="2" placeholder="Descrição opcional..."
                              class="w-full px-4 py-2.5 border border-gray-200 rounded-lg focus:ring-2 focus:ring-brand-500 outline-none resize-none"></textarea>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Cor</label>
                    <div class="flex gap-2">
                        <template x-for="color in colors" :key="color">
                            <button @click="newCollection.color = color"
                                    class="w-8 h-8 rounded-full border-2 transition"
                                    :style="`background-color: ${color}`"
                                    :class="newCollection.color === color ? 'border-gray-900 scale-110' : 'border-transparent'">
                            </button>
                        </template>
                    </div>
                </div>
                <div class="flex gap-3 pt-2">
                    <button @click="showCreate = false" class="flex-1 py-2.5 bg-gray-100 text-gray-700 rounded-lg font-medium">Cancelar</button>
                    <button @click="createCollection()" class="flex-1 py-2.5 bg-brand-500 text-white rounded-lg font-medium hover:bg-brand-600 transition">Criar</button>
                </div>
            </div>
        </div>
    </div>

    <!-- Collections Grid -->
    {% if collections %}
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
        {% for collection in collections %}
        <div class="bg-white rounded-xl shadow-sm p-5 card-hover border-l-4" style="border-color: {{ collection.color }}">
            <div class="flex items-start justify-between mb-3">
                <div>
                    <h3 class="font-bold text-gray-900">{{ collection.name }}</h3>
                    {% if collection.description %}
                    <p class="text-sm text-gray-500 mt-1">{{ collection.description }}</p>
                    {% endif %}
                </div>
                <span class="px-2.5 py-1 text-xs font-bold rounded-full bg-gray-100 text-gray-600">
                    {{ collection.video_count }} vídeos
                </span>
            </div>
            <p class="text-xs text-gray-400">Criada em {{ collection.created_at.strftime('%d/%m/%Y') }}</p>
        </div>
        {% endfor %}
    </div>
    {% else %}
    <div class="bg-white rounded-xl p-16 text-center shadow-sm">
        <div class="text-6xl mb-4">📁</div>
        <h3 class="text-lg font-medium text-gray-900 mb-2">Nenhuma coleção criada</h3>
        <p class="text-gray-500 mb-6">Crie coleções para organizar seus vídeos favoritos</p>
        <button @click="showCreate = true"
                class="inline-flex items-center px-6 py-3 bg-brand-500 text-white rounded-xl hover:bg-brand-600 font-medium transition">
            + Nova Coleção
        </button>
    </div>
    {% endif %}
</div>
{% endblock %}

{% block scripts %}
<script>
function collectionsApp() {
    return {
        showCreate: false,
        colors: ['#6366f1', '#ef4444', '#f97316', '#eab308', '#22c55e', '#3b82f6', '#8b5cf6', '#ec4899'],
        newCollection: { name: '', description: '', color: '#6366f1' },

        async createCollection() {
            if (!this.newCollection.name) { showToast('Informe o nome da coleção', 'warning'); return; }
            try {
                const params = new URLSearchParams(this.newCollection);
                await apiCall(`/api/collections?${params}`, 'POST');
                showToast('Coleção criada com sucesso!', 'success');
                this.showCreate = false;
                window.location.reload();
            } catch (e) {}
        },
    };
}
</script>
{% endblock %}
