@php
    /** @var \App\Models\Assessment $assessment */
    /** @var \Illuminate\Support\Collection $answersByCode */
    $questions = $assessment->template->questions;
    $grouped = $questions->groupBy(fn ($q) => $q->section ?: 'General');
@endphp

<div class="space-y-8">
    @foreach ($grouped as $section => $sectionQuestions)
        <div>
            <h3 class="text-sm font-semibold uppercase tracking-wide text-slate-500">{{ $section }}</h3>
            <div class="mt-4 space-y-4">
                @foreach ($sectionQuestions as $question)
                    @php
                        $name = 'answers['.$question->code.']';
                        $old = old('answers.'.$question->code, $answersByCode[$question->code] ?? null);
                        $options = $question->options ?? [];
                        $choices = $options['choices'] ?? [];
                    @endphp
                    <div>
                        <label class="block text-sm font-medium text-slate-700">
                            {{ $question->label }}
                            @if ($question->is_required)<span class="text-red-500">*</span>@endif
                        </label>
                        @if ($question->help_text)
                            <p class="mt-0.5 text-xs text-slate-400">{{ $question->help_text }}</p>
                        @endif

                        @switch($question->answer_type)
                            @case('textarea')
                                <textarea name="{{ $name }}" rows="3" class="mt-1 w-full rounded-lg border-slate-300 text-sm">{{ $old }}</textarea>
                                @break

                            @case('number')
                            @case('integer')
                            @case('scale')
                                <input
                                    type="number"
                                    name="{{ $name }}"
                                    value="{{ $old }}"
                                    @if(isset($options['min'])) min="{{ $options['min'] }}" @endif
                                    @if(isset($options['max'])) max="{{ $options['max'] }}" @endif
                                    @if($question->answer_type === 'integer') step="1" @else step="any" @endif
                                    class="mt-1 w-full rounded-lg border-slate-300 text-sm"
                                >
                                @break

                            @case('boolean')
                                <select name="{{ $name }}" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                                    <option value="">—</option>
                                    <option value="1" @selected($old === true || $old === 1 || $old === '1')>Yes</option>
                                    <option value="0" @selected($old === false || $old === 0 || $old === '0')>No</option>
                                </select>
                                @break

                            @case('date')
                                <input type="date" name="{{ $name }}" value="{{ $old instanceof \Carbon\Carbon ? $old->format('Y-m-d') : $old }}" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                                @break

                            @case('datetime')
                                <input type="datetime-local" name="{{ $name }}" value="{{ $old instanceof \Carbon\Carbon ? $old->format('Y-m-d\TH:i') : $old }}" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                                @break

                            @case('single_choice')
                            @case('score_item')
                                <select name="{{ $name }}" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                                    <option value="">—</option>
                                    @foreach ($choices as $choice)
                                        @php
                                            $code = (string) ($choice['code'] ?? '');
                                            $label = $choice['label'] ?? $code;
                                            $selected = (string) $old === $code
                                                || (isset($choice['score']) && (string) $old === (string) $choice['score']);
                                        @endphp
                                        <option value="{{ $code }}" @selected($selected)>
                                            {{ $label }}@if(isset($choice['score'])) ({{ $choice['score'] }})@endif
                                        </option>
                                    @endforeach
                                </select>
                                @break

                            @case('multi_choice')
                                @php $selected = is_array($old) ? $old : []; @endphp
                                <div class="mt-2 space-y-2">
                                    @foreach ($choices as $choice)
                                        @php $code = (string) ($choice['code'] ?? ''); @endphp
                                        <label class="flex items-center gap-2 text-sm text-slate-700">
                                            <input
                                                type="checkbox"
                                                name="{{ $name }}[]"
                                                value="{{ $code }}"
                                                class="rounded border-slate-300"
                                                @checked(in_array($code, array_map('strval', $selected), true))
                                            >
                                            {{ $choice['label'] ?? $code }}
                                        </label>
                                    @endforeach
                                </div>
                                @break

                            @case('calculated')
                                <p class="mt-1 text-sm text-slate-500">{{ $old ?? '—' }}</p>
                                @break

                            @default
                                <input type="text" name="{{ $name }}" value="{{ $old }}" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                        @endswitch

                        @error('answers.'.$question->code)
                            <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                        @enderror
                    </div>
                @endforeach
            </div>
        </div>
    @endforeach

    <div>
        <label class="block text-sm font-medium text-slate-700">Notes</label>
        <textarea name="notes" rows="2" class="mt-1 w-full rounded-lg border-slate-300 text-sm">{{ old('notes', $assessment->notes) }}</textarea>
    </div>
</div>
