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

{% block title %}{% trans "Appreciations" %} - 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 %}
<!-- Page Header -->
<div class="page-header-gradient">
    <div class="flex justify-between items-center">
        <div>
            <h1 class="text-2xl font-bold flex items-center gap-3">
                <i data-lucide="heart" class="w-7 h-7"></i>
                {% trans "Appreciations" %}
            </h1>
            <p class="text-sm opacity-90 mt-1">{% trans "Manage and review patient appreciation submissions" %}</p>
        </div>
        <div class="flex items-center gap-3">
            <a href="{% url 'core:public_submit_landing' %}" target="_blank" class="bg-white/20 text-white border-2 border-white/50 px-5 py-2.5 rounded-xl text-sm font-bold hover:bg-white/30 flex items-center gap-2 transition">
                <i data-lucide="plus" class="w-4 h-4"></i> {% trans "New Appreciation" %}
            </a>
            <div class="bg-white/20 rounded-xl px-5 py-3 text-center">
                <p class="text-3xl font-black">{{ stats.draft }}</p>
                <p class="text-xs font-semibold opacity-80">{% trans "Pending" %}</p>
            </div>
        </div>
    </div>
</div>

<!-- Stats Cards -->
<div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">
    <div class="section-card p-5">
        <div class="flex items-center gap-4">
            <div class="section-icon bg-slate-100">
                <i data-lucide="inbox" class="w-6 h-6 text-slate-600"></i>
            </div>
            <div>
                <p class="text-xs font-bold text-slate uppercase tracking-wider mb-1">{% trans "Total" %}</p>
                <p class="text-2xl font-black text-navy">{{ stats.total }}</p>
            </div>
        </div>
    </div>
    <div class="section-card p-5">
        <div class="flex items-center gap-4">
            <div class="section-icon bg-amber-100">
                <i data-lucide="clock" class="w-6 h-6 text-amber-600"></i>
            </div>
            <div>
                <p class="text-xs font-bold text-slate uppercase tracking-wider mb-1">{% trans "Draft" %}</p>
                <p class="text-2xl font-black text-navy">{{ stats.draft }}</p>
            </div>
        </div>
    </div>
    <div class="section-card p-5">
        <div class="flex items-center gap-4">
            <div class="section-icon bg-blue-100">
                <i data-lucide="zap" class="w-6 h-6 text-blue-600"></i>
            </div>
            <div>
                <p class="text-xs font-bold text-slate uppercase tracking-wider mb-1">{% trans "Activated" %}</p>
                <p class="text-2xl font-black text-navy">{{ stats.activated }}</p>
            </div>
        </div>
    </div>
    <div class="section-card p-5">
        <div class="flex items-center gap-4">
            <div class="section-icon bg-green-100">
                <i data-lucide="check-circle" class="w-6 h-6 text-green-600"></i>
            </div>
            <div>
                <p class="text-xs font-bold text-slate uppercase tracking-wider mb-1">{% trans "Sent" %}</p>
                <p class="text-2xl font-black text-navy">{{ stats.sent }}</p>
            </div>
        </div>
    </div>
</div>

<!-- Quick Links -->
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-6">
    <a href="{% url 'appreciation:leaderboard_view' %}" class="group">
        <div class="section-card p-5 hover:border-amber-400">
            <div class="flex items-center gap-4">
                <div class="section-icon bg-amber-100 group-hover:bg-amber-200 transition">
                    <i data-lucide="trophy" class="w-6 h-6 text-amber-600"></i>
                </div>
                <div>
                    <h3 class="font-bold text-navy">{% trans "Leaderboard" %}</h3>
                    <p class="text-sm text-slate">{% trans "See top performers" %}</p>
                </div>
            </div>
        </div>
    </a>
    <a href="{% url 'appreciation:my_badges_view' %}" class="group">
        <div class="section-card p-5 hover:border-blue-400">
            <div class="flex items-center gap-4">
                <div class="section-icon bg-blue-100 group-hover:bg-blue-200 transition">
                    <i data-lucide="award" class="w-6 h-6 text-blue-600"></i>
                </div>
                <div>
                    <h3 class="font-bold text-navy">{% trans "My Badges" %}</h3>
                    <p class="text-sm text-slate">{% trans "View earned badges" %}</p>
                </div>
            </div>
        </div>
    </a>
</div>

