@props([
    'exhibits' => collect(),   // Collection<WitnessStatementExhibit>
    'evidence' => collect(),   // Collection<DsarEvidenceItem> (confirmed)
    'editable' => true,
    'showAvailable' => true,
])

@php
    // A confirmed finding is "already exhibited" when it is in the exhibit set (per finding).
    $exhibitedItemIds = $exhibits->pluck('dsar_evidence_item_id')->filter()->all();
    $available = $evidence->reject(fn ($i) => in_array($i->id, $exhibitedItemIds, true))->values();
@endphp

<div {{ $attributes->merge(['class' => 'witness-exhibit-list']) }}>
    {{-- Included exhibits --}}
    <div class="d-flex align-items-center justify-content-between mb-2">
        <h6 class="fw-semibold text-uppercase text-muted small mb-0">
            <span class="mdi mdi-paperclip" aria-hidden="true"></span>
            Included exhibits
        </h6>
        <span class="badge rounded-pill bg-primary-subtle text-primary-emphasis">{{ $exhibits->count() }}</span>
    </div>

    @if($exhibits->isEmpty())
        <p class="text-muted small">No exhibits included yet. Add confirmed evidence below.</p>
    @else
        <ul class="list-group list-group-flush mb-3">
            @foreach($exhibits as $exhibit)
                <li class="list-group-item px-0 d-flex align-items-start gap-2" wire:key="exhibit-{{ $exhibit->id }}">
                    <span class="badge bg-dark-subtle text-dark-emphasis mt-1">{{ $exhibit->exhibit_ref }}</span>
                    <div class="flex-grow-1 min-w-0">
                        <div class="small fw-semibold text-truncate">{{ $exhibit->document?->display_name ?? 'Document' }}</div>
                        <div class="text-muted" style="font-size: .8rem;">
                            @if($exhibit->page)Page {{ $exhibit->page }} · @endif{{ $exhibit->label }}
                        </div>
                    </div>
                    @if($editable)
                        <button type="button" class="btn btn-sm btn-outline-danger"
                                wire:click="removeExhibit({{ $exhibit->id }})"
                                aria-label="Remove exhibit {{ $exhibit->exhibit_ref }}">
                            <span class="mdi mdi-close" aria-hidden="true"></span>
                        </button>
                    @endif
                </li>
            @endforeach
        </ul>
    @endif

    {{-- Suggested (confirmed but not yet exhibited) --}}
    @if($showAvailable)
        <div class="d-flex align-items-center justify-content-between mb-2">
            <h6 class="fw-semibold text-uppercase text-muted small mb-0">
                <span class="mdi mdi-lightbulb-on-outline" aria-hidden="true"></span>
                Suggested evidence
            </h6>
            <span class="badge rounded-pill bg-light text-dark border">{{ $available->count() }}</span>
        </div>

        @if($available->isEmpty())
            <p class="text-muted small mb-0">All confirmed evidence has been exhibited.</p>
        @else
            <ul class="list-group list-group-flush">
                @foreach($available as $item)
                    <li class="list-group-item px-0 d-flex align-items-start gap-2" wire:key="avail-{{ $item->id }}">
                        <div class="flex-grow-1 min-w-0">
                            <x-witness.evidence-source-badge
                                :category="$item->category"
                                :severity="$item->severity"
                                :page="$item->page"
                                :document="$item->document?->display_name" />
                            <div class="text-muted mt-1" style="font-size: .8rem;">“{{ \Illuminate\Support\Str::limit($item->quote, 90) }}”</div>
                        </div>
                        @if($editable)
                            <button type="button" class="btn btn-sm btn-outline-primary"
                                    wire:click="addExhibit({{ $item->id }})"
                                    aria-label="Add evidence as exhibit">
                                <span class="mdi mdi-plus" aria-hidden="true"></span>
                            </button>
                        @endif
                    </li>
                @endforeach
            </ul>
        @endif
    @endif
</div>
