@props([
    'optionsConfig' => [],   // config('witness_statement.generation_options')
    'selected' => [],        // array<key,bool>
    'statement' => null,     // WitnessStatement (for `requires` checks)
    'disabled' => false,
])

@php
    // Only options that materially affect the CONTENTS of the generated pack.
    // Delivery choices (post/email) belong to the post-generation sending flow.
    $deliveryKeys = ['send_post', 'send_email'];
    $packOptions = collect($optionsConfig)->reject(fn ($o) => in_array($o['key'], $deliveryKeys, true));

    $requiresMet = function ($requires) use ($statement) {
        if (! $requires) {
            return true;
        }
        return match ($requires) {
            'claim.bankDetail' => (bool) $statement?->claim?->bankDetail,
            default => true,
        };
    };
@endphp

<fieldset {{ $attributes->merge(['class' => 'witness-pack-options']) }}>
    <legend class="visually-hidden">Statement pack contents</legend>
    <div class="d-flex flex-column gap-1">
        @foreach($packOptions as $option)
            @php
                $permitted = empty($option['permission']) || auth()->user()?->can($option['permission']);
                $available = $requiresMet($option['requires'] ?? null);
                $isOn = (bool) ($selected[$option['key']] ?? false);
            @endphp
            {{-- Hide options the user can't use; explain the ones merely unavailable. --}}
            @if($permitted)
                <div class="form-check">
                    <input class="form-check-input" type="checkbox"
                           id="pack-opt-{{ $option['key'] }}"
                           wire:click="toggleOption('{{ $option['key'] }}')"
                           @checked($isOn && $available)
                           @disabled($disabled || ! $available)>
                    <label class="form-check-label small" for="pack-opt-{{ $option['key'] }}">
                        {{ $option['label'] }}
                        @unless($available)
                            <span class="d-block text-muted" style="font-size:.75rem;">
                                <span class="mdi mdi-information-outline" aria-hidden="true"></span>
                                Unavailable — add the client's bank details to enable.
                            </span>
                        @endunless
                    </label>
                </div>
            @endif
        @endforeach
    </div>
</fieldset>
