{# app/templates/store/roles.html #}
{% extends "store/base.html" %}
{% from 'shared/macros/headers.html' import page_header_flex, refresh_button %}
{% from 'shared/macros/alerts.html' import loading_state, error_state %}
{% from 'shared/macros/modals.html' import modal_simple %}

{% block title %}Role Management{% endblock %}

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

{% block content %}
<!-- Page Header -->
{% call page_header_flex(title='Role Management', subtitle='Create and manage custom roles with granular permissions') %}
    <div class="flex items-center gap-4">
        {{ refresh_button(loading_var='loading', onclick='loadRoles()', variant='secondary') }}
        <button
            @click="openCreateModal()"
            class="flex items-center px-4 py-2 text-sm font-medium leading-5 text-white transition-colors duration-150 bg-purple-600 border border-transparent rounded-lg hover:bg-purple-700 focus:outline-none focus:shadow-outline-purple"
        >
            <span x-html="$icon('plus', 'w-4 h-4 mr-2')"></span>
            Create Role
        </button>
    </div>
{% endcall %}

{{ loading_state('Loading roles...') }}

{{ error_state('Error loading roles') }}

<!-- Roles List -->
<div x-show="!loading && !error" class="space-y-6">
    <template x-for="role in roles" :key="role.id">
        <div class="bg-white rounded-lg shadow-xs dark:bg-gray-800 p-6">
            <div class="flex items-center justify-between mb-4">
                <div>
                    <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="role.name"></h3>
                    <p class="text-sm text-gray-500 dark:text-gray-400">
                        <span x-text="(role.permissions || []).length"></span> permissions
                        <template x-if="isPresetRole(role.name)">
                            <span class="ml-2 px-2 py-0.5 text-xs font-medium bg-blue-100 text-blue-800 rounded-full dark:bg-blue-900 dark:text-blue-200">Preset</span>
                        </template>
                        <template x-if="!isPresetRole(role.name)">
                            <span class="ml-2 px-2 py-0.5 text-xs font-medium bg-green-100 text-green-800 rounded-full dark:bg-green-900 dark:text-green-200">Custom</span>
                        </template>
                    </p>
                </div>
                <div class="flex items-center gap-2">
                    <button
                        @click="openEditModal(role)"
                        class="px-3 py-1.5 text-sm font-medium text-purple-600 bg-purple-50 rounded-lg hover:bg-purple-100 dark:text-purple-400 dark:bg-purple-900/20 dark:hover:bg-purple-900/40"
                    >
                        <span x-html="$icon('pencil', 'w-4 h-4 inline mr-1')"></span>
                        Edit
                    </button>
                    <button
                        x-show="!isPresetRole(role.name)"
                        @click="confirmDelete(role)"
                        class="px-3 py-1.5 text-sm font-medium text-red-600 bg-red-50 rounded-lg hover:bg-red-100 dark:text-red-400 dark:bg-red-900/20 dark:hover:bg-red-900/40"
                    >
                        <span x-html="$icon('trash', 'w-4 h-4 inline mr-1')"></span>
                        Delete
                    </button>
                </div>
            </div>

            <!-- Permission tags -->
            <div class="flex flex-wrap gap-1.5">
                <template x-for="perm in (role.permissions || [])" :key="perm">
                    <span class="px-2 py-0.5 text-xs font-medium bg-gray-100 text-gray-600 rounded dark:bg-gray-700 dark:text-gray-300" x-text="perm"></span>
                </template>
                <template x-if="!role.permissions || role.permissions.length === 0">
                    <span class="text-sm text-gray-400 dark:text-gray-500">No permissions assigned</span>
                </template>
            </div>
        </div>
    </template>

    <template x-if="roles.length === 0 && !loading">
        <div class="text-center py-12 text-gray-500 dark:text-gray-400">
            <span x-html="$icon('shield', 'w-12 h-12 mx-auto mb-4 opacity-50')"></span>
            <p>No roles found. Create a custom role to get started.</p>
        </div>
    </template>
</div>

<!-- Create/Edit Role Modal -->
{% call modal_simple('roleModal', 'editingRole ? "Edit Role" : "Create Role"', 'showRoleModal') %}
<div class="space-y-4">
    <!-- Role Name -->
    <div>
        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Role Name</label>
        <input
            type="text"
            x-model="roleForm.name"
            placeholder="e.g. Content Editor"
            class="w-full px-3 py-2 text-sm border rounded-lg dark:bg-gray-700 dark:border-gray-600 dark:text-gray-300 focus:ring-purple-500 focus:border-purple-500"
        />
    </div>

    <!-- Permission Matrix -->
    <div>
        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Permissions</label>
        <div class="max-h-96 overflow-y-auto border rounded-lg dark:border-gray-600">
            <template x-for="category in permissionCategories" :key="category.id">
                <div class="border-b last:border-b-0 dark:border-gray-600">
                    <div class="px-4 py-2 bg-gray-50 dark:bg-gray-700/50 flex items-center justify-between">
                        <span class="text-sm font-semibold text-gray-700 dark:text-gray-300 capitalize" x-text="category.label"></span>
                        <button
                            @click="toggleCategory(category)"
                            class="text-xs text-purple-600 hover:text-purple-800 dark:text-purple-400"
                            x-text="isCategoryFullySelected(category) ? 'Deselect All' : 'Select All'"
                        ></button>
                    </div>
                    <div class="px-4 py-2 grid grid-cols-1 sm:grid-cols-2 gap-1">
                        <template x-for="perm in category.permissions" :key="perm.id">
                            <label class="flex items-start gap-2 py-1.5 cursor-pointer">
                                <input
                                    type="checkbox"
                                    :value="perm.id"
                                    :checked="roleForm.permissions.includes(perm.id)"
                                    @change="togglePermission(perm.id)"
                                    class="w-4 h-4 mt-0.5 text-purple-600 border-gray-300 rounded focus:ring-purple-500"
                                />
                                <div class="flex flex-col">
                                    <span class="flex items-center gap-1">
                                        <span class="text-sm text-gray-700 dark:text-gray-300" x-text="perm.label"></span>
                                        <span
                                            x-show="perm.description"
                                            :title="perm.description"
                                            x-html="$icon('information-circle', 'w-3.5 h-3.5 text-gray-400 dark:text-gray-500 cursor-help')"
                                        ></span>
                                    </span>
                                    <span class="text-xs text-gray-400 dark:text-gray-500 font-mono" x-text="perm.id"></span>
                                </div>
                                <template x-if="perm.is_owner_only">
                                    <span class="ml-auto px-1.5 py-0.5 text-xs bg-amber-100 text-amber-700 rounded dark:bg-amber-900/30 dark:text-amber-400">Owner</span>
                                </template>
                            </label>
                        </template>
                    </div>
                </div>
            </template>
        </div>
    </div>

    <!-- Actions -->
    <div class="flex justify-end gap-3 pt-2">
        <button
            @click="showRoleModal = false"
            class="px-4 py-2 text-sm font-medium text-gray-700 bg-gray-100 rounded-lg hover:bg-gray-200 dark:text-gray-300 dark:bg-gray-700 dark:hover:bg-gray-600"
        >Cancel</button>
        <button
            @click="saveRole()"
            :disabled="saving || !roleForm.name.trim()"
            class="px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700 disabled:opacity-50"
        >
            <span x-show="saving" class="inline-block animate-spin mr-1">&#8635;</span>
            <span x-text="editingRole ? 'Update Role' : 'Create Role'"></span>
        </button>
    </div>
</div>
{% endcall %}
{% endblock %}

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