@extends('layouts.admin')

@section('content')
<div class="container mx-auto px-4 py-8">
    <div class="max-w-4xl mx-auto">
        <div class="flex justify-between items-center mb-6">
            <h1 class="text-3xl font-bold text-gray-900">Audit Log Details</h1>
            <a href="{{ route('admin.audit-logs.index') }}" class="bg-gray-500 hover:bg-gray-700 text-white font-bold py-2 px-4 rounded">
                Back to Logs
            </a>
        </div>

        <div class="bg-white shadow-md rounded-lg overflow-hidden">
            <div class="px-6 py-4 bg-gray-50 border-b border-gray-200">
                <h2 class="text-xl font-semibold text-gray-900">{{ $auditLog->formatted_action }}</h2>
                <p class="text-sm text-gray-500">{{ $auditLog->log_date->format('F j, Y \a\t g:i A') }} ({{ $auditLog->time_ago }})</p>
            </div>
            
            <div class="px-6 py-4">
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <h3 class="text-lg font-medium text-gray-900 mb-4">Log Information</h3>
                        <dl class="space-y-3">
                            <div>
                                <dt class="text-sm font-medium text-gray-500">Log ID</dt>
                                <dd class="text-sm text-gray-900">#{{ $auditLog->log_id }}</dd>
                            </div>
                            <div>
                                <dt class="text-sm font-medium text-gray-500">Action</dt>
                                <dd class="text-sm">
                                    <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full 
                                        @if($auditLog->action === 'login') bg-green-100 text-green-800
                                        @elseif($auditLog->action === 'logout') bg-red-100 text-red-800
                                        @elseif(str_contains($auditLog->action, 'created')) bg-blue-100 text-blue-800
                                        @elseif(str_contains($auditLog->action, 'updated')) bg-yellow-100 text-yellow-800
                                        @elseif(str_contains($auditLog->action, 'deleted')) bg-red-100 text-red-800
                                        @else bg-gray-100 text-gray-800
                                        @endif">
                                        {{ $auditLog->formatted_action }}
                                    </span>
                                </dd>
                            </div>
                            <div>
                                <dt class="text-sm font-medium text-gray-500">Description</dt>
                                <dd class="text-sm text-gray-900">{{ $auditLog->description ?? 'No description provided' }}</dd>
                            </div>
                            <div>
                                <dt class="text-sm font-medium text-gray-500">Date & Time</dt>
                                <dd class="text-sm text-gray-900">{{ $auditLog->log_date->format('F j, Y \a\t g:i A') }}</dd>
                            </div>
                        </dl>
                    </div>
                    
                    <div>
                        <h3 class="text-lg font-medium text-gray-900 mb-4">User Information</h3>
                        @if($auditLog->user)
                            <div class="flex items-center p-4 bg-blue-50 rounded-lg">
                                <div class="w-12 h-12 bg-blue-500 rounded-full flex items-center justify-center mr-4">
                                    <span class="text-white text-lg font-medium">
                                        {{ substr($auditLog->user->name, 0, 1) }}
                                    </span>
                                </div>
                                <div>
                                    <div class="text-sm font-medium text-gray-900">{{ $auditLog->user->name }}</div>
                                    <div class="text-sm text-gray-500">{{ $auditLog->user->email }}</div>
                                    <div class="text-xs text-gray-400">Role: {{ ucfirst($auditLog->user->role) }}</div>
                                </div>
                            </div>
                        @else
                            <div class="p-4 bg-yellow-50 rounded-lg">
                                <div class="text-sm text-yellow-800">
                                    <svg class="w-5 h-5 inline mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L3.732 16.5c-.77.833.192 2.5 1.732 2.5z"></path>
                                    </svg>
                                    User information not available (User may have been deleted)
                                </div>
                            </div>
                        @endif
                    </div>
                </div>

                <!-- Technical Details -->
                <div class="mt-6">
                    <h3 class="text-lg font-medium text-gray-900 mb-4">Technical Details</h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                        <div>
                            <dl class="space-y-3">
                                <div>
                                    <dt class="text-sm font-medium text-gray-500">IP Address</dt>
                                    <dd class="text-sm text-gray-900">{{ $auditLog->ip_address ?? 'Not recorded' }}</dd>
                                </div>
                                <div>
                                    <dt class="text-sm font-medium text-gray-500">User Agent</dt>
                                    <dd class="text-sm text-gray-900 break-all">{{ $auditLog->user_agent ?? 'Not recorded' }}</dd>
                                </div>
                            </dl>
                        </div>
                        <div>
                            <dl class="space-y-3">
                                <div>
                                    <dt class="text-sm font-medium text-gray-500">Model Type</dt>
                                    <dd class="text-sm text-gray-900">{{ $auditLog->model_type ?? 'N/A' }}</dd>
                                </div>
                                <div>
                                    <dt class="text-sm font-medium text-gray-500">Model ID</dt>
                                    <dd class="text-sm text-gray-900">{{ $auditLog->model_id ?? 'N/A' }}</dd>
                                </div>
                            </dl>
                        </div>
                    </div>
                </div>

                <!-- Data Changes -->
                @if($auditLog->old_values || $auditLog->new_values)
                <div class="mt-6">
                    <h3 class="text-lg font-medium text-gray-900 mb-4">Data Changes</h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                        @if($auditLog->old_values)
                        <div>
                            <h4 class="text-sm font-medium text-gray-700 mb-2">Previous Values</h4>
                            <div class="bg-gray-50 p-4 rounded-lg">
                                <pre class="text-xs text-gray-600 whitespace-pre-wrap">{{ json_encode($auditLog->old_values, JSON_PRETTY_PRINT) }}</pre>
                            </div>
                        </div>
                        @endif

                        @if($auditLog->new_values)
                        <div>
                            <h4 class="text-sm font-medium text-gray-700 mb-2">New Values</h4>
                            <div class="bg-green-50 p-4 rounded-lg">
                                <pre class="text-xs text-gray-600 whitespace-pre-wrap">{{ json_encode($auditLog->new_values, JSON_PRETTY_PRINT) }}</pre>
                            </div>
                        </div>
                        @endif
                    </div>
                </div>
                @endif
            </div>
        </div>
    </div>
</div>
@endsection
