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

@php
    // Which confirmed findings are already included as exhibits (per finding).
    $exhibitedItemIds = $exhibits->pluck('dsar_evidence_item_id')->filter()->all();
@endphp

<div {{ $attributes->merge(['class' => 'witness-exhibits-panel']) }}>
    <div class="d-flex align-items-center gap-2 mb-1">
        <h5 class="mb-0">
            <span class="mdi mdi-paperclip" aria-hidden="true"></span> Exhibits
        </h5>
    </div>
    <p class="text-muted small mb-2">
        Documents marked as Relevant in the Evidence Review have been pulled through as exhibits.
    </p>

    <span class="badge rounded-pill bg-success-subtle text-success-emphasis mb-3">
        <span class="mdi mdi-file-check-outline" aria-hidden="true"></span>
        {{ $evidence->count() }} Relevant {{ Str::plural('Document', $evidence->count()) }}
    </span>

    @if($evidence->isEmpty())
        <div class="text-muted small py-3 text-center">
            <span class="mdi mdi-file-search-outline d-block fs-4" aria-hidden="true"></span>
            No relevant evidence yet. Confirm findings in Evidence Review to pull them through.
        </div>
    @else
        <ul class="list-group list-group-flush mb-3">
            @foreach($evidence as $item)
                @php
                    $isExhibited = in_array($item->id, $exhibitedItemIds, true);
                    $source = $item->careSetting?->institution?->display_name
                        ?? $item->document?->type?->display_name
                        ?? 'Evidence';
                @endphp
                <li class="list-group-item px-0 d-flex align-items-center gap-2" wire:key="exh-item-{{ $item->id }}">
                    <span class="mdi mdi-file-pdf-box fs-4 text-danger" aria-hidden="true"></span>
                    <div class="flex-grow-1 min-w-0">
                        <div class="small fw-semibold text-truncate" title="{{ $item->document?->display_name }}">
                            {{ $item->document?->display_name ?? 'Document' }}
                        </div>
                        <div class="text-muted text-truncate" style="font-size: .78rem;">{{ $source }}</div>
                    </div>
                    @if($item->page)
                        <span class="badge bg-light text-dark border">Page {{ $item->page }}</span>
                    @endif
                    @if($editable)
                        <button type="button"
                                class="btn btn-sm btn-link p-0 {{ $isExhibited ? 'text-warning' : 'text-muted' }}"
                                wire:click="toggleExhibit({{ $item->id }})"
                                title="{{ $isExhibited ? 'Remove from exhibits' : 'Include as exhibit' }}"
                                aria-pressed="{{ $isExhibited ? 'true' : 'false' }}"
                                aria-label="{{ $isExhibited ? 'Remove exhibit' : 'Add exhibit' }}">
                            <span class="mdi {{ $isExhibited ? 'mdi-star' : 'mdi-star-outline' }} fs-5" aria-hidden="true"></span>
                        </button>
                    @elseif($isExhibited)
                        <span class="mdi mdi-star fs-5 text-warning" aria-hidden="true" title="Included exhibit"></span>
                    @endif
                </li>
            @endforeach
        </ul>
    @endif

    @if($client)
        <div class="text-center">
            <a href="{{ route('clients.evidence', $client) }}" class="btn btn-sm btn-outline-primary" target="_blank" rel="noopener">
                View in Evidence Review
                <span class="mdi mdi-open-in-new" aria-hidden="true"></span>
            </a>
        </div>
    @endif
</div>
