{% extends "layouts/base.html" %}
{% load i18n %}
{% load static %}
{% load social_filters %}
{% load star_rating %}
{% load social_icons %}

{% block title %}{% trans "Social Media Monitoring" %} - PX360{% endblock %}

{% block extra_css %}
<style>
.page-header-gradient {
    background: linear-gradient(135deg, #005696 0%, #0069a8 50%, #007bbd 100%);
    color: white;
    padding: 1.5rem 2rem;
    border-radius: 1rem;
    margin-bottom: 1.5rem;
    box-shadow: 0 10px 15px -3px rgba(0, 86, 150, 0.2);
}

.section-card {
    background: white;
    border-radius: 1rem;
    border: 2px solid #e2e8f0;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    transition: all 0.3s ease;
}

.section-card:hover {
    border-color: #005696;
    box-shadow: 0 10px 25px -5px rgba(0, 86, 150, 0.15);
}

.section-header {
    padding: 1rem 1.5rem;
    border-bottom: 2px solid #e2e8f0;
    background: linear-gradient(to right, #f8fafc, #f1f5f9);
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.section-icon {
    width: 40px;
    height: 40px;
    border-radius: 0.75rem;
    display: flex;
    align-items: center;
    justify-content: center;
}
</style>
{% endblock %}

{% block content %}
<div class="p-6 md:p-8 bg-gradient-to-br from-light to-blue-50 min-h-screen">
    <!-- Page Header -->
    <div class="page-header-gradient">
        <div class="flex flex-col md:flex-row justify-between items-start md:items-center gap-4">
            <div class="flex items-center gap-3">
                <div class="section-icon bg-white/20">
                    <i data-lucide="message-circle" class="w-6 h-6 text-white"></i>
                </div>
                <div>
                    <h1 class="text-2xl font-bold">
                        {% trans "Social Media Monitoring" %}
                    </h1>
                    <p class="text-white/80 text-sm">
                        {% trans "Track social media mentions and sentiment across all platforms" %}
                    </p>
                </div>
            </div>
            <div class="flex gap-3">
                <a href="{% url 'social:social_analytics' %}" class="inline-flex items-center gap-2 px-5 py-2.5 bg-white/20 text-white rounded-xl font-semibold hover:bg-white/30 transition">
                    <i data-lucide="bar-chart-3" class="w-4 h-4"></i>
                    {% trans "Analytics" %}
                </a>
                <a href="{% url 'social:social_export_csv' %}" class="inline-flex items-center gap-2 px-5 py-2.5 bg-white/20 text-white rounded-xl font-semibold hover:bg-white/30 transition">
                    <i data-lucide="file-spreadsheet" class="w-4 h-4"></i> CSV
                </a>
                <a href="{% url 'social:social_export_excel' %}" class="inline-flex items-center gap-2 px-5 py-2.5 bg-white text-navy rounded-xl font-semibold hover:bg-gray-100 transition">
                    <i data-lucide="file-spreadsheet" class="w-4 h-4"></i> Excel
                </a>
            </div>
        </div>
    </div>

    <!-- Platform Cards -->
    <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-4 mb-8">
        {% for platform_code, platform_name in platforms %}
        <a href="{% url 'social:social_platform' platform_code %}" class="group">
            <div class="bg-white p-6 rounded-2xl shadow-sm border-2 border-blue-100 hover:border-purple hover:shadow-lg hover:-translate-y-1 transition-all duration-200">
                <div class="text-center">
                    <div class="w-14 h-14 bg-gradient-to-br from-blue-100 to-purple-100 rounded-2xl flex items-center justify-center mx-auto mb-3 group-hover:shadow-lg transition">
                        {% social_icon platform_code %}
                    </div>
                    <h6 class="font-bold text-navy mb-1">{{ platform_name }}</h6>
                    <p class="text-xs text-slate">
                        {{ stats|lookup:platform_code }} {% trans "comments" %}
                    </p>
                </div>
            </div>
        </a>
        {% endfor %}
    </div>

    <!-- Statistics Cards -->
    <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-8">
        <div class="section-card">
            <div class="section-header">
                <div class="section-icon bg-gradient-to-br from-blue to-navy">
                    <i data-lucide="message-square" class="w-5 h-5 text-white"></i>
                </div>
                <div>
                    <p class="text-xs font-semibold text-slate uppercase">{% trans "Total Comments" %}</p>
                    <p class="text-2xl font-bold text-navy">{{ stats.total }}</p>
                </div>
            </div>
        </div>

        <div class="section-card">
            <div class="section-header">
                <div class="section-icon bg-gradient-to-br from-emerald-500 to-green-500">
                    <i data-lucide="smile" class="w-5 h-5 text-white"></i>
                </div>
                <div>
                    <p class="text-xs font-semibold text-slate uppercase">{% trans "Positive" %}</p>
                    <p class="text-2xl font-bold text-navy">{{ stats.positive }}</p>
                    <p class="text-xs text-slate">{% widthratio stats.positive stats.total 100 %}% {% trans "of total" %}</p>
                </div>
            </div>
        </div>

        <div class="section-card">
            <div class="section-header">
                <div class="section-icon bg-gradient-to-br from-slate-400 to-slate-500">
                    <i data-lucide="meh" class="w-5 h-5 text-white"></i>
                </div>
                <div>
                    <p class="text-xs font-semibold text-slate uppercase">{% trans "Neutral" %}</p>
                    <p class="text-2xl font-bold text-navy">{{ stats.neutral }}</p>
                    <p class="text-xs text-slate">{% widthratio stats.neutral stats.total 100 %}% {% trans "of total" %}</p>
                </div>
            </div>
        </div>

        <div class="section-card">
            <div class="section-header">
                <div class="section-icon bg-gradient-to-br from-red-500 to-rose-500">
                    <i data-lucide="frown" class="w-5 h-5 text-white"></i>
                </div>
                <div>
                    <p class="text-xs font-semibold text-slate uppercase">{% trans "Negative" %}</p>
                    <p class="text-2xl font-bold text-navy">{{ stats.negative }}</p>
                    <p class="text-xs text-slate">{% widthratio stats.negative stats.total 100 %}% {% trans "of total" %}</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <div class="section-card mb-8">
        <details class="group">
            <summary class="section-header cursor-pointer list-none">
                <div class="flex items-center justify-between w-full">
                    <div class="flex items-center gap-2">
                        <i data-lucide="filter" class="w-5 h-5 text-navy"></i>
                        <h3 class="text-lg font-bold text-navy">{% trans "Advanced Filters" %}</h3>
                    </div>
                    <i data-lucide="chevron-down" class="w-5 h-5 text-slate group-open:rotate-180 transition-transform"></i>
                </div>
            </summary>
            <div class="p-6">
                <form method="get" class="grid grid-cols-1 md:grid-cols-5 gap-4">
                    <div>
                        <label class="block text-sm font-bold text-navy mb-2">{% trans "Platform" %}</label>
                        <select name="platform" class="w-full px-4 py-2.5 border-2 border-blue-100 rounded-xl text-navy focus:ring-2 focus:ring-blue focus:border-transparent transition bg-white">
                            <option value="">{% trans "All Platforms" %}</option>
                            {% for platform_code, platform_name in platforms %}
                            <option value="{{ platform_code }}" {% if filters.platform == platform_code %}selected{% endif %}>
                                {{ platform_name }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-bold text-navy mb-2">{% trans "Sentiment" %}</label>
                        <select name="sentiment" class="w-full px-4 py-2.5 border-2 border-blue-100 rounded-xl text-navy focus:ring-2 focus:ring-blue focus:border-transparent transition bg-white">
                            <option value="">{% trans "All Sentiments" %}</option>
                            <option value="positive" {% if filters.sentiment == 'positive' %}selected{% endif %}>{% trans "Positive" %}</option>
                            <option value="neutral" {% if filters.sentiment == 'neutral' %}selected{% endif %}>{% trans "Neutral" %}</option>
                            <option value="negative" {% if filters.sentiment == 'negative' %}selected{% endif %}>{% trans "Negative" %}</option>
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-bold text-navy mb-2">{% trans "Date From" %}</label>
                        <input type="date" name="date_from" class="w-full px-4 py-2.5 border-2 border-blue-100 rounded-xl text-navy focus:ring-2 focus:ring-blue focus:border-transparent transition" value="{{ filters.date_from|default:'' }}">
                    </div>
                    <div>
                        <label class="block text-sm font-bold text-navy mb-2">{% trans "Date To" %}</label>
                        <input type="date" name="date_to" class="w-full px-4 py-2.5 border-2 border-blue-100 rounded-xl text-navy focus:ring-2 focus:ring-blue focus:border-transparent transition" value="{{ filters.date_to|default:'' }}">
                    </div>
                    <div class="flex items-end">
                        <button type="submit" class="w-full inline-flex items-center justify-center gap-2 px-4 py-2.5 bg-gradient-to-r from-blue to-navy text-white rounded-xl font-semibold hover:from-navy hover:to-blue transition shadow-lg shadow-blue-200">
                            <i data-lucide="search" class="w-4 h-4"></i> {% trans "Filter" %}
                        </button>
                    </div>
                </form>
            </div>
        </details>
    </div>

    <!-- Comments Feed -->
    <div class="section-card">
        <div class="section-header">
            <i data-lucide="message-circle" class="w-5 h-5 text-navy"></i>
            <h3 class="text-lg font-bold text-navy">{% trans "Comments Feed" %}</h3>
        </div>
        <div class="p-6">
            <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                {% for comment in comments %}
                <div class="bg-white rounded-2xl shadow-sm border border-blue-100 hover:shadow-lg hover:-translate-y-1 transition-all duration-200 overflow-hidden">
                    <div class="p-6">
                        <div class="flex flex-wrap gap-2 mb-4">
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-gradient-to-r from-blue to-navy text-white">
                                {{ comment.get_platform_display }}
                            </span>
                            {% if comment.rating %}
                            <span class="inline-flex items-center gap-1 px-3 py-1 rounded-full text-xs font-bold bg-yellow-100 text-yellow-700">
                                <i data-lucide="star" class="w-3 h-3 fill-current"></i> {{ comment.rating }}/5
                            </span>
                            {% endif %}
                            {% if comment.ai_analysis %}
                                {% with sentiment=comment.ai_analysis.sentiment.classification.en %}
                                    {% if sentiment == 'positive' %}
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-emerald-100 text-emerald-700">
                                        <i data-lucide="smile" class="w-3 h-3"></i> {% trans "Positive" %}
                                    </span>
                                    {% elif sentiment == 'negative' %}
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-red-100 text-red-700">
                                        <i data-lucide="frown" class="w-3 h-3"></i> {% trans "Negative" %}
                                    </span>
                                    {% else %}
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-slate-100 text-slate-700">
                                        <i data-lucide="meh" class="w-3 h-3"></i> {% trans "Neutral" %}
                                    </span>
                                    {% endif %}
                                {% endwith %}
                            {% else %}
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-slate-100 text-slate-700">
                                {% trans "Not Analyzed" %}
                            </span>
                            {% endif %}
                        </div>

                        {% if comment.author %}
                        <p class="text-sm font-bold text-navy mb-3">@{{ comment.author }}</p>
                        {% endif %}

                        <p class="text-slate mb-4 line-clamp-3">{{ comment.comments|truncatewords:30 }}</p>

                        <div class="flex items-center justify-between pt-4 border-t border-blue-50">
                            <div class="flex items-center gap-4">
                                <div class="flex items-center gap-2 text-slate text-sm">
                                    <i data-lucide="heart" class="w-4 h-4 text-red-500"></i>
                                    <span class="font-medium">{{ comment.like_count }}</span>
                                </div>
                                <div class="flex items-center gap-2 text-slate text-sm">
                                    <i data-lucide="message-square" class="w-4 h-4 text-blue-500"></i>
                                    <span class="font-medium">{{ comment.reply_count }}</span>
                                </div>
                            </div>
                            <div class="text-right">
                                <a href="{% url 'social:social_comment_detail' comment.pk %}" class="inline-flex items-center gap-2 px-4 py-2 bg-blue-50 text-blue-700 rounded-xl font-semibold hover:bg-blue-100 transition text-sm">
                                    {% trans "View Details" %}
                                    <i data-lucide="arrow-right" class="w-4 h-4"></i>
                                </a>
                            </div>
                        </div>

                        <div class="mt-3 text-right">
                            <p class="text-xs text-slate">
                                {% if comment.published_at %}
                                    {{ comment.published_at|date:"M d, Y H:i" }}
                                {% else %}
                                    {{ comment.scraped_at|date:"M d, Y H:i" }}
                                {% endif %}
                            </p>
                        </div>
                    </div>
                </div>
                {% empty %}
                <div class="col-span-full">
                    <div class="flex flex-col items-center py-12">
                        <div class="w-20 h-20 bg-gradient-to-br from-blue-100 to-purple-100 rounded-full flex items-center justify-center mb-4">
                            <i data-lucide="message-circle" class="w-10 h-10 text-blue-500"></i>
                        </div>
                        <p class="text-slate font-medium">{% trans "No comments found" %}</p>
                    </div>
                </div>
                {% endfor %}
            </div>
        </div>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <div class="flex items-center justify-center gap-2 mt-8">
        {% if page_obj.has_previous %}
        <a href="?page={{ page_obj.previous_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}"
           class="inline-flex items-center gap-2 px-4 py-2 bg-white border-2 border-blue-200 text-blue-700 rounded-xl font-semibold hover:bg-blue-50 transition">
            <i data-lucide="chevron-left" class="w-4 h-4"></i>
        </a>
        {% endif %}

        <span class="inline-flex items-center px-4 py-2 bg-gradient-to-r from-blue to-navy text-white rounded-xl font-semibold">
            {{ page_obj.number }} / {{ page_obj.paginator.num_pages }}
        </span>

        {% if page_obj.has_next %}
        <a href="?page={{ page_obj.next_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}"
           class="inline-flex items-center gap-2 px-4 py-2 bg-white border-2 border-blue-200 text-blue-700 rounded-xl font-semibold hover:bg-blue-50 transition">
            <i data-lucide="chevron-right" class="w-4 h-4"></i>
        </a>
        {% endif %}
    </div>
    {% endif %}
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    lucide.createIcons();
});
</script>
{% endblock %}