<!-- Filters -->
<div class="section-card mb-6">
    <div class="section-header">
        <div class="section-icon bg-slate-100">
            <i data-lucide="filter" class="w-5 h-5 text-slate-600"></i>
        </div>
        <h3 class="font-bold text-navy">{% trans "Filters" %}</h3>
    </div>
    <div class="p-4">
        <form method="get" class="flex flex-wrap gap-3 items-end">
            <div class="flex-1 min-w-[200px]">
                <label class="block text-xs font-bold text-slate uppercase tracking-wider mb-1.5">{% trans "Search" %}</label>
                <input type="text" name="search" value="{{ search_query|default:'' }}" placeholder="{% trans 'Search messages...' %}" class="w-full px-3 py-2.5 border-2 border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy/20 focus:border-navy outline-none">
            </div>
            <div class="min-w-[180px]">
                <label class="block text-xs font-bold text-slate uppercase tracking-wider mb-1.5">{% trans "Status" %}</label>
                <select name="status" class="w-full px-3 py-2.5 border-2 border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy/20 focus:border-navy outline-none">
                    <option value="">{% trans "All Status" %}</option>
                    <option value="draft" {% if status_filter == 'draft' %}selected{% endif %}>{% trans "Draft" %}</option>
                    <option value="activated" {% if status_filter == 'activated' %}selected{% endif %}>{% trans "Activated" %}</option>
                    <option value="ai_analyzed" {% if status_filter == 'ai_analyzed' %}selected{% endif %}>{% trans "AI Analyzed" %}</option>
                    <option value="sent" {% if status_filter == 'sent' %}selected{% endif %}>{% trans "Sent" %}</option>
                    <option value="acknowledged" {% if status_filter == 'acknowledged' %}selected{% endif %}>{% trans "Acknowledged" %}</option>
                </select>
            </div>
            <button type="submit" class="px-5 py-2.5 bg-navy text-white rounded-xl text-sm font-bold hover:bg-blue transition flex items-center gap-2">
                <i data-lucide="search" class="w-4 h-4"></i> {% trans "Search" %}
            </button>
            <a href="{% url 'appreciation:appreciation_list' %}" class="px-5 py-2.5 border-2 border-slate-200 rounded-xl text-sm font-bold text-slate hover:bg-slate-50 transition">{% trans "Clear" %}</a>
        </form>
    </div>
</div>

