@php
    /** @var \App\Models\Branch $branch */
    /** @var \Illuminate\Support\Collection|\App\Models\User[] $assignedUsers */
    /** @var \Illuminate\Support\Collection|\App\Models\User[] $availableUsers */
    /** @var \Illuminate\Support\Collection|\App\Models\Branch[] $otherBranches */
@endphp

<x-app-layout>
    <x-slot name="header">
        {{ __('Branch Users') }}
    </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 space-y-4">
            <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3">
                <div>
                    <h1 class="text-xl font-semibold text-gray-900">
                        Users for {{ $branch->name }}
                    </h1>
                    <p class="mt-1 text-sm text-gray-600">
                        Code: <span class="font-mono text-gray-800">{{ $branch->code ?? '—' }}</span>
                        · Status:
                        @if ($branch->is_active)
                            <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>
                        @else
                            <span class="inline-flex items-center rounded-full bg-gray-100 px-2 py-0.5 text-xs font-medium text-gray-700">
                                Inactive
                            </span>
                        @endif
                        @if ($branch->is_default)
                            <span class="inline-flex items-center rounded-full bg-blue-50 px-2 py-0.5 text-xs font-medium text-blue-700 ml-2">
                                Default branch
                            </span>
                        @endif
                    </p>
                </div>

                <a
                    href="{{ route('settings.branches.index') }}"
                    class="inline-flex items-center rounded-md border border-gray-200 px-3 py-1.5 text-xs sm:text-sm text-gray-700 hover:bg-gray-50 whitespace-nowrap"
                >
                    Back to Branches
                </a>
            </div>

            <div class="border-t border-gray-100 pt-4">
                <h2 class="text-sm font-semibold text-gray-800 mb-2">
                    Add user to this branch
                </h2>

                <form
                    method="POST"
                    action="{{ route('settings.branches.users.assign', $branch->id) }}"
                    class="flex flex-col sm:flex-row items-stretch sm:items-end gap-3"
                >
                    @csrf
                    <div class="flex-1">
                        <label for="user_id" class="block text-xs font-medium text-gray-700">User</label>
                        <select
                            id="user_id"
                            name="user_id"
                            class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-red-500 focus:ring-red-500 text-sm"
                        >
                            <option value="">Select a user…</option>
                            @foreach ($availableUsers as $user)
                                <option value="{{ $user->id }}">
                                    {{ $user->name }} {{ $user->surname }} — {{ $user->email }}
                                    @if ($user->branch && $user->branch_id !== $branch->id)
                                        (Current branch: {{ $user->branch->name }})
                                    @endif
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div>
                        <button
                            type="submit"
                            class="inline-flex items-center rounded-md bg-[var(--dealer-accent-color)] px-4 py-2 text-sm font-semibold text-[var(--dealer-accent-text-color)] shadow-sm hover:bg-[var(--dealer-accent-hover-color)] whitespace-nowrap"
                        >
                            Add to Branch
                        </button>
                    </div>
                </form>
            </div>
        </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">Roles</th>
                            <th class="px-4 py-2 text-left font-semibold text-gray-700">Status</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 ($assignedUsers as $user)
                            <tr>
                                <td class="px-4 py-2 text-gray-900">
                                    {{ $user->name }} {{ $user->surname }}
                                </td>
                                <td class="px-4 py-2 text-gray-700">
                                    {{ $user->email }}
                                </td>
                                <td class="px-4 py-2 text-gray-700">
                                    {{ $user->department->name ?? '—' }}
                                </td>
                                <td class="px-4 py-2 text-gray-700">
                                    @php
                                        $roles = $user->getRoleNames();
                                    @endphp
                                    @if ($roles->isEmpty())
                                        <span class="text-xs text-gray-400">No roles</span>
                                    @else
                                        <div class="flex flex-wrap gap-1">
                                            @foreach ($roles as $role)
                                                <span class="inline-flex items-center rounded-full bg-gray-100 px-2 py-0.5 text-xs font-medium text-gray-700">
                                                    {{ $role }}
                                                </span>
                                            @endforeach
                                        </div>
                                    @endif
                                </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">
                                    @if ($otherBranches->isEmpty())
                                        <span class="text-xs text-gray-400">
                                            No additional branches
                                        </span>
                                    @else
                                        <form
                                            method="POST"
                                            action="{{ route('settings.branches.users.reassign', [$branch->id, $user->id]) }}"
                                            class="flex items-center gap-2 justify-end"
                                        >
                                            @csrf

                                            <label for="new_branch_id_{{ $user->id }}" class="sr-only">New branch</label>
                                            <select
                                                id="new_branch_id_{{ $user->id }}"
                                                name="new_branch_id"
                                                class="rounded-md border-gray-300 shadow-sm focus:border-[var(--dealer-accent-color)] focus:ring-[var(--dealer-accent-color)] text-xs"
                                            >
                                                @foreach ($otherBranches as $otherBranch)
                                                    <option
                                                        value="{{ $otherBranch->id }}"
                                                        @selected($otherBranch->is_default)
                                                    >
                                                        {{ $otherBranch->name }}{{ $otherBranch->is_default ? ' (Default)' : '' }}
                                                    </option>
                                                @endforeach
                                            </select>

                                            <button
                                                type="submit"
                                                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"
                                            >
                                                Move to Branch
                                            </button>
                                        </form>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="px-4 py-4 text-center text-sm text-gray-500">
                                    No users are currently assigned to this branch.
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</x-app-layout>

