@props([
    'answer',                 // App\Models\Witness\WitnessStatementAnswer (review_status = suggested)
    'question' => null,       // App\Support\Witness\WitnessQuestion
    'editingAnswerId' => null,
    'editingValue' => '',
])

@php
    $prov = $answer->provenance ?? [];
    $supporting = $prov['supporting'] ?? [];
    $isEditing = $editingAnswerId === $answer->id;
    $display = is_array($answer->value) ? implode(', ', $answer->value) : (string) $answer->value;
@endphp

<div class="witness-suggestion border rounded p-3 bg-warning-subtle bg-opacity-25 border-warning-subtle"
     wire:key="suggestion-{{ $answer->id }}">
    <div class="d-flex justify-content-between align-items-start gap-2 mb-2">
        <span class="badge rounded-pill bg-warning-subtle text-warning-emphasis fw-semibold">
            <span class="mdi mdi-lightbulb-on-outline" aria-hidden="true"></span>
            Suggested from evidence
        </span>
        <span class="text-muted small">Not yet accepted — review before use</span>
    </div>

    @if($question)
        <div class="fw-semibold small mb-1">{{ $question->label }}</div>
    @endif

    @if($isEditing)
        <textarea class="form-control mb-2" rows="4" wire:model="editingValue"
                  aria-label="Edit suggested answer">{{ $editingValue }}</textarea>
        <div class="d-flex gap-2">
            <button type="button" class="btn btn-sm btn-primary" wire:click="saveEditedSuggestion">
                <span class="mdi mdi-content-save" aria-hidden="true"></span> Save & accept
            </button>
            <button type="button" class="btn btn-sm btn-outline-secondary" wire:click="cancelEditingSuggestion">
                Cancel
            </button>
        </div>
    @else
        <blockquote class="border-start border-3 border-warning ps-3 mb-2 small text-body">
            {!! nl2br(e(\Illuminate\Support\Str::limit($display, 400))) !!}
        </blockquote>

        {{-- Source references — never present machine output as unquestionably correct --}}
        @if(! empty($supporting))
            <details class="mb-2">
                <summary class="small text-muted" style="cursor: pointer;">
                    {{ count($supporting) }} source reference{{ count($supporting) === 1 ? '' : 's' }}
                </summary>
                <ul class="list-unstyled mt-2 mb-0">
                    @foreach($supporting as $src)
                        <li class="mb-2">
                            <x-witness.evidence-source-badge
                                :category="$src['category'] ?? 'other'"
                                :severity="$src['severity'] ?? null"
                                :page="$src['page'] ?? null"
                                :document="$src['document'] ?? null" />
                            <div class="text-muted mt-1" style="font-size: .8rem;">“{{ \Illuminate\Support\Str::limit($src['excerpt'] ?? '', 140) }}”</div>
                        </li>
                    @endforeach
                </ul>
            </details>
        @endif

        <div class="d-flex flex-wrap gap-2">
            <button type="button" class="btn btn-sm btn-success" wire:click="acceptSuggestion({{ $answer->id }})">
                <span class="mdi mdi-check" aria-hidden="true"></span> Accept
            </button>
            <button type="button" class="btn btn-sm btn-outline-secondary" wire:click="startEditingSuggestion({{ $answer->id }})">
                <span class="mdi mdi-pencil" aria-hidden="true"></span> Edit
            </button>
            <button type="button" class="btn btn-sm btn-outline-danger" wire:click="rejectSuggestion({{ $answer->id }})">
                <span class="mdi mdi-close" aria-hidden="true"></span> Reject
            </button>
        </div>
    @endif
</div>
