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

<x-app-layout title="Team">
    <x-settings.page title="Team" description="Manage who can access Care, their roles, and which branch each member belongs to.">
        <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">Team</span>
                @if ($canViewBranches)
                    <a href="{{ route('care.branches.index') }}" class="ml-3 text-indigo-600 hover:text-indigo-800">Branches</a>
                @endif
            </div>
            @if ($canManageMembers)
                <a href="{{ route('care.members.create') }}" class="btn-primary">Add member</a>
            @endif
        </div>

        <x-settings.card title="Team members" description="{{ number_format($heroStats['total']) }} members · {{ number_format($heroStats['admins']) }} administrators · {{ number_format($heroStats['clinical']) }} clinical · {{ number_format($heroStats['branch_scoped']) }} branch-scoped">
            @if ($members->isEmpty())
                <p class="text-sm text-slate-500">No team members yet.</p>
            @else
                <div class="overflow-x-auto">
                    <table class="min-w-full text-sm">
                        <thead class="text-left text-xs uppercase text-slate-500">
                            <tr>
                                <th class="pb-2 pr-4">Member</th>
                                <th class="pb-2 pr-4">Role</th>
                                <th class="pb-2 pr-4">Branch</th>
                                <th class="pb-2"></th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-slate-100">
                            @foreach ($members as $teamMember)
                                @php
                                    $display = str_contains($teamMember->user_ref, '@')
                                        ? $teamMember->user_ref
                                        : (\App\Models\User::where('public_id', $teamMember->user_ref)->value('email') ?? $teamMember->user_ref);
                                @endphp
                                <tr>
                                    <td class="py-3 pr-4">{{ $display }}</td>
                                    <td class="py-3 pr-4">{{ $roles[$teamMember->role] ?? $teamMember->role }}</td>
                                    <td class="py-3 pr-4">{{ $teamMember->branch?->name ?? 'All branches' }}</td>
                                    <td class="py-3 text-right">
                                        @if ($canManageMembers && $teamMember->user_ref !== auth()->user()->public_id)
                                            <x-confirm-dialog
                                                :name="'remove-member-'.$teamMember->id"
                                                title="Remove this member?"
                                                message="They will lose access to this Care organization."
                                                :action="route('care.members.destroy', $teamMember)"
                                                method="DELETE"
                                                confirm-label="Remove member"
                                            >
                                                <x-slot:trigger>
                                                    <button type="button" class="text-sm font-medium text-red-600 hover:text-red-800">Remove</button>
                                                </x-slot:trigger>
                                            </x-confirm-dialog>
                                        @endif
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            @endif
        </x-settings.card>
    </x-settings.page>
</x-app-layout>
