@extends('layouts.super-admin', ['title' => 'Subscriptions - Booklio Super Admin', 'pageTitle' => 'Subscriptions', 'active' => 'subscriptions'])

@section('content')
    <div class="space-y-5">
        <div class="flex flex-col gap-3 lg:flex-row lg:items-end lg:justify-between">
            <p class="text-sm font-semibold text-booklio-muted">Track billing cycle, trial, payment, and renewal status for every business.</p>
            <form method="GET" class="grid gap-3 sm:grid-cols-4">
                <input name="q" value="{{ $filters['q'] ?? '' }}" placeholder="Search business..." class="h-11 rounded-lg border border-booklio-border bg-white px-3 text-sm font-bold shadow-soft">
                <select name="plan_id" class="h-11 rounded-lg border border-booklio-border bg-white px-3 text-sm font-bold shadow-soft">
                    <option value="all">All plans</option>
                    @foreach ($plans as $plan)
                        <option value="{{ $plan->id }}" @selected(($filters['plan_id'] ?? 'all') == $plan->id)>{{ $plan->name }}</option>
                    @endforeach
                </select>
                <select name="status" class="h-11 rounded-lg border border-booklio-border bg-white px-3 text-sm font-bold shadow-soft">
                    <option value="all">All statuses</option>
                    @foreach (\App\Enums\SubscriptionStatus::options() as $value => $label)
                        <option value="{{ $value }}" @selected(($filters['status'] ?? 'all') === $value)>{{ $label }}</option>
                    @endforeach
                </select>
                <x-ui.button type="submit">Filter</x-ui.button>
            </form>
        </div>

        <x-ui.card title="Subscriptions List" description="Business, plan, billing cycle, dates, trial, payment, and renewal state.">
            <x-ui.table :headers="['Business', 'Plan', 'Billing cycle', 'Start date', 'End date', 'Trial status', 'Payment status', 'Renewal status', 'Actions']">
                @forelse ($subscriptions as $subscription)
                    @php($lastPayment = $subscription->payments->first())
                    <tr class="hover:bg-slate-50">
                        <td class="font-extrabold">{{ $subscription->business?->name }}</td>
                        <td class="font-bold">{{ $subscription->plan?->name ?? 'No plan' }}</td>
                        <td class="font-semibold">{{ Str::headline($subscription->billing_cycle) }}</td>
                        <td>{{ $subscription->starts_at?->format('M d, Y') ?? '-' }}</td>
                        <td>{{ $subscription->ends_at?->format('M d, Y') ?? '-' }}</td>
                        <td><x-ui.badge :variant="$subscription->isTrial() ? 'warning' : 'muted'">{{ $subscription->isTrial() ? 'Active' : 'Ended' }}</x-ui.badge></td>
                        <td><x-ui.badge :variant="$lastPayment?->status?->value === 'paid' ? 'success' : 'warning'">{{ $lastPayment?->status?->label() ?? 'Pending' }}</x-ui.badge></td>
                        <td class="font-semibold">{{ $subscription->renews_at ? 'Renews '.$subscription->renews_at->format('M d, Y') : $subscription->status_label }}</td>
                        <td><x-ui.button size="sm" variant="secondary" href="{{ route('super-admin.subscriptions.show', $subscription) }}">View</x-ui.button></td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="9">
                            <div class="rounded-lg border border-dashed border-booklio-border bg-slate-50 p-8 text-center">
                                <p class="text-base font-extrabold text-booklio-dark">No subscriptions found</p>
                                <p class="mt-2 text-sm text-booklio-muted">Try another search or filter.</p>
                            </div>
                        </td>
                    </tr>
                @endforelse
            </x-ui.table>

            <x-ui.pagination :paginator="$subscriptions" class="mt-4" />
        </x-ui.card>
    </div>
@endsection
