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

{% block title %}{% trans "Sentiment Analysis Results" %}{% 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;
}

.progress-bar {
    height: 20px;
    background: #e2e8f0;
    border-radius: 10px;
    overflow: hidden;
}

.progress-bar-fill {
    height: 100%;
    background: linear-gradient(135deg, #005696, #007bbd);
    border-radius: 10px;
    transition: width 0.3s ease;
}
</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="brain" class="w-6 h-6 text-white"></i>
                </div>
                <div>
                    <h1 class="text-2xl font-bold">{% trans "Sentiment Analysis Results" %}</h1>
                    <p class="text-white/80 text-sm">{% trans "AI-powered sentiment analysis of text content" %}</p>
                </div>
            </div>
            <div class="flex gap-3">
                <a href="{% url 'ai_engine:analyze_text' %}" class="inline-flex items-center gap-2 px-5 py-2.5 bg-white text-navy rounded-xl font-bold hover:bg-gray-100 transition">
                    <i data-lucide="plus-circle" class="w-4 h-4"></i> {% trans "Analyze Text" %}
                </a>
                <a href="{% url 'ai_engine:sentiment_dashboard' %}" class="inline-flex items-center gap-2 px-5 py-2.5 bg-white/20 text-white rounded-xl font-bold hover:bg-white/30 transition">
                    <i data-lucide="bar-chart-3" class="w-4 h-4"></i> {% trans "Dashboard" %}
                </a>
            </div>
        </div>
    </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-blue-100">
                    <i data-lucide="bar-chart-2" class="w-5 h-5 text-blue-600"></i>
                </div>
                <div>
                    <p class="text-xs font-semibold text-slate uppercase">{% trans "Total Results" %}</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-emerald-100">
                    <i data-lucide="smile" class="w-5 h-5 text-emerald-600"></i>
                </div>
                <div>
                    <p class="text-xs font-semibold text-slate uppercase">{% trans "Positive" %}</p>
                    <p class="text-2xl font-bold text-emerald-600">{{ stats.positive }} <small class="text-sm text-slate">({{ stats.positive_pct }}%)</small></p>
                </div>
            </div>
        </div>
        <div class="section-card">
            <div class="section-header">
                <div class="section-icon bg-slate-100">
                    <i data-lucide="meh" class="w-5 h-5 text-slate-600"></i>
                </div>
                <div>
                    <p class="text-xs font-semibold text-slate uppercase">{% trans "Neutral" %}</p>
                    <p class="text-2xl font-bold text-slate-600">{{ stats.neutral }} <small class="text-sm text-slate">({{ stats.neutral_pct }}%)</small></p>
                </div>
            </div>
        </div>
        <div class="section-card">
            <div class="section-header">
                <div class="section-icon bg-red-100">
                    <i data-lucide="frown" class="w-5 h-5 text-red-600"></i>
                </div>
                <div>
                    <p class="text-xs font-semibold text-slate uppercase">{% trans "Negative" %}</p>
                    <p class="text-2xl font-bold text-red-600">{{ stats.negative }} <small class="text-sm text-slate">({{ stats.negative_pct }}%)</small></p>
                </div>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <div class="section-card mb-8">
        <div class="section-header">
            <div class="section-icon bg-navy/10">
                <i data-lucide="filter" class="w-5 h-5 text-navy"></i>
            </div>
            <h3 class="font-bold text-navy">{% trans "Filters" %}</h3>
        </div>
        <div class="p-6">
            <form method="get" class="grid grid-cols-1 md:grid-cols-4 gap-4">
                <div>
                    <label class="block text-sm font-bold text-navy mb-2">{% trans "Sentiment" %}</label>
                    {{ filter_form.sentiment }}
                </div>
                <div>
                    <label class="block text-sm font-bold text-navy mb-2">{% trans "Language" %}</label>
                    {{ filter_form.language }}
                </div>
                <div>
                    <label class="block text-sm font-bold text-navy mb-2">{% trans "AI Service" %}</label>
                    {{ filter_form.ai_service }}
                </div>
                <div>
                    <label class="block text-sm font-bold text-navy mb-2">{% trans "Min Confidence" %}</label>
                    {{ filter_form.min_confidence }}
                </div>
                <div class="md:col-span-3">
                    <label class="block text-sm font-bold text-navy mb-2">{% trans "Search" %}</label>
                    {{ filter_form.search }}
                </div>
                <div class="flex items-end gap-2">
                    <button type="submit" class="inline-flex items-center gap-2 px-5 py-2.5 bg-navy text-white rounded-xl font-bold hover:bg-blue transition">
                        <i data-lucide="filter" class="w-4 h-4"></i> {% trans "Apply Filters" %}
                    </button>
                    <a href="{% url 'ai_engine:sentiment_list' %}" class="inline-flex items-center gap-2 px-5 py-2.5 border-2 border-slate-200 text-slate rounded-xl font-bold hover:bg-slate-50 transition">
                        <i data-lucide="x-circle" class="w-4 h-4"></i> {% trans "Clear" %}
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Results Table -->
    <div class="section-card">
        <div class="section-header flex justify-between items-center">
            <div class="flex items-center gap-2">
                <div class="section-icon bg-navy/10">
                    <i data-lucide="list" class="w-5 h-5 text-navy"></i>
                </div>
                <h3 class="font-bold text-navy">{% trans "Results" %} ({{ page_obj.paginator.count }})</h3>
            </div>
            <div>
                <select class="px-4 py-2 border-2 border-slate-200 rounded-xl text-navy focus:ring-2 focus:ring-blue focus:border-transparent transition" onchange="window.location.href='?page_size=' + this.value + '{% for key, value in filters.items %}{% if key != 'page_size' %}&{{ key }}={{ value }}{% endif %}{% endfor %}'">
                    <option value="25" {% if request.GET.page_size == '25' %}selected{% endif %}>25 per page</option>
                    <option value="50" {% if request.GET.page_size == '50' %}selected{% endif %}>50 per page</option>
                    <option value="100" {% if request.GET.page_size == '100' %}selected{% endif %}>100 per page</option>
                </select>
            </div>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="bg-blue-50">
                    <tr>
                        <th class="px-6 py-4 text-left text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Text" %}</th>
                        <th class="px-6 py-4 text-left text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Sentiment" %}</th>
                        <th class="px-6 py-4 text-left text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Score" %}</th>
                        <th class="px-6 py-4 text-left text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Confidence" %}</th>
                        <th class="px-6 py-4 text-left text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Language" %}</th>
                        <th class="px-6 py-4 text-left text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Related To" %}</th>
                        <th class="px-6 py-4 text-left text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Date" %}</th>
                        <th class="px-6 py-4 text-left text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Actions" %}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-blue-50">
                    {% for result in results %}
                    <tr class="hover:bg-blue-50/50 transition">
                        <td class="px-6 py-4">
                            <div class="truncate max-w-xs" title="{{ result.text }}">
                                {{ result.text }}
                            </div>
                        </td>
                        <td class="px-6 py-4">
                            {% if result.sentiment == 'positive' %}
                                <span class="inline-flex items-center gap-1 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 result.sentiment == 'negative' %}
                                <span class="inline-flex items-center gap-1 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 gap-1 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 %}
                        </td>
                        <td class="px-6 py-4">
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-blue-100 text-blue-700">{{ result.sentiment_score|floatformat:2 }}</span>
                        </td>
                        <td class="px-6 py-4">
                            <div class="progress-bar">
                                <div class="progress-bar-fill" style="width: {{ result.confidence|floatformat:0 }}%"></div>
                            </div>
                            <span class="text-xs text-slate mt-1">{{ result.confidence|floatformat:0 }}%</span>
                        </td>
                        <td class="px-6 py-4">
                            {% if result.language == 'ar' %}
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-cyan-100 text-cyan-700">العربية</span>
                            {% else %}
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-blue-100 text-blue-700">English</span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4">
                            {% if result.content_type %}
                                <small class="text-slate">{{ result.content_type.model }}</small>
                            {% else %}
                                <small class="text-slate">-</small>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4">
                            <small class="text-slate">{{ result.created_at|date:"Y-m-d H:i" }}</small>
                        </td>
                        <td class="px-6 py-4">
                            <a href="{% url 'ai_engine:sentiment_detail' result.id %}" 
                               class="inline-flex items-center gap-1 px-3 py-2 text-navy bg-blue-50 rounded-lg hover:bg-blue-100 transition font-medium text-sm">
                                <i data-lucide="eye" class="w-4 h-4"></i>
                            </a>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="8" class="text-center py-12">
                            <i data-lucide="inbox" class="w-16 h-16 text-slate-300 mx-auto mb-4"></i>
                            <p class="text-slate font-medium">{% trans "No sentiment results found." %}</p>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        
        <!-- Pagination -->
        {% if page_obj.has_other_pages %}
        <div class="px-6 py-4 border-t border-slate-100">
            <nav class="flex justify-center">
                <ul class="flex gap-1">
                    {% if page_obj.has_previous %}
                        <li>
                            <a class="px-3 py-2 text-slate hover:bg-slate-100 rounded-lg transition" href="?page=1{% for key, value in filters.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">
                                <i data-lucide="chevrons-left" class="w-4 h-4"></i>
                            </a>
                        </li>
                        <li>
                            <a class="px-3 py-2 text-slate hover:bg-slate-100 rounded-lg transition" href="?page={{ page_obj.previous_page_number }}{% for key, value in filters.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">
                                <i data-lucide="chevron-left" class="w-4 h-4"></i>
                            </a>
                        </li>
                    {% endif %}
                    
                    <li>
                        <span class="px-4 py-2 bg-navy text-white rounded-lg font-bold">
                            {% trans "Page" %} {{ page_obj.number }} {% trans "of" %} {{ page_obj.paginator.num_pages }}
                        </span>
                    </li>
                    
                    {% if page_obj.has_next %}
                        <li>
                            <a class="px-3 py-2 text-slate hover:bg-slate-100 rounded-lg transition" href="?page={{ page_obj.next_page_number }}{% for key, value in filters.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">
                                <i data-lucide="chevron-right" class="w-4 h-4"></i>
                            </a>
                        </li>
                        <li>
                            <a class="px-3 py-2 text-slate hover:bg-slate-100 rounded-lg transition" href="?page={{ page_obj.paginator.num_pages }}{% for key, value in filters.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">
                                <i data-lucide="chevrons-right" class="w-4 h-4"></i>
                            </a>
                        </li>
                    {% endif %}
                </ul>
            </nav>
        </div>
        {% endif %}
    </div>
</div>

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