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

{% block title %}{{ platform_display }} - {% 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);
}

.form-section {
    background: #fff;
    border: 2px solid #e2e8f0;
    border-radius: 1rem;
    padding: 1.5rem;
    margin-bottom: 1.5rem;
    transition: all 0.3s ease;
}
.form-section:hover {
    border-color: #005696;
    box-shadow: 0 4px 12px rgba(0, 86, 150, 0.1);
}

.form-label {
    display: block;
    font-size: 0.875rem;
    font-weight: 600;
    color: #1e293b;
    margin-bottom: 0.5rem;
}

.form-control, .form-select {
    width: 100%;
    padding: 0.75rem 1rem;
    border: 2px solid #e2e8f0;
    border-radius: 0.75rem;
    font-size: 0.875rem;
    transition: all 0.2s ease;
}
.form-control:focus, .form-select:focus {
    outline: none;
    border-color: #005696;
    box-shadow: 0 0 0 3px rgba(0, 86, 150, 0.1);
}

.hh-btn-primary {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1.5rem;
    background: #005696;
    color: white;
    border-radius: 0.75rem;
    font-weight: 600;
    transition: all 0.2s ease;
    border: none;
    cursor: pointer;
}
.hh-btn-primary:hover {
    background: #007bbd;
}

.hh-btn-secondary {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1.5rem;
    background: white;
    color: #64748b;
    border: 2px solid #e2e8f0;
    border-radius: 0.75rem;
    font-weight: 600;
    transition: all 0.2s ease;
}
.hh-btn-secondary:hover {
    background: #f1f5f9;
    border-color: #005696;
}
</style>
{% endblock %}

