<x-app-layout>
    <x-slot name="header">
        {{ __('Dealer Provisioning Summary') }}
    </x-slot>

    <div class="p-6 space-y-6">
        <div class="bg-white border border-gray-200 rounded-xl shadow-sm p-6 space-y-3">
            @php
                $counts = $summary['counts'] ?? null;
                $users = $summary['users'] ?? $summary;
                $created = $users['created'] ?? [];
                $existing = $users['existing'] ?? [];
                $runType = $summary['run_type'] ?? null;
                $showPasswords = $runType !== 'reprovision' && !empty($created) && isset($created[0]['temp_password']);
            @endphp

            <div>
                <h1 class="text-xl font-semibold text-gray-900">
                    Provisioning Summary for {{ $dealer->name }}
                </h1>
                <p class="mt-0.5 text-xs text-gray-500">
                    Domain: {{ $dealer->domain ?? 'n/a' }}
                </p>
                <p class="mt-1 text-sm text-gray-600">
                    @if ($runType === 'reprovision')
                        This summary reflects the latest re-provisioning run. Existing user passwords are never reset and are not displayed here.
                    @else
                        These details are shown once after provisioning. Temporary passwords will not be displayed again.
                    @endif
                </p>
            </div>

            @if (! $summary)
                <p class="text-sm text-gray-600">
                    No recent provisioning summary available for this dealer. Temporary passwords are only shown immediately after initial provisioning.
                </p>
            @else
                @if ($counts)
                    <div class="mt-4">
                        <h2 class="text-sm font-semibold text-gray-900">Provisioning Counts</h2>
                        <dl class="mt-2 grid grid-cols-1 sm:grid-cols-3 gap-3 text-xs text-gray-700">
                            <div class="bg-gray-50 rounded-lg px-3 py-2">
                                <dt class="font-medium text-gray-900">Departments created</dt>
                                <dd class="mt-0.5">{{ $counts['departments_created'] ?? 0 }}</dd>
                            </div>
                            <div class="bg-gray-50 rounded-lg px-3 py-2">
                                <dt class="font-medium text-gray-900">Roles created</dt>
                                <dd class="mt-0.5">{{ $counts['roles_created'] ?? 0 }}</dd>
                            </div>
                            <div class="bg-gray-50 rounded-lg px-3 py-2">
                                <dt class="font-medium text-gray-900">Permissions created</dt>
                                <dd class="mt-0.5">{{ $counts['permissions_created'] ?? 0 }}</dd>
                            </div>
                            <div class="bg-gray-50 rounded-lg px-3 py-2">
                                <dt class="font-medium text-gray-900">Users created</dt>
                                <dd class="mt-0.5">{{ $counts['users_created'] ?? 0 }}</dd>
                            </div>
                            <div class="bg-gray-50 rounded-lg px-3 py-2">
                                <dt class="font-medium text-gray-900">Existing users aligned</dt>
                                <dd class="mt-0.5">{{ $counts['users_existing'] ?? 0 }}</dd>
                            </div>
                        </dl>
                    </div>
                @endif

                @if (!empty($created))
                    <div class="mt-4">
                        <h2 class="text-sm font-semibold text-gray-900">Newly Created Users</h2>
                        @if ($showPasswords)
                            <p class="mt-1 text-xs text-gray-500">
                                Share these temporary passwords securely with each user and ask them to change on first login.
                            </p>
                        @else
                            <p class="mt-1 text-xs text-gray-500">
                                Temporary passwords are not displayed for this run.
                            </p>
                        @endif
                        <div class="mt-3 overflow-x-auto">
                            <table class="min-w-full text-sm divide-y divide-gray-200">
                                <thead class="bg-gray-50">
                                    <tr>
                                        <th class="px-3 py-2 text-left font-semibold text-gray-700">Name</th>
                                        <th class="px-3 py-2 text-left font-semibold text-gray-700">Email</th>
                                        <th class="px-3 py-2 text-left font-semibold text-gray-700">Role</th>
                                        @if ($showPasswords)
                                            <th class="px-3 py-2 text-left font-semibold text-gray-700">Temporary Password</th>
                                        @endif
                                    </tr>
                                </thead>
                                <tbody class="divide-y divide-gray-100">
                                    @foreach ($created as $user)
                                        <tr>
                                            <td class="px-3 py-2 text-gray-900">{{ $user['name'] }}</td>
                                            <td class="px-3 py-2 text-gray-800 font-mono text-xs">{{ $user['email'] }}</td>
                                            <td class="px-3 py-2 text-gray-800">{{ $user['role'] }}</td>
                                            @if ($showPasswords)
                                                <td class="px-3 py-2 font-mono text-xs text-gray-900">
                                                    {{ $user['temp_password'] }}
                                                </td>
                                            @endif
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                @endif

                @if (!empty($existing))
                    <div class="mt-6">
                        <h2 class="text-sm font-semibold text-gray-900">Existing Users (updated)</h2>
                        <p class="mt-1 text-xs text-gray-500">
                            These users already existed. Their roles and departments were aligned with the dealer baseline.
                        </p>
                        <div class="mt-3 overflow-x-auto">
                            <table class="min-w-full text-sm divide-y divide-gray-200">
                                <thead class="bg-gray-50">
                                    <tr>
                                        <th class="px-3 py-2 text-left font-semibold text-gray-700">Name</th>
                                        <th class="px-3 py-2 text-left font-semibold text-gray-700">Email</th>
                                        <th class="px-3 py-2 text-left font-semibold text-gray-700">Role</th>
                                    </tr>
                                </thead>
                                <tbody class="divide-y divide-gray-100">
                                    @foreach ($existing as $user)
                                        <tr>
                                            <td class="px-3 py-2 text-gray-900">{{ $user['name'] }}</td>
                                            <td class="px-3 py-2 text-gray-800 font-mono text-xs">{{ $user['email'] }}</td>
                                            <td class="px-3 py-2 text-gray-800">{{ $user['role'] }}</td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                @endif
            @endif

            <div class="pt-4">
                <a
                    href="{{ route('settings.dealers.index') }}"
                    class="inline-flex items-center rounded-md border border-gray-200 px-4 py-2 text-sm text-gray-700 hover:bg-gray-50"
                >
                    Back to Dealers
                </a>
            </div>
        </div>
    </div>
</x-app-layout>

