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

{% block title %}Comment #{{ comment.id }} - {% trans "Social Media Monitoring" %} - PX360{% endblock %}

{% block content %}
<div class="p-6 md:p-8 bg-gradient-to-br from-light to-blue-50 min-h-screen">
    <!-- Breadcrumb -->
    <nav class="mb-6">
        <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><a href="{% url 'social:social_platform' comment.platform %}" class="text-blue hover:text-navy font-medium">{{ comment.get_platform_display }}</a></li>
            <li><i data-lucide="chevron-right" class="w-4 h-4 text-slate"></i></li>
            <li class="text-navy font-semibold">{% trans "Comment Details" %}</li>
        </ol>
    </nav>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <!-- Comment Card -->
        <div class="lg:col-span-2 space-y-6">
            <!-- Main Comment Card -->
            <div class="bg-white rounded-2xl shadow-sm border border-blue-100 overflow-hidden">
                <div class="p-6 border-b border-blue-100 bg-gradient-to-r from-blue-50 to-transparent">
                    <div class="flex flex-wrap justify-between items-start gap-3">
                        <div class="flex flex-wrap gap-2">
                            <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 %}
                            {% 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>
                </div>
                <div class="p-6">
                    {% if comment.author %}
                    <p class="text-lg font-bold text-navy mb-4">@{{ comment.author }}</p>
                    {% endif %}

                    <p class="text-slate text-lg leading-relaxed mb-6">{{ comment.comments }}</p>

                    <div class="grid grid-cols-3 gap-4 p-4 bg-blue-50 rounded-xl mb-4">
                        <div class="text-center">
                            <div class="w-12 h-12 bg-red-100 rounded-xl flex items-center justify-center mx-auto mb-2">
                                <i data-lucide="heart" class="w-6 h-6 text-red-500"></i>
                            </div>
                            <p class="text-2xl font-bold text-navy">{{ comment.like_count }}</p>
                            <p class="text-xs text-slate">{% trans "Likes" %}</p>
                        </div>
                        <div class="text-center">
                            <div class="w-12 h-12 bg-blue-100 rounded-xl flex items-center justify-center mx-auto mb-2">
                                <i data-lucide="message-square" class="w-6 h-6 text-blue-500"></i>
                            </div>
                            <p class="text-2xl font-bold text-navy">{{ comment.reply_count }}</p>
                            <p class="text-xs text-slate">{% trans "Replies" %}</p>
                        </div>
                        <div class="text-center">
                            <div class="w-12 h-12 bg-slate-100 rounded-xl flex items-center justify-center mx-auto mb-2">
                                <i data-lucide="clock" class="w-6 h-6 text-slate-500"></i>
                            </div>
                            <p class="text-2xl font-bold text-navy">{{ comment.scraped_at|timesince }}</p>
                            <p class="text-xs text-slate">{% trans "Ago" %}</p>
                        </div>
                    </div>

                    {% if comment.post_url %}
                    <a href="{{ comment.post_url }}" target="_blank" class="inline-flex items-center gap-2 px-5 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="external-link" class="w-4 h-4"></i>
                        {% trans "View Original Post" %}
                    </a>
                    {% endif %}
                </div>
            </div>

            <!-- AI Analysis -->
            {% if comment.ai_analysis %}
            <div class="bg-white rounded-2xl shadow-sm border border-blue-100 overflow-hidden">
                <div class="p-6 border-b border-blue-100 bg-gradient-to-r from-purple-50 to-transparent">
                    <h3 class="text-lg font-bold text-navy flex items-center gap-2">
                        <i data-lucide="sparkles" class="w-5 h-5 text-purple-500"></i>
                        {% trans "AI Analysis" %}
                    </h3>
                </div>
                <div class="p-6">
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                        <div class="p-4 bg-gradient-to-br from-emerald-50 to-green-50 rounded-xl border border-emerald-100">
                            <p class="text-sm font-semibold text-slate uppercase mb-2">{% trans "Sentiment" %}</p>
                            <p class="text-3xl font-bold {% if comment.ai_analysis.sentiment.classification.en == 'positive' %}text-emerald-600{% elif comment.ai_analysis.sentiment.classification.en == 'negative' %}text-red-600{% else %}text-slate-600{% endif %}">
                                {{ comment.ai_analysis.sentiment.classification.en|title }}
                            </p>
                            <p class="text-sm text-slate mt-1">{{ comment.ai_analysis.sentiment.classification.ar }}</p>
                        </div>
                        <div class="p-4 bg-gradient-to-br from-blue-50 to-indigo-50 rounded-xl border border-blue-100">
                            <p class="text-sm font-semibold text-slate uppercase mb-2">{% trans "Sentiment Score" %}</p>
                            <p class="text-3xl font-bold text-blue-600">{{ comment.ai_analysis.sentiment.score|floatformat:2 }}</p>
                            <p class="text-sm text-slate mt-1">{% trans "Confidence" %}: {{ comment.ai_analysis.sentiment.confidence|floatformat:2 }}</p>
                        </div>
                    </div>

                    {% if comment.ai_analysis.summaries.en %}
                    <div class="mb-6">
                        <h4 class="text-sm font-bold text-navy mb-2">{% trans "Summary (English)" %}</h4>
                        <p class="text-slate p-4 bg-blue-50 rounded-xl">{{ comment.ai_analysis.summaries.en }}</p>
                    </div>
                    {% endif %}

                    {% if comment.ai_analysis.summaries.ar %}
                    <div class="mb-6">
                        <h4 class="text-sm font-bold text-navy mb-2">{% trans "الملخص (Arabic)" %}</h4>
                        <p class="text-slate p-4 bg-blue-50 rounded-xl" dir="rtl">{{ comment.ai_analysis.summaries.ar }}</p>
                    </div>
                    {% endif %}

                    {% if comment.ai_analysis.keywords.en %}
                    <div class="mb-6">
                        <h4 class="text-sm font-bold text-navy mb-2">{% trans "Keywords" %}</h4>
                        <div class="flex flex-wrap gap-2">
                            {% for keyword in comment.ai_analysis.keywords.en %}
                            <span class="inline-flex items-center px-3 py-1.5 bg-blue-50 text-blue-700 rounded-xl text-sm font-medium">
                                {{ keyword }}
                            </span>
                            {% endfor %}
                        </div>
                    </div>
                    {% endif %}

                    {% if comment.ai_analysis.topics.en %}
                    <div>
                        <h4 class="text-sm font-bold text-navy mb-2">{% trans "Topics" %}</h4>
                        <div class="flex flex-wrap gap-2">
                            {% for topic in comment.ai_analysis.topics.en %}
                            <span class="inline-flex items-center px-3 py-1.5 bg-purple-50 text-purple-700 rounded-xl text-sm font-medium">
                                <i data-lucide="hash" class="w-3 h-3 mr-1"></i>
                                {{ topic }}
                            </span>
                            {% endfor %}
                        </div>
                    </div>
                    {% endif %}
                </div>
            </div>
            {% endif %}

            <!-- Raw Data -->
            <div class="bg-white rounded-2xl shadow-sm border border-blue-100 overflow-hidden">
                <details class="group">
                    <summary class="flex items-center justify-between p-6 cursor-pointer list-none">
                        <h3 class="text-lg font-bold text-navy flex items-center gap-2">
                            <i data-lucide="code" class="w-5 h-5 text-slate"></i>
                            {% trans "Raw Data" %}
                        </h3>
                        <i data-lucide="chevron-down" class="w-5 h-5 text-slate group-open:rotate-180 transition-transform"></i>
                    </summary>
                    <div class="px-6 pb-6">
                        <pre class="bg-slate-50 p-4 rounded-xl text-xs text-slate overflow-x-auto">{{ comment.raw_data }}</pre>
                    </div>
                </details>
            </div>
        </div>

        <!-- Sidebar -->
        <div class="space-y-6">
            <!-- Comment Info -->
            <div class="bg-white rounded-2xl shadow-sm border border-blue-100 overflow-hidden">
                <div class="p-6 border-b border-blue-100 bg-gradient-to-r from-blue-50 to-transparent">
                    <h3 class="text-lg font-bold text-navy flex items-center gap-2">
                        <i data-lucide="info" class="w-5 h-5 text-blue"></i>
                        {% trans "Comment Info" %}
                    </h3>
                </div>
                <div class="p-6">
                    <dl class="space-y-4">
                        <div class="flex justify-between items-center pb-3 border-b border-blue-50">
                            <dt class="text-sm font-semibold text-slate">{% trans "Comment ID" %}</dt>
                            <dd class="text-sm font-bold text-navy">{{ comment.comment_id }}</dd>
                        </div>
                        <div class="flex justify-between items-center pb-3 border-b border-blue-50">
                            <dt class="text-sm font-semibold text-slate">{% trans "Post ID" %}</dt>
                            <dd class="text-sm font-bold text-navy">{{ comment.post_id }}</dd>
                        </div>
                        <div class="flex justify-between items-center pb-3 border-b border-blue-50">
                            <dt class="text-sm font-semibold text-slate">{% trans "Platform" %}</dt>
                            <dd class="text-sm font-bold text-navy">{{ comment.get_platform_display }}</dd>
                        </div>
                        <div class="flex justify-between items-center pb-3 border-b border-blue-50">
                            <dt class="text-sm font-semibold text-slate">{% trans "Published" %}</dt>
                            <dd class="text-sm font-bold text-navy">
                                {% if comment.published_at %}
                                    {{ comment.published_at|date:"M d, Y" }}
                                {% else %}
                                    <span class="text-slate">-</span>
                                {% endif %}
                            </dd>
                        </div>
                        <div class="flex justify-between items-center">
                            <dt class="text-sm font-semibold text-slate">{% trans "Scraped" %}</dt>
                            <dd class="text-sm font-bold text-navy">{{ comment.scraped_at|date:"M d, Y" }}</dd>
                        </div>
                    </dl>
                </div>
            </div>

            <!-- Entities -->
            {% if comment.ai_analysis.entities %}
            <div class="bg-white rounded-2xl shadow-sm border border-blue-100 overflow-hidden">
                <div class="p-6 border-b border-blue-100 bg-gradient-to-r from-purple-50 to-transparent">
                    <h3 class="text-lg font-bold text-navy flex items-center gap-2">
                        <i data-lucide="tags" class="w-5 h-5 text-purple-500"></i>
                        {% trans "Entities" %}
                    </h3>
                </div>
                <div class="p-6">
                    <div class="space-y-2">
                        {% for entity in comment.ai_analysis.entities|slice:":10" %}
                        <div class="flex items-center justify-between p-3 bg-purple-50 rounded-xl">
                            <span class="text-sm font-medium text-navy">{% if entity.text %}{{ entity.text }}{% else %}{{ entity }}{% endif %}</span>
                            {% if entity.type %}
                            <span class="inline-flex items-center px-2 py-1 bg-white text-slate-700 rounded-lg text-xs font-bold">
                                {{ entity.type }}
                            </span>
                            {% endif %}
                        </div>
                        {% endfor %}
                    </div>
                </div>
            </div>
            {% endif %}

            <!-- Actions -->
            <div class="bg-white rounded-2xl shadow-sm border border-blue-100 overflow-hidden">
                <div class="p-6 border-b border-blue-100 bg-gradient-to-r from-blue-50 to-transparent">
                    <h3 class="text-lg font-bold text-navy flex items-center gap-2">
                        <i data-lucide="zap" class="w-5 h-5 text-blue"></i>
                        {% trans "Actions" %}
                    </h3>
                </div>
                <div class="p-6">
                    <div class="space-y-3">
                        <button class="w-full inline-flex items-center justify-center gap-2 px-4 py-3 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="plus-circle" class="w-4 h-4"></i>
                            {% trans "Create PX Action" %}
                        </button>
                        <button class="w-full inline-flex items-center justify-center gap-2 px-4 py-3 bg-emerald-500 text-white rounded-xl font-semibold hover:bg-emerald-600 transition">
                            <i data-lucide="check-circle" class="w-4 h-4"></i>
                            {% trans "Mark as Reviewed" %}
                        </button>
                        <button class="w-full inline-flex items-center justify-center gap-2 px-4 py-3 bg-orange-500 text-white rounded-xl font-semibold hover:bg-orange-600 transition">
                            <i data-lucide="flag" class="w-4 h-4"></i>
                            {% trans "Flag for Follow-up" %}
                        </button>
                        <button class="w-full inline-flex items-center justify-center gap-2 px-4 py-3 bg-white border-2 border-red-200 text-red-600 rounded-xl font-semibold hover:bg-red-50 transition">
                            <i data-lucide="trash-2" class="w-4 h-4"></i>
                            {% trans "Delete Comment" %}
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

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