<x-app-layout title="Practitioners">
    <div class="space-y-6">
        <x-care.page-hero
            badge="Doctors · Clinicians"
            title="Practitioners"
            description="People who can be assigned on appointments, the clinical queue, consultations, and prescriptions. Link a team member so they can also sign in."
            :stats="[
                ['value' => number_format($heroStats['total']), 'label' => 'Practitioners'],
                ['value' => number_format($heroStats['active']), 'label' => 'Active'],
                ['value' => number_format($heroStats['linked']), 'label' => 'Linked to team'],
            ]">
            <x-slot name="actions">
                <a href="{{ route('care.practitioners.create') }}" class="btn-primary">Add practitioner</a>
            </x-slot>
        </x-care.page-hero>

        @if (session('success'))
            <p class="rounded-lg bg-emerald-50 px-3 py-2 text-sm text-emerald-800">{{ session('success') }}</p>
        @endif

        <div class="overflow-hidden rounded-2xl border border-slate-200 bg-white">
            <table class="min-w-full text-sm">
                <thead class="bg-slate-50 text-left text-xs uppercase text-slate-500">
                    <tr>
                        <th class="px-4 py-3">Name</th>
                        <th class="px-4 py-3">Specialty</th>
                        <th class="px-4 py-3">Branch</th>
                        <th class="px-4 py-3">Team login</th>
                        <th class="px-4 py-3">Status</th>
                        <th class="px-4 py-3"></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-50">
                    @forelse ($practitioners as $practitioner)
                        <tr>
                            <td class="px-4 py-3 font-medium">{{ $practitioner->name }}</td>
                            <td class="px-4 py-3">{{ $practitioner->specialty ?: '—' }}</td>
                            <td class="px-4 py-3">
                                @php $branchNames = $practitioner->branches->pluck('name'); @endphp
                                @if ($branchNames->isNotEmpty())
                                    {{ $branchNames->implode(', ') }}
                                @elseif ($practitioner->branch)
                                    {{ $practitioner->branch->name }}
                                @else
                                    <span class="text-amber-700">Unassigned</span>
                                @endif
                            </td>
                            <td class="px-4 py-3">{{ $practitioner->member?->user_ref ?? '—' }}</td>
                            <td class="px-4 py-3">
                                <span class="inline-flex rounded-full px-2 py-0.5 text-xs font-medium {{ $practitioner->is_active ? 'bg-emerald-50 text-emerald-700' : 'bg-slate-100 text-slate-600' }}">
                                    {{ $practitioner->is_active ? 'Active' : 'Inactive' }}
                                </span>
                            </td>
                            <td class="px-4 py-3 text-right">
                                <a href="{{ route('care.practitioners.edit', $practitioner) }}" class="text-sky-600">Edit</a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="px-4 py-6 text-center text-slate-500">
                                No practitioners yet. Add doctors here so they appear on appointments and the queue.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</x-app-layout>
