@extends('layouts.app')

@section('title', 'Subscriptions')
@section('heading', 'Subscriptions')

@section('content')
<div class="w-full">
    <x-superadmin.page-header
        eyebrow="Billing"
        title="Subscriptions"
        subtitle="Overview of all tenant subscriptions by plan, status and licence expiry."
    />

    <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">Active companies</p>
            <p class="mt-1 text-xl font-bold text-slate-900">{{ $totalActive ?? 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-sky-500" aria-hidden="true"></span>
            <p class="text-[11px] font-semibold uppercase tracking-wide text-slate-400">On trial</p>
            <p class="mt-1 text-xl font-bold text-slate-900">{{ $totalTrial ?? 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-violet-500" aria-hidden="true"></span>
            <p class="text-[11px] font-semibold uppercase tracking-wide text-slate-400">Revenue (INR)</p>
            <p class="mt-1 text-xl font-bold text-slate-900">{{ config('app.currency_symbol', '₹') }}{{ number_format($totalRevenue ?? 0, 0) }}</p>
        </div>
    </div>

    <div class="mb-4 rounded-2xl border border-slate-200 bg-white p-4 shadow-sm">
        <form method="get" class="flex flex-col gap-3 sm:flex-row sm:flex-wrap sm:items-center">
            <input type="text" name="search" value="{{ request('search') }}" placeholder="Search company, email…"
                   class="w-full flex-1 rounded-xl border border-slate-200 px-3 py-2.5 text-sm focus:border-primary focus:outline-none focus:ring-2 focus:ring-primary/20 sm:min-w-[200px]">
            <select name="plan_id" class="rounded-xl border border-slate-200 px-3 py-2.5 text-sm focus:border-primary focus:outline-none js-select2" style="width: 160px">
                <option value="">All plans</option>
                @foreach($plans as $plan)
                    <option value="{{ $plan->id }}" {{ (string) request('plan_id') === (string) $plan->id ? 'selected' : '' }}>{{ $plan->name }}</option>
                @endforeach
            </select>
            <select name="status" class="rounded-xl border border-slate-200 px-3 py-2.5 text-sm focus:border-primary focus:outline-none js-select2" style="width: 150px">
                <option value="">All statuses</option>
                <option value="active" {{ request('status') === 'active' ? 'selected' : '' }}>Active</option>
                <option value="inactive" {{ request('status') === 'inactive' ? 'selected' : '' }}>Inactive</option>
                <option value="suspended" {{ request('status') === 'suspended' ? 'selected' : '' }}>Suspended</option>
            </select>
            <button type="submit" class="inline-flex rounded-xl bg-primary px-4 py-2.5 text-sm font-semibold text-white shadow-sm hover:opacity-95">Filter</button>
        </form>
    </div>

    <div class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm">
        <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 expiry</th>
                        <th class="px-4 py-3 text-[11px] font-semibold uppercase tracking-wide text-slate-400">Last payment</th>
                        <th class="px-4 py-3 text-[11px] font-semibold uppercase tracking-wide text-slate-400">Amount</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    @forelse($companies as $company)
                        @php
                            $lastPayment = $company->subscriptionPayments->first();
                            $daysLeft = $company->daysUntilLicenceExpiry();
                            $expiringSoon = $company->isInExpiryWarningWindow();
                        @endphp
                        <tr class="hover:bg-slate-50/80">
                            <td class="whitespace-nowrap px-4 py-3.5 text-slate-900">
                                <span class="font-semibold">{{ $company->name }}</span>
                                @if($company->short_name)
                                    <span class="ml-1 text-slate-400">({{ $company->short_name }})</span>
                                @endif
                            </td>
                            <td class="whitespace-nowrap px-4 py-3.5 text-slate-600">{{ $company->subscriptionPlan?->name ?? '—' }}</td>
                            <td class="whitespace-nowrap px-4 py-3.5">
                                @if($company->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($company->status === 'suspended')
                                    <span class="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">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-500">{{ ucfirst($company->status ?? '—') }}</span>
                                @endif
                            </td>
                            <td class="whitespace-nowrap px-4 py-3.5 text-slate-600">
                                {{ $company->licence_expire_on ? $company->licence_expire_on->format('d M Y') : '—' }}
                                @if($expiringSoon && $daysLeft !== null)
                                    <span class="ml-1 inline-flex rounded-full bg-amber-50 px-1.5 py-0.5 text-[10px] font-bold uppercase text-amber-800 ring-1 ring-inset ring-amber-200">
                                        {{ $daysLeft < 0 ? 'Expired' : ($daysLeft === 0 ? 'Today' : $daysLeft.'d') }}
                                    </span>
                                @endif
                            </td>
                            <td class="whitespace-nowrap px-4 py-3.5 text-slate-600">
                                {{ ($lastPayment && $lastPayment->paid_at) ? $lastPayment->paid_at->format('d M Y') : '—' }}
                            </td>
                            <td class="whitespace-nowrap px-4 py-3.5 font-semibold text-slate-900">
                                @if($lastPayment)
                                    {{ config('app.currency_symbol', '₹') }}{{ number_format($lastPayment->amount, 2) }}
                                @else
                                    —
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="px-4 py-12 text-center text-slate-500">No subscriptions found for the selected filters.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($companies->hasPages())
            <div class="border-t border-slate-100 px-4 py-3">{{ $companies->links() }}</div>
        @endif
    </div>
</div>
@endsection
