<?php $this->assign('title', 'Bookings'); ?>

<main class="min-h-screen pt-16 md:pt-20 pb-20 bg-neutral-50">
    <div class="max-w-7xl mx-auto px-6 lg:px-8">

        <!-- Header with animation -->
        <div class="text-center md:text-left mb-12 animate-fade-in">
            <h1 class="text-4xl md:text-5xl font-serif font-semibold text-neutral-800 tracking-tight">
                Bookings Management
            </h1>
            <p class="mt-4 text-lg text-neutral-600">
                Review and manage all workshop bookings: approve, reject, or view details.
            </p>
        </div>

        <!-- Table card -->
        <div class="bg-white/80 backdrop-blur-sm rounded-2xl shadow-xl border border-neutral-200/60 overflow-hidden transition-all duration-300 hover:shadow-2xl">

            <?php if (empty($bookings)): ?>

                <div class="py-16 text-center">
                    <p class="text-xl text-neutral-500 font-medium">
                        No bookings found at the moment.
                    </p>
                    <p class="mt-2 text-neutral-600">
                        New reservations will appear here once users book a lesson.
                    </p>
                </div>

            <?php else: ?>

                <div class="overflow-x-auto">
                    <table class="w-full text-left border-collapse">

                        <thead class="bg-neutral-100/80 backdrop-blur-sm">
                            <tr class="border-b border-neutral-200">
                                <th class="px-6 py-5 text-lg font-semibold text-neutral-700">User</th>
                                <th class="px-6 py-5 text-lg font-semibold text-neutral-700">Lesson</th>
                                <th class="px-6 py-5 text-lg font-semibold text-neutral-700">Date</th>
                                <th class="px-6 py-5 text-lg font-semibold text-neutral-700">Status</th>
                                <th class="px-6 py-5 text-lg font-semibold text-neutral-700">Payment</th>
                                <th class="px-6 py-5 text-lg font-semibold text-neutral-700 text-right pr-8">Actions</th>
                            </tr>
                        </thead>

                        <tbody class="divide-y divide-neutral-100">
                            <?php foreach ($bookings as $b): ?>
                                <tr class="hover:bg-primary-50/30 transition-colors duration-200">
                                    <td class="px-6 py-5 text-neutral-800 font-medium">
                                        <?= h($b->user->email) ?>
                                    </td>
                                    <td class="px-6 py-5 text-neutral-700">
                                        <?= h($b->lesson->lesson_name) ?>
                                    </td>
                                    <td class="px-6 py-5 text-neutral-600">
                                        <?= h($b->booking_date->format('d M Y • H:i')) ?>
                                    </td>
                                    <td class="px-6 py-5">
                                        <?php
                                            $statusClass = match ($b->status) {
                                                'pending'   => 'bg-amber-100 text-amber-800 border-amber-300',
                                                'approved'  => 'bg-primary-100 text-primary-800 border-primary-300',
                                                'rejected'  => 'bg-red-100 text-red-800 border-red-300',
                                                'cancelled' => 'bg-gray-100 text-gray-700 border-gray-300',
                                                default     => 'bg-gray-100 text-gray-700 border-gray-300',
                                            };
                                        ?>
                                        <span class="inline-flex px-4 py-1.5 rounded-full text-sm font-medium border <?= $statusClass ?>">
                                            <?= ucfirst(h($b->status)) ?>
                                        </span>
                                    </td>
                                    <td class="px-6 py-5 text-neutral-600">
                                        <?php
                                            $payment = $b->payment->payment_status ?? 'N/A';
                                            $paymentClass = match ($payment) {
                                                'paid'       => 'text-primary-600 font-medium',
                                                'pending'    => 'text-amber-600',
                                                'failed'     => 'text-red-600',
                                                default      => 'text-neutral-500',
                                            };
                                        ?>
                                        <span class="<?= $paymentClass ?>">
                                            <?= ucfirst(h($payment)) ?>
                                        </span>
                                    </td>
                                    <td class="px-6 py-5 text-right pr-8">
                                        <div class="flex items-center justify-end gap-3 flex-wrap">
                                            <?php if (in_array($b->status, ['pending'])): ?>
                                                <?= $this->Html->link(
                                                    'Approve',
                                                    ['action' => 'updateBooking', $b->id, 'approved'],
                                                    ['class' => 'inline-flex items-center px-5 py-2.5 text-sm font-semibold rounded-lg bg-primary-500 text-white hover:bg-primary-600 transition-all duration-300 shadow-sm hover:shadow']
                                                ) ?>

                                                <?= $this->Html->link(
                                                    'Reject',
                                                    ['action' => 'updateBooking', $b->id, 'rejected'],
                                                    ['class' => 'inline-flex items-center px-5 py-2.5 text-sm font-semibold rounded-lg bg-white border-2 border-red-400 text-red-600 hover:bg-red-50 transition-all duration-300']
                                                ) ?>
                                            <?php else: ?>
                                                <span class="text-sm text-neutral-400 italic">N/A</span>
                                            <?php endif; ?>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>

                    </table>
                </div>

            <?php endif; ?>

        </div>

    </div>
</main>