@extends('layouts.app')

@section('title', 'System Audit Trail')

@section('content')
<div class="space-y-6">
    <div>
        <h1 class="text-xl font-bold text-slate-900 dark:text-white">Immutable Audit Log Trail</h1>
        <p class="text-xs text-slate-500 dark:text-slate-400">Complete immutable record of all system events, role changes, permission modifications, logins, and status updates.</p>
    </div>

    <!-- Audit Logs Table (Requirement 34) -->
    <div class="bg-white dark:bg-slate-800 rounded-3xl border border-slate-200 dark:border-slate-700/60 shadow-sm overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-left rtl:text-right text-xs">
                <thead class="bg-slate-50 dark:bg-slate-900/60 border-b border-slate-200 dark:border-slate-700 text-slate-500 uppercase tracking-wider font-bold">
                    <tr>
                        <th class="px-6 py-4">Timestamp</th>
                        <th class="px-6 py-4">User</th>
                        <th class="px-6 py-4">Module</th>
                        <th class="px-6 py-4">Action</th>
                        <th class="px-6 py-4">IP Address</th>
                        <th class="px-6 py-4">Details</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-200 dark:divide-slate-700/60 font-medium">
                    @forelse($auditLogs as $log)
                        <tr class="hover:bg-slate-50 dark:hover:bg-slate-700/30">
                            <td class="px-6 py-4 font-mono text-slate-900 dark:text-white">{{ $log->created_at->format('Y-m-d H:i:s') }}</td>
                            <td class="px-6 py-4 font-bold text-slate-800 dark:text-slate-200">
                                {{ $log->user ? $log->user->name : 'System / Guest' }}
                            </td>
                            <td class="px-6 py-4">
                                <span class="px-2 py-0.5 rounded bg-sky-100 dark:bg-sky-950 text-sky-700 dark:text-sky-300 font-bold text-[10px]">
                                    {{ strtoupper($log->module) }}
                                </span>
                            </td>
                            <td class="px-6 py-4 font-semibold text-slate-700 dark:text-slate-300">{{ $log->action }}</td>
                            <td class="px-6 py-4 font-mono text-slate-500 text-[11px]">{{ $log->ip_address ?? '127.0.0.1' }}</td>
                            <td class="px-6 py-4 text-slate-400 text-[11px] font-mono">
                                @if($log->new_values)
                                    {{ json_encode($log->new_values) }}
                                @else
                                    Record ID: {{ $log->record_id ?? 'N/A' }}
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="px-6 py-8 text-center text-slate-400 text-xs">No audit logs recorded yet.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="p-4 border-t border-slate-200 dark:border-slate-700">
            {{ $auditLogs->links() }}
        </div>
    </div>
</div>
@endsection
