<x-app-layout>
    <x-slot:heading>Users</x-slot:heading>

    <x-page-header title="Users" description="Manage farm staff accounts and roles.">
        <x-slot:actions>
            <a href="{{ route('users.create') }}"
               class="inline-flex items-center gap-2 rounded-lg bg-primary-600 px-4 py-2 text-sm font-semibold text-white hover:bg-primary-700">
                + Add User
            </a>
        </x-slot:actions>
    </x-page-header>

    <div class="rounded-xl border border-gray-100 bg-white shadow-sm overflow-hidden">
        <table class="w-full text-sm">
            <thead class="bg-gray-50 text-xs uppercase tracking-wider text-gray-500">
                <tr>
                    <th class="px-5 py-3 text-left">Name</th>
                    <th class="px-5 py-3 text-left">Email</th>
                    <th class="px-5 py-3 text-left">Phone</th>
                    <th class="px-5 py-3 text-left">Role</th>
                    <th class="px-5 py-3 text-center">Active</th>
                    <th class="px-5 py-3 text-right"></th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-50">
                @foreach($users as $user)
                <tr class="hover:bg-gray-50">
                    <td class="px-5 py-3">
                        <div class="flex items-center gap-3">
                            <div class="flex h-8 w-8 items-center justify-center rounded-full bg-primary-100 text-xs font-semibold text-primary-700">
                                {{ strtoupper(substr($user->name, 0, 1)) }}
                            </div>
                            <span class="font-medium text-gray-900">{{ $user->name }}</span>
                            @if($user->id === auth()->id())
                                <span class="rounded-full bg-gray-100 px-1.5 py-0.5 text-xs text-gray-500">You</span>
                            @endif
                        </div>
                    </td>
                    <td class="px-5 py-3 text-gray-600">{{ $user->email }}</td>
                    <td class="px-5 py-3 text-gray-600">{{ $user->phone ?? '—' }}</td>
                    <td class="px-5 py-3">
                        @php
                            $roleColours = ['admin' => 'bg-purple-100 text-purple-800', 'manager' => 'bg-blue-100 text-blue-800', 'farm_hand' => 'bg-green-100 text-green-800', 'viewer' => 'bg-gray-100 text-gray-700'];
                        @endphp
                        <span class="rounded-full px-2 py-0.5 text-xs font-medium {{ $roleColours[$user->role] ?? '' }} capitalize">
                            {{ ucwords(str_replace('_', ' ', $user->role)) }}
                        </span>
                    </td>
                    <td class="px-5 py-3 text-center">
                        @if($user->is_active)
                            <span class="inline-block h-2 w-2 rounded-full bg-green-500"></span>
                        @else
                            <span class="inline-block h-2 w-2 rounded-full bg-gray-300"></span>
                        @endif
                    </td>
                    <td class="px-5 py-3 text-right flex items-center justify-end gap-3">
                        <a href="{{ route('users.edit', $user) }}" class="text-xs font-medium text-primary-600 hover:underline">Edit</a>
                        @if($user->id !== auth()->id() && !$user->isAdmin())
                        <form method="POST" action="{{ route('impersonate.start', $user) }}">
                            @csrf
                            <button type="submit" class="text-xs font-medium text-amber-600 hover:text-amber-800">Impersonate</button>
                        </form>
                        @endif
                        @if($user->id !== auth()->id())
                        <form method="POST" action="{{ route('users.destroy', $user) }}"
                              onsubmit="return confirm('Remove this user?')">
                            @csrf @method('DELETE')
                            <button type="submit" class="text-xs text-red-400 hover:text-red-600">Remove</button>
                        </form>
                        @endif
                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</x-app-layout>
