@props([
    'statement',
    'evidence' => collect(),            // all confirmed evidence
    'contextualEvidence' => collect(),  // section-scoped (or all when toggled)
    'exhibits' => collect(),
    'progress' => null,
    'activeSectionTitle' => null,
    'showAllEvidence' => false,
    'editable' => true,
])

@php
    $exhibitedItemIds = $exhibits->pluck('dsar_evidence_item_id')->filter()->all();
    $items = $showAllEvidence ? $evidence : $contextualEvidence;
@endphp

<div {{ $attributes->merge(['class' => 'witness-evidence-panel']) }}>
    <div class="d-flex align-items-center justify-content-between mb-2">
        <h2 class="h6 fw-semibold mb-0"><span class="mdi mdi-folder-search-outline" aria-hidden="true"></span> Evidence</h2>
        <a href="{{ route('clients.evidence', $statement->client) }}" class="small" target="_blank" rel="noopener">
            Evidence Review <span class="mdi mdi-open-in-new" aria-hidden="true"></span>
        </a>
    </div>

    {{-- Scope toggle: this section vs all --}}
    <div class="btn-group btn-group-sm w-100 mb-3" role="group" aria-label="Evidence scope">
        <button type="button" class="btn {{ $showAllEvidence ? 'btn-outline-secondary' : 'btn-primary' }}"
                wire:click="$set('showAllEvidence', false)" aria-pressed="{{ $showAllEvidence ? 'false' : 'true' }}">
            This section
        </button>
        <button type="button" class="btn {{ $showAllEvidence ? 'btn-primary' : 'btn-outline-secondary' }}"
                wire:click="$set('showAllEvidence', true)" aria-pressed="{{ $showAllEvidence ? 'true' : 'false' }}">
            All ({{ $evidence->count() }})
        </button>
    </div>

    @if(! $showAllEvidence && $activeSectionTitle)
        <p class="text-muted mb-2" style="font-size:.78rem;">Supporting <span class="fw-semibold">{{ $activeSectionTitle }}</span></p>
    @endif

    @if($items->isEmpty())
        <div class="text-muted small py-3 text-center border rounded">
            <span class="mdi mdi-file-search-outline d-block fs-4" aria-hidden="true"></span>
            @if($showAllEvidence)
                No confirmed evidence for this claim yet.
            @else
                No evidence linked to this section.
                @if($evidence->isNotEmpty())
                    <button type="button" class="btn btn-sm btn-link p-0" wire:click="$set('showAllEvidence', true)">View all evidence</button>
                @endif
            @endif
        </div>
    @else
        <ul class="list-unstyled mb-0 d-flex flex-column gap-2">
            @foreach($items as $item)
                @php
                    $isExhibited = in_array($item->id, $exhibitedItemIds, true);
                    $source = $item->careSetting?->institution?->display_name ?? $item->document?->type?->display_name ?? 'Evidence';
                    $rawUrl = $item->document ? route('documents.raw', $item->document).($item->page ? '#page='.$item->page : '') : null;
                @endphp
                <li class="border rounded p-2" wire:key="ctx-ev-{{ $item->id }}">
                    <x-witness.evidence-source-badge :category="$item->category" :severity="$item->severity" />
                    <div class="small fw-semibold text-truncate mt-1" title="{{ $item->document?->display_name }}">
                        {{ $item->document?->display_name ?? 'Document' }}
                    </div>
                    <div class="text-muted d-flex flex-wrap gap-2" style="font-size:.75rem;">
                        <span>{{ $source }}</span>
                        @if($item->page)<span>· Page {{ $item->page }}</span>@endif
                        <span class="text-success">· <span class="mdi mdi-check-decagram-outline" aria-hidden="true"></span> Confirmed</span>
                    </div>

                    <div class="d-flex align-items-center gap-2 mt-2">
                        @if($rawUrl)
                            <a href="{{ $rawUrl }}" target="_blank" rel="noopener" class="btn btn-sm btn-outline-secondary">
                                <span class="mdi mdi-open-in-new" aria-hidden="true"></span> Open
                            </a>
                        @endif
                        @if($editable)
                            @if($isExhibited)
                                <button type="button" class="btn btn-sm btn-success" wire:click="toggleExhibit({{ $item->id }})">
                                    <span class="mdi mdi-check" aria-hidden="true"></span> Exhibit added
                                </button>
                            @else
                                <button type="button" class="btn btn-sm btn-outline-primary" wire:click="toggleExhibit({{ $item->id }})">
                                    <span class="mdi mdi-paperclip-plus" aria-hidden="true"></span> Add exhibit
                                </button>
                            @endif
                        @elseif($isExhibited)
                            <span class="badge bg-success-subtle text-success-emphasis">Included exhibit</span>
                        @endif
                    </div>
                </li>
            @endforeach
        </ul>
    @endif

    @if($progress)
        <hr>
        <x-witness.missing-evidence-summary :progress="$progress" />
    @endif
</div>
