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

{% block title %}{% trans "Acknowledgement Compliance Report" %} - PX360{% 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="flex flex-col md:flex-row justify-between items-start md:items-center gap-4 mb-8">
        <div class="flex items-center gap-3">
            <div class="flex items-center justify-center w-14 h-14 bg-gradient-to-br from-amber-500 to-orange-500 rounded-2xl shadow-lg shadow-amber-200">
                <i data-lucide="pie-chart" class="w-8 h-8 text-white"></i>
            </div>
            <div>
                <h1 class="text-2xl font-bold text-navy">
                    {% trans "Compliance Report" %}
                </h1>
                <p class="text-slate text-sm">
                    {% trans "Track acknowledgement completion rates" %}
                </p>
            </div>
        </div>
        <div class="flex gap-3">
            <a href="{% url 'accounts:acknowledgements:ack_dashboard' %}" class="inline-flex items-center gap-2 px-5 py-2.5 bg-white border-2 border-slate-200 text-slate-700 rounded-xl font-semibold hover:bg-slate-50 transition">
                <i data-lucide="arrow-left" class="w-4 h-4"></i>
                {% trans "Back" %}
            </a>
        </div>
    </div>

    <!-- Stats Cards -->
    <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-8">
        <div class="bg-white rounded-2xl shadow-sm border border-slate-100 p-6">
            <div class="flex items-center gap-3">
                <div class="w-12 h-12 bg-blue-100 rounded-xl flex items-center justify-center">
                    <i data-lucide="users" class="w-6 h-6 text-blue-600"></i>
                </div>
                <div>
                    <p class="text-xs font-semibold text-slate uppercase">{% trans "Total Users" %}</p>
                    <p class="text-2xl font-bold text-navy">{{ stats.total_users }}</p>
                </div>
            </div>
        </div>
        <div class="bg-white rounded-2xl shadow-sm border border-slate-100 p-6">
            <div class="flex items-center gap-3">
                <div class="w-12 h-12 bg-emerald-100 rounded-xl flex items-center justify-center">
                    <i data-lucide="check-circle" class="w-6 h-6 text-emerald-600"></i>
                </div>
                <div>
                    <p class="text-xs font-semibold text-slate uppercase">{% trans "Fully Compliant" %}</p>
                    <p class="text-2xl font-bold text-emerald-600">{{ stats.compliant_users }}</p>
                </div>
            </div>
        </div>
        <div class="bg-white rounded-2xl shadow-sm border border-slate-100 p-6">
            <div class="flex items-center gap-3">
                <div class="w-12 h-12 bg-amber-100 rounded-xl flex items-center justify-center">
                    <i data-lucide="clock" class="w-6 h-6 text-amber-600"></i>
                </div>
                <div>
                    <p class="text-xs font-semibold text-slate uppercase">{% trans "Pending" %}</p>
                    <p class="text-2xl font-bold text-amber-600">{{ stats.pending_users }}</p>
                </div>
            </div>
        </div>
        <div class="bg-white rounded-2xl shadow-sm border border-slate-100 p-6">
            <div class="flex items-center gap-3">
                <div class="w-12 h-12 bg-gradient-to-br from-emerald-500 to-green-500 rounded-xl flex items-center justify-center">
                    <i data-lucide="percent" class="w-6 h-6 text-white"></i>
                </div>
                <div>
                    <p class="text-xs font-semibold text-slate uppercase">{% trans "Compliance Rate" %}</p>
                    <p class="text-2xl font-bold text-emerald-600">{{ stats.compliance_rate }}%</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Category Breakdown -->
    <div class="bg-white rounded-2xl shadow-sm border border-slate-100 overflow-hidden mb-8">
        <div class="px-6 py-5 border-b border-slate-100 bg-gradient-to-r from-amber-50 to-transparent">
            <h2 class="text-lg font-bold text-navy flex items-center gap-2">
                <i data-lucide="bar-chart-3" class="w-5 h-5 text-amber-500"></i>
                {% trans "Compliance by Category" %}
            </h2>
        </div>
        <div class="p-6">
            {% if category_stats %}
            <div class="space-y-4">
                {% for cat_stat in category_stats %}
                <div class="p-4 border border-slate-100 rounded-xl">
                    <div class="flex flex-col md:flex-row md:items-center justify-between gap-4 mb-3">
                        <div class="flex items-center gap-3">
                            <div class="w-10 h-10 rounded-xl flex items-center justify-center" style="background-color: {{ cat_stat.category.color }}20">
                                <i data-lucide="{{ cat_stat.category.icon|default:'folder' }}" class="w-5 h-5" style="color: {{ cat_stat.category.color }}"></i>
                            </div>
                            <div>
                                <p class="font-semibold text-navy">{{ cat_stat.category.get_localized_name }}</p>
                                <p class="text-xs text-slate">{{ cat_stat.total_items }} {% trans "items" %}</p>
                            </div>
                        </div>
                        <div class="text-right">
                            <p class="text-2xl font-bold {% widthratio cat_stat.completion_rate 1 100 %}">
                                {{ cat_stat.completion_rate|floatformat:0 }}%
                            </p>
                            <p class="text-xs text-slate">{{ cat_stat.signed }}/{{ cat_stat.total }} {% trans "completed" %}</p>
                        </div>
                    </div>
                    <div class="w-full bg-slate-100 rounded-full h-3">
                        <div class="h-3 rounded-full transition-all duration-500" style="width: {{ cat_stat.completion_rate|floatformat:0 }}%; background-color: {{ cat_stat.category.color }}"></div>
                    </div>
                </div>
                {% endfor %}
            </div>
            {% else %}
            <div class="text-center py-12">
                <p class="text-slate">{% trans "No category data available" %}</p>
            </div>
            {% endif %}
        </div>
    </div>

    <!-- Non-Compliant Users -->
    <div class="bg-white rounded-2xl shadow-sm border border-slate-100 overflow-hidden">
        <div class="px-6 py-5 border-b border-slate-100 bg-gradient-to-r from-red-50 to-transparent">
            <h2 class="text-lg font-bold text-navy flex items-center gap-2">
                <i data-lucide="alert-triangle" class="w-5 h-5 text-red-500"></i>
                {% trans "Non-Compliant Users" %}
            </h2>
        </div>
        <div class="p-6">
            {% if non_compliant_users %}
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead>
                        <tr class="border-b border-slate-200">
                            <th class="text-left py-3 px-4 text-xs font-semibold text-slate uppercase">{% trans "User" %}</th>
                            <th class="text-left py-3 px-4 text-xs font-semibold text-slate uppercase">{% trans "Department" %}</th>
                            <th class="text-left py-3 px-4 text-xs font-semibold text-slate uppercase">{% trans "Pending Items" %}</th>
                            <th class="text-left py-3 px-4 text-xs font-semibold text-slate uppercase">{% trans "Completion" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for user_data in non_compliant_users %}
                        <tr class="border-b border-slate-100 hover:bg-slate-50 transition">
                            <td class="py-4 px-4">
                                <div class="flex items-center gap-3">
                                    <div class="w-10 h-10 bg-gradient-to-br from-blue to-navy rounded-full flex items-center justify-center">
                                        <span class="text-white font-semibold text-sm">{{ user_data.user.get_initials }}</span>
                                    </div>
                                    <div>
                                        <p class="font-semibold text-navy">{{ user_data.user.get_full_name }}</p>
                                        <p class="text-sm text-slate">{{ user_data.user.email }}</p>
                                    </div>
                                </div>
                            </td>
                            <td class="py-4 px-4 text-slate">
                                {{ user_data.user.department.name|default:"-" }}
                            </td>
                            <td class="py-4 px-4">
                                <span class="inline-flex items-center px-2.5 py-1 bg-red-100 text-red-700 rounded-lg text-xs font-semibold">
                                    {{ user_data.pending_count }} {% trans "pending" %}
                                </span>
                            </td>
                            <td class="py-4 px-4">
                                <div class="flex items-center gap-2">
                                    <div class="w-24 bg-slate-100 rounded-full h-2">
                                        <div class="bg-amber-500 h-2 rounded-full" style="width: {{ user_data.completion_rate }}%"></div>
                                    </div>
                                    <span class="text-sm text-slate">{{ user_data.completion_rate }}%</span>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <div class="text-center py-12">
                <div class="w-16 h-16 bg-emerald-100 rounded-full flex items-center justify-center mx-auto mb-4">
                    <i data-lucide="check-circle" class="w-8 h-8 text-emerald-500"></i>
                </div>
                <h3 class="text-lg font-bold text-navy mb-2">{% trans "All Users Compliant!" %}</h3>
                <p class="text-slate">{% trans "All users have completed their required acknowledgements." %}</p>
            </div>
            {% endif %}
        </div>
    </div>
</div>

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