{# app/templates/admin/admin-users.html #}
{% extends "admin/base.html" %}
{% from 'shared/macros/pagination.html' import pagination %}
{% from 'shared/macros/headers.html' import page_header %}
{% from 'shared/macros/alerts.html' import loading_state, error_state %}
{% from 'shared/macros/tables.html' import table_wrapper, table_header %}
{% from 'shared/macros/modals.html' import confirm_modal_dynamic %}

{% block title %}Admin Users{% endblock %}

{% block alpine_data %}adminUsersPage(){% endblock %}

{% block content %}
{{ page_header('Admin User Management', subtitle='Manage super admins and platform admins', action_label='Create Admin User', action_url='/admin/admin-users/create', action_icon='user-plus') }}

{{ loading_state('Loading admin users...') }}

{{ error_state('Error loading admin users') }}

<!-- Stats Cards -->
<div x-show="!loading" class="grid gap-6 mb-8 md:grid-cols-2 xl:grid-cols-4">
    <!-- Card: Total Admin Users -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-purple-500 bg-purple-100 rounded-full dark:text-purple-100 dark:bg-purple-500">
            <span x-html="$icon('shield', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Total Admins
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.total_admins || 0">
                0
            </p>
        </div>
    </div>

    <!-- Card: Super Admins -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-orange-500 bg-orange-100 rounded-full dark:text-orange-100 dark:bg-orange-500">
            <span x-html="$icon('star', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Super Admins
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.super_admins || 0">
                0
            </p>
        </div>
    </div>

    <!-- Card: Platform Admins -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-blue-500 bg-blue-100 rounded-full dark:text-blue-100 dark:bg-blue-500">
            <span x-html="$icon('globe-alt', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Platform Admins
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.platform_admins || 0">
                0
            </p>
        </div>
    </div>

    <!-- Card: Active Admins -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-green-500 bg-green-100 rounded-full dark:text-green-100 dark:bg-green-500">
            <span x-html="$icon('user-check', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Active
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.active_admins || 0">
                0
            </p>
        </div>
    </div>
</div>

<!-- Search and Filters -->
<div x-show="!loading" class="mb-6 px-4 py-3 bg-white rounded-lg shadow-md dark:bg-gray-800">
    <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4">
        <!-- Search Bar -->
        <div class="flex-1 max-w-md">
            <div class="relative">
                <input
                    type="text"
                    x-model="filters.search"
                    @input="debouncedSearch()"
                    placeholder="Search by name, email, or username..."
                    class="w-full pl-10 pr-4 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:bg-gray-700 dark:text-gray-300"
                >
                <div class="absolute inset-y-0 left-0 flex items-center pl-3">
                    <span x-html="$icon('search', 'w-5 h-5 text-gray-400')"></span>
                </div>
            </div>
        </div>

        <!-- Filters -->
        <div class="flex flex-wrap gap-3">
            <!-- Admin Role Filter -->
            <select
                x-model="filters.role"
                @change="pagination.page = 1; loadAdminUsers()"
                class="px-4 py-2 text-sm text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none"
            >
                <option value="">All Admin Roles</option>
                <option value="super_admin">Super Admins</option>
                <option value="platform_admin">Platform Admins</option>
            </select>

            <!-- Status Filter -->
            <select
                x-model="filters.is_active"
                @change="pagination.page = 1; loadAdminUsers()"
                class="px-4 py-2 text-sm text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none"
            >
                <option value="">All Status</option>
                <option value="true">Active</option>
                <option value="false">Inactive</option>
            </select>

            <!-- Refresh Button -->
            <button
                @click="loadAdminUsers(); loadStats()"
                class="flex items-center px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700 focus:outline-none transition-colors"
                title="Refresh admin users"
            >
                <span x-html="$icon('refresh', 'w-4 h-4 mr-2')"></span>
                Refresh
            </button>
        </div>
    </div>
</div>

<!-- Admin Users Table -->
<div x-show="!loading">
    {% call table_wrapper() %}
            {{ table_header(['Admin', 'Email', 'Role', 'Platforms', 'Status', 'Last Login', 'Actions']) }}
            <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
                <!-- Empty State -->
                <template x-if="adminUsers.length === 0">
                    <tr>
                        <td colspan="7" class="px-4 py-8 text-center text-gray-600 dark:text-gray-400">
                            <div class="flex flex-col items-center">
                                <span x-html="$icon('shield', 'w-12 h-12 text-gray-400 mb-4')"></span>
                                <p class="text-lg font-medium">No admin users found</p>
                                <p class="text-sm" x-text="filters.search ? 'Try adjusting your search or filters' : 'Create your first admin user to get started'"></p>
                            </div>
                        </td>
                    </tr>
                </template>

                <!-- Admin User Rows -->
                <template x-for="admin in adminUsers" :key="admin.id">
                    <tr class="text-gray-700 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700">
                        <!-- Admin Info -->
                        <td class="px-4 py-3">
                            <div class="flex items-center text-sm">
                                <div class="relative hidden w-8 h-8 mr-3 rounded-full md:block">
                                    <div class="absolute inset-0 rounded-full flex items-center justify-center text-white font-semibold text-sm"
                                         :class="admin.role === 'super_admin' ? 'bg-orange-500' : 'bg-purple-500'"
                                         x-text="(admin.username || 'A').charAt(0).toUpperCase()">
                                    </div>
                                </div>
                                <div>
                                    <p class="font-semibold" x-text="admin.username"></p>
                                    <p class="text-xs text-gray-600 dark:text-gray-400" x-text="admin.full_name || ''"></p>
                                </div>
                            </div>
                        </td>

                        <!-- Email -->
                        <td class="px-4 py-3 text-sm" x-text="admin.email"></td>

                        <!-- Role -->
                        <td class="px-4 py-3 text-xs">
                            <span class="px-2 py-1 font-semibold leading-tight rounded-full"
                                  :class="admin.role === 'super_admin'
                                      ? 'text-orange-700 bg-orange-100 dark:bg-orange-700 dark:text-orange-100'
                                      : 'text-purple-700 bg-purple-100 dark:bg-purple-700 dark:text-purple-100'"
                                  x-text="admin.role === 'super_admin' ? 'Super Admin' : 'Platform Admin'">
                            </span>
                        </td>

                        <!-- Platforms -->
                        <td class="px-4 py-3 text-sm">
                            <template x-if="admin.role === 'super_admin'">
                                <span class="text-gray-500 dark:text-gray-400 italic">All platforms</span>
                            </template>
                            <template x-if="admin.role !== 'super_admin'">
                                <div class="flex flex-wrap gap-1">
                                    <template x-for="platform in (admin.platforms || []).slice(0, 3)" :key="platform.id">
                                        <span class="px-2 py-0.5 text-xs bg-gray-100 dark:bg-gray-700 rounded" x-text="platform.code"></span>
                                    </template>
                                    <template x-if="(admin.platforms || []).length > 3">
                                        <span class="px-2 py-0.5 text-xs text-gray-500" x-text="'+' + ((admin.platforms || []).length - 3) + ' more'"></span>
                                    </template>
                                    <template x-if="(admin.platforms || []).length === 0">
                                        <span class="text-gray-500 dark:text-gray-400 italic">None assigned</span>
                                    </template>
                                </div>
                            </template>
                        </td>

                        <!-- Status -->
                        <td class="px-4 py-3 text-xs">
                            <span class="px-2 py-1 font-semibold leading-tight rounded-full"
                                  :class="admin.is_active ? 'text-green-700 bg-green-100 dark:bg-green-700 dark:text-green-100' : 'text-red-700 bg-red-100 dark:bg-red-700 dark:text-red-100'"
                                  x-text="admin.is_active ? 'Active' : 'Inactive'">
                            </span>
                        </td>

                        <!-- Last Login -->
                        <td class="px-4 py-3 text-sm" x-text="admin.last_login ? formatDate(admin.last_login) : 'Never'"></td>

                        <!-- Actions -->
                        <td class="px-4 py-3">
                            <div class="flex items-center space-x-2 text-sm">
                                <!-- View Button -->
                                <a
                                    :href="'/admin/admin-users/' + admin.id"
                                    class="flex items-center justify-center p-2 text-blue-600 rounded-lg hover:bg-blue-50 dark:text-blue-400 dark:hover:bg-gray-700 focus:outline-none transition-colors"
                                    title="View admin user"
                                >
                                    <span x-html="$icon('eye', 'w-5 h-5')"></span>
                                </a>

                                <!-- Edit Button -->
                                <a
                                    :href="'/admin/admin-users/' + admin.id + '/edit'"
                                    class="flex items-center justify-center p-2 text-purple-600 rounded-lg hover:bg-purple-50 dark:text-purple-400 dark:hover:bg-gray-700 focus:outline-none transition-colors"
                                    title="Edit admin user"
                                >
                                    <span x-html="$icon('edit', 'w-5 h-5')"></span>
                                </a>

                                <!-- Delete Button (disabled for self) -->
                                <button
                                    @click="adminToDelete = admin; showDeleteModal = true"
                                    :disabled="admin.id === currentUserId"
                                    class="flex items-center justify-center p-2 text-red-600 rounded-lg hover:bg-red-50 dark:text-red-400 dark:hover:bg-gray-700 focus:outline-none transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
                                    :title="admin.id === currentUserId ? 'Cannot delete yourself' : 'Delete admin user'"
                                >
                                    <span x-html="$icon('delete', 'w-5 h-5')"></span>
                                </button>
                            </div>
                        </td>
                    </tr>
                </template>
            </tbody>
    {% endcall %}

    {{ pagination() }}
</div>

<!-- Delete Admin User Confirmation Modal (Step 1) -->
{{ confirm_modal_dynamic(
    'deleteAdminUserModal',
    'Delete Admin User',
    "'Are you sure you want to delete admin user \"' + (adminToDelete?.username || '') + '\"? This action cannot be undone.'",
    'confirmDeleteStep()',
    'showDeleteModal',
    'Delete',
    'Cancel',
    'danger'
) }}

<!-- Delete Admin User Final Confirmation Modal (Step 2) -->
{{ confirm_modal_dynamic(
    'deleteAdminUserFinalModal',
    'Final Confirmation',
    "'FINAL CONFIRMATION: Are you absolutely sure you want to permanently delete \"' + (adminToDelete?.username || '') + '\"?'",
    'deleteAdminUser(adminToDelete)',
    'showDeleteFinalModal',
    'Permanently Delete',
    'Cancel',
    'danger'
) }}
{% endblock %}

{% block extra_scripts %}
<script defer src="{{ url_for('tenancy_static', path='admin/js/admin-users.js') }}"></script>
{% endblock %}
