@props([
    'stages' => [],
    'currentStage' => null,
    'stageFlow' => [],
    'stageRoute' => null,
    'visit' => null,
    'canAdvance' => false,
])

@php
    $stageList = collect($stages)
        ->filter(fn ($stage) => is_array($stage) && ($stage['code'] ?? '') !== '')
        ->values();
    $current = $currentStage ? (string) $currentStage : null;
    $canMutate = (bool) $canAdvance && is_string($stageRoute) && $stageRoute !== '' && $visit;
    $nextStep = ($current && isset($stageFlow[$current])) ? $stageFlow[$current] : null;
    $canMoveNext = $canMutate
        && is_array($nextStep)
        && ($nextStep['next'] ?? null)
        && ($nextStep['next'] ?? null) !== $current;
@endphp

@if ($visit && $stageList->isNotEmpty())
    <nav
        {{ $attributes->merge([
            'class' => 'rounded-2xl border border-slate-200 bg-white px-3 py-3 sm:px-4',
            'aria-label' => 'Visit stages',
            'data-care-stage-bar' => '1',
        ]) }}
    >
        <div class="flex flex-wrap gap-2">
            @foreach ($stageList as $stage)
                @php
                    $code = (string) $stage['code'];
                    $label = (string) ($stage['label'] ?? ucfirst(str_replace('_', ' ', $code)));
                    $isActive = $current === $code;
                    $pillBase = 'inline-flex items-center rounded-full px-3 py-1.5 text-xs font-semibold transition';
                    $pillActive = $pillBase.' bg-violet-700 text-white shadow-sm';
                    $pillIdle = $pillBase.' border border-slate-200 bg-white text-slate-800 hover:bg-slate-50';
                    $pillReadonly = $pillBase.' border border-slate-200 bg-white text-slate-800';
                @endphp

                @if ($canMutate)
                    <form method="POST" action="{{ route($stageRoute, $visit) }}" class="inline">
                        @csrf
                        <input type="hidden" name="stage" value="{{ $code }}">
                        <button type="submit" @class([$pillActive => $isActive, $pillIdle => ! $isActive])>
                            {{ $label }}
                        </button>
                    </form>
                @else
                    <span
                        @class([$pillActive => $isActive, $pillReadonly => ! $isActive])
                        @if (! $isActive) title="View-only access" @endif
                    >
                        {{ $label }}
                    </span>
                @endif
            @endforeach

            @if ($canMoveNext)
                <form method="POST" action="{{ route($stageRoute, $visit) }}" class="inline">
                    @csrf
                    <input type="hidden" name="stage" value="{{ $nextStep['next'] }}">
                    <button
                        type="submit"
                        class="inline-flex items-center rounded-full bg-emerald-600 px-3 py-1.5 text-xs font-semibold text-white shadow-sm hover:bg-emerald-700"
                        data-care-stage-move
                    >
                        {{ $nextStep['label'] }} →
                    </button>
                </form>
            @endif
        </div>
    </nav>
@endif
