<div class="p-6">
    <!-- Header -->
    <div class="flex items-center justify-between mb-6">
        <div>
            <h1 class="text-2xl font-semibold text-zinc-900 dark:text-white">Manage Roles & Permissions</h1>
            <p class="text-zinc-600 dark:text-zinc-400">
                Configure access control for {{ $user->name }}
                @if($user->customer)
                    <span class="inline-flex items-center px-2 py-1 ml-2 text-xs font-medium bg-purple-100 dark:bg-purple-900 text-purple-800 dark:text-purple-200 rounded">
                        Customer Account
                    </span>
                @endif
            </p>
        </div>
        <div class="flex items-center space-x-3">
            @if($user->customer)
            <a href="{{ route('customers.show', $user->customer) }}" 
               class="inline-flex items-center px-4 py-2 text-purple-700 dark:text-purple-300 bg-purple-100 dark:bg-purple-700 text-sm font-medium rounded-md hover:bg-purple-200 dark:hover:bg-purple-600 transition-colors" 
               wire:navigate>
                View Customer
            </a>
            @endif
            <a href="{{ route('users.show', $user) }}" 
               class="inline-flex items-center px-4 py-2 text-zinc-700 dark:text-zinc-300 bg-zinc-100 dark:bg-zinc-700 text-sm font-medium rounded-md hover:bg-zinc-200 dark:hover:bg-zinc-600 transition-colors" 
               wire:navigate>
                Back to User
            </a>
        </div>
    </div>

    @if($user->customer)
    <!-- Customer Portal Notice -->
    <div class="bg-purple-50 dark:bg-purple-900/20 border border-purple-200 dark:border-purple-800 rounded-lg p-4 mb-6">
        <div class="flex items-center">
            <svg class="w-5 h-5 text-purple-600 dark:text-purple-400 mr-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
            </svg>
            <div>
                <h3 class="text-sm font-medium text-purple-800 dark:text-purple-200">Customer Account</h3>
                <p class="text-sm text-purple-600 dark:text-purple-400">
                    This user is linked to customer #{{ $user->customer->id }} ({{ $user->customer->first_name }} {{ $user->customer->last_name }}). 
                    For customer portal access, use the "Customer Portal" preset.
                </p>
            </div>
        </div>
    </div>

    @unless($user->hasRole('customer_portal'))
    <!-- Customer Portal Role Warning -->
    <div class="bg-yellow-50 dark:bg-yellow-900/20 border border-yellow-200 dark:border-yellow-800 rounded-lg p-4 mb-6">
        <div class="flex items-center">
            <svg class="w-5 h-5 text-yellow-600 dark:text-yellow-400 mr-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L3.732 15c-.77.833.192 2.5 1.732 2.5z"></path>
            </svg>
            <div>
                <h3 class="text-sm font-medium text-yellow-800 dark:text-yellow-200">Missing Customer Portal Access</h3>
                <p class="text-sm text-yellow-600 dark:text-yellow-400">
                    This customer user doesn't have the "Customer Portal" role. Click the preset above to grant customer portal access.
                </p>
            </div>
        </div>
    </div>
    @endunless
    @endif

    <!-- Role Management -->
    <div class="bg-white dark:bg-zinc-800 border border-zinc-200 dark:border-zinc-700 rounded-lg p-6 mb-6">
        <h3 class="text-lg font-medium text-zinc-900 dark:text-white mb-4">Role Assignment</h3>
        
        <!-- Quick Role Presets -->
        <div class="mb-6">
            <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">
                Quick Presets
            </label>
            <div class="flex flex-wrap gap-2">
                <button type="button" 
                        wire:click="applyRolePreset('manager')"
                        class="px-3 py-1 text-xs font-medium text-blue-600 dark:text-blue-400 bg-blue-50 dark:bg-blue-900/20 rounded-md hover:bg-blue-100 dark:hover:bg-blue-900/30 transition-colors">
                    Manager Preset
                </button>
                <button type="button" 
                        wire:click="applyRolePreset('technician')"
                        class="px-3 py-1 text-xs font-medium text-green-600 dark:text-green-400 bg-green-50 dark:bg-green-900/20 rounded-md hover:bg-green-100 dark:hover:bg-green-900/30 transition-colors">
                    Technician Preset
                </button>
                <button type="button" 
                        wire:click="applyRolePreset('receptionist')"
                        class="px-3 py-1 text-xs font-medium text-purple-600 dark:text-purple-400 bg-purple-50 dark:bg-purple-900/20 rounded-md hover:bg-purple-100 dark:hover:bg-purple-900/30 transition-colors">
                    Receptionist Preset
                </button>
                <button type="button" 
                        wire:click="applyRolePreset('parts_clerk')"
                        class="px-3 py-1 text-xs font-medium text-amber-600 dark:text-amber-400 bg-amber-50 dark:bg-amber-900/20 rounded-md hover:bg-amber-100 dark:hover:bg-amber-900/30 transition-colors">
                    Parts Clerk Preset
                </button>
                <button type="button" 
                        wire:click="applyRolePreset('customer_portal')"
                        class="px-3 py-1 text-xs font-medium text-pink-600 dark:text-pink-400 bg-pink-50 dark:bg-pink-900/20 rounded-md hover:bg-pink-100 dark:hover:bg-pink-900/30 transition-colors">
                    Customer Portal
                </button>
            </div>
        </div>

        <!-- Role Selection -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
            @foreach($availableRoles as $role)
            <label class="flex items-start p-4 bg-zinc-50 dark:bg-zinc-700 rounded-lg cursor-pointer hover:bg-zinc-100 dark:hover:bg-zinc-600 transition-colors">
                <input type="checkbox" 
                       wire:model="selectedRoles" 
                       value="{{ $role->id }}"
                       class="rounded border-zinc-300 dark:border-zinc-600 text-blue-600 shadow-sm focus:border-blue-500 focus:ring-blue-500 mt-1">
                <div class="ml-3">
                    <div class="text-sm font-medium text-zinc-900 dark:text-white">{{ $role->display_name }}</div>
                    @if($role->description)
                    <div class="text-xs text-zinc-500 dark:text-zinc-400 mt-1">{{ $role->description }}</div>
                    @endif
                    <div class="text-xs text-zinc-400 dark:text-zinc-500 mt-1">
                        {{ $role->permissions->count() }} permissions
                    </div>
                </div>
            </label>
            @endforeach
        </div>

        <div class="mt-6">
            <button wire:click="updateRoles" 
                    class="px-6 py-2 bg-blue-600 text-white text-sm font-medium rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 disabled:opacity-50 disabled:cursor-not-allowed"
                    wire:loading.attr="disabled">
                <span wire:loading.remove>Update Roles</span>
                <span wire:loading>Updating...</span>
            </button>
        </div>
    </div>

    <!-- Permission Management -->
    <div class="bg-white dark:bg-zinc-800 border border-zinc-200 dark:border-zinc-700 rounded-lg p-6">
        <div class="flex items-center justify-between mb-4">
            <h3 class="text-lg font-medium text-zinc-900 dark:text-white">Individual Permissions</h3>
            <div class="flex items-center space-x-3">
                <button wire:click="selectAllPermissions" 
                        class="text-xs text-blue-600 dark:text-blue-400 hover:text-blue-700 dark:hover:text-blue-300">
                    Select All
                </button>
                <button wire:click="deselectAllPermissions" 
                        class="text-xs text-red-600 dark:text-red-400 hover:text-red-700 dark:hover:text-red-300">
                    Deselect All
                </button>
            </div>
        </div>

        <!-- Permission Groups -->
        @foreach($groupedPermissions as $module => $permissions)
        <div class="mb-6">
            <div class="flex items-center justify-between mb-3">
                <h4 class="text-sm font-medium text-zinc-900 dark:text-white">{{ ucfirst($module) }} Module</h4>
                <div class="flex items-center space-x-2">
                    <button wire:click="selectModulePermissions('{{ $module }}')" 
                            class="text-xs text-blue-600 dark:text-blue-400 hover:text-blue-700 dark:hover:text-blue-300">
                        Select All
                    </button>
                    <button wire:click="deselectModulePermissions('{{ $module }}')" 
                            class="text-xs text-red-600 dark:text-red-400 hover:text-red-700 dark:hover:text-red-300">
                        Deselect All
                    </button>
                </div>
            </div>
            
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3">
                @foreach($permissions as $permission)
                <label class="flex items-center p-3 bg-zinc-50 dark:bg-zinc-700 rounded-md cursor-pointer hover:bg-zinc-100 dark:hover:bg-zinc-600 transition-colors">
                    <input type="checkbox" 
                           wire:model="selectedPermissions" 
                           value="{{ $permission->id }}"
                           class="rounded border-zinc-300 dark:border-zinc-600 text-blue-600 shadow-sm focus:border-blue-500 focus:ring-blue-500">
                    <div class="ml-3">
                        <div class="text-sm text-zinc-900 dark:text-white">
                            {{ str_replace($module.'.', '', $permission->name) }}
                        </div>
                        @if($permission->description)
                        <div class="text-xs text-zinc-500 dark:text-zinc-400 mt-1">{{ $permission->description }}</div>
                        @endif
                    </div>
                </label>
                @endforeach
            </div>
        </div>
        @endforeach

        <div class="mt-6 pt-6 border-t border-zinc-200 dark:border-zinc-700">
            <div class="flex items-center justify-between">
                <div class="text-sm text-zinc-600 dark:text-zinc-400">
                    Selected: {{ count($selectedPermissions) }} permissions
                </div>
                <button wire:click="updatePermissions" 
                        class="px-6 py-2 bg-green-600 text-white text-sm font-medium rounded-md hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-green-500 focus:ring-offset-2 disabled:opacity-50 disabled:cursor-not-allowed"
                        wire:loading.attr="disabled">
                    <span wire:loading.remove>Update Permissions</span>
                    <span wire:loading>Updating...</span>
                </button>
            </div>
        </div>
    </div>

    <!-- Bulk Actions -->
    <div class="mt-6 bg-zinc-50 dark:bg-zinc-900 border border-zinc-200 dark:border-zinc-700 rounded-lg p-6">
        <h3 class="text-lg font-medium text-zinc-900 dark:text-white mb-4">Bulk Actions</h3>
        <div class="flex flex-wrap gap-3">
            <button wire:click="removeAllRoles" 
                    wire:confirm="Are you sure you want to remove all roles from this user?"
                    class="px-4 py-2 text-sm font-medium text-red-600 dark:text-red-400 bg-red-50 dark:bg-red-900/20 rounded-md hover:bg-red-100 dark:hover:bg-red-900/30 transition-colors">
                Remove All Roles
            </button>
            <button wire:click="removeAllPermissions" 
                    wire:confirm="Are you sure you want to remove all individual permissions from this user?"
                    class="px-4 py-2 text-sm font-medium text-red-600 dark:text-red-400 bg-red-50 dark:bg-red-900/20 rounded-md hover:bg-red-100 dark:hover:bg-red-900/30 transition-colors">
                Remove All Permissions
            </button>
            <button wire:click="resetToDefault" 
                    wire:confirm="Are you sure you want to reset this user to default permissions?"
                    class="px-4 py-2 text-sm font-medium text-amber-600 dark:text-amber-400 bg-amber-50 dark:bg-amber-900/20 rounded-md hover:bg-amber-100 dark:hover:bg-amber-900/30 transition-colors">
                Reset to Default
            </button>
        </div>
    </div>
</div>
