@php
    $member = auth()->user()
        ? app(\App\Services\Care\OrganizationResolver::class)->memberFor(auth()->user())
        : null;
    $permissions = app(\App\Services\Care\CarePermissions::class);
    $canManageBranches = $permissions->can($member, 'admin.branches.manage');
    $canViewTeam = $permissions->can($member, 'admin.members.view');
@endphp

<x-app-layout title="Branches">
    <x-settings.page title="Branches" description="Organize clinic locations and sites so patients, departments, and staff stay scoped to the right branch.">
        <div class="mb-4 flex flex-wrap items-center justify-between gap-3">
            <div class="flex flex-wrap items-center gap-2 text-sm">
                <a href="{{ route('care.settings') }}" class="text-slate-500 hover:text-slate-800">Settings</a>
                <span class="text-slate-300">/</span>
                <span class="font-medium text-slate-900">Branches</span>
                @if ($canViewTeam)
                    <a href="{{ route('care.members.index') }}" class="ml-3 text-indigo-600 hover:text-indigo-800">Team</a>
                @endif
            </div>
            @if ($canManageBranches)
                <a href="{{ route('care.branches.create') }}" class="btn-primary">Add branch</a>
            @endif
        </div>

        <x-settings.card title="All branches" description="{{ number_format($heroStats['total']) }} total · {{ number_format($heroStats['active']) }} active · {{ number_format($heroStats['departments']) }} department(s)">
            @forelse ($branches as $branch)
                <div class="flex items-center justify-between gap-4 border-b border-slate-50 py-3 last:border-b-0">
                    <div>
                        <p class="font-medium text-slate-900">{{ $branch->name }}</p>
                        <p class="text-sm text-slate-500">{{ $branch->address ?? 'No address' }} · {{ $branch->departments_count }} department(s)</p>
                    </div>
                    <div class="flex items-center gap-3">
                        @if ($canManageBranches)
                            <a href="{{ route('care.branches.edit', $branch) }}" class="text-sm font-medium text-slate-600 hover:text-slate-900">Edit</a>
                        @endif
                    </div>
                </div>
            @empty
                <p class="text-sm text-slate-500">No branches yet.</p>
            @endforelse
        </x-settings.card>
    </x-settings.page>
</x-app-layout>
