@php
    $stats = $sheet['statistics'] ?? [];
    $roleLabel = fn (int $count, string $role) => $role.' ('.$count.')';
    $permissions = $sheet['permissions'] ?? [];
    $userList = $sheet['human_permissions'] ?? [];
    $serviceList = $sheet['service_permissions'] ?? [];
    $ownedByMe = $sheet['owned_by_me'] ?? null;
@endphp

<article class="mb-12 pb-12 border-b border-slate-200 last:border-0 last:pb-0 last:mb-0">
    <div class="mb-6">
        <h2 class="text-xl font-bold text-slate-900">
            {{ $sheet['label'] ?? 'Sheet' }}
            @if (!empty($sheet['file_title']))
                <span class="font-normal text-slate-600">— {{ $sheet['file_title'] }}</span>
            @endif
        </h2>
        @if (!empty($sheet['spreadsheet_id']))
            <p class="text-slate-500 text-sm font-mono mt-1">{{ $sheet['spreadsheet_id'] }}</p>
        @endif
    </div>

    @if (!empty($sheet['error']))
        <div class="rounded-xl border border-red-200 bg-red-50 px-5 py-4 text-sm text-red-800 whitespace-pre-line">
            {{ $sheet['error'] }}
        </div>
    @else
        @if ($ownedByMe === false)
            <div class="mb-6 rounded-xl border border-amber-300 bg-amber-50 px-5 py-4 text-sm text-amber-900">
                <p class="font-semibold">Google không trả email — tài khoản OAuth không phải chủ sheet này.</p>
            </div>
        @endif

        <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-8">
            <div class="bg-white rounded-xl border shadow-sm p-4">
                <p class="text-xs text-slate-500 uppercase tracking-wide">Tổng user</p>
                <p class="text-2xl font-bold text-slate-900 mt-1">{{ $stats['total_users'] ?? 0 }}</p>
            </div>
            <div class="bg-violet-50 rounded-xl border border-violet-200 shadow-sm p-4">
                <p class="text-xs text-violet-700 uppercase tracking-wide">Service</p>
                <p class="text-2xl font-bold text-violet-900 mt-1">{{ $stats['total_service_accounts'] ?? 0 }}</p>
            </div>
            <div class="bg-emerald-50 rounded-xl border border-emerald-200 shadow-sm p-4">
                <p class="text-xs text-emerald-700 uppercase tracking-wide">Người</p>
                <p class="text-2xl font-bold text-emerald-900 mt-1">{{ $stats['total_human_accounts'] ?? 0 }}</p>
            </div>
            <div class="bg-slate-50 rounded-xl border shadow-sm p-4">
                <p class="text-xs text-slate-500 uppercase tracking-wide">Quyền khác</p>
                <p class="text-2xl font-bold text-slate-900 mt-1">{{ $stats['other_permissions'] ?? 0 }}</p>
            </div>
        </div>

        <section class="bg-violet-50 border-2 border-violet-300 rounded-xl shadow-sm overflow-hidden mb-6">
            <div class="px-5 py-3 border-b border-violet-200 bg-violet-100/60">
                <h3 class="font-semibold text-violet-900">Tài khoản service</h3>
                @if (!empty($stats['service_by_role']))
                    <p class="text-xs text-violet-800 mt-1">{{ collect($stats['service_by_role'])->map($roleLabel)->implode(', ') }}</p>
                @endif
            </div>
            <div class="overflow-x-auto bg-white">
                <table class="w-full text-sm">
                    <thead class="text-left text-violet-900 border-b bg-violet-50">
                        <tr>
                            <th class="px-5 py-2">Email</th>
                            <th class="px-5 py-2">Tên</th>
                            <th class="px-5 py-2">Quyền</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($serviceList as $perm)
                            <tr class="border-b border-violet-50">
                                <td class="px-5 py-2 font-mono text-xs">{{ $perm['email'] ?? ($perm['email_hidden'] ?? false ? '(ẩn)' : '—') }}</td>
                                <td class="px-5 py-2">{{ $perm['name'] ?? '—' }}</td>
                                <td class="px-5 py-2">{{ $perm['role'] }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="3" class="px-5 py-4 text-center text-slate-500">Không có</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </section>

        <section class="bg-emerald-50 border-2 border-emerald-300 rounded-xl shadow-sm overflow-hidden mb-6">
            <div class="px-5 py-3 border-b border-emerald-200 bg-emerald-100/60">
                <h3 class="font-semibold text-emerald-900">Tài khoản người</h3>
                @if (!empty($stats['human_by_role']))
                    <p class="text-xs text-emerald-800 mt-1">{{ collect($stats['human_by_role'])->map($roleLabel)->implode(', ') }}</p>
                @endif
            </div>
            <div class="overflow-x-auto bg-white">
                <table class="w-full text-sm">
                    <thead class="text-left text-emerald-900 border-b bg-emerald-50">
                        <tr>
                            <th class="px-5 py-2">Email</th>
                            <th class="px-5 py-2">Tên</th>
                            <th class="px-5 py-2">Quyền</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($userList as $perm)
                            <tr class="border-b border-emerald-50">
                                <td class="px-5 py-2 font-semibold">{{ $perm['email'] ?? ($perm['email_hidden'] ?? false ? '(ẩn)' : '—') }}</td>
                                <td class="px-5 py-2">{{ $perm['name'] ?? '—' }}</td>
                                <td class="px-5 py-2">{{ $perm['role'] }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="3" class="px-5 py-4 text-center text-slate-500">Không có</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </section>

        @php
            $otherPermissions = collect($permissions)->filter(fn ($p) => ($p['type'] ?? '') !== 'user')->values();
        @endphp
        @if ($otherPermissions->isNotEmpty())
            <section class="bg-white rounded-xl border shadow-sm overflow-hidden">
                <div class="px-5 py-3 border-b bg-slate-50 font-semibold text-sm">Quyền khác</div>
                <div class="overflow-x-auto text-sm">
                    <table class="w-full">
                        <tbody>
                            @foreach ($otherPermissions as $perm)
                                <tr class="border-b border-slate-50">
                                    <td class="px-5 py-2">{{ $perm['type'] }}</td>
                                    <td class="px-5 py-2">{{ $perm['email'] ?? $perm['name'] ?? '—' }}</td>
                                    <td class="px-5 py-2">{{ $perm['role'] }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </section>
        @endif
    @endif
</article>
