@extends('layouts.admin_layout')

@section('content')

<main class="h-full pb-16 overflow-y-auto">
    <div class="container grid px-6 mx-auto">
        <h2 class="my-6 text-2xl font-semibold text-gray-700 dark:text-gray-200">
            Redemptions for <span class="text-blue-500">{{ $coupon->code }}</span>
        </h2>

        <a href="{{ route('admin.coupons') }}"
            class="inline-flex items-center px-4 py-2 mb-6 text-sm font-medium text-gray-700 transition-colors duration-150 bg-white border border-gray-300 hover:bg-gray-100 dark:bg-gray-800 dark:text-gray-300"
            style="width: fit-content;">
            ← Back to Coupons
        </a>

        <!-- Coupon Info -->
        <div class="p-4 mb-6 bg-white rounded-lg shadow-md dark:bg-gray-800">
            <div class="grid grid-cols-2 gap-4 md:grid-cols-4">
                <div>
                    <span class="text-sm text-gray-600 dark:text-gray-400">Type:</span>
                    <p class="font-semibold">{{ ucfirst($coupon->type) }}</p>
                </div>
                <div>
                    <span class="text-sm text-gray-600 dark:text-gray-400">Value:</span>
                    @if($coupon->type === 'balance')
                        <p class="font-semibold">USDT {{ number_format($coupon->amount, 2) }}</p>
                    @else
                        <p class="font-semibold">{{ $coupon->product->name ?? 'N/A' }} x{{ $coupon->quantity }}</p>
                    @endif
                </div>
                <div>
                    <span class="text-sm text-gray-600 dark:text-gray-400">Uses:</span>
                    <p class="font-semibold">{{ $coupon->used_count }} / {{ $coupon->max_uses }}</p>
                </div>
                <div>
                    <span class="text-sm text-gray-600 dark:text-gray-400">Status:</span>
                    @if($coupon->canBeUsed())
                        <p class="font-semibold text-green-600">Active</p>
                    @else
                        <p class="font-semibold text-red-600">Inactive</p>
                    @endif
                </div>
            </div>
        </div>

        <!-- Redemptions Table -->
        <div class="w-full overflow-hidden rounded-lg shadow-xs">
            <div class="w-full overflow-x-auto">
                <table class="w-full whitespace-no-wrap">
                    <thead>
                        <tr class="text-xs font-semibold tracking-wide text-left text-gray-500 uppercase border-b dark:border-gray-700 bg-gray-50 dark:text-gray-400 dark:bg-gray-800">
                            <th class="px-4 py-3">User</th>
                            <th class="px-4 py-3">Redeemed At</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
                        @forelse($redemptions as $redemption)
                            <tr class="text-gray-700 dark:text-gray-400">
                                <td class="px-4 py-3">
                                    <div class="flex items-center text-sm">
                                        <div>
                                            <a href="{{ route('admin.user_info', $redemption->user->id) }}" class="hover:text-blue-600">
                                                <p class="font-semibold">{{ $redemption->user->first_name }} {{ $redemption->user->last_name }}</p>
                                                <p class="text-xs text-gray-600 dark:text-gray-400">
                                                    {{ $redemption->user->email }} - [{{ $redemption->user->username }}]
                                                </p>
                                            </a>
                                        </div>
                                    </div>
                                </td>
                                <td class="px-4 py-3 text-sm">
                                    {{ $redemption->redeemed_at->format('M j, Y H:i') }}
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="2" class="px-4 py-8 text-center text-gray-500 dark:text-gray-400">
                                    <p class="text-lg font-semibold">No redemptions yet</p>
                                    <p class="mt-2 text-sm">This coupon has not been used</p>
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <!-- Pagination -->
            @if($redemptions->hasPages())
                <div class="px-4 py-3 border-t dark:border-gray-700 bg-gray-50 dark:bg-gray-900">
                    {{ $redemptions->links() }}
                </div>
            @endif
        </div>
    </div>
</main>

@endsection