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

@section('content')
    <div class="space-y-5">
        @if (session('status'))
            <div class="rounded-lg border border-emerald-100 bg-emerald-50 px-4 py-3 text-sm font-bold text-emerald-700">{{ session('status') }}</div>
        @endif

        <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">Tenant profile, owner details, usage, subscription, and administrative actions.</p>
            <div class="flex flex-col gap-2 sm:flex-row">
                <x-ui.button variant="secondary" href="{{ route('super-admin.businesses.index') }}">Back to businesses</x-ui.button>
                <form method="POST" action="{{ route('super-admin.businesses.login-as-owner', $business) }}">
                    @csrf
                    <x-ui.button type="submit">Login as business owner</x-ui.button>
                </form>
                @if ($business->status === 'suspended')
                    <form method="POST" action="{{ route('super-admin.businesses.activate', $business) }}">
                        @csrf
                        @method('PATCH')
                        <x-ui.button type="submit" variant="secondary">Activate business</x-ui.button>
                    </form>
                @else
                    <form method="POST" action="{{ route('super-admin.businesses.suspend', $business) }}" onsubmit="return confirm('Suspend this business?');">
                        @csrf
                        @method('PATCH')
                        <x-ui.button type="submit" variant="danger">Suspend business</x-ui.button>
                    </form>
                @endif
            </div>
        </div>

        <div class="grid gap-5 xl:grid-cols-[minmax(0,1fr)_360px]">
            <div class="space-y-5">
                <x-ui.card title="Business Profile" description="Public tenant identity and current platform status.">
                    <div class="flex flex-col gap-5 sm:flex-row sm:items-center">
                        <span class="flex h-20 w-20 shrink-0 items-center justify-center rounded-2xl bg-booklio-primary text-2xl font-extrabold text-white">{{ Str::of($business->name)->explode(' ')->map(fn ($part) => Str::substr($part, 0, 1))->take(2)->join('') }}</span>
                        <div class="min-w-0 flex-1">
                            <h2 class="text-2xl font-extrabold">{{ $business->name }}</h2>
                            <p class="mt-1 text-sm font-semibold text-booklio-muted">{{ $business->address ?? 'No address added' }}</p>
                            <div class="mt-3 flex flex-wrap gap-2">
                                <x-ui.badge :variant="app(\App\Services\SuperAdmin\PlatformAdminService::class)->businessStatusVariant($business->status)">{{ Str::headline($business->status) }}</x-ui.badge>
                                <x-ui.badge>{{ $business->subscription?->plan?->name ?? 'No plan' }}</x-ui.badge>
                                <x-ui.badge variant="muted">Created {{ $business->created_at?->format('M d, Y') }}</x-ui.badge>
                            </div>
                        </div>
                    </div>
                </x-ui.card>

                <x-ui.card title="Owner Info" description="Primary business owner account.">
                    <div class="grid gap-4 sm:grid-cols-3">
                        @foreach ([['Owner', $business->owner?->name ?? '-'], ['Email', $business->owner?->email ?? $business->email ?? '-'], ['Phone', $business->phone ?? '-']] as $item)
                            <div class="rounded-lg border border-booklio-border bg-slate-50 p-4">
                                <p class="text-xs font-bold text-booklio-muted">{{ $item[0] }}</p>
                                <p class="mt-2 text-sm font-extrabold">{{ $item[1] }}</p>
                            </div>
                        @endforeach
                    </div>
                </x-ui.card>

                <x-ui.card title="Subscription History" description="Plan changes and billing lifecycle events.">
                    <x-ui.table :headers="['Plan', 'Billing cycle', 'Start date', 'End date', 'Payment status']">
                        @forelse ($subscriptionHistory as $subscription)
                            <tr>
                                <td class="font-semibold">{{ $subscription->plan?->name ?? 'No plan' }}</td>
                                <td class="font-semibold">{{ Str::headline($subscription->billing_cycle) }}</td>
                                <td class="font-semibold">{{ $subscription->starts_at?->format('M d, Y') ?? '-' }}</td>
                                <td class="font-semibold">{{ $subscription->ends_at?->format('M d, Y') ?? '-' }}</td>
                                <td><x-ui.badge :variant="$subscription->status_badge_variant">{{ $subscription->status_label }}</x-ui.badge></td>
                            </tr>
                        @empty
                            <tr><td colspan="5" class="text-center text-booklio-muted">No subscription history yet.</td></tr>
                        @endforelse
                    </x-ui.table>
                </x-ui.card>
            </div>

            <div class="space-y-5">
                <x-ui.card title="Current Plan" description="Latest subscription summary.">
                    <div class="space-y-3 text-sm">
                        @foreach ([['Plan', $business->subscription?->plan?->name ?? 'No plan'], ['Billing cycle', $business->subscription ? Str::headline($business->subscription->billing_cycle) : '-'], ['Renewal', $business->subscription?->renews_at?->format('M d, Y') ?? 'Not scheduled'], ['Revenue', $paidRevenue]] as $row)
                            <div class="flex items-center justify-between gap-4 border-b border-booklio-border pb-3">
                                <span class="font-bold text-booklio-muted">{{ $row[0] }}</span>
                                <span class="text-right font-extrabold">{{ $row[1] }}</span>
                            </div>
                        @endforeach
                    </div>
                </x-ui.card>

                <x-ui.card title="Usage Limits" description="Current tenant usage across enabled modules.">
                    <div class="grid gap-3">
                        @foreach ($usage as $item)
                            <div class="rounded-lg border border-booklio-border bg-slate-50 p-4">
                                <p class="text-xs font-bold text-booklio-muted">{{ $item['label'] }}</p>
                                <p class="mt-2 text-xl font-extrabold">{{ $item['value'] }}</p>
                            </div>
                        @endforeach
                    </div>
                </x-ui.card>
            </div>
        </div>
    </div>
@endsection