{% block content %}
<div class="p-6">
    <!-- Breadcrumb -->
    <nav class="mb-4">
        <ol class="flex items-center gap-2 text-sm text-slate">
            <li><a href="{% url 'social:social_comment_list' %}" class="text-blue hover:text-navy font-medium">{% trans "Social Media" %}</a></li>
            <li><i data-lucide="chevron-right" class="w-4 h-4 text-slate"></i></li>
            <li class="text-navy font-semibold">{{ platform_display }}</li>
        </ol>
    </nav>

    <!-- 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="flex items-center justify-center w-14 h-14 bg-white/20 rounded-2xl">
                    {% social_icon platform %}
                </div>
                <div>
                    <h1 class="text-2xl font-bold">{{ platform_display }}</h1>
                    <p class="text-blue-100 text-sm">{% trans "Monitor and analyze" %} {{ platform_display }} {% trans "comments" %}</p>
                </div>
            </div>
            <div class="flex gap-3">
                <a href="{% url 'social:social_analytics' %}?platform={{ platform }}" class="hh-btn hh-btn-secondary bg-white/10 border-white/30 text-white hover:bg-white hover:text-navy">
                    <i data-lucide="bar-chart-3" class="w-4 h-4"></i>
                    {% trans "View Analytics" %}
                </a>
                <a href="{% url 'social:social_comment_list' %}" class="hh-btn hh-btn-primary bg-white text-navy hover:bg-blue-50">
                    <i data-lucide="grid-3x3" class="w-4 h-4"></i>
                    {% trans "All Platforms" %}
                </a>
            </div>
        </div>
    </div>

    <!-- Platform Statistics -->
    <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-4 mb-8">
        <div class="form-section text-center mb-0">
            <p class="text-xs font-semibold text-slate uppercase mb-1">{% trans "Total" %}</p>
            <p class="text-3xl font-bold text-navy">{{ stats.total }}</p>
        </div>
        <div class="form-section text-center mb-0 border-emerald-200">
            <p class="text-xs font-semibold text-slate uppercase mb-1">{% trans "Positive" %}</p>
            <p class="text-3xl font-bold text-emerald-600">{{ stats.positive }}</p>
        </div>
        <div class="form-section text-center mb-0">
            <p class="text-xs font-semibold text-slate uppercase mb-1">{% trans "Neutral" %}</p>
            <p class="text-3xl font-bold text-slate-600">{{ stats.neutral }}</p>
        </div>
        <div class="form-section text-center mb-0 border-red-200">
            <p class="text-xs font-semibold text-slate uppercase mb-1">{% trans "Negative" %}</p>
            <p class="text-3xl font-bold text-red-600">{{ stats.negative }}</p>
        </div>
        <div class="form-section text-center mb-0">
            <p class="text-xs font-semibold text-slate uppercase mb-1">{% trans "Avg Sentiment" %}</p>
            <p class="text-3xl font-bold text-blue-600">{{ stats.avg_sentiment|floatformat:2 }}</p>
        </div>
        <div class="form-section text-center mb-0 border-orange-200">
            <p class="text-xs font-semibold text-slate uppercase mb-1">{% trans "Engagement" %}</p>
            <p class="text-3xl font-bold text-orange-600">{{ stats.total_likes|add:stats.total_replies }}</p>
        </div>
    </div>

    <!-- Time Filter & Search -->
    <div class="form-section p-0 overflow-hidden mb-8">
        <form method="get" class="p-6">
            <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-7 gap-4">
                <div>
                    <label class="form-label">{% trans "Time Period" %}</label>
                    <select name="time_filter" class="form-control" onchange="this.form.submit()">
                        <option value="all" {% if time_filter == 'all' %}selected{% endif %}>{% trans "All Time" %}</option>
                        <option value="today" {% if time_filter == 'today' %}selected{% endif %}>{% trans "Today" %}</option>
                        <option value="week" {% if time_filter == 'week' %}selected{% endif %}>{% trans "This Week" %}</option>
                        <option value="month" {% if time_filter == 'month' %}selected{% endif %}>{% trans "This Month" %}</option>
                    </select>
                </div>
                <div>
                    <label class="form-label">{% trans "Sentiment" %}</label>
                    <select name="sentiment" class="form-control">
                        <option value="">{% trans "All" %}</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="form-label">{% trans "Date From" %}</label>
                    <input type="date" name="date_from" class="form-control" value="{{ filters.date_from|default:'' }}">
                </div>
                <div>
                    <label class="form-label">{% trans "Date To" %}</label>
                    <input type="date" name="date_to" class="form-control" value="{{ filters.date_to|default:'' }}">
                </div>
                <div class="col-span-2">
                    <label class="form-label">{% trans "Search" %}</label>
                    <div class="relative">
                        <input type="text" name="search" class="form-control pl-10" placeholder="{% trans 'Search comments...' %}" value="{{ filters.search|default:'' }}">
                        <i data-lucide="search" class="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-slate"></i>
                    </div>
                </div>
                <div class="flex items-end">
                    <button type="submit" class="hh-btn hh-btn-primary w-full justify-center">
                        <i data-lucide="search" class="w-4 h-4"></i>
                        {% trans "Filter" %}
                    </button>
                </div>
            </div>
        </form>
    </div>

    <!-- Comments Feed -->
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
        {% for comment in comments %}
        <div class="form-section mb-0 border-l-4" style="border-left-color: {{ platform_color }};">
            <div class="flex flex-wrap justify-between items-start gap-3 mb-4">
                <div class="flex flex-wrap gap-2">
                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold text-white" style="background-color: {{ platform_color }};">
                        {{ 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|star_rating }} {{ 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>
                <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>

            {% 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>

            {% if comment.ai_analysis and comment.ai_analysis.keywords.en %}
            <div class="flex flex-wrap gap-2 mb-4">
                {% for keyword in comment.ai_analysis.keywords.en|slice:":5" %}
                <span class="inline-flex items-center px-2 py-1 bg-slate-100 text-slate-700 rounded-lg text-xs font-medium">
                    {{ keyword }}
                </span>
                {% endfor %}
            </div>
            {% endif %}

            <div class="flex items-center justify-between pt-4 border-t border-slate-200">
                <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>
                <a href="{% url 'social:social_comment_detail' comment.pk %}" class="hh-btn hh-btn-primary text-sm">
                    {% trans "View Details" %}
                    <i data-lucide="arrow-right" class="w-4 h-4"></i>
                </a>
            </div>
        </div>
        {% empty %}
        <div class="col-span-full">
            <div class="form-section text-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 mx-auto 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 for this platform" %}</p>
            </div>
        </div>
        {% endfor %}
    </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="hh-btn hh-btn-secondary">
            <i data-lucide="chevron-left" class="w-4 h-4"></i>
        </a>
        {% endif %}

        <span class="hh-btn hh-btn-primary">
            {{ 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="hh-btn hh-btn-secondary">
            <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 %}
