<x-app-layout>
    <x-slot name="header">
        <div class="rounded-2xl border border-sky-200/70 bg-gradient-to-r from-sky-50 to-green-50 px-5 py-4">
            <h2 class="text-xl font-semibold leading-tight text-sky-900">{{ __('Admin Dashboard') }}</h2>
            <p class="mt-1 text-sm text-sky-700">{{ __('System-wide activity, fleet totals, billing, and company health.') }}</p>
        </div>
    </x-slot>

    <div class="py-8">
        <div class="mx-auto max-w-7xl space-y-8 px-4 sm:px-6 lg:px-8">
            <div class="grid gap-6 md:grid-cols-2 xl:grid-cols-4">
                <div class="rounded-2xl border border-cyan-200 bg-gradient-to-br from-cyan-50 via-white to-slate-50 p-6 shadow-sm">
                    <p class="text-sm font-medium text-teal-700">{{ __('Companies') }}</p>
                    <p class="mt-3 text-3xl font-semibold text-gray-900">{{ number_format($stats['companies']) }}</p>
                    <p class="mt-2 text-sm text-gray-500">{{ number_format($stats['owners']) }} {{ __('owners on platform') }}</p>
                </div>
                <div class="rounded-2xl border border-emerald-200 bg-gradient-to-br from-emerald-50 via-white to-slate-50 p-6 shadow-sm">
                    <p class="text-sm font-medium text-emerald-700">{{ __('Revenue') }}</p>
                    <p class="mt-3 text-3xl font-semibold text-gray-900">${{ number_format($stats['revenue'], 2) }}</p>
                    <p class="mt-2 text-sm text-gray-500">${{ number_format($stats['pending_revenue'], 2) }} {{ __('pending') }}</p>
                </div>
                <div class="rounded-2xl border border-orange-200 bg-gradient-to-br from-orange-50 via-white to-slate-50 p-6 shadow-sm">
                    <p class="text-sm font-medium text-orange-700">{{ __('Subscriptions') }}</p>
                    <p class="mt-3 text-3xl font-semibold text-gray-900">{{ number_format($stats['active_subscriptions']) }}</p>
                    <p class="mt-2 text-sm text-gray-500">{{ __('active paid subscriptions') }}</p>
                </div>
                <div class="rounded-2xl border border-violet-200 bg-gradient-to-br from-violet-50 via-white to-fuchsia-50 p-6 shadow-sm">
                    <p class="text-sm font-medium text-violet-700">{{ __('Fleet Assets') }}</p>
                    <p class="mt-3 text-3xl font-semibold text-gray-900">{{ number_format($stats['trucks']) }}</p>
                    <p class="mt-2 text-sm text-gray-500">{{ number_format($stats['drivers']) }} {{ __('drivers,') }} {{ number_format($stats['loads']) }} {{ __('loads') }}</p>
                </div>
            </div>

            <div class="grid gap-8 xl:grid-cols-[1.3fr,0.9fr]">
                <section class="overflow-hidden rounded-2xl border border-sky-200 bg-gradient-to-br from-white to-sky-50 shadow-sm">
                    <div class="flex items-center justify-between border-b border-sky-200 bg-gradient-to-r from-sky-50 to-white px-6 py-4">
                        <div>
                            <h3 class="text-lg font-semibold text-gray-900">{{ __('Companies Overview') }}</h3>
                            <p class="text-sm text-gray-500">{{ __('Recent companies with package, fleet, and revenue snapshot.') }}</p>
                        </div>
                        <a href="{{ route('admin.companies.index') }}" class="text-sm font-medium text-sky-700 hover:text-sky-800">{{ __('View all') }}</a>
                    </div>
                    <div class="overflow-x-auto">
                        <table class="min-w-full divide-y divide-sky-100">
                            <thead class="bg-sky-50/40">
                                <tr>
                                    <th class="px-6 py-3 text-left text-xs font-semibold uppercase tracking-wide text-sky-700">{{ __('Company') }}</th>
                                    <th class="px-6 py-3 text-left text-xs font-semibold uppercase tracking-wide text-sky-700">{{ __('Package') }}</th>
                                    <th class="px-6 py-3 text-left text-xs font-semibold uppercase tracking-wide text-sky-700">{{ __('Fleet') }}</th>
                                    <th class="px-6 py-3 text-left text-xs font-semibold uppercase tracking-wide text-sky-700">{{ __('Revenue') }}</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-sky-100 bg-white">
                                @forelse ($companies as $item)
                                    <tr>
                                        <td class="px-6 py-4 align-top">
                                            <div class="font-medium text-gray-900">{{ $item['company']->name }}</div>
                                            <div class="text-sm text-gray-600">{{ $item['owner']?->email ?? __('No owner assigned') }}</div>
                                        </td>
                                        <td class="px-6 py-4 align-top">
                                            <span class="inline-flex rounded-full bg-sky-100 px-3 py-1 text-xs font-semibold text-sky-800">
                                                {{ $item['package_name'] ?? __('No active plan') }}
                                            </span>
                                        </td>
                                        <td class="px-6 py-4 align-top text-sm text-gray-600">
                                            {{ $item['trucks_count'] }} {{ __('trucks') }}
                                            <span class="mx-1 text-gray-300">/</span>
                                            {{ $item['drivers_count'] }} {{ __('drivers') }}
                                            <span class="mx-1 text-gray-300">/</span>
                                            {{ $item['loads_count'] }} {{ __('loads') }}
                                        </td>
                                        <td class="px-6 py-4 align-top">
                                            <span class="inline-flex rounded-full bg-sky-100 px-3 py-1 text-sm font-semibold text-sky-800">
                                                ${{ number_format($item['revenue'], 2) }}
                                            </span>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="4" class="px-6 py-6 text-center text-sm text-gray-500">{{ __('No company records yet.') }}</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </section>

                <section class="overflow-hidden rounded-2xl border border-sky-200 bg-gradient-to-br from-white to-sky-50 shadow-sm">
                    <div class="border-b border-sky-200 bg-gradient-to-r from-sky-50 to-white px-6 py-4">
                        <h3 class="text-lg font-semibold text-gray-900">{{ __('Recent Payments') }}</h3>
                        <p class="text-sm text-gray-500">{{ __('Latest billing activity across all companies.') }}</p>
                    </div>
                    <div class="divide-y divide-gray-100">
                        @forelse ($recentPayments as $payment)
                            <div class="flex items-start justify-between gap-4 px-6 py-4">
                                <div>
                                    <p class="font-medium text-gray-900">{{ $payment->subscription->user->name }}</p>
                                    <p class="text-sm text-gray-500">{{ $payment->subscription->user->company?->name ?? __('No company') }}</p>
                                    <p class="mt-1 text-xs text-gray-400">{{ $payment->created_at->format('M d, Y H:i') }}</p>
                                </div>
                                <div class="text-right">
                                    <p class="font-semibold text-gray-900">${{ number_format($payment->amount, 2) }}</p>
                                    <span class="mt-1 inline-flex rounded-full px-3 py-1 text-xs font-semibold {{ $payment->status === 'completed' ? 'bg-sky-100 text-sky-800' : ($payment->status === 'pending' ? 'bg-amber-100 text-amber-800' : 'bg-rose-100 text-rose-800') }}">
                                        {{ ucfirst($payment->status) }}
                                    </span>
                                </div>
                            </div>
                        @empty
                            <div class="px-6 py-6 text-sm text-gray-500">{{ __('No payments found.') }}</div>
                        @endforelse
                    </div>
                </section>
            </div>
        </div>
    </div>
</x-app-layout>
