<x-app-layout>
    <x-slot name="header">
        <div class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
            <div>
                <p class="text-sm font-semibold uppercase tracking-wide text-blue-600 dark:text-blue-400">Growth</p>
                <h2 class="mt-1 text-2xl font-bold text-slate-950 dark:text-white">Coupons</h2>
            </div>
            <a href="{{ route('admin.coupons.create') }}" class="rounded-lg bg-blue-600 px-4 py-2 text-sm font-semibold text-white transition hover:-translate-y-0.5 hover:bg-blue-700 dark:bg-blue-500 dark:hover:bg-blue-400">Create Coupon</a>
        </div>
    </x-slot>

    <div class="overflow-hidden rounded-lg border border-blue-100 bg-white shadow-sm dark:border-blue-900/50 dark:bg-slate-900">
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-blue-100 dark:divide-blue-900/50">
                <thead class="bg-blue-50 dark:bg-blue-950/40">
                    <tr>
                        <th class="px-4 py-3 text-left text-xs font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Code</th>
                        <th class="px-4 py-3 text-left text-xs font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Discount</th>
                        <th class="px-4 py-3 text-left text-xs font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Usage</th>
                        <th class="px-4 py-3 text-left text-xs font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Expiry</th>
                        <th class="px-4 py-3 text-left text-xs font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Status</th>
                        <th class="px-4 py-3 text-right text-xs font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-blue-50 bg-white dark:divide-blue-900/40 dark:bg-slate-900">
                    @forelse ($coupons as $coupon)
                        <tr>
                            <td class="px-4 py-4 text-sm font-black text-slate-950 dark:text-white">{{ $coupon->code }}</td>
                            <td class="px-4 py-4 text-sm text-slate-600 dark:text-slate-300">
                                {{ $coupon->discount_type === 'percentage' ? $coupon->discount_value.'%' : number_format((float) $coupon->discount_value, 2) }}
                            </td>
                            <td class="px-4 py-4 text-sm text-slate-600 dark:text-slate-300">{{ $coupon->used_count }} / {{ $coupon->usage_limit ?? 'Unlimited' }}</td>
                            <td class="px-4 py-4 text-sm text-slate-600 dark:text-slate-300">{{ $coupon->expiry_date?->format('M d, Y') ?? 'No expiry' }}</td>
                            <td class="px-4 py-4 text-sm font-bold capitalize {{ $coupon->isActive() ? 'text-emerald-600 dark:text-emerald-400' : 'text-red-600 dark:text-red-400' }}">{{ $coupon->status }}</td>
                            <td class="px-4 py-4 text-right text-sm">
                                <div class="flex justify-end gap-3">
                                    <a href="{{ route('admin.coupons.edit', $coupon) }}" class="font-semibold text-blue-700 hover:text-blue-900 dark:text-blue-300">Edit</a>
                                    <form method="POST" action="{{ route('admin.coupons.toggle-status', $coupon) }}">
                                        @csrf
                                        @method('PATCH')
                                        <button class="font-semibold text-slate-600 hover:text-slate-900 dark:text-slate-300 dark:hover:text-white">Toggle</button>
                                    </form>
                                    <form method="POST" action="{{ route('admin.coupons.destroy', $coupon) }}" onsubmit="return confirm('Delete this coupon?');">
                                        @csrf
                                        @method('DELETE')
                                        <button class="font-semibold text-red-600 hover:text-red-800 dark:text-red-300">Delete</button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="px-4 py-10 text-center text-sm text-slate-500 dark:text-slate-400">No coupons created yet.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="border-t border-blue-100 px-4 py-3 dark:border-blue-900/50">{{ $coupons->links() }}</div>
    </div>
</x-app-layout>
