<x-app-layout>
    <x-slot name="title">Activity Logs</x-slot>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('System Activity Logs') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 bg-white border-b border-gray-200">
                    
                    <!-- Filters -->
                    <form method="GET" action="{{ route('activity-logs.index') }}" class="mb-6 bg-gray-50 p-4 rounded-lg flex flex-col sm:flex-row gap-4 items-end">
                        <div class="w-full sm:w-1/4">
                            <label class="block text-sm font-medium text-gray-700">Action / Event</label>
                            <select name="event" class="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 sm:text-sm">
                                <option value="">All Events</option>
                                <option value="created" {{ request('event') == 'created' ? 'selected' : '' }}>Created</option>
                                <option value="updated" {{ request('event') == 'updated' ? 'selected' : '' }}>Updated</option>
                                <option value="deleted" {{ request('event') == 'deleted' ? 'selected' : '' }}>Deleted</option>
                            </select>
                        </div>
                        <div class="w-full sm:w-1/4">
                            <label class="block text-sm font-medium text-gray-700">User (Causer)</label>
                            <select name="causer_id" class="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 sm:text-sm">
                                <option value="">All Users</option>
                                @foreach($users as $user)
                                    <option value="{{ $user->id }}" {{ request('causer_id') == $user->id ? 'selected' : '' }}>{{ $user->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div>
                            <button type="submit" class="px-4 py-2 bg-primary-600 text-white rounded hover:bg-primary-700 text-sm font-medium">Filter</button>
                            <a href="{{ route('activity-logs.index') }}" class="px-4 py-2 bg-gray-200 text-gray-700 rounded hover:bg-gray-300 text-sm font-medium ml-2">Clear</a>
                        </div>
                    </form>

                    <div class="overflow-x-auto border border-gray-200 sm:rounded-lg">
                        <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">User</th>
                                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Action</th>
                                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Subject</th>
                                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Details</th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-gray-200">
                                @forelse($logs as $log)
                                    <tr>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                            {{ $log->created_at->format('d M Y, h:i A') }}
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            <div class="text-sm font-medium text-gray-900">
                                                {{ $log->causer ? $log->causer->name : 'System' }}
                                            </div>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            @php
                                                $eventClass = 'bg-gray-100 text-gray-800';
                                                if($log->event === 'created') $eventClass = 'bg-green-100 text-green-800';
                                                if($log->event === 'updated') $eventClass = 'bg-blue-100 text-blue-800';
                                                if($log->event === 'deleted') $eventClass = 'bg-red-100 text-red-800';
                                            @endphp
                                            <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full {{ $eventClass }}">
                                                {{ ucfirst($log->event) }}
                                            </span>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                            {{ class_basename($log->subject_type) }} #{{ $log->subject_id }}
                                        </td>
                                        <td class="px-6 py-4 text-sm text-gray-500">
                                            @if($log->properties && $log->properties->count() > 0)
                                                @if(isset($log->properties['old']) && isset($log->properties['attributes']))
                                                    <div class="space-y-2">
                                                        @foreach($log->properties['attributes'] as $key => $newValue)
                                                            @if(isset($log->properties['old'][$key]) && $log->properties['old'][$key] != $newValue)
                                                                <div class="text-xs">
                                                                    <span class="font-semibold text-gray-700">{{ $key }}:</span>
                                                                    <span class="line-through text-red-500 mr-1">{{ is_array($log->properties['old'][$key]) ? json_encode($log->properties['old'][$key]) : $log->properties['old'][$key] }}</span>
                                                                    <span class="text-green-600 font-medium">{{ is_array($newValue) ? json_encode($newValue) : $newValue }}</span>
                                                                </div>
                                                            @endif
                                                        @endforeach
                                                    </div>
                                                @elseif(isset($log->properties['attributes']))
                                                    <div class="text-xs">
                                                        <span class="font-semibold text-green-600">New Record:</span>
                                                        <span class="text-gray-500 truncate block max-w-xs" title="{{ json_encode($log->properties['attributes']) }}">
                                                            {{ Str::limit(json_encode($log->properties['attributes']), 100) }}
                                                        </span>
                                                    </div>
                                                @else
                                                    <div class="text-xs truncate max-w-xs" title="{{ json_encode($log->properties) }}">
                                                        {{ Str::limit(json_encode($log->properties), 50) }}
                                                    </div>
                                                @endif
                                            @else
                                                -
                                            @endif
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="5" class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 text-center">No activity logs found.</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                    
                    <div class="mt-4">
                        {{ $logs->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
