@props([
    'versions' => null,   // Collection<WitnessStatementVersion>
])

@php $versions = $versions ?? collect(); @endphp

<div {{ $attributes->merge(['class' => 'witness-version-history']) }}>
    @if($versions->isEmpty())
        {{-- Compact empty state — no permanent empty panel. --}}
        <p class="text-muted small mb-0 d-flex align-items-center gap-2">
            <span class="mdi mdi-history" aria-hidden="true"></span>
            No versions generated yet.
        </p>
    @else
        <button class="btn btn-sm btn-link px-0 text-decoration-none w-100 text-start d-flex justify-content-between align-items-center"
                type="button" data-bs-toggle="collapse" data-bs-target="#versionHistory"
                aria-expanded="false" aria-controls="versionHistory">
            <span class="fw-semibold">
                <span class="mdi mdi-history" aria-hidden="true"></span>
                Version history ({{ $versions->count() }})
            </span>
            <span class="mdi mdi-chevron-down" aria-hidden="true"></span>
        </button>
        <div class="collapse" id="versionHistory">
            <ol class="list-unstyled mb-0 pt-2">
                @foreach($versions as $version)
                    <li class="d-flex gap-2 pb-2" wire:key="wsv-{{ $version->id }}">
                        <span class="badge rounded-pill bg-primary align-self-start">v{{ $version->version_number }}</span>
                        <div class="flex-grow-1 small">
                            <div class="fw-semibold">
                                @if($version->document)
                                    <a href="{{ route('documents.view', $version->document) }}" target="_blank" rel="noopener">
                                        Preview <span class="mdi mdi-open-in-new" aria-hidden="true"></span>
                                    </a>
                                @else
                                    Version {{ $version->version_number }}
                                @endif
                            </div>
                            <div class="text-muted">
                                {{ $version->generated_at?->format('d/m/Y H:i') }}
                                @if($version->generatedBy) · {{ $version->generatedBy->name }} @endif
                            </div>
                            @if(! empty($version->options))
                                <div class="text-muted" style="font-size:.75rem;">
                                    {{ collect($version->options)->filter()->keys()->map(fn ($k) => str_replace('_', ' ', $k))->implode(', ') ?: 'No pack options' }}
                                </div>
                            @endif
                        </div>
                    </li>
                @endforeach
            </ol>
        </div>
    @endif
</div>