<!-- Table -->
<div class="section-card p-0">
    <table class="w-full text-left border-collapse">
        <thead class="bg-slate-50 border-b uppercase text-[10px] font-bold text-slate tracking-wider">
            <tr>
                <th class="px-6 py-4">{% trans "From" %}</th>
                <th class="px-6 py-4">{% trans "Message" %}</th>
                <th class="px-6 py-4">{% trans "Staff" %}</th>
                <th class="px-6 py-4">{% trans "Department" %}</th>
                <th class="px-6 py-4">{% trans "Category" %}</th>
                <th class="px-6 py-4 text-center">{% trans "Status" %}</th>
                <th class="px-6 py-4">{% trans "Date" %}</th>
                <th class="px-6 py-4 text-right">{% trans "Actions" %}</th>
            </tr>
        </thead>
        <tbody class="text-sm divide-y divide-slate-100">
            {% for apr in appreciations %}
            <tr class="hover:bg-light/30 transition-colors cursor-pointer group" onclick="window.location.href='{% url 'appreciation:appreciation_detail' pk=apr.pk %}'">
                <td class="px-6 py-4">
                    {% if apr.metadata.submitted_by_name %}
                    <span class="font-semibold text-slate-800">{{ apr.metadata.submitted_by_name }}</span>
                    {% elif apr.sender %}
                    <span class="font-semibold text-slate-800">{{ apr.sender.get_full_name }}</span>
                    {% elif apr.is_anonymous %}
                    <span class="text-slate-400 italic">{% trans "Anonymous" %}</span>
                    {% else %}
                    <span class="text-slate-400">-</span>
                    {% endif %}
                </td>
                <td class="px-6 py-4 max-w-xs">
                    <span class="text-slate-700 text-sm truncate block">{{ apr.message_en|truncatewords:8 }}</span>
                </td>
                <td class="px-6 py-4">
                    {% if apr.metadata.staff_name_mentioned %}
                    <span class="text-slate-700">{{ apr.metadata.staff_name_mentioned }}</span>
                    {% else %}
                    <span class="text-slate-400">-</span>
                    {% endif %}
                </td>
                <td class="px-6 py-4 text-slate-600">{{ apr.department.name|default:"-" }}</td>
                <td class="px-6 py-4">
                    {% if apr.category %}
                    <span class="bg-blue-100 text-blue-700 px-2 py-0.5 rounded text-[10px] font-bold">{{ apr.category.name_en }}</span>
                    {% else %}
                    <span class="text-slate-400">-</span>
                    {% endif %}
                </td>
                <td class="px-6 py-4 text-center">
                    <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase
                        {% if apr.status == 'draft' %}bg-amber-100 text-amber-700
                        {% elif apr.status == 'activated' %}bg-blue-100 text-blue-700
                        {% elif apr.status == 'ai_analyzed' %}bg-purple-100 text-purple-700
                        {% elif apr.status == 'sent' %}bg-green-100 text-green-700
                        {% elif apr.status == 'acknowledged' %}bg-emerald-100 text-emerald-700
                        {% else %}bg-slate-100 text-slate-700{% endif %}">
                        {{ apr.get_status_display }}
                    </span>
                </td>
                <td class="px-6 py-4 text-slate-600 whitespace-nowrap">{{ apr.created_at|date:"M d, Y" }}</td>
                <td class="px-6 py-4 text-right">
                    <div class="flex justify-end gap-2 opacity-0 group-hover:opacity-100 transition-opacity">
                        <a href="{% url 'appreciation:appreciation_detail' pk=apr.pk %}" class="p-1.5 text-navy hover:bg-white rounded border border-transparent hover:border-slate-200" title="{% trans 'View' %}">
                            <i data-lucide="eye" class="w-4 h-4"></i>
                        </a>
                    </div>
                </td>
            </tr>
            {% empty %}
            <tr>
                <td colspan="8" class="px-6 py-12 text-center">
                    <i data-lucide="heart" class="w-12 h-12 mx-auto text-slate-300 mb-3"></i>
                    <p class="text-slate">{% trans "No appreciations found" %}</p>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <div class="bg-slate-50 px-8 py-4 flex items-center justify-between border-t">
        <div class="flex items-center gap-4">
            <span class="text-xs text-slate font-medium">
                {% trans "Showing" %} <span class="font-bold text-navy">{{ page_obj.start_index }}-{{ page_obj.end_index }}</span> {% trans "of" %} <span class="font-bold text-navy">{{ page_obj.paginator.count }}</span> {% trans "entries" %}
            </span>
        </div>
        <div class="flex items-center gap-2">
            {% if page_obj.has_previous %}
            <a href="?page={{ page_obj.previous_page_number }}&status={{ status_filter }}&search={{ search_query }}" class="px-3 py-1.5 border border-slate-200 rounded-lg text-xs font-bold text-slate hover:bg-white transition">
                <i data-lucide="chevron-left" class="w-4 h-4"></i>
            </a>
            {% endif %}

            {% for num in page_obj.paginator.page_range %}
                {% if page_obj.paginator.num_pages <= 7 or num == page_obj.number or num == 1 or num == page_obj.paginator.num_pages %}
                <a href="?page={{ num }}&status={{ status_filter }}&search={{ search_query }}"
                   class="px-3 py-1.5 rounded-lg text-xs font-bold transition {% if num == page_obj.number %}bg-navy text-white{% else %}border border-slate-200 text-slate hover:bg-white{% endif %}">
                    {{ num }}
                </a>
                {% elif num == page_obj.number|add:"-1" or num == page_obj.number|add:"1" %}
                {% elif num == 2 and page_obj.number > 4 %}
                <span class="px-1 text-slate-400">...</span>
                {% elif num == page_obj.paginator.num_pages|add:"-1" and page_obj.number < page_obj.paginator.num_pages|add:"-3" %}
                <span class="px-1 text-slate-400">...</span>
                {% endif %}
            {% endfor %}

            {% if page_obj.has_next %}
            <a href="?page={{ page_obj.next_page_number }}&status={{ status_filter }}&search={{ search_query }}" class="px-3 py-1.5 border border-slate-200 rounded-lg text-xs font-bold text-slate hover:bg-white transition">
                <i data-lucide="chevron-right" class="w-4 h-4"></i>
            </a>
            {% endif %}
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}
