{% extends "layouts/base.html.jinja" %}

{% block title %}User Management - {{ request.app.title }}{% endblock %}

{% block content %}
<style>
    /* Dashboard specific layout */
    .dashboard-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 2.5rem;
    }

    .header-text h1 {
        font-size: 2rem;
        font-weight: 700;
        margin-bottom: 0.5rem;
        letter-spacing: -0.025em;
    }

    /* Custom Table with Grid */
    .user-table {
        display: flex;
        flex-direction: column;
        width: 100%;
    }

    .table-header {
        display: grid;
        grid-template-columns: 2fr 1fr 1fr 2.5fr 1.5fr;
        padding: 1.25rem 2rem;
        background: rgba(255, 255, 255, 0.02);
        border-bottom: 1px solid var(--border-color);
        font-size: 0.75rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: var(--text-muted);
    }

    .table-row {
        display: grid;
        grid-template-columns: 2fr 1fr 1fr 2.5fr 1.5fr;
        padding: 1.5rem 2rem;
        align-items: center;
        border-bottom: 1px solid var(--border-color);
        transition: all 0.2s ease;
    }

    .table-row:last-child {
        border-bottom: none;
    }

    .table-row:hover {
        background: rgba(255, 255, 255, 0.03);
    }

    .user-main {
        display: flex;
        flex-direction: column;
        gap: 0.375rem;
    }

    .user-email {
        font-weight: 600;
        font-size: 1rem;
    }

    .user-id {
        font-size: 0.75rem;
        color: var(--text-muted);
        font-family: monospace;
    }

    .actions {
        display: flex;
        gap: 0.75rem;
        justify-content: flex-end;
    }

    .action-btn {
        width: 36px;
        height: 36px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 0.75rem;
        border: 1px solid var(--border-color);
        background: rgba(255, 255, 255, 0.05);
        color: var(--text-muted);
        cursor: pointer;
        transition: all 0.2s;
    }

    .action-btn:hover {
        border-color: var(--accent-color);
        color: var(--accent-color);
        background: rgba(56, 189, 248, 0.1);
        transform: translateY(-2px);
    }

    /* Modal - Reimplemented without Bootstrap */
    .modal-backdrop {
        display: none;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.4);
        backdrop-filter: blur(12px);
        z-index: 1000;
        align-items: flex-start;
        /* Start from top to allow scrolling */
        justify-content: center;
        padding: 0.5rem 1.5rem;
        opacity: 0;
        transition: opacity 0.3s ease;
        overflow-y: auto;
        /* Handle long modals */
    }

    .modal-content {
        width: 100%;
        max-width: 600px;
        margin-top: 0.5rem;
        margin-bottom: 0.25rem;
        transform: translateY(20px);
        transition: all 0.4s cubic-bezier(0.16, 1, 0.3, 1);
        border: 1px solid rgba(255, 255, 255, 0.1);
        box-shadow: 0 50px 100px -20px rgba(0, 0, 0, 0.5);
    }

    .modal-backdrop.show {
        display: flex;
        opacity: 1;
    }

    .modal-backdrop.show .modal-content {
        transform: translateY(0);
    }

    .modal-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-bottom: 1.5rem;
        border-bottom: 1px solid var(--border-color);
        margin-bottom: 2rem;
    }

    .modal-title {
        font-size: 1.5rem;
        font-weight: 700;
        letter-spacing: -0.025em;
    }

    .close-modal {
        background: rgba(255, 255, 255, 0.05);
        border: 1px solid var(--border-color);
        color: var(--text-muted);
        cursor: pointer;
        width: 32px;
        height: 32px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.2s;
    }

    .close-modal:hover {
        background: rgba(239, 68, 68, 0.1);
        color: var(--danger);
        border-color: var(--danger);
    }

    .roles-selection-grid {
        display: grid;
        grid-template-columns: 1fr;
        gap: 0.75rem;
        max-height: 300px;
        overflow-y: auto;
        padding: 0.5rem;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 0.75rem;
    }

    .role-selector {
        padding: 0.75rem;
        border-radius: 0.5rem;
        background: rgba(255, 255, 255, 0.03);
        border: 1px solid rgba(255, 255, 255, 0.05);
        transition: all 0.2s;
    }

    .role-selector:hover {
        background: rgba(255, 255, 255, 0.06);
        border-color: rgba(255, 255, 255, 0.1);
    }

    .role-info {
        display: flex;
        flex-direction: column;
        gap: 0.125rem;
    }

    .role-name {
        font-weight: 600;
        font-size: 0.9375rem;
    }

    .text-xs {
        font-size: 0.75rem;
    }

    /* Filter and Pagination Styles */
    .filter-bar {
        display: flex;
        gap: 1rem;
        margin-bottom: 1.5rem;
        align-items: center;
        flex-wrap: wrap;
    }

    .search-input-wrapper {
        position: relative;
        flex: 1;
        min-width: 300px;
    }

    .search-input-wrapper i {
        position: absolute;
        left: 1rem;
        top: 50%;
        transform: translateY(-50%);
        color: var(--text-muted);
    }

    .search-input-wrapper input {
        padding-left: 2.75rem !important;
    }

    .pagination-container {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 1.5rem 2rem;
        background: rgba(255, 255, 255, 0.02);
        border-top: 1px solid var(--border-color);
    }

    .pagination-info {
        font-size: 0.875rem;
        color: var(--text-muted);
    }

    .pagination-info b {
        color: var(--text-color);
    }

    .pagination-actions {
        display: flex;
        gap: 0.5rem;
    }

    .page-link {
        padding: 0.5rem 1rem;
        border-radius: 0.5rem;
        border: 1px solid var(--border-color);
        background: rgba(255, 255, 255, 0.05);
        color: var(--text-color);
        text-decoration: none;
        font-size: 0.875rem;
        transition: all 0.2s;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .page-link:hover:not(.disabled) {
        border-color: var(--accent-color);
        background: rgba(56, 189, 248, 0.1);
        color: var(--accent-color);
    }

    .page-link.disabled {
        opacity: 0.5;
        cursor: not-allowed;
        pointer-events: none;
    }
</style>

<div class="dashboard-header animate-fade-in">
    <div class="header-text">
        <h1>{{ request.app.title }}</h1>
        <p class="text-muted">Control system access and user permissions at a granular level.</p>
    </div>
    <button class="btn btn-primary" onclick="toggleModal('createUserModal')">
        <i class="fas fa-plus"></i>
        <span>Create New User</span>
    </button>
</div>

<div class="glass-card animate-fade-in" style="overflow: hidden; animation-delay: 0.1s;">
    <div style="padding: 1.5rem 2rem; border-bottom: 1px solid var(--border-color);">
        <form action="{{ url_for('dashboard_index') }}" method="GET" class="filter-bar">
            <div class="search-input-wrapper">
                <i class="fas fa-search"></i>
                <input type="text" name="filter" value="{{ filter }}" class="form-control"
                    placeholder="Filter by name, status, verified or roles...">
            </div>
            <input type="hidden" name="pageSize" value="{{ pageSize }}">
            <button type="submit" class="btn btn-primary">
                <span>Filter</span>
            </button>
            {% if filter %}
            <a href="{{ url_for('dashboard_index') }}?pageSize={{ pageSize }}" class="btn btn-outline">
                <span>Clear</span>
            </a>
            {% endif %}
        </form>
    </div>

    <div class="user-table">
        <div class="table-header">
            <div>User Identity</div>
            <div>Status</div>
            <div>Verified</div>
            <div>Access Roles</div>
            <div style="text-align: right;">Actions</div>
        </div>

        {% if not users %}
        <div class="table-row" style="display: flex; flex-direction: column; text-align: center; padding: 6rem 2rem;">
            <i class="fas fa-users-slash text-muted" style="font-size: 3rem; margin-bottom: 1.5rem;"></i>
            <p class="text-muted font-bold">No users found in the secure registry.</p>
            <p class="text-muted text-sm" style="margin-top: 0.5rem;">Start by adding your first administrative or
                standard user.</p>
        </div>
        {% endif %}

        {% for user in users %}
        <div class="table-row">
            <div class="user-main">
                <span class="user-email text-accent">{{ user.email }}</span>
                <span class="user-id" title="{{ user.id }}">UID-{{ user.id|string|truncate(12, True, '...') }}</span>
            </div>
            <div>
                {% if user.is_active %}
                <span class="badge badge-success">Active</span>
                {% else %}
                <span class="badge badge-danger">Inactive</span>
                {% endif %}
            </div>
            <div>
                {% if user.is_verified %}
                <div class="flex items-center gap-2" style="color: var(--success);">
                    <i class="fas fa-check-circle"></i>
                    <span class="text-sm font-bold">Verified</span>
                </div>
                {% else %}
                <div class="flex items-center gap-2" style="color: var(--warning);">
                    <i class="fas fa-clock"></i>
                    <span class="text-sm font-bold">Pending</span>
                </div>
                {% endif %}
            </div>
            <div class="flex" style="flex-wrap: wrap; gap: 0.375rem;">
                {% for role in user.roles %}
                <span class="badge badge-muted">{{ role.name }}</span>
                {% endfor %}
                {% if not user.roles %}
                <span class="text-muted italic small">No roles</span>
                {% endif %}
            </div>
            <div class="actions">
                {% if 'roles:manage' in user_perms %}
                <button class="action-btn" title="Manage User Roles"
                    onclick="toggleModal('editUserRolesModal-{{ user.id }}')">
                    <i class="fas fa-user-tag"></i>
                </button>
                {% endif %}
                <form
                    action="{{ url_for('verify_user_action', user_id=user.id) }}?page={{ page }}&pageSize={{ pageSize }}&filter={{ filter }}"
                    method="POST">
                    <button type="submit" class="action-btn" title="Toggle Verification Status">
                        <i class="fas fa-user-check"></i>
                    </button>
                </form>
                <form
                    action="{{ url_for('toggle_user_active', user_id=user.id) }}?page={{ page }}&pageSize={{ pageSize }}&filter={{ filter }}"
                    method="POST">
                    <button type="submit" class="action-btn" title="Toggle Account Access">
                        <i class="fas fa-power-off"></i>
                    </button>
                </form>
            </div>
        </div>
        {% endfor %}
    </div>

    <div class="pagination-container">
        <div class="pagination-info">
            Showing <b>{{ users|length }}</b> of <b>{{ filtered_users }}</b> users
            {% if filter %}
            (filtered from <b>{{ total_users }}</b> total)
            {% else %}
            registry total
            {% endif %}
        </div>
        <div class="pagination-actions">
            <a href="{{ url_for('dashboard_index') }}?page={{ page - 1 }}&pageSize={{ pageSize }}&filter={{ filter }}"
                class="page-link {% if page <= 0 %}disabled{% endif %}">
                <i class="fas fa-chevron-left"></i>
                <span>Previous</span>
            </a>
            <a href="{{ url_for('dashboard_index') }}?page={{ page + 1 }}&pageSize={{ pageSize }}&filter={{ filter }}"
                class="page-link {% if (page + 1) * pageSize >= filtered_users %}disabled{% endif %}">
                <span>Next</span>
                <i class="fas fa-chevron-right"></i>
            </a>
        </div>
    </div>
</div>

<!-- Edit User Roles Modals (Moved outside table to avoid cropping) -->
{% if 'roles:manage' in user_perms %}
{% for user in users %}
<div id="editUserRolesModal-{{ user.id }}" class="modal-backdrop"
    onclick="if(event.target === this) toggleModal('editUserRolesModal-{{ user.id }}')">
    <div class="modal-content glass-card p-6" style="padding: 2.5rem;">
        <div class="modal-header">
            <span class="modal-title">Edit User Roles</span>
            <button class="close-modal" onclick="toggleModal('editUserRolesModal-{{ user.id }}')">&times;</button>
        </div>
        <p class="mb-6 text-muted">Assign or remove access roles for <span class="text-accent">{{ user.email
                }}</span>.</p>
        <form
            action="{{ url_for('update_user_roles', user_id=user.id) }}?page={{ page }}&pageSize={{ pageSize }}&filter={{ filter }}"
            method="POST">
            <div class="form-group">
                <label class="form-label">Available Roles</label>
                <div class="roles-selection-grid">
                    {% set system_roles = [] %}
                    {% set custom_roles = [] %}
                    {% for role in all_roles %}
                    {% if role.is_default %}
                    {% set _ = system_roles.append(role) %}
                    {% else %}
                    {% set _ = custom_roles.append(role) %}
                    {% endif %}
                    {% endfor %}

                    {% if system_roles %}
                    <div class="group-label" style="font-size: 0.6rem; padding: 0.5rem; color: var(--success);">
                        System Roles</div>
                    {% for role in system_roles %}
                    <label class="checkbox-group role-selector">
                        <input type="checkbox" name="role_ids" value="{{ role.id }}" {% if role in user.roles
                            %}checked{% endif %}>
                        <div class="role-info">
                            <span class="role-name">{{ role.name }} <i
                                    class="fas fa-lock text-xs opacity-50 ml-1"></i></span>
                            <span class="role-desc text-xs text-muted">{{ role.description or 'System defined
                                role' }}</span>
                        </div>
                    </label>
                    {% endfor %}
                    {% endif %}

                    {% if custom_roles %}
                    <div class="group-label"
                        style="font-size: 0.6rem; padding: 0.5rem; color: var(--accent-color); margin-top: 0.5rem;">
                        Organization Roles</div>
                    {% for role in custom_roles %}
                    <label class="checkbox-group role-selector">
                        <input type="checkbox" name="role_ids" value="{{ role.id }}" {% if role in user.roles
                            %}checked{% endif %}>
                        <div class="role-info">
                            <span class="role-name">{{ role.name }}</span>
                            <span class="role-desc text-xs text-muted">{{ role.description or 'Custom role'
                                }}</span>
                        </div>
                    </label>
                    {% endfor %}
                    {% endif %}
                </div>
            </div>
            <div class="flex" style="gap: 1.25rem; margin-top: 2rem;">
                <button type="button" class="btn btn-outline w-full" style="justify-content: center; height: 3rem;"
                    onclick="toggleModal('editUserRolesModal-{{ user.id }}')">Cancel</button>
                <button type="submit" class="btn btn-primary w-full"
                    style="justify-content: center; height: 3rem;">Update Roles</button>
            </div>
        </form>
    </div>
</div>
{% endfor %}
{% endif %}

<!-- Provision New User Modal -->
<div id="createUserModal" class="modal-backdrop" onclick="if(event.target === this) toggleModal('createUserModal')">
    <div class="modal-content glass-card p-6" style="padding: 2.5rem;">
        <div class="modal-header">
            <span class="modal-title">Provision New User</span>
            <button class="close-modal" onclick="toggleModal('createUserModal')">&times;</button>
        </div>
        <form action="{{ url_for('create_user_action') }}" method="POST">
            <div class="form-group">
                <label class="form-label">Network Identity (Email)</label>
                <input type="email" name="email" class="form-control" placeholder="name@organization.com" required>
            </div>
            <div class="form-group">
                <label class="form-label">Secure Passphrase</label>
                <input type="password" name="password" class="form-control" placeholder="••••••••••••" required>
            </div>
            <div class="form-group">
                <label class="checkbox-group">
                    <input type="checkbox" name="is_verified" value="true">
                    <span class="form-label" style="margin: 0;">Bypass Email Verification</span>
                </label>
            </div>
            <div class="flex" style="gap: 1.25rem; margin-top: 2rem;">
                <button type="button" class="btn btn-outline w-full" style="justify-content: center; height: 3rem;"
                    onclick="toggleModal('createUserModal')">Cancel</button>
                <button type="submit" class="btn btn-primary w-full"
                    style="justify-content: center; height: 3rem;">Initiate Provisioning</button>
            </div>
        </form>
    </div>
</div>
{% endblock %}