@extends('layouts.app')

@section('title', 'Audit Logs')

@section('content')
    <x-breadcrumbs :items="[
        ['label' => 'Dashboard', 'url' => route('dashboard')],
        ['label' => 'Administration'],
        ['label' => 'Audit Logs'],
    ]" />

    <x-page-header title="Audit Logs" subtitle="System activity including invoices, payments, bookings, and space layouts" />

    <div class="vem-filter-bar">
        <form method="GET" action="{{ route('admin.audit-logs.index') }}" class="row g-3 align-items-end">
            <div class="col-md-3">
                <label for="search" class="form-label">Search</label>
                <input type="search" name="search" id="search" class="form-control form-control-sm"
                       value="{{ request('search') }}" placeholder="Action, reason, user...">
            </div>
            <div class="col-md-3">
                <label for="action" class="form-label">Action</label>
                <select name="action" id="action" class="form-select form-select-sm">
                    <option value="">All actions</option>
                    @foreach ($actions as $action)
                        <option value="{{ $action }}" @selected(request('action') === $action)>
                            {{ \App\Services\AuditService::actionLabel($action) }}
                        </option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-2">
                <label for="user_id" class="form-label">User</label>
                <select name="user_id" id="user_id" class="form-select form-select-sm">
                    <option value="">All users</option>
                    @foreach ($users as $user)
                        <option value="{{ $user->id }}" @selected(request('user_id') == $user->id)>{{ $user->name }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-2">
                <label for="date_from" class="form-label">From</label>
                <input type="date" name="date_from" id="date_from" class="form-control form-control-sm" value="{{ request('date_from') }}">
            </div>
            <div class="col-md-2">
                <label for="date_to" class="form-label">To</label>
                <input type="date" name="date_to" id="date_to" class="form-control form-control-sm" value="{{ request('date_to') }}">
            </div>
            <div class="col-12 d-flex gap-2">
                <button type="submit" class="btn btn-primary btn-sm">Filter</button>
                <a href="{{ route('admin.audit-logs.index') }}" class="btn btn-outline-secondary btn-sm">Reset</a>
            </div>
        </form>
    </div>

    <x-card flush>
        @if ($logs->isEmpty())
            <x-empty-state icon="journal-text" title="No audit logs found" />
        @else
            <div class="table-responsive">
                <table class="table table-sm align-middle mb-0">
                    <thead>
                        <tr>
                            <th>Date/Time</th>
                            <th>User</th>
                            <th>Action</th>
                            <th>Subject</th>
                            <th>Reason</th>
                            <th class="text-end">Details</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($logs as $log)
                            @php
                                $hasDetails = ! empty($log->old_values) || ! empty($log->new_values);
                            @endphp
                            <tr>
                                <td><x-datetime :value="$log->created_at" datetime /></td>
                                <td>{{ $log->user?->name ?? 'System' }}</td>
                                <td>
                                    <div class="fw-semibold">{{ \App\Services\AuditService::actionLabel($log->action) }}</div>
                                    <code class="small text-muted">{{ $log->action }}</code>
                                </td>
                                <td>
                                    @if ($log->subject)
                                        {{ class_basename($log->subject_type) }} #{{ $log->subject_id }}
                                    @else
                                        —
                                    @endif
                                </td>
                                <td>{{ $log->reason ?? '—' }}</td>
                                <td class="text-end">
                                    @if ($hasDetails)
                                        <button class="btn btn-outline-secondary btn-sm" type="button"
                                                data-bs-toggle="collapse" data-bs-target="#audit-detail-{{ $log->id }}"
                                                aria-expanded="false">
                                            View
                                        </button>
                                    @else
                                        —
                                    @endif
                                </td>
                            </tr>
                            @if ($hasDetails)
                                <tr class="collapse" id="audit-detail-{{ $log->id }}">
                                    <td colspan="6" class="bg-body-tertiary">
                                        <div class="row g-3 p-2">
                                            <div class="col-md-6">
                                                <div class="small text-muted mb-1">Before</div>
                                                <pre class="small mb-0 bg-white border rounded p-2" style="max-height: 220px; overflow:auto;">{{ json_encode($log->old_values ?? new \stdClass(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) }}</pre>
                                            </div>
                                            <div class="col-md-6">
                                                <div class="small text-muted mb-1">After</div>
                                                <pre class="small mb-0 bg-white border rounded p-2" style="max-height: 220px; overflow:auto;">{{ json_encode($log->new_values ?? new \stdClass(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) }}</pre>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            @endif
                        @endforeach
                    </tbody>
                </table>
            </div>
            @if ($logs->hasPages())
                <div class="p-3 border-top">{{ $logs->links() }}</div>
            @endif
        @endif
    </x-card>
@endsection
