<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 - User Details') }}
            </h2>
            <div class="flex space-x-2">
                <x-secondary-button onclick="window.location='{{ route('admin.users.index') }}'">
                    Back to Users
                </x-secondary-button>
            </div>
        </div>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <!-- Admin Actions Bar -->
            <div class="bg-gray-50 border border-gray-200 rounded-lg p-4 mb-6">
                <div class="flex items-center justify-between">
                    <div class="flex items-center">
                        <span class="text-gray-700 font-medium">Admin Actions:</span>
                    </div>
                    <div class="flex space-x-2">
                        @if($user->id !== auth()->id())
                            <form method="POST" action="{{ route('admin.users.impersonate', $user) }}" class="inline">
                                @csrf
                                <x-secondary-button type="submit" onclick="return confirm('Login as {{ $user->name }}? You will be able to see the site as they do.')">
                                    <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"></path>
                                    </svg>
                                    Login as User
                                </x-secondary-button>
                            </form>
                        @endif

                        @if($user->is_active && !$user->is_admin)
                            <form method="POST" action="{{ route('admin.users.ban', $user) }}" class="inline">
                                @csrf
                                <x-danger-button onclick="return confirm('Ban this user? This will also disable all their items.')">
                                    Ban User
                                </x-danger-button>
                            </form>
                        @elseif(!$user->is_active)
                            <form method="POST" action="{{ route('admin.users.unban', $user) }}" class="inline">
                                @csrf
                                <x-secondary-button type="submit">
                                    Unban User
                                </x-secondary-button>
                            </form>
                        @endif
                        @if($user->review_status !== 'approved')
                            <form method="POST" action="{{ route('admin.users.approve', $user) }}" class="inline">
                                @csrf
                                <x-secondary-button type="submit">
                                    Approve User
                                </x-secondary-button>
                            </form>
                        @endif
                        @if($user->review_status !== 'flagged')
                            <form method="POST" action="{{ route('admin.users.flag', $user) }}" class="inline">
                                @csrf
                                <x-secondary-button type="submit">
                                    Flag User
                                </x-secondary-button>
                            </form>
                        @endif

                    </div>
                </div>
            </div>

            <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
                <!-- User Details -->
                <div class="lg:col-span-2 space-y-6">
                    <!-- Profile Info -->
                    <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                        <div class="p-6">
                            <div class="flex items-center mb-6">
                                <div class="flex-shrink-0 h-20 w-20">
                                    <div class="h-20 w-20 rounded-full bg-gray-200 flex items-center justify-center">
                                        <span class="text-gray-500 font-medium text-2xl">
                                            {{ strtoupper(substr($user->name, 0, 1)) }}
                                        </span>
                                    </div>
                                </div>
                                <div class="ml-6">
                                    <h1 class="text-2xl font-bold text-gray-900">{{ $user->name }}</h1>
                                    <p class="text-gray-600">{{ $user->email }}</p>
                                    <div class="flex items-center mt-2 space-x-4">
                                        <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full {{ $user->is_active ? 'bg-gray-100 text-gray-700' : 'bg-red-50 text-red-600' }}">
                                            {{ $user->is_active ? 'Active' : 'Banned' }}
                                        </span>
                                        @if($user->is_admin)
                                            <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-gray-100 text-gray-700">
                                                Admin
                                            </span>
                                        @endif
                                        @if($user->review_status === 'pending')
                                            <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-gray-100 text-gray-600">Pending Review</span>
                                        @elseif($user->review_status === 'flagged')
                                            <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-gray-100 text-gray-600">Flagged</span>
                                        @elseif($user->review_status === 'approved')
                                            <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-gray-100 text-gray-600">Approved</span>
                                        @endif
                                        @if($user->rating > 0)
                                            <div class="flex items-center">
                                                <span class="text-yellow-400">★</span>
                                                <span class="ml-1 text-sm font-medium">{{ number_format($user->rating, 1) }}</span>
                                            </div>
                                        @endif
                                    </div>
                                </div>
                            </div>

                            @if($user->bio || $user->location || $user->phone)
                                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                                    @if($user->location)
                                        <div>
                                            <span class="text-sm font-medium text-gray-600">Location:</span>
                                            <span class="text-sm text-gray-900 ml-2">{{ $user->location }}</span>
                                        </div>
                                    @endif
                                    @if($user->phone)
                                        <div>
                                            <span class="text-sm font-medium text-gray-600">Phone:</span>
                                            <span class="text-sm text-gray-900 ml-2">{{ $user->phone }}</span>
                                        </div>
                                    @endif
                                </div>
                                
                                @if($user->bio)
                                    <div class="mt-4">
                                        <span class="text-sm font-medium text-gray-600">Bio:</span>
                                        <p class="text-sm text-gray-900 mt-1">{{ $user->bio }}</p>
                                    </div>
                                @endif
                            @endif
                        </div>
                    </div>

                    <!-- User Items -->
                    <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                        <div class="p-6">
                            <div class="flex justify-between items-center mb-4">
                                <h3 class="text-lg font-semibold text-gray-900">Items ({{ $user->items->count() }})</h3>
                                <a href="{{ route('admin.items.index', ['search' => $user->email]) }}" class="text-gray-600 hover:opacity-60 transition-opacity text-sm">View All</a>
                            </div>
                            
                            @if($user->items->count() > 0)
                                <div class="space-y-3">
                                    @foreach($user->items->take(5) as $item)
                                        <div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
                                            <div class="flex items-center">
                                                <div class="flex-shrink-0 h-12 w-12">
                                                    @if($item->primaryPhoto)
                                                        <img class="h-12 w-12 rounded-lg object-cover" src="{{ asset($item->primaryPhoto->url) }}" alt="{{ $item->title }}">
                                                    @else
                                                        <div class="h-12 w-12 rounded-lg bg-gray-200 flex items-center justify-center">
                                                            <span class="text-gray-400 text-xs">No Image</span>
                                                        </div>
                                                    @endif
                                                </div>
                                                <div class="ml-4">
                                                    <p class="font-medium text-sm">
                                                        <a href="{{ route('admin.items.show', $item) }}" class="text-gray-700 hover:opacity-60 transition-opacity">
                                                            {{ Str::limit($item->title, 40) }}
                                                        </a>
                                                    </p>
                                                    <p class="text-xs text-gray-600">{{ $item->category?->name ?? 'No Category' }} • {{ $item->created_at->diffForHumans() }}</p>
                                                </div>
                                            </div>
                                            <div class="flex flex-col items-end space-y-1">
                                                @if($item->price)
                                                    <span class="text-sm font-medium">${{ number_format($item->price, 2) }}</span>
                                                @endif
                                                <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full {{ $item->status === 'active' ? 'bg-gray-100 text-gray-700' : ($item->status === 'removed' ? 'bg-red-50 text-red-600' : ($item->status === 'flagged' ? 'bg-gray-100 text-gray-600' : 'bg-gray-100 text-gray-700')) }}">
                                                    {{ ucfirst(str_replace('_', ' ', $item->status)) }}
                                                </span>
                                            </div>
                                        </div>
                                    @endforeach
                                    
                                    @if($user->items->count() > 5)
                                        <div class="text-center pt-2">
                                            <a href="{{ route('admin.items.index', ['search' => $user->email]) }}" class="text-gray-600 hover:opacity-60 transition-opacity text-sm">
                                                View {{ $user->items->count() - 5 }} more items
                                            </a>
                                        </div>
                                    @endif
                                </div>
                            @else
                                <p class="text-gray-500 text-sm">No items listed</p>
                            @endif
                        </div>
                    </div>

                    <!-- Transaction History -->
                    <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                        <div class="p-6">
                            <h3 class="text-lg font-semibold text-gray-900 mb-4">Transaction History</h3>
                            
                            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                                <!-- Sales -->
                                <div>
                                    <h4 class="font-medium text-gray-900 mb-3">Sales ({{ $user->soldTransactions->count() }})</h4>
                                    @if($user->soldTransactions->count() > 0)
                                        <div class="space-y-2">
                                            @foreach($user->soldTransactions->take(3) as $transaction)
                                                <div class="text-sm">
                                                    <p class="font-medium">{{ $transaction->item->title }}</p>
                                                    <p class="text-gray-600">Sold to {{ $transaction->buyer->name }}</p>
                                                    <p class="text-gray-500">${{ number_format($transaction->amount, 2) }} • {{ $transaction->created_at->format('M j, Y') }}</p>
                                                </div>
                                            @endforeach
                                        </div>
                                    @else
                                        <p class="text-gray-500 text-sm">No sales</p>
                                    @endif
                                </div>

                                <!-- Purchases -->
                                <div>
                                    <h4 class="font-medium text-gray-900 mb-3">Purchases ({{ $user->boughtTransactions->count() }})</h4>
                                    @if($user->boughtTransactions->count() > 0)
                                        <div class="space-y-2">
                                            @foreach($user->boughtTransactions->take(3) as $transaction)
                                                <div class="text-sm">
                                                    <p class="font-medium">{{ $transaction->item->title }}</p>
                                                    <p class="text-gray-600">Bought from {{ $transaction->seller->name }}</p>
                                                    <p class="text-gray-500">${{ number_format($transaction->amount, 2) }} • {{ $transaction->created_at->format('M j, Y') }}</p>
                                                </div>
                                            @endforeach
                                        </div>
                                    @else
                                        <p class="text-gray-500 text-sm">No purchases</p>
                                    @endif
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Feedback -->
                    <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                        <div class="p-6">
                            <h3 class="text-lg font-semibold text-gray-900 mb-4">Feedback</h3>
                            
                            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                                <!-- Received Feedback -->
                                <div>
                                    <h4 class="font-medium text-gray-900 mb-3">Received ({{ $user->receivedFeedback->count() }})</h4>
                                    @if($user->receivedFeedback->count() > 0)
                                        <div class="space-y-3">
                                            @foreach($user->receivedFeedback->take(3) as $feedback)
                                                <div class="text-sm border-l-4 border-gray-200 pl-3">
                                                    <div class="flex items-center mb-1">
                                                        <div class="flex items-center">
                                                            @for($i = 1; $i <= 5; $i++)
                                                                <span class="text-{{ $i <= $feedback->rating ? 'yellow' : 'gray' }}-400">★</span>
                                                            @endfor
                                                        </div>
                                                        <span class="ml-2 text-gray-600">{{ $feedback->rating }}/5</span>
                                                    </div>
                                                    @if($feedback->comment)
                                                        <p class="text-gray-700">{{ $feedback->comment }}</p>
                                                    @endif
                                                    <p class="text-gray-500 mt-1">From {{ $feedback->giver->name }} • {{ $feedback->created_at->diffForHumans() }}</p>
                                                </div>
                                            @endforeach
                                        </div>
                                    @else
                                        <p class="text-gray-500 text-sm">No feedback received</p>
                                    @endif
                                </div>

                                <!-- Given Feedback -->
                                <div>
                                    <h4 class="font-medium text-gray-900 mb-3">Given ({{ $user->givenFeedback->count() }})</h4>
                                    @if($user->givenFeedback->count() > 0)
                                        <div class="space-y-3">
                                            @foreach($user->givenFeedback->take(3) as $feedback)
                                                <div class="text-sm border-l-4 border-gray-200 pl-3">
                                                    <div class="flex items-center mb-1">
                                                        <div class="flex items-center">
                                                            @for($i = 1; $i <= 5; $i++)
                                                                <span class="text-{{ $i <= $feedback->rating ? 'yellow' : 'gray' }}-400">★</span>
                                                            @endfor
                                                        </div>
                                                        <span class="ml-2 text-gray-600">{{ $feedback->rating }}/5</span>
                                                    </div>
                                                    @if($feedback->comment)
                                                        <p class="text-gray-700">{{ $feedback->comment }}</p>
                                                    @endif
                                                    <p class="text-gray-500 mt-1">To {{ $feedback->receiver->name }} • {{ $feedback->created_at->diffForHumans() }}</p>
                                                </div>
                                            @endforeach
                                        </div>
                                    @else
                                        <p class="text-gray-500 text-sm">No feedback given</p>
                                    @endif
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Sidebar -->
                <div class="space-y-6">
                    <!-- Account Stats -->
                    <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                        <div class="p-6">
                            <h3 class="text-lg font-semibold text-gray-900 mb-4">Account Stats</h3>
                            <div class="space-y-3">
                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Member Since:</span>
                                    <span class="text-sm text-gray-900">{{ $user->created_at->format('M j, Y') }}</span>
                                </div>
                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Last Active:</span>
                                    <span class="text-sm text-gray-900">{{ $user->updated_at->diffForHumans() }}</span>
                                </div>
                                @if($user->reviewed_at)
                                    <div class="flex justify-between">
                                        <span class="text-sm font-medium text-gray-600">Reviewed:</span>
                                        <span class="text-sm text-gray-900">{{ $user->reviewed_at->format('M j, Y') }}</span>
                                    </div>
                                    @if($user->reviewer)
                                        <div class="flex justify-between">
                                            <span class="text-sm font-medium text-gray-600">Reviewed By:</span>
                                            <span class="text-sm text-gray-900">{{ $user->reviewer->name }}</span>
                                        </div>
                                    @endif
                                @endif
                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Items Listed:</span>
                                    <span class="text-sm text-gray-900">{{ $user->items->count() }}</span>
                                </div>
                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Items Sold:</span>
                                    <span class="text-sm text-gray-900 font-medium">{{ $user->soldTransactions->count() }}</span>
                                </div>
                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Items Bought:</span>
                                    <span class="text-sm text-gray-900 font-medium">{{ $user->boughtTransactions->count() }}</span>
                                </div>
                                @if($user->rating > 0)
                                    <div class="flex justify-between">
                                        <span class="text-sm font-medium text-gray-600">Rating:</span>
                                        <div class="flex items-center">
                                            <span class="text-yellow-400">★</span>
                                            <span class="ml-1 text-sm text-gray-900">{{ number_format($user->rating, 1) }}</span>
                                            <span class="ml-1 text-xs text-gray-500">({{ $user->receivedFeedback->count() }})</span>
                                        </div>
                                    </div>
                                @endif
                            </div>
                        </div>
                    </div>

                    <!-- Trust Links Management -->
                    <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                        <div class="p-6">
                            <h3 class="text-lg font-semibold text-gray-900 mb-4">Trust Links</h3>

                            @if($user->trustLinks->count() > 0)
                                <div class="mb-6 overflow-x-auto">
                                    <table class="w-full text-sm">
                                        <thead>
                                            <tr class="border-b border-gray-200 bg-gray-50">
                                                <th class="px-3 py-2 text-left text-xs font-semibold text-gray-700">Service</th>
                                                <th class="px-3 py-2 text-left text-xs font-semibold text-gray-700">Remote ID</th>
                                                <th class="px-3 py-2 text-left text-xs font-semibold text-gray-700">Status</th>
                                                <th class="px-3 py-2 text-left text-xs font-semibold text-gray-700">Code</th>
                                                <th class="px-3 py-2 text-left text-xs font-semibold text-gray-700">Expires</th>
                                                <th class="px-3 py-2 text-left text-xs font-semibold text-gray-700">Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach($user->trustLinks as $link)
                                                <tr class="border-b border-gray-200 hover:bg-gray-50">
                                                    <td class="px-3 py-3 text-gray-900 font-medium">{{ $link->service->name }}</td>
                                                    <td class="px-3 py-3 text-gray-600">
                                                        @if($link->remote_user_url)
                                                            <a href="{{ $link->remote_user_url }}" target="_blank" class="text-blue-600 hover:text-blue-700 underline text-xs">{{ $link->remote_id }}</a>
                                                        @else
                                                            {{ $link->remote_id }}
                                                        @endif
                                                    </td>
                                                    <td class="px-3 py-3">
                                                        <span class="px-2 py-1 rounded text-xs font-semibold
                                                            @if($link->status === 'verified')
                                                                bg-green-100 text-green-700
                                                            @elseif($link->status === 'submitted')
                                                                bg-yellow-100 text-yellow-700
                                                            @elseif($link->status === 'failed')
                                                                bg-red-100 text-red-700
                                                            @else
                                                                bg-gray-100 text-gray-700
                                                            @endif
                                                        ">
                                                            {{ ucfirst(str_replace('_', ' ', $link->status)) }}
                                                        </span>
                                                    </td>
                                                    <td class="px-3 py-3 font-mono text-xs text-gray-600">
                                                        @if($link->challenge_code)
                                                            <code class="bg-gray-100 px-2 py-1 rounded">{{ $link->challenge_code }}</code>
                                                        @else
                                                            --
                                                        @endif
                                                    </td>
                                                    <td class="px-3 py-3 text-sm text-gray-600">
                                                        @if($link->challenge_expires_at)
                                                            <span class="text-xs">{{ $link->challenge_expires_at->format('M j, Y') }}</span>
                                                            @if($link->isExpired())
                                                                <span class="text-red-600">(expired)</span>
                                                            @endif
                                                        @else
                                                            --
                                                        @endif
                                                    </td>
                                                    <td class="px-3 py-3 text-right space-x-1">
                                                        @if($link->status === 'submitted' && $link->service->verification_mode === 'automated')
                                                            <form method="POST" action="{{ route('admin.trust-links.check-proof', $link) }}" class="inline">
                                                                @csrf
                                                                <button type="submit" class="text-blue-600 hover:text-blue-700 text-xs font-medium">Check</button>
                                                            </form>
                                                        @endif
                                                        @if($link->status !== 'verified' && $link->service->verification_mode === 'manual')
                                                            <form method="POST" action="{{ route('admin.trust-links.verify', $link) }}" class="inline">
                                                                @csrf
                                                                <button type="submit" class="text-green-600 hover:text-green-700 text-xs font-medium">Verify</button>
                                                            </form>
                                                        @endif
                                                        <form method="POST" action="{{ route('admin.trust-links.destroy', $link) }}" class="inline" onsubmit="return confirm('Delete this trust link?');">
                                                            @csrf
                                                            @method('DELETE')
                                                            <button type="submit" class="text-red-600 hover:text-red-700 text-xs font-medium">Delete</button>
                                                        </form>
                                                    </td>
                                                </tr>
                                            @endforeach
                                        </tbody>
                                    </table>
                                </div>
                            @else
                                <p class="text-gray-600 text-sm mb-4">No trust links yet.</p>
                            @endif

                            <!-- Add Trust Link Form -->
                            <div class="border-t border-gray-200 pt-4">
                                <h4 class="font-medium text-gray-900 mb-3 text-sm">Add Trust Link</h4>
                                <form method="POST" action="{{ route('admin.users.trust.store', $user) }}" class="space-y-3">
                                    @csrf
                                    <div class="flex gap-2">
                                        <div class="flex-1">
                                            <select name="trust_service_id" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-gray-400 focus:border-gray-400 text-sm" required>
                                                <option value="">Select a service...</option>
                                                @foreach(\App\Models\TrustService::active()->get() as $service)
                                                    <option value="{{ $service->id }}">{{ $service->name }}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                        <div class="flex-1">
                                            <input type="text" name="remote_id" placeholder="Username/ID" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-gray-400 focus:border-gray-400 text-sm" required>
                                        </div>
                                        <div class="flex-1">
                                            <input type="url" name="remote_user_url" placeholder="Profile URL" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-gray-400 focus:border-gray-400 text-sm" required>
                                        </div>
                                        <button type="submit" class="px-4 py-2 bg-gray-800 text-white rounded-lg hover:bg-gray-700 text-sm font-medium">Add</button>
                                    </div>
                                    <input type="text" name="note" placeholder="Internal note (optional)" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-gray-400 focus:border-gray-400 text-sm">
                                </form>
                            </div>
                        </div>
                    </div>

                    <!-- Admin Edit Form -->
                    <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                        <div class="p-6">
                            <h3 class="text-lg font-semibold text-gray-900 mb-4">Admin Edit</h3>
                            <form method="POST" action="{{ route('admin.users.update', $user) }}" class="space-y-4">
                                @csrf
                                @method('PATCH')

                                <div class="flex items-center">
                                    <input id="is_active" name="is_active" type="checkbox" value="1" {{ $user->is_active ? 'checked' : '' }} class="h-4 w-4 text-gray-800 focus:ring-gray-400 border-gray-300 rounded">
                                    <label for="is_active" class="ml-2 block text-sm text-gray-900">
                                        Account Active
                                    </label>
                                </div>

                                <div>
                                    <x-input-label for="admin_notes" :value="__('Admin Notes')" />
                                    <textarea id="admin_notes" name="admin_notes" rows="3" class="mt-1 block w-full border-gray-300 focus:border-gray-400 focus:ring-gray-400 rounded-md shadow-sm" placeholder="Internal notes about this user...">{{ $user->admin_notes }}</textarea>
                                </div>

                                <x-primary-button type="submit" class="w-full">
                                    Update User
                                </x-primary-button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Login History -->
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6">
                    <h3 class="text-lg font-semibold text-gray-900 mb-4">Login History</h3>

                    @if($user->loginHistory->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">Date & Time</th>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">IP Address</th>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Device / Browser</th>
                                    </tr>
                                </thead>
                                <tbody class="bg-white divide-y divide-gray-200">
                                    @foreach($user->loginHistory->take(50) as $login)
                                        <tr class="hover:bg-gray-50">
                                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                                {{ $login->logged_in_at->format('M j, Y g:i A') }}
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm font-mono text-gray-600">
                                                {{ $login->ip_address }}
                                            </td>
                                            <td class="px-6 py-4 text-sm text-gray-600">
                                                <div class="max-w-xs truncate" title="{{ $login->user_agent }}">
                                                    {{ $login->user_agent ? Str::limit($login->user_agent, 60) : 'Unknown' }}
                                                </div>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>

                        @if($user->loginHistory->count() > 50)
                            <div class="mt-4 text-center">
                                <p class="text-sm text-gray-500">Showing 50 most recent logins out of {{ $user->loginHistory->count() }} total</p>
                            </div>
                        @endif
                    @else
                        <p class="text-gray-500 text-sm">No login history available.</p>
                    @endif
                </div>
            </div>
        </div>
    </div>
</x-app-layout>