<x-app-layout :title="'Pathways · '.$patient->fullName()">
    <x-care.consultation-return :consultation="$returnConsultation ?? null" />

    <div class="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
        <div>
            <p class="text-xs font-medium uppercase tracking-wide text-slate-500">Condition pathways</p>
            <h1 class="text-2xl font-semibold text-slate-900">{{ $patient->fullName() }}</h1>
            <p class="mt-1 text-sm text-slate-500">
                <a href="{{ route('care.patients.show', $patient) }}" class="text-sky-600 hover:text-sky-700">{{ $patient->patient_number }}</a>
                ·
                <a href="{{ route('care.assessments.index', $patient) }}" class="text-sky-600 hover:text-sky-700">Assessments</a>
            </p>
        </div>
    </div>

    <section class="mt-6 rounded-2xl border border-slate-200 bg-white p-6">
        <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Active pathways</h2>
        @if ($active->isEmpty())
            <p class="mt-3 text-sm text-slate-400">No active pathways.</p>
        @else
            <ul class="mt-4 space-y-3 text-sm">
                @foreach ($active as $row)
                    <li class="flex flex-wrap items-center justify-between gap-2 rounded-xl border border-slate-100 bg-slate-50 px-4 py-3">
                        <div>
                            <p class="font-medium text-slate-900">{{ $row->pathway->name }}</p>
                            <p class="text-xs text-slate-400">
                                Activated {{ $row->activated_at?->format('d M Y H:i') }}
                                @if ($row->activation_diagnosis_text)
                                    · {{ \Illuminate\Support\Str::limit($row->activation_diagnosis_text, 80) }}
                                @endif
                            </p>
                        </div>
                        @if ($canManage)
                            <form method="POST" action="{{ route('care.pathways.deactivate', [$patient, $row]) }}">
                                @csrf
                                <button type="submit" class="text-sm text-red-600 hover:text-red-700">Deactivate</button>
                            </form>
                        @endif
                    </li>
                @endforeach
            </ul>
        @endif
    </section>

    @if ($canManage && $catalog->isNotEmpty())
        <section class="mt-6 rounded-2xl border border-slate-200 bg-white p-6">
            <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Add a condition pathway</h2>
            <form method="POST" action="{{ route('care.pathways.store', $patient) }}" class="mt-4 flex flex-wrap items-end gap-3">
                @csrf
                <div class="min-w-[14rem] flex-1">
                    <label class="block text-xs text-slate-500">Pathway</label>
                    <select name="pathway_code" required class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                        <option value="">Select…</option>
                        @foreach ($catalog as $pathway)
                            <option value="{{ $pathway->code }}">{{ $pathway->name }} ({{ $pathway->code }})</option>
                        @endforeach
                    </select>
                </div>
                <button type="submit" class="btn-primary">Activate</button>
            </form>
            <p class="mt-2 text-xs text-slate-400">Required disease assessments are drafted when their content packs are seeded.</p>
        </section>
    @endif

    <section class="mt-6 overflow-hidden rounded-2xl border border-slate-200 bg-white">
        <div class="border-b border-slate-100 px-4 py-3 text-sm font-semibold uppercase tracking-wide text-slate-500">History</div>
        <table class="min-w-full divide-y divide-slate-100 text-sm">
            <thead class="bg-slate-50 text-left text-xs font-semibold uppercase text-slate-500">
                <tr>
                    <th class="px-4 py-3">Pathway</th>
                    <th class="px-4 py-3">Status</th>
                    <th class="px-4 py-3">Activated</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-slate-100">
                @forelse ($history as $row)
                    <tr>
                        <td class="px-4 py-3 font-medium">{{ $row->pathway?->name ?? '—' }}</td>
                        <td class="px-4 py-3">{{ $statuses[$row->status] ?? $row->status }}</td>
                        <td class="px-4 py-3 text-slate-600">{{ $row->activated_at?->format('d M Y H:i') ?? '—' }}</td>
                    </tr>
                @empty
                    <tr><td colspan="3" class="px-4 py-8 text-center text-slate-400">No pathway history.</td></tr>
                @endforelse
            </tbody>
        </table>
        @if ($history->hasPages())
            <div class="border-t border-slate-100 px-4 py-3">{{ $history->links() }}</div>
        @endif
    </section>
</x-app-layout>
