@props([
    'progress',        // WitnessProgress
    'statement',       // WitnessStatement
    'optionsConfig' => [],
    'options' => [],
    'versions' => null,
])

@php
    $ready = $progress->isReadyToGenerate();
    $blockers = $progress->blockers();
    $primary = $progress->primaryBlocker();
    $metrics = [
        ['label' => 'Required answered', 'value' => $progress->requiredAnswered.'/'.$progress->requiredTotal, 'ok' => $progress->requiredUnanswered === []],
        ['label' => 'Suggestions reviewed', 'value' => $progress->suggestionsReviewed().'/'.$progress->suggestionsTotal, 'ok' => $progress->awaitingReview === 0],
        ['label' => 'Conflicts', 'value' => (string) count($progress->conflicts), 'ok' => $progress->conflicts === []],
        ['label' => 'Exhibits included', 'value' => (string) $progress->exhibitCount, 'ok' => true],
    ];
@endphp

<div {{ $attributes->merge(['class' => 'witness-readiness']) }}>
    <h2 class="h6 fw-semibold mb-3">
        <span class="mdi mdi-clipboard-check-outline" aria-hidden="true"></span> Statement Readiness
    </h2>

    {{-- Metrics --}}
    <dl class="row row-cols-2 g-2 mb-3 small">
        @foreach($metrics as $m)
            <div class="col">
                <div class="border rounded p-2 h-100">
                    <dt class="text-muted fw-normal">{{ $m['label'] }}</dt>
                    <dd class="mb-0 fw-bold d-flex align-items-center gap-1 {{ $m['ok'] ? 'text-success-emphasis' : 'text-body' }}">
                        <span class="mdi {{ $m['ok'] ? 'mdi-check' : 'mdi-dots-horizontal' }}" aria-hidden="true"></span>
                        {{ $m['value'] }}
                    </dd>
                </div>
            </div>
        @endforeach
    </dl>

    {{-- Next best action / generate --}}
    @if($ready)
        <div class="alert alert-success py-2 px-3 small d-flex align-items-center gap-2 mb-2">
            <span class="mdi mdi-check-circle" aria-hidden="true"></span> Everything checks out.
        </div>
        <button type="button" class="btn btn-success w-100" wire:click="generate">
            <span class="mdi mdi-file-document-check-outline" aria-hidden="true"></span> Generate Witness Statement
        </button>
    @else
        <p class="text-muted small mb-1">Next step</p>
        <x-witness.readiness-blocker :blocker="$primary" primary />

        @if(count($blockers) > 1)
            <button class="btn btn-sm btn-link px-0 mt-1" type="button" data-bs-toggle="collapse"
                    data-bs-target="#readinessMore" aria-expanded="false" aria-controls="readinessMore">
                {{ count($blockers) - 1 }} more {{ \Illuminate\Support\Str::plural('issue', count($blockers) - 1) }}
            </button>
            <div class="collapse" id="readinessMore">
                <ul class="list-unstyled mb-0">
                    @foreach(array_slice($blockers, 1) as $b)
                        <x-witness.readiness-blocker :blocker="$b" />
                    @endforeach
                </ul>
            </div>
        @endif
    @endif

    <hr>

    {{-- Statement pack (content-affecting options only) --}}
    <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="#statementPack" aria-expanded="false" aria-controls="statementPack">
        <span class="fw-semibold"><span class="mdi mdi-package-variant-closed" aria-hidden="true"></span> Statement pack</span>
        <span class="mdi mdi-chevron-down" aria-hidden="true"></span>
    </button>
    <div class="collapse" id="statementPack">
        <div class="pt-2">
            <x-witness.generation-pack-options :options-config="$optionsConfig" :selected="$options" :statement="$statement" />
            <p class="text-muted mt-2 mb-0" style="font-size:.78rem;">
                Post and email delivery are handled after generation, from the sending / print queue.
            </p>
        </div>
    </div>

    <hr>

    {{-- Version history (collapsed; compact empty state) --}}
    <x-witness.version-history :versions="$versions" />
</div>
