@extends('layouts.app')

@section('title', 'Audit Log Entry')

@section('content')
    <x-page-header title="Audit Log Entry" :subtitle="$log->description">
        <x-slot:breadcrumbs>
            <li class="breadcrumb-item"><a href="{{ route('audit.index') }}">Audit Logs</a></li>
            <li class="breadcrumb-item active" aria-current="page">#{{ $log->id }}</li>
        </x-slot:breadcrumbs>
    </x-page-header>

    <div class="row g-3">
        <div class="col-xl-5">
            <x-panel title="Event">
                <x-detail-list>
                    <x-detail-row label="Event">
                        <x-status-badge :status="$log->event" />
                    </x-detail-row>
                    <x-detail-row label="Timestamp">
                        {{ $log->created_at->format(setting('datetime_format', 'd M Y, H:i')) }}
                    </x-detail-row>
                    <x-detail-row label="User">
                        {{ $log->user->name ?? 'System' }}
                    </x-detail-row>
                    <x-detail-row label="Module">
                        {{ $log->auditable_type ? class_basename($log->auditable_type) : '—' }}
                    </x-detail-row>
                    <x-detail-row label="Record ID">
                        {{ $log->auditable_id ?? '—' }}
                    </x-detail-row>
                    <x-detail-row label="IP Address">
                        <span class="ams-mono">{{ $log->ip_address ?? '—' }}</span>
                    </x-detail-row>
                    <x-detail-row label="User Agent">
                        <span class="ams-text-xs">{{ $log->user_agent ?? '—' }}</span>
                    </x-detail-row>
                    <x-detail-row label="URL">
                        <span class="ams-text-xs ams-mono">{{ $log->url ?? '—' }}</span>
                    </x-detail-row>
                </x-detail-list>
            </x-panel>
        </div>

        <div class="col-xl-7">
            <x-panel title="Description">
                <p class="mb-0">{{ $log->description }}</p>
            </x-panel>

            @php $changes = $log->changes(); @endphp

            @if (count($changes) > 0)
                <x-panel title="Changes" subtitle="Every attribute this event touched, before and after." class="mt-3">
                    <x-table.wrapper>
                        <x-slot:head>
                            <tr>
                                <th>Field</th>
                                <th>Before</th>
                                <th>After</th>
                            </tr>
                        </x-slot:head>

                        @foreach ($changes as $field => $change)
                            <tr>
                                <td class="ams-cell-code">{{ $field }}</td>
                                <td class="ams-text-xs">{{ \Illuminate\Support\Str::limit(is_scalar($change['old']) ? (string) $change['old'] : json_encode($change['old']), 80) ?: '—' }}</td>
                                <td class="ams-text-xs">{{ \Illuminate\Support\Str::limit(is_scalar($change['new']) ? (string) $change['new'] : json_encode($change['new']), 80) ?: '—' }}</td>
                            </tr>
                        @endforeach
                    </x-table.wrapper>
                </x-panel>
            @endif
        </div>
    </div>
@endsection
