<x-app-layout>
    <x-slot name="header">
        <div class="flex justify-between items-center">
            <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                {{ __('Admin - Manage Users') }}
            </h2>
            <a href="{{ route('admin.dashboard') }}" class="inline-flex items-center px-4 py-2 bg-gray-600 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 transition ease-in-out duration-150">
                Back to Admin
            </a>
        </div>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <!-- Search and Filters -->
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg mb-6">
                <div class="p-6">
                    <form method="GET" class="space-y-4">
                        <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                            <!-- Search -->
                            <div>
                                <x-input-label for="search" :value="__('Search')" />
                                <x-text-input id="search" class="block mt-1 w-full" type="text" name="search" :value="request('search')" placeholder="Name or email..." />
                            </div>

                            <!-- Status -->
                            <div>
                                <x-input-label for="status" :value="__('Status')" />
                                <select id="status" name="status" class="mt-1 block w-full border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm">
                                    <option value="pending"  {{ request('status', 'pending') == 'pending'  ? 'selected' : '' }}>Unreviewed</option>
                                    <option value="flagged"  {{ request('status') == 'flagged'  ? 'selected' : '' }}>Flagged</option>
                                    <option value=""         {{ request('status') === ''        ? 'selected' : '' }}>All Users</option>
                                    <option value="active"   {{ request('status') == 'active'   ? 'selected' : '' }}>Active</option>
                                    <option value="inactive" {{ request('status') == 'inactive' ? 'selected' : '' }}>Banned</option>
                                    <option value="admin"    {{ request('status') == 'admin'    ? 'selected' : '' }}>Admin</option>
                                    <option value="deleted"  {{ request('status') == 'deleted'  ? 'selected' : '' }}>Deleted Accounts</option>
                                </select>
                            </div>

                            <!-- Submit -->
                            <div class="flex items-end">
                                <x-primary-button type="submit" class="w-full">
                                    {{ __('Filter') }}
                                </x-primary-button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            <!-- Users Table -->
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6">
                    @if($users->count() > 0)
                        <div class="overflow-x-auto">
                            <table class="min-w-full divide-y divide-gray-200">
                                <thead class="bg-gray-50">
                                    <tr>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">User</th>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Activity</th>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Items</th>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Transactions</th>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Joined</th>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
                                    </tr>
                                </thead>
                                <tbody class="bg-white divide-y divide-gray-200">
                                    @foreach($users as $user)
                                        <tr class="hover:bg-gray-50">
                                            <td class="px-6 py-4 whitespace-nowrap">
                                                <div class="flex items-center">
                                                    <div class="flex-shrink-0 h-12 w-12">
                                                        <div class="h-12 w-12 rounded-full bg-gray-200 flex items-center justify-center">
                                                            <span class="text-gray-500 font-medium text-lg">
                                                                {{ strtoupper(substr($user->name, 0, 1)) }}
                                                            </span>
                                                        </div>
                                                    </div>
                                                    <div class="ml-4">
                                                        <div class="text-sm font-medium text-gray-900">
                                                            <a href="{{ route('admin.users.show', $user) }}" class="hover:text-blue-600">
                                                                {{ $user->name }}
                                                            </a>
                                                        </div>
                                                        <div class="text-sm text-gray-500">{{ $user->email }}</div>
                                                        @if($user->location)
                                                            <div class="text-xs text-gray-400">{{ $user->location }}</div>
                                                        @endif
                                                    </div>
                                                </div>
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                                <div class="text-sm">
                                                    @if($user->rating > 0)
                                                        <div class="flex items-center">
                                                            <span class="text-yellow-400">★</span>
                                                            <span class="ml-1">{{ number_format($user->rating, 1) }}</span>
                                                        </div>
                                                    @else
                                                        <span class="text-gray-400">No rating</span>
                                                    @endif
                                                </div>
                                                <div class="text-xs text-gray-500">
                                                    Last seen: {{ $user->updated_at->diffForHumans() }}
                                                </div>
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                                <div class="text-center">
                                                    <div class="font-medium">{{ $user->items_count }}</div>
                                                    <div class="text-xs text-gray-500">items</div>
                                                </div>
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                                <div class="flex space-x-4">
                                                    <div class="text-center">
                                                        <div class="font-medium text-green-600">{{ $user->sold_transactions_count }}</div>
                                                        <div class="text-xs text-gray-500">sold</div>
                                                    </div>
                                                    <div class="text-center">
                                                        <div class="font-medium text-blue-600">{{ $user->bought_transactions_count }}</div>
                                                        <div class="text-xs text-gray-500">bought</div>
                                                    </div>
                                                </div>
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap">
                                                <div class="flex flex-col space-y-1">
                                                    @if($user->trashed())
                                                        <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-gray-100 text-gray-800">
                                                            Deleted
                                                        </span>
                                                    @else
                                                        <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full {{ $user->is_active ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800' }}">
                                                            {{ $user->is_active ? 'Active' : 'Banned' }}
                                                        </span>
                                                    @endif
                                                    @if($user->is_admin)
                                                        <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-purple-100 text-purple-800">
                                                            Admin
                                                        </span>
                                                    @endif
                                                    @if(!$user->trashed() && $user->is_active)
                                                        @if($user->review_status === 'pending')
                                                            <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-yellow-100 text-yellow-800">Pending</span>
                                                        @elseif($user->review_status === 'flagged')
                                                            <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-orange-100 text-orange-800">Flagged</span>
                                                        @elseif($user->review_status === 'approved')
                                                            <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-teal-100 text-teal-800">Approved</span>
                                                        @endif
                                                    @endif
                                                </div>
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                                {{ $user->created_at->format('M j, Y') }}
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                                <div class="flex flex-col space-y-1">
                                                    <a href="{{ route('admin.users.show', $user) }}" class="text-blue-600 hover:text-blue-900">View</a>
                                                    
                                                    @if($user->id !== auth()->id())
                                                        <form method="POST" action="{{ route('admin.users.impersonate', $user) }}" class="inline">
                                                            @csrf
                                                            <button type="submit" class="text-indigo-600 hover:text-indigo-900 text-left" onclick="return confirm('Login as {{ $user->name }}? You will be able to see the site as they do.')">Login as User</button>
                                                        </form>
                                                    @endif
                                                    
                                                    @if($user->is_active && !$user->is_admin)
                                                        <form method="POST" action="{{ route('admin.users.ban', $user) }}" class="inline">
                                                            @csrf
                                                            <button type="submit" class="text-red-600 hover:text-red-900 text-left" onclick="return confirm('Ban {{ $user->name }} ({{ $user->email }})?\n\n{{ $user->location ? 'Shop: ' . $user->location : 'No shop location' }}\n\nThis cannot be easily undone.')">Ban</button>
                                                        </form>
                                                    @elseif(!$user->is_active)
                                                        <form method="POST" action="{{ route('admin.users.unban', $user) }}" class="inline">
                                                            @csrf
                                                            <button type="submit" class="text-green-600 hover:text-green-900 text-left">Unban</button>
                                                        </form>
                                                    @endif
                                                    @if($user->review_status !== 'approved')
                                                        <form method="POST" action="{{ route('admin.users.approve', $user) }}" class="inline">
                                                            @csrf
                                                            <button type="submit" class="text-teal-600 hover:text-teal-900 text-left">Approve</button>
                                                        </form>
                                                    @endif
                                                    @if($user->review_status !== 'flagged')
                                                        <form method="POST" action="{{ route('admin.users.flag', $user) }}" class="inline">
                                                            @csrf
                                                            <button type="submit" class="text-orange-600 hover:text-orange-900 text-left">Flag</button>
                                                        </form>
                                                    @endif

                                                </div>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>

                        <!-- Pagination -->
                        <div class="mt-6">
                            {{ $users->links() }}
                        </div>
                    @else
                        <div class="text-center py-12">
                            <h3 class="text-lg font-semibold text-gray-900 mb-2">No users found</h3>
                            <p class="text-gray-600">Try adjusting your search or filter criteria.</p>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</x-app-layout>