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

{% block title %}{% trans "Appreciation Leaderboard" %} - {% endblock %}

{% block extra_css %}
<style>
    .page-header-gradient {
        background: linear-gradient(135deg, #f59e0b 0%, #d97706 50%, #b45309 100%);
        color: white;
        padding: 1.5rem 2rem;
        border-radius: 1rem;
        margin-bottom: 1.5rem;
        box-shadow: 0 10px 15px -3px rgba(245, 158, 11, 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 %}
<!-- Breadcrumb -->
<nav aria-label="breadcrumb" class="mb-6">
    <ol class="flex items-center gap-2 text-sm">
        <li>
            <a href="{% url 'dashboard:command-center' %}" class="flex items-center gap-2 text-gray-400 hover:text-navy transition">
                <i data-lucide="home" class="w-4 h-4"></i>
                <span>{% trans 'Home' %}</span>
            </a>
        </li>
        <li class="text-gray-300">
            <i data-lucide="chevron-right" class="w-4 h-4"></i>
        </li>
        <li>
            <a href="{% url 'appreciation:appreciation_list' %}" class="text-gray-400 hover:text-navy transition">{% trans "Appreciation" %}</a>
        </li>
        <li class="text-gray-300">
            <i data-lucide="chevron-right" class="w-4 h-4"></i>
        </li>
        <li class="text-navy font-medium" aria-current="page">{% trans "Leaderboard" %}</li>
    </ol>
</nav>

<!-- Page Header -->
<div class="page-header-gradient">
    <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4">
        <div>
            <h1 class="text-2xl font-bold flex items-center gap-3">
                <i data-lucide="trophy" class="w-7 h-7"></i>
                {% trans "Appreciation Leaderboard" %}
            </h1>
            <p class="text-sm opacity-90 mt-1">{% trans "Top performers by appreciation received and sent" %}</p>
        </div>
        <div class="flex items-center gap-3">
            <a href="{% url 'appreciation:appreciation_list' %}" 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="heart" class="w-4 h-4"></i> {% trans "Appreciations" %}
            </a>
        </div>
    </div>
</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="min-w-[140px]">
                <label class="block text-xs font-bold text-slate uppercase tracking-wider mb-1.5">{% trans "Year" %}</label>
                <select id="year" name="year" 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">
                    {% for y in years %}
                        <option value="{{ y }}" {% if y == selected_year %}selected{% endif %}>{{ y }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="min-w-[160px]">
                <label class="block text-xs font-bold text-slate uppercase tracking-wider mb-1.5">{% trans "Month" %}</label>
                <select id="month" name="month" 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">
                    {% for m in months %}
                        <option value="{{ m.0 }}" {% if m.0 == selected_month %}selected{% endif %}>{{ m.1 }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="min-w-[200px]">
                <label class="block text-xs font-bold text-slate uppercase tracking-wider mb-1.5">{% trans "Department" %}</label>
                <select id="department" name="department" data-tomselect 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 Departments" %}</option>
                    {% for department in departments %}
                        <option value="{{ department.id }}" {% if filters.department == department.id|stringformat:"s" %}selected{% endif %}>
                            {{ department.name }}
                        </option>
                    {% endfor %}
                </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 "Apply" %}
            </button>
            <a href="{% url 'appreciation:leaderboard_view' %}" class="px-5 py-2.5 border-2 border-slate-200 rounded-xl text-sm font-bold text-slate hover:bg-slate-50 transition flex items-center gap-2">
                <i data-lucide="rotate-ccw" class="w-4 h-4"></i> {% trans "Reset" %}
            </a>
        </form>
    </div>
</div>

<!-- Leaderboard Table -->
<div class="section-card p-0">
    {% if page_obj %}
        <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 "Rank" %}</th>
                    <th class="px-6 py-4">{% trans "Recipient" %}</th>
                    <th class="px-6 py-4">{% trans "Hospital" %}</th>
                    <th class="px-6 py-4">{% trans "Department" %}</th>
                    <th class="px-6 py-4 text-center">{% trans "Received" %}</th>
                    <th class="px-6 py-4 text-center">{% trans "Sent" %}</th>
                    <th class="px-6 py-4 text-center">{% trans "Hospital Rank" %}</th>
                </tr>
            </thead>
            <tbody class="text-sm divide-y divide-slate-100">
                {% for item in page_obj %}
                <tr class="hover:bg-light/30 transition-colors {% if item.get_recipient_name == request.user.get_full_name %}bg-navy/5{% endif %}">
                    <td class="px-6 py-4">
                        {% if forloop.counter <= 3 %}
                            <span class="inline-flex items-center gap-1 px-2.5 py-1 rounded-full text-xs font-bold
                                {% if forloop.counter == 1 %}bg-amber-100 text-amber-700
                                {% elif forloop.counter == 2 %}bg-slate-200 text-slate-700
                                {% else %}bg-orange-100 text-orange-700{% endif %}">
                                {% if forloop.counter == 1 %}<i data-lucide="crown" class="w-3.5 h-3.5"></i>{% endif %}
                                #{{ forloop.counter }}
                            </span>
                        {% else %}
                            <span class="text-slate-400 text-sm">#{{ forloop.counter }}</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4">
                        <div class="flex items-center gap-2">
                            <div class="w-8 h-8 rounded-full bg-navy/10 flex items-center justify-center">
                                <i data-lucide="user" class="w-4 h-4 text-navy"></i>
                            </div>
                            <span class="font-semibold text-slate-800">{{ item.get_recipient_name }}</span>
                        </div>
                    </td>
                    <td class="px-6 py-4 text-slate-600">{{ item.hospital.name }}</td>
                    <td class="px-6 py-4 text-slate-600">{% if item.department %}{{ item.department.name }}{% else %}-{% endif %}</td>
                    <td class="px-6 py-4 text-center">
                        <span class="px-2.5 py-1 rounded-full text-xs font-bold bg-green-100 text-green-700">{{ item.received_count }}</span>
                    </td>
                    <td class="px-6 py-4 text-center">
                        <span class="px-2.5 py-1 rounded-full text-xs font-bold bg-blue-100 text-blue-700">{{ item.sent_count }}</span>
                    </td>
                    <td class="px-6 py-4 text-center">
                        {% if item.hospital_rank %}
                            {% if item.hospital_rank <= 3 %}
                                <span class="px-2.5 py-1 rounded-full text-xs font-bold bg-amber-100 text-amber-700">
                                    #{{ item.hospital_rank }}
                                </span>
                            {% else %}
                                <span class="px-2.5 py-1 rounded-full text-xs font-bold bg-slate-100 text-slate-700">
                                    #{{ item.hospital_rank }}
                                </span>
                            {% endif %}
                        {% else %}
                            <span class="text-slate-400">-</span>
                        {% endif %}
                    </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 }}{% if selected_year %}&year={{ selected_year }}{% endif %}{% if selected_month %}&month={{ selected_month }}{% endif %}{% if filters.department %}&department={{ filters.department }}{% endif %}" 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 }}{% if selected_year %}&year={{ selected_year }}{% endif %}{% if selected_month %}&month={{ selected_month }}{% endif %}{% if filters.department %}&department={{ filters.department }}{% endif %}"
                       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 }}{% if selected_year %}&year={{ selected_year }}{% endif %}{% if selected_month %}&month={{ selected_month }}{% endif %}{% if filters.department %}&department={{ filters.department }}{% endif %}" 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 %}
    {% else %}
        <div class="text-center py-16">
            <i data-lucide="trophy" class="w-16 h-16 mx-auto text-slate-300 mb-4"></i>
            <h3 class="text-lg font-bold text-slate-500 mb-1">{% trans "No appreciations found for this period" %}</h3>
            <p class="text-sm text-slate-400">{% trans "Try changing the filters or select a different time period" %}</p>
        </div>
    {% endif %}
