@extends('layouts.app')

@section('title', 'Super Admin Dashboard')
@section('heading', 'Platform Dashboard')

@section('content')
@php
    $symbol = config('app.currency_symbol', '₹');
@endphp
<div class="w-full">
    <x-superadmin.page-header
        eyebrow="Platform"
        title="Dashboard"
        subtitle="Yearly subscriptions, licence health, offline requests, and recent billing activity."
    >
        <x-slot:actions>
            <a href="{{ route('superadmin.offline-requests.index') }}" class="inline-flex rounded-xl border border-slate-200 px-4 py-2.5 text-sm font-semibold text-slate-700 hover:bg-slate-50">
                Offline requests
                @if(($pendingOfflineRequests ?? 0) > 0)
                    <span class="ml-2 inline-flex min-w-[1.25rem] items-center justify-center rounded-full bg-amber-100 px-1.5 text-[10px] font-bold text-amber-800">{{ $pendingOfflineRequests }}</span>
                @endif
            </a>
            <a href="{{ route('superadmin.payments.index') }}" class="inline-flex rounded-xl bg-primary px-4 py-2.5 text-sm font-semibold text-white shadow-sm hover:opacity-95">Payments</a>
        </x-slot:actions>
    </x-superadmin.page-header>

    <div class="mb-6 grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
        <div class="relative overflow-hidden rounded-2xl border border-slate-200 bg-white px-4 py-3 pl-5 shadow-sm">
            <span class="absolute inset-y-0 left-0 w-1.5 rounded-l-2xl bg-sky-500" aria-hidden="true"></span>
            <p class="text-[11px] font-semibold uppercase tracking-wide text-slate-400">Active tenants</p>
            <p class="mt-1 text-xl font-bold text-slate-900">{{ $activeCompanies }}</p>
            <p class="mt-0.5 text-xs text-slate-500">{{ $totalCompanies }} total · {{ $newThisMonth }} new this month</p>
        </div>
        <div class="relative overflow-hidden rounded-2xl border border-slate-200 bg-white px-4 py-3 pl-5 shadow-sm">
            <span class="absolute inset-y-0 left-0 w-1.5 rounded-l-2xl bg-amber-500" aria-hidden="true"></span>
            <p class="text-[11px] font-semibold uppercase tracking-wide text-slate-400">Licence expiring</p>
            <p class="mt-1 text-xl font-bold {{ $expiringSoon > 0 ? 'text-amber-700' : 'text-slate-900' }}">{{ $expiringSoon }}</p>
            <p class="mt-0.5 text-xs text-slate-500">Within 30 days</p>
        </div>
        <div class="relative overflow-hidden rounded-2xl border border-slate-200 bg-white px-4 py-3 pl-5 shadow-sm">
            <span class="absolute inset-y-0 left-0 w-1.5 rounded-l-2xl bg-rose-500" aria-hidden="true"></span>
            <p class="text-[11px] font-semibold uppercase tracking-wide text-slate-400">Suspended</p>
            <p class="mt-1 text-xl font-bold {{ $suspendedCompanies > 0 ? 'text-red-700' : 'text-slate-900' }}">{{ $suspendedCompanies }}</p>
            <p class="mt-0.5 text-xs text-slate-500">Access blocked until renew</p>
        </div>
        <div class="relative overflow-hidden rounded-2xl border border-slate-200 bg-white px-4 py-3 pl-5 shadow-sm">
            <span class="absolute inset-y-0 left-0 w-1.5 rounded-l-2xl bg-violet-500" aria-hidden="true"></span>
            <p class="text-[11px] font-semibold uppercase tracking-wide text-slate-400">Pending offline</p>
            <p class="mt-1 text-xl font-bold {{ $pendingOfflineRequests > 0 ? 'text-amber-700' : 'text-slate-900' }}">{{ $pendingOfflineRequests }}</p>
            <p class="mt-0.5 text-xs text-slate-500">Awaiting approval</p>
        </div>
    </div>

    <div class="mb-6 grid gap-3 sm:grid-cols-3">
        <div class="relative overflow-hidden rounded-2xl border border-slate-200 bg-white px-4 py-3 pl-5 shadow-sm">
            <span class="absolute inset-y-0 left-0 w-1.5 rounded-l-2xl bg-emerald-500" aria-hidden="true"></span>
            <p class="text-[11px] font-semibold uppercase tracking-wide text-slate-400">Revenue this month</p>
            <p class="mt-1 text-xl font-bold text-slate-900">{{ $symbol }}{{ number_format($revenueThisMonth, 0) }}</p>
        </div>
        <div class="relative overflow-hidden rounded-2xl border border-slate-200 bg-white px-4 py-3 pl-5 shadow-sm">
            <span class="absolute inset-y-0 left-0 w-1.5 rounded-l-2xl bg-teal-600" aria-hidden="true"></span>
            <p class="text-[11px] font-semibold uppercase tracking-wide text-slate-400">Total collected</p>
            <p class="mt-1 text-xl font-bold text-slate-900">{{ $symbol }}{{ number_format($revenueAll, 0) }}</p>
        </div>
        <div class="relative overflow-hidden rounded-2xl border border-slate-200 bg-white px-4 py-3 pl-5 shadow-sm">
            <span class="absolute inset-y-0 left-0 w-1.5 rounded-l-2xl bg-indigo-500" aria-hidden="true"></span>
            <p class="text-[11px] font-semibold uppercase tracking-wide text-slate-400">Tenant users</p>
            <p class="mt-1 text-xl font-bold text-slate-900">{{ number_format($totalUsers) }}</p>
        </div>
    </div>

    <div class="mb-6 flex flex-wrap gap-2">
        <a href="{{ route('superadmin.companies.index') }}" class="inline-flex rounded-xl bg-primary px-3.5 py-2 text-sm font-semibold text-white shadow-sm hover:opacity-95">Companies</a>
        <a href="{{ route('superadmin.subscriptions.index') }}" class="inline-flex rounded-xl border border-slate-200 bg-white px-3.5 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Subscriptions</a>
        <a href="{{ route('superadmin.plans.index') }}" class="inline-flex rounded-xl border border-slate-200 bg-white px-3.5 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Plans</a>
        <a href="{{ route('superadmin.coupons.index') }}" class="inline-flex rounded-xl border border-slate-200 bg-white px-3.5 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Coupons</a>
        <a href="{{ route('superadmin.offline-requests.index') }}" class="inline-flex rounded-xl border border-slate-200 bg-white px-3.5 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Offline requests</a>
        <a href="{{ route('superadmin.payment-settings.index') }}" class="inline-flex rounded-xl border border-slate-200 bg-white px-3.5 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Payment settings</a>
    </div>

    <div class="grid gap-4 lg:grid-cols-2">
        <section class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm">
            <div class="flex items-center justify-between border-b border-slate-100 px-5 py-4">
                <h3 class="text-sm font-semibold text-slate-900">Pending offline requests</h3>
                <a href="{{ route('superadmin.offline-requests.index') }}" class="inline-flex items-center gap-1 text-xs font-semibold text-primary hover:underline">
                    View all <i class="bi bi-chevron-right text-[10px]"></i>
                </a>
            </div>
            <div class="divide-y divide-slate-100">
                @forelse($pendingRequests as $req)
                    <div class="flex items-center justify-between gap-3 px-5 py-3.5">
                        <div class="min-w-0">
                            <p class="truncate text-sm font-semibold text-slate-900">{{ $req->company?->name ?? '—' }}</p>
                            <p class="mt-0.5 text-xs text-slate-500">
                                {{ $req->subscriptionPlan?->name ?? 'Plan' }}
                                · {{ $symbol }}{{ number_format((float) ($req->amount ?? 0), 0) }}
                                @if($req->is_upgrade ?? false)
                                    · <span class="text-amber-700">Upgrade</span>
                                @endif
                            </p>
                        </div>
                        <a href="{{ route('superadmin.offline-requests.approve', $req) }}" class="shrink-0 rounded-xl border border-slate-200 px-3 py-1.5 text-xs font-semibold text-slate-700 hover:bg-slate-50">Review</a>
                    </div>
                @empty
                    <p class="px-5 py-10 text-center text-sm text-slate-500">No pending offline requests.</p>
                @endforelse
            </div>
        </section>

        <section class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm">
            <div class="flex items-center justify-between border-b border-slate-100 px-5 py-4">
                <h3 class="text-sm font-semibold text-slate-900">Licence expiring (30 days)</h3>
                <a href="{{ route('superadmin.subscriptions.index') }}" class="text-xs font-semibold text-primary hover:underline">Subscriptions</a>
            </div>
            <div class="divide-y divide-slate-100">
                @forelse($expiringCompanies as $c)
                    @php $days = $c->daysUntilLicenceExpiry(); @endphp
                    <div class="flex items-center justify-between gap-3 px-5 py-3.5">
                        <div class="min-w-0">
                            <p class="truncate text-sm font-semibold text-slate-900">{{ $c->name }}</p>
                            <p class="mt-0.5 text-xs text-slate-500">{{ $c->subscriptionPlan?->name ?? 'No plan' }} · until {{ $c->licence_expire_on?->format('d M Y') }}</p>
                        </div>
                        <span class="shrink-0 inline-flex rounded-full bg-amber-50 px-2 py-0.5 text-[10px] font-bold uppercase tracking-wide text-amber-800 ring-1 ring-inset ring-amber-200">
                            {{ $days === 0 ? 'Today' : $days.'d' }}
                        </span>
                    </div>
                @empty
                    <p class="px-5 py-10 text-center text-sm text-slate-500">No licences expiring in the next 30 days.</p>
                @endforelse
            </div>
        </section>

        <section class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm">
            <div class="flex items-center justify-between border-b border-slate-100 px-5 py-4">
                <h3 class="text-sm font-semibold text-slate-900">Tenants by yearly plan</h3>
                <a href="{{ route('superadmin.plans.index') }}" class="text-xs font-semibold text-primary hover:underline">Manage plans</a>
            </div>
            <ul class="divide-y divide-slate-100">
                @forelse($companiesByPlan as $row)
                    <li class="flex items-center justify-between gap-3 px-5 py-3">
                        <div class="min-w-0">
                            <p class="truncate text-sm font-semibold text-slate-900">
                                {{ $row['plan'] }}
                                @if($row['is_custom'])
                                    <span class="ml-1 inline-flex rounded-full bg-slate-100 px-1.5 py-0.5 text-[9px] font-bold uppercase text-slate-500">Custom</span>
                                @endif
                            </p>
                            <p class="mt-0.5 text-xs text-slate-500">
                                {{ $symbol }}{{ number_format($row['yearly'], 0) }}/yr
                                · {{ $row['seats'] !== null ? $row['seats'].' seats' : 'Unlimited' }}
                            </p>
                        </div>
                        <span class="text-sm font-bold text-slate-900">{{ $row['total'] }}</span>
                    </li>
                @empty
                    <li class="px-5 py-10 text-center text-sm text-slate-500">No plans defined yet.</li>
                @endforelse
            </ul>
        </section>

        <section class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm">
            <div class="flex items-center justify-between border-b border-slate-100 px-5 py-4">
                <h3 class="text-sm font-semibold text-slate-900">Recent payments</h3>
                <a href="{{ route('superadmin.payments.index') }}" class="inline-flex items-center gap-1 text-xs font-semibold text-primary hover:underline">
                    View all <i class="bi bi-chevron-right text-[10px]"></i>
                </a>
            </div>
            <div class="divide-y divide-slate-100">
                @forelse($recentPayments as $sp)
                    <div class="flex items-center justify-between gap-3 px-5 py-3.5">
                        <div class="min-w-0">
                            <p class="truncate text-sm font-semibold text-slate-900">{{ $sp->company?->name ?? '—' }}</p>
                            <p class="mt-0.5 text-xs text-slate-500">
                                {{ ucfirst($sp->payment_method) }}
                                · {{ $sp->paid_at?->format('d M Y') ?? $sp->created_at?->format('d M Y') }}
                            </p>
                        </div>
                        <div class="shrink-0 text-right">
                            <p class="text-sm font-bold text-slate-900">{{ $symbol }}{{ number_format((float) $sp->amount, 0) }}</p>
                            @if($sp->status === 'completed')
                                <span class="text-[10px] font-bold uppercase tracking-wide text-emerald-700">Paid</span>
                            @else
                                <span class="text-[10px] font-bold uppercase tracking-wide text-slate-500">{{ ucfirst($sp->status) }}</span>
                            @endif
                        </div>
                    </div>
                @empty
                    <p class="px-5 py-10 text-center text-sm text-slate-500">No subscription payments yet.</p>
                @endforelse
            </div>
        </section>
    </div>

    <section class="mt-4 overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm">
        <div class="flex items-center justify-between border-b border-slate-100 px-5 py-4">
            <h3 class="text-sm font-semibold text-slate-900">Recent tenants</h3>
            <a href="{{ route('superadmin.companies.index') }}" class="inline-flex items-center gap-1 text-xs font-semibold text-primary hover:underline">
                View all companies <i class="bi bi-chevron-right text-[10px]"></i>
            </a>
        </div>
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-slate-100 text-sm">
                <thead class="bg-slate-50/80 text-left">
                    <tr>
                        <th class="px-4 py-3 text-[11px] font-semibold uppercase tracking-wide text-slate-400">Company</th>
                        <th class="px-4 py-3 text-[11px] font-semibold uppercase tracking-wide text-slate-400">Plan</th>
                        <th class="px-4 py-3 text-[11px] font-semibold uppercase tracking-wide text-slate-400">Status</th>
                        <th class="px-4 py-3 text-[11px] font-semibold uppercase tracking-wide text-slate-400">Licence until</th>
                        <th class="px-4 py-3 text-[11px] font-semibold uppercase tracking-wide text-slate-400"></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    @forelse($recentCompanies as $c)
                        <tr class="hover:bg-slate-50/80">
                            <td class="whitespace-nowrap px-4 py-3.5 font-semibold text-slate-900">{{ $c->name }}</td>
                            <td class="whitespace-nowrap px-4 py-3.5 text-slate-600">{{ $c->subscriptionPlan?->name ?? '—' }}</td>
                            <td class="whitespace-nowrap px-4 py-3.5">
                                @if($c->status === 'active')
                                    <span class="inline-flex rounded-full bg-emerald-50 px-2 py-0.5 text-[10px] font-bold uppercase tracking-wide text-emerald-700 ring-1 ring-inset ring-emerald-200">Active</span>
                                @elseif($c->status === 'suspended')
                                    <span class="inline-flex rounded-full bg-red-50 px-2 py-0.5 text-[10px] font-bold uppercase tracking-wide text-red-700 ring-1 ring-inset ring-red-200">Suspended</span>
                                @else
                                    <span class="inline-flex rounded-full bg-slate-100 px-2 py-0.5 text-[10px] font-bold uppercase tracking-wide text-slate-600">{{ ucfirst($c->status) }}</span>
                                @endif
                            </td>
                            <td class="whitespace-nowrap px-4 py-3.5 text-slate-600">
                                {{ $c->licence_expire_on?->format('d M Y') ?? '—' }}
                                @if($c->isInExpiryWarningWindow())
                                    <span class="ml-1 inline-flex rounded-full bg-amber-50 px-1.5 py-0.5 text-[9px] font-bold uppercase text-amber-800">Soon</span>
                                @endif
                            </td>
                            <td class="whitespace-nowrap px-4 py-3.5 text-right">
                                <a href="{{ route('superadmin.companies.change-plan', $c) }}" class="text-xs font-semibold text-primary hover:underline">Change plan</a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" class="px-4 py-12 text-center text-slate-500">No companies yet.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </section>
</div>
@endsection
