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

<div class="py-10 px-6 lg:px-8 max-w-7xl mx-auto">
    <div class="mb-10">
        <h1 class="text-4xl md:text-5xl font-serif font-bold text-neutral-900 mb-2">
            Published teacher sessions
        </h1>
        <p class="text-lg text-neutral-600">
            These dates power the home-page availability assistant and are the source of truth for AI answers.
        </p>
    </div>

    <div class="bg-white/80 backdrop-blur-sm rounded-2xl shadow-2xl border border-neutral-200/50 overflow-hidden mb-12">
        <div class="overflow-x-auto">
            <table class="w-full min-w-max">
                <thead class="bg-neutral-50/80 border-b border-neutral-200">
                    <tr>
                        <th class="px-6 py-4 text-left text-sm font-semibold text-neutral-700 uppercase tracking-wider">Date</th>
                        <th class="px-6 py-4 text-left text-sm font-semibold text-neutral-700 uppercase tracking-wider">Time</th>
                        <th class="px-6 py-4 text-left text-sm font-semibold text-neutral-700 uppercase tracking-wider">Teacher</th>
                        <th class="px-6 py-4 text-left text-sm font-semibold text-neutral-700 uppercase tracking-wider">Workshop</th>
                        <th class="px-6 py-4 text-left text-sm font-semibold text-neutral-700 uppercase tracking-wider">Notes</th>
                        <th class="px-6 py-4 text-center text-sm font-semibold text-neutral-700 uppercase tracking-wider">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-neutral-100">
                    <?php if ($slots->isEmpty()): ?>
                        <tr>
                            <td colspan="6" class="px-6 py-12 text-center text-neutral-500">No sessions yet.</td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($slots as $s): ?>
                            <tr class="hover:bg-primary-50/30">
                                <td class="px-6 py-4 whitespace-nowrap text-neutral-900"><?= h($s->session_date?->format('Y-m-d') ?? '') ?></td>
                                <td class="px-6 py-4 text-neutral-700"><?= h($s->time_label ?: 'N/A') ?></td>
                                <td class="px-6 py-4 text-neutral-900 font-medium"><?= h($s->teacher->name ?? '') ?></td>
                                <td class="px-6 py-4 text-neutral-700"><?= h($s->lesson->lesson_name ?? 'N/A') ?></td>
                                <td class="px-6 py-4 text-neutral-600 max-w-xs"><?= h($s->notes ?: 'N/A') ?></td>
                                <td class="px-6 py-4 text-center">
                                    <?= $this->Form->postLink(
                                        'Remove',
                                        ['action' => 'deleteTeacherAvailabilitySlot', $s->id],
                                        [
                                            'class' => 'text-sm font-semibold text-red-600 hover:text-red-800',
                                            'confirm' => 'Remove this published session?',
                                        ]
                                    ) ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div class="max-w-xl">
        <h2 class="font-serif text-2xl font-bold text-neutral-900 mb-4">Add session</h2>
        <?= $this->Form->create($slot, ['class' => 'space-y-5']) ?>
        <?= $this->Form->control('teacher_id', [
            'label' => 'Teacher',
            'type' => 'select',
            'options' => collection($teachers)->combine('id', 'name')->toArray(),
            'empty' => 'Choose…',
            'required' => true,
            'class' => 'w-full rounded-xl border border-neutral-200 px-4 py-3',
        ]) ?>
        <?= $this->Form->control('lesson_id', [
            'label' => 'Workshop (optional)',
            'type' => 'select',
            'options' => collection($lessons)->combine('id', 'lesson_name')->toArray(),
            'empty' => 'N/A',
            'class' => 'w-full rounded-xl border border-neutral-200 px-4 py-3',
        ]) ?>
        <?= $this->Form->control('session_date', [
            'label' => 'Session date',
            'type' => 'date',
            'required' => true,
            'class' => 'w-full rounded-xl border border-neutral-200 px-4 py-3',
        ]) ?>
        <?= $this->Form->control('time_label', [
            'label' => 'Time (e.g. 10:00 to 13:00)',
            'class' => 'w-full rounded-xl border border-neutral-200 px-4 py-3',
        ]) ?>
        <?= $this->Form->control('notes', [
            'label' => 'Notes',
            'type' => 'textarea',
            'rows' => 2,
            'class' => 'w-full rounded-xl border border-neutral-200 px-4 py-3',
        ]) ?>
        <?= $this->Form->button('Add session', [
            'class' => 'inline-flex items-center rounded-full bg-primary-500 px-8 py-3 text-base font-semibold text-white hover:bg-primary-600',
        ]) ?>
        <?= $this->Form->end() ?>
    </div>
</div>