</div>

<!-- Quick Actions -->
<div class="grid grid-cols-1 md:grid-cols-3 gap-4 mt-6">
    <a href="{% url 'appreciation:appreciation_list' %}" class="group">
        <div class="section-card p-5 hover:border-green-400">
            <div class="flex items-center gap-4">
                <div class="section-icon bg-green-100 group-hover:bg-green-200 transition">
                    <i data-lucide="send" class="w-6 h-6 text-green-600"></i>
                </div>
                <div>
                    <h3 class="font-bold text-navy">{% trans "Send Appreciation" %}</h3>
                    <p class="text-sm text-slate">{% trans "Share your appreciation with colleagues" %}</p>
                </div>
            </div>
        </div>
    </a>
    <a href="{% url 'appreciation:my_badges_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="award" class="w-6 h-6 text-amber-600"></i>
                </div>
                <div>
                    <h3 class="font-bold text-navy">{% trans "View Badges" %}</h3>
                    <p class="text-sm text-slate">{% trans "See your earned badges" %}</p>
                </div>
            </div>
        </div>
    </a>
    <a href="{% url 'appreciation:appreciation_list' %}" 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="list" class="w-6 h-6 text-blue-600"></i>
                </div>
                <div>
                    <h3 class="font-bold text-navy">{% trans "All Appreciations" %}</h3>
                    <p class="text-sm text-slate">{% trans "View your appreciations" %}</p>
                </div>
            </div>
        </div>
    </a>
</div>
{% endblock %}

{% block extra_js %}
{{ block.super }}
<script>
lucide.createIcons();
</script>
{% endblock %}
