@extends('layouts.admin')

@section('title', 'Subscriptions')
@section('page-title', 'Subscriptions')

@section('content')
<div class="space-y-6">

    @if(session('success'))
        <div class="rounded-2xl bg-green-50 border border-green-200 px-4 py-3 text-sm text-green-800">{{ session('success') }}</div>
    @endif
    @if(session('error'))
        <div class="rounded-2xl bg-red-50 border border-red-200 px-4 py-3 text-sm text-red-800">{{ session('error') }}</div>
    @endif

    <section class="grid gap-4 md:grid-cols-2 xl:grid-cols-5">
        <div class="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm"><p class="text-sm font-semibold uppercase tracking-[0.24em] text-slate-500">Total</p><p class="mt-4 text-4xl font-black text-slate-900">{{ number_format($stats['total']) }}</p></div>
        <div class="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm"><p class="text-sm font-semibold uppercase tracking-[0.24em] text-slate-500">Active</p><p class="mt-4 text-4xl font-black text-accent-600">{{ number_format($stats['active']) }}</p></div>
        <div class="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm"><p class="text-sm font-semibold uppercase tracking-[0.24em] text-slate-500">Expired</p><p class="mt-4 text-4xl font-black text-brand-600">{{ number_format($stats['expired']) }}</p></div>
        <div class="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm"><p class="text-sm font-semibold uppercase tracking-[0.24em] text-slate-500">Revenue</p><p class="mt-4 text-4xl font-black text-slate-900">{{ number_format($stats['revenue'], 2) }}</p></div>
        <div class="rounded-3xl border {{ $stats['crypto_pending'] > 0 ? 'border-amber-300 bg-amber-50' : 'border-slate-200 bg-white' }} p-6 shadow-sm">
            <p class="text-sm font-semibold uppercase tracking-[0.24em] {{ $stats['crypto_pending'] > 0 ? 'text-amber-700' : 'text-slate-500' }}">🪙 Crypto Pending</p>
            <p class="mt-4 text-4xl font-black {{ $stats['crypto_pending'] > 0 ? 'text-amber-600' : 'text-slate-900' }}">{{ number_format($stats['crypto_pending']) }}</p>
        </div>
    </section>

    <section class="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm">
        <form method="GET" class="grid gap-4 lg:grid-cols-[1.5fr_1fr_1fr_auto]">
            <input type="text" name="q" value="{{ request('q') }}" placeholder="Search by user" class="w-full rounded-2xl border border-slate-300 px-4 py-3 text-sm outline-none transition focus:border-brand-600 focus:ring-4 focus:ring-brand-600/10">
            <select name="plan" class="w-full rounded-2xl border border-slate-300 px-4 py-3 text-sm outline-none transition focus:border-brand-600 focus:ring-4 focus:ring-brand-600/10">
                <option value="">All plans</option>
                @foreach(['starter' => 'Free Always (Starter)', 'growth' => 'Growth', 'pro' => 'Pro'] as $val => $label)
                    <option value="{{ $val }}" @selected(request('plan') === $val)>{{ $label }}</option>
                @endforeach
            </select>
            <select name="source" class="w-full rounded-2xl border border-slate-300 px-4 py-3 text-sm outline-none transition focus:border-brand-600 focus:ring-4 focus:ring-brand-600/10">
                <option value="">All sources</option>
                @foreach(['voucher', 'admin', 'iap', 'manual', 'stripe', 'paypal', 'crypto'] as $source)
                    <option value="{{ $source }}" @selected(request('source') === $source)>{{ strtoupper($source) }}</option>
                @endforeach
            </select>
            <button type="submit" class="rounded-2xl bg-brand-600 px-5 py-3 text-sm font-semibold text-white transition hover:bg-brand-700">Filter</button>
        </form>
    </section>

    <section class="rounded-3xl border border-slate-200 bg-white shadow-sm">
        <div class="overflow-x-auto">
            <table class="min-w-full text-left text-sm">
                <thead class="border-b border-slate-200 bg-slate-50 text-slate-500">
                    <tr>
                        <th class="px-6 py-4 font-semibold">User</th>
                        <th class="px-6 py-4 font-semibold">Plan</th>
                        <th class="px-6 py-4 font-semibold">Source</th>
                        <th class="px-6 py-4 font-semibold">Amount</th>
                        <th class="px-6 py-4 font-semibold">Start</th>
                        <th class="px-6 py-4 font-semibold">Expiry</th>
                        <th class="px-6 py-4 font-semibold">Status</th>
                        <th class="px-6 py-4 font-semibold">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    @forelse($subscriptions as $subscription)
                        <tr class="table-row-hover {{ $subscription->source === 'crypto' && $subscription->payment_status === 'pending' ? 'bg-amber-50/50' : '' }}">
                            <td class="px-6 py-5">
                                <div class="font-semibold text-slate-900">{{ $subscription->user->name ?? 'Unknown user' }}</div>
                                <div class="text-xs text-slate-500">{{ $subscription->user->email ?? 'No email' }}</div>
                            </td>
                            <td class="px-6 py-5">
                                @php
                                    $planLabel = match($subscription->plan) {
                                        'starter', 'free' => 'Free Always',
                                        'growth'          => 'Growth',
                                        'pro'             => 'Pro',
                                        default           => ucfirst($subscription->plan),
                                    };
                                    $isLifetime = is_null($subscription->expires_at) && $subscription->is_active;
                                    $periodLabel = $isLifetime ? 'Lifetime' : ucfirst($subscription->period ?? '');
                                @endphp
                                <span class="rounded-full bg-slate-100 px-3 py-1 text-xs font-semibold text-slate-700">{{ $planLabel }}</span>
                                @if($periodLabel)
                                    <span class="ml-1 rounded-full bg-slate-50 border border-slate-200 px-2 py-0.5 text-xs text-slate-500">{{ $periodLabel }}</span>
                                @endif
                            </td>
                            <td class="px-6 py-5 text-slate-600">{{ strtoupper($subscription->source) }}</td>
                            <td class="px-6 py-5 text-slate-600">
                                {{ strtoupper($subscription->currency ?: 'USD') }} {{ number_format($subscription->amount_paid ?? 0, 2) }}
                                @if($subscription->tx_hash)
                                    <div class="mt-1 text-xs text-slate-400 font-mono truncate max-w-[140px]" title="{{ $subscription->tx_hash }}">{{ $subscription->tx_hash }}</div>
                                @endif
                                @if($subscription->proof_image)
                                    <a href="{{ asset('storage/' . $subscription->proof_image) }}" target="_blank" class="mt-1 inline-block">
                                        <img src="{{ asset('storage/' . $subscription->proof_image) }}"
                                             alt="Payment proof"
                                             class="h-10 w-auto rounded border border-amber-300 object-cover hover:opacity-80 transition"
                                             title="Click to view full proof image">
                                    </a>
                                @endif
                            </td>
                            <td class="px-6 py-5 text-slate-600">{{ $subscription->starts_at?->format('d M Y') ?? 'N/A' }}</td>
                            <td class="px-6 py-5 text-slate-600">{{ $subscription->expires_at?->format('d M Y') ?? 'Lifetime' }}</td>
                            <td class="px-6 py-5">
                                @if($subscription->source === 'crypto' && $subscription->payment_status === 'pending')
                                    <span class="rounded-full bg-amber-100 px-3 py-1 text-xs font-semibold text-amber-800">🕐 Crypto Pending</span>
                                @elseif($subscription->source === 'crypto' && $subscription->payment_status === 'approved')
                                    <span class="rounded-full bg-green-100 px-3 py-1 text-xs font-semibold text-green-800">Crypto Approved</span>
                                @elseif($subscription->source === 'crypto' && $subscription->payment_status === 'rejected')
                                    <span class="rounded-full bg-red-100 px-3 py-1 text-xs font-semibold text-red-700">Crypto Rejected</span>
                                @else
                                    <span class="rounded-full px-3 py-1 text-xs font-semibold {{ $subscription->isExpired() || !$subscription->is_active ? 'badge-expired' : 'badge-active' }}">{{ $subscription->isExpired() || !$subscription->is_active ? 'Expired' : 'Active' }}</span>
                                @endif
                            </td>
                            <td class="px-6 py-5">
                                @if($subscription->source === 'crypto' && $subscription->payment_status === 'pending')
                                    <div class="flex gap-2">
                                        <form method="POST" action="{{ route('admin.subscriptions.approve-crypto', $subscription) }}" onsubmit="return confirm('Approve this crypto payment and activate the plan?')">
                                            @csrf
                                            <button type="submit" class="rounded-xl bg-green-600 px-3 py-1.5 text-xs font-semibold text-white hover:bg-green-700 transition">Approve</button>
                                        </form>
                                        <form method="POST" action="{{ route('admin.subscriptions.reject-crypto', $subscription) }}" onsubmit="return confirm('Reject this crypto payment?')">
                                            @csrf
                                            <button type="submit" class="rounded-xl bg-red-500 px-3 py-1.5 text-xs font-semibold text-white hover:bg-red-600 transition">Reject</button>
                                        </form>
                                    </div>
                                @elseif($subscription->is_active && !$subscription->isExpired())
                                    <form method="POST" action="{{ route('admin.subscriptions.cancel', $subscription) }}" onsubmit="return confirm('Cancel this subscription? The user will lose access.')">
                                        @csrf
                                        <button type="submit" class="rounded-xl bg-slate-500 px-3 py-1.5 text-xs font-semibold text-white hover:bg-slate-600 transition">Cancel</button>
                                    </form>
                                @else
                                    <span class="text-slate-400 text-xs">—</span>
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="8" class="px-6 py-16 text-center text-sm text-slate-500">No subscriptions found.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="border-t border-slate-200 px-6 py-4">{{ $subscriptions->links() }}</div>
    </section>
</div>
@endsection
