@php
    $selectedAudit = $selectedAudit ?? null;
    $selectedAuditLink = $selectedAuditLink ?? null;
@endphp

@if ($selectedAudit)
    <div class="flex flex-col gap-6">
        <div class="flex flex-wrap items-center gap-3">
            <span class="inline-flex items-center rounded-full bg-gray-100 px-3 py-1 text-xs font-medium text-gray-700 dark:bg-gray-800 dark:text-gray-200">
                {{ \App\Support\Badges\BadgeRenderer::label(\App\Support\Badges\BadgeDomain::AuditOutcome)($selectedAudit->normalizedOutcome()->value) }}
            </span>
            <span class="inline-flex items-center rounded-full bg-gray-100 px-3 py-1 text-xs font-medium text-gray-700 dark:bg-gray-800 dark:text-gray-200">
                {{ \App\Support\Badges\BadgeRenderer::label(\App\Support\Badges\BadgeDomain::AuditActorType)($selectedAudit->actorSnapshot()->type->value) }}
            </span>

            @if (is_array($selectedAuditLink))
                <a
                    class="inline-flex items-center rounded-lg border border-gray-300 px-3 py-2 text-sm font-medium text-gray-700 transition hover:bg-gray-50 dark:border-gray-700 dark:text-gray-200 dark:hover:bg-gray-900"
                    href="{{ $selectedAuditLink['url'] }}"
                >
                    {{ $selectedAuditLink['label'] }}
                </a>
            @endif
        </div>

        <div class="grid gap-4 lg:grid-cols-3">
            <div class="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900">
                <div class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
                    Actor
                </div>
                <div class="mt-2 text-sm font-medium text-gray-900 dark:text-gray-100">
                    {{ $selectedAudit->actorDisplayLabel() }}
                </div>
                <div class="mt-1 text-sm text-gray-600 dark:text-gray-300">
                    {{ $selectedAudit->actorSnapshot()->type->label() }}
                </div>
                @if ($selectedAudit->actorSnapshot()->email)
                    <div class="mt-1 text-xs text-gray-500 dark:text-gray-400">
                        {{ $selectedAudit->actorSnapshot()->email }}
                    </div>
                @endif
            </div>

            <div class="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900">
                <div class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
                    Target
                </div>
                <div class="mt-2 text-sm font-medium text-gray-900 dark:text-gray-100">
                    {{ $selectedAudit->targetDisplayLabel() ?? 'No target snapshot' }}
                </div>
                <div class="mt-1 text-sm text-gray-600 dark:text-gray-300">
                    {{ $selectedAudit->resource_type ? ucfirst(str_replace('_', ' ', $selectedAudit->resource_type)) : 'Workspace event' }}
                </div>
            </div>

            <div class="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900">
                <div class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
                    Scope
                </div>
                <div class="mt-2 text-sm font-medium text-gray-900 dark:text-gray-100">
                    {{ $selectedAudit->tenant?->name ?? 'Workspace-wide event' }}
                </div>
                <div class="mt-1 text-sm text-gray-600 dark:text-gray-300">
                    Workspace #{{ $selectedAudit->workspace_id }}
                </div>
            </div>
        </div>

        <div class="grid gap-4 lg:grid-cols-2">
            <div class="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900">
                <div class="text-sm font-semibold text-gray-900 dark:text-gray-100">
                    Readable context
                </div>

                @if ($selectedAudit->contextItems() === [])
                    <div class="mt-3 text-sm text-gray-600 dark:text-gray-300">
                        No additional context was recorded for this event.
                    </div>
                @else
                    <dl class="mt-3 space-y-3">
                        @foreach ($selectedAudit->contextItems() as $item)
                            <div>
                                <dt class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
                                    {{ $item['label'] }}
                                </dt>
                                <dd class="mt-1 text-sm text-gray-900 dark:text-gray-100">
                                    {{ is_bool($item['value']) ? ($item['value'] ? 'true' : 'false') : $item['value'] }}
                                </dd>
                            </div>
                        @endforeach
                    </dl>
                @endif
            </div>

            <div class="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900">
                <div class="text-sm font-semibold text-gray-900 dark:text-gray-100">
                    Technical metadata
                </div>

                <dl class="mt-3 space-y-3">
                    @foreach ($selectedAudit->technicalMetadata() as $label => $value)
                        <div>
                            <dt class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
                                {{ $label }}
                            </dt>
                            <dd class="mt-1 break-all text-sm text-gray-900 dark:text-gray-100">
                                {{ $value }}
                            </dd>
                        </div>
                    @endforeach
                </dl>
            </div>
        </div>
    </div>
@endif