<x-app-layout>
    <x-slot name="header">
        {{ __('User Management') }}
    </x-slot>

    <div class="p-6 space-y-6">
        @if (session('status'))
            <div class="rounded-md bg-green-50 border border-green-200 px-4 py-3 text-sm text-green-800">
                {{ session('status') }}
            </div>
        @endif

        @if ($errors->any())
            <div class="rounded-md bg-red-50 border border-red-200 px-4 py-3 text-sm text-red-800">
                <ul class="list-disc pl-5 space-y-1">
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        <div class="bg-white border border-gray-200 rounded-xl shadow-sm p-5 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3">
            <div class="min-w-0">
                <h1 class="text-xl font-semibold text-gray-900">Users</h1>
                <p class="mt-1 text-sm text-gray-600">
                    Manage users for this dealer context. Roles and departments are dealer-scoped.
                </p>
            </div>

            @can('settings.users.manage')
                <a
                    href="{{ route('settings.users.create') }}"
                    class="inline-flex w-full sm:w-auto justify-center items-center rounded-lg bg-[var(--dealer-accent-color)] px-3 py-1.5 text-xs sm:px-4 sm:py-2 sm:text-sm font-semibold text-[var(--dealer-accent-text-color)] shadow-sm hover:bg-[var(--dealer-accent-hover-color)] whitespace-nowrap"
                >
                    Add User
                </a>
            @endcan
        </div>

        <div class="bg-white border border-gray-200 rounded-xl shadow-sm p-4 space-y-4">
            <form method="GET" action="{{ route('settings.users.index') }}" class="flex flex-wrap items-end gap-3">
                <div class="w-full sm:w-64">
                    <label for="q" class="block text-xs font-medium text-gray-600">Search</label>
                    <input
                        id="q"
                        name="q"
                        type="text"
                        value="{{ request('q') }}"
                        class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-red-500 focus:ring-red-500 text-sm"
                        placeholder="Name, surname, email"
                    >
                </div>

                <div>
                    <label for="status" class="block text-xs font-medium text-gray-600">Status</label>
                    <select
                        id="status"
                        name="status"
                        class="mt-1 block w-40 rounded-md border-gray-300 shadow-sm focus:border-red-500 focus:ring-red-500 text-sm"
                    >
                        <option value="">All</option>
                        <option value="active" @selected(request('status') === 'active')>Active</option>
                        <option value="disabled" @selected(request('status') === 'disabled')>Disabled</option>
                    </select>
                </div>

                <div class="flex-1"></div>

                <div>
                    <button
                        type="submit"
                        class="inline-flex items-center rounded-md bg-gray-900 px-4 py-2 text-sm font-semibold text-white shadow-sm hover:bg-gray-800"
                    >
                        Filter
                    </button>
                </div>
            </form>
        </div>

        <div class="bg-white border border-gray-200 rounded-xl shadow-sm overflow-hidden">
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200 text-sm">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-4 py-2 text-left font-semibold text-gray-700">Name</th>
                            <th class="px-4 py-2 text-left font-semibold text-gray-700">Email</th>
                            <th class="px-4 py-2 text-left font-semibold text-gray-700">Department</th>
                            <th class="px-4 py-2 text-left font-semibold text-gray-700">Branch</th>
                            <th class="px-4 py-2 text-left font-semibold text-gray-700">Roles</th>
                            <th class="px-4 py-2 text-left font-semibold text-gray-700">Status</th>
                            <th class="px-4 py-2 text-left font-semibold text-gray-700">Created</th>
                            <th class="px-4 py-2 text-center font-semibold text-gray-700">Actions</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100">
                        @forelse ($users as $user)
                            <tr>
                                <td class="px-4 py-2 text-gray-900">
                                    {{ $user->full_name }}
                                </td>
                                <td class="px-4 py-2 text-gray-700">
                                    {{ $user->email }}
                                    @if ($user->is_builtin)
                                        <span class="ml-1 inline-flex items-center rounded-full bg-blue-50 px-1.5 py-0.5 text-[10px] font-medium text-blue-700 align-middle">
                                            Built-in
                                        </span>
                                    @endif
                                </td>
                                <td class="px-4 py-2 text-gray-700">
                                    {{ optional($user->department)->name ?? '—' }}
                                </td>
                                <td class="px-4 py-2 text-gray-700">
                                    {{ optional($user->branch)->name ?? '—' }}
                                </td>
                                <td class="px-4 py-2 text-gray-700">
                                    {{ $user->roles->pluck('name')->join(', ') ?: '—' }}
                                </td>
                                <td class="px-4 py-2">
                                    @if ($user->is_disabled)
                                        <span class="inline-flex items-center rounded-full bg-gray-100 px-2 py-0.5 text-xs font-medium text-gray-700">
                                            Disabled
                                        </span>
                                    @else
                                        <span class="inline-flex items-center rounded-full bg-green-50 px-2 py-0.5 text-xs font-medium text-green-700">
                                            Active
                                        </span>
                                    @endif
                                </td>
                                <td class="px-4 py-2 text-gray-500">
                                    {{ $user->created_at?->format('Y-m-d') ?? '—' }}
                                </td>
                                <td class="px-4 py-2">
                                    <div class="flex items-center justify-end gap-2">
                                        @can('settings.users.manage')
                                            <a
                                                href="{{ route('settings.users.edit', $user->id) }}"
                                                class="inline-flex items-center rounded-md border border-gray-200 px-2 py-1 text-xs text-gray-700 hover:bg-gray-50 whitespace-nowrap"
                                            >
                                                Edit
                                            </a>
                                        @endcan

                                        @can('settings.users.security')
                                            @if (auth()->id() !== $user->id)
                                                <form
                                                    id="user-toggle-{{ $user->id }}"
                                                    method="POST"
                                                    action="{{ route('settings.users.toggle-disabled', $user->id) }}"
                                                >
                                                    @csrf
                                                    @method('PATCH')
                                                    <button
                                                        type="button"
                                                        @click="$store.__confirm.openConfirm('Are you sure you want to change this user\'s status?', 'user-toggle-{{ $user->id }}')"
                                                        class="inline-flex items-center rounded-md border border-gray-200 px-2 py-1 text-xs text-gray-700 hover:bg-gray-50 whitespace-nowrap"
                                                    >
                                                        {{ $user->is_disabled ? 'Enable' : 'Disable' }}
                                                    </button>
                                                </form>

                                                <form
                                                    id="user-reset-{{ $user->id }}"
                                                    method="POST"
                                                    action="{{ route('settings.users.reset-password', $user->id) }}"
                                                >
                                                    @csrf
                                                    <button
                                                        type="button"
                                                        @click="$store.__confirm.openConfirm('Reset password and force change on next login?', 'user-reset-{{ $user->id }}')"
                                                        class="inline-flex items-center rounded-md border border-gray-200 px-2 py-1 text-xs text-gray-700 hover:bg-gray-50 whitespace-nowrap"
                                                    >
                                                        Reset Password
                                                    </button>
                                                </form>

                                                @if (! $user->is_builtin)
                                                    <form
                                                        id="user-delete-{{ $user->id }}"
                                                        method="POST"
                                                        action="{{ route('settings.users.destroy', $user->id) }}"
                                                    >
                                                        @csrf
                                                        @method('DELETE')
                                                        <button
                                                            type="button"
                                                            @click="$store.__confirm.openConfirm('Are you sure you want to delete this user?', 'user-delete-{{ $user->id }}')"
                                                            class="inline-flex items-center rounded-md border border-red-200 px-2 py-1 text-xs text-red-700 hover:bg-red-50 whitespace-nowrap"
                                                        >
                                                            Delete
                                                        </button>
                                                    </form>
                                                @endif
                                            @endif
                                        @endcan
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="px-4 py-4 text-center text-sm text-gray-500">
                                    No users found.
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <div class="px-4 py-3 border-t border-gray-100">
                {{ $users->links() }}
            </div>
        </div>

        @if ($deletedUsers->isNotEmpty())
            <div class="bg-white border border-gray-200 rounded-xl shadow-sm overflow-hidden">
                <div class="px-4 py-3 border-b border-gray-100 flex items-center justify-between">
                    <div>
                        <h2 class="text-sm font-semibold text-gray-900">Deleted Users</h2>
                        <p class="mt-1 text-xs text-gray-500">
                            These users have been soft deleted and are not able to log in.
                        </p>
                    </div>
                </div>

                <div class="overflow-x-auto">
                    <table class="min-w-full divide-y divide-gray-200 text-sm">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-4 py-2 text-left font-semibold text-gray-700">Name</th>
                                <th class="px-4 py-2 text-left font-semibold text-gray-700">Email</th>
                                <th class="px-4 py-2 text-left font-semibold text-gray-700">Department</th>
                                <th class="px-4 py-2 text-left font-semibold text-gray-700">Roles</th>
                                <th class="px-4 py-2 text-left font-semibold text-gray-700">Deleted At</th>
                                <th class="px-4 py-2 text-center font-semibold text-gray-700">Actions</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-100">
                            @foreach ($deletedUsers as $user)
                                <tr>
                                    <td class="px-4 py-2 text-gray-900">
                                        {{ $user->full_name }}
                                    </td>
                                    <td class="px-4 py-2 text-gray-700">
                                        {{ $user->email }}
                                    </td>
                                    <td class="px-4 py-2 text-gray-700">
                                        {{ optional($user->department)->name ?? '—' }}
                                    </td>
                                    <td class="px-4 py-2 text-gray-700">
                                        {{ $user->roles->pluck('name')->join(', ') ?: '—' }}
                                    </td>
                                    <td class="px-4 py-2 text-gray-500">
                                        {{ $user->deleted_at?->format('Y-m-d H:i') ?? '—' }}
                                    </td>
                                    <td class="px-4 py-2">
                                        <div class="flex items-center justify-center">
                                            <form
                                                method="POST"
                                                action="{{ route('settings.users.restore', $user->id) }}"
                                            >
                                                @csrf
                                                @method('PATCH')
                                                <button
                                                    type="submit"
                                                    class="inline-flex items-center rounded-md border border-gray-200 px-3 py-1.5 text-xs font-medium text-gray-700 hover:bg-gray-50 whitespace-nowrap"
                                                >
                                                    Restore
                                                </button>
                                            </form>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        @endif
    </div>
</x-app-layout>

