<?php
declare(strict_types=1);

/**
 * @var \App\View\AppView $this
 * @var \App\Model\Entity\Teacher $teacher
 * @var \Cake\Collection\CollectionInterface<\App\Model\Entity\AvailabilitySlot> $slots
 */
$slotList = $slots->toList();

// Group slots by day for nicer display
$byDay = [];
foreach ($slotList as $s) {
    $byDay[$s->starts_at->format('Y-m-d')][] = $s;
}
?>
<?php $this->assign('title', 'Edit ' . h($teacher->full_name) . ' · CandleCraft Admin'); ?>

<section class="cc-admin-page">
    <a class="cc-msg-back" href="<?= $this->Url->build(['_name' => 'admin_teachers']) ?>">← Back to teachers</a>

    <div class="cc-admin-page__head">
        <div>
            <div class="cc-eyebrow" style="margin-bottom: 12px;">Edit teacher</div>
            <h1><?= h($teacher->full_name) ?></h1>
        </div>
        <div>
            <a class="cc-btn cc-btn--outline cc-btn--sm" href="<?= $this->Url->build(['_name' => 'teacher_profile', 'slug' => $teacher->slug]) ?>" target="_blank">View public profile →</a>
        </div>
    </div>

    <div style="display: grid; grid-template-columns: 1.2fr 1fr; gap: 24px; align-items: flex-start;">
        <!-- Profile editor -->
        <div class="cc-admin-form-card">
            <h2 style="font-size: 18px; margin-bottom: 18px;">Profile</h2>
            <?= $this->Form->create($teacher) ?>
                <div class="cc-form-row">
                    <div>
                        <label class="cc-label" for="full_name">Full name</label>
                        <?= $this->Form->control('full_name', ['label' => false, 'class' => 'cc-input', 'id' => 'full_name']) ?>
                    </div>
                    <div>
                        <label class="cc-label" for="slug">Slug</label>
                        <?= $this->Form->control('slug', ['label' => false, 'class' => 'cc-input', 'id' => 'slug']) ?>
                    </div>
                </div>
                <div class="cc-form-row cc-form-row--single">
                    <div>
                        <label class="cc-label" for="role">Role / title</label>
                        <?= $this->Form->control('role', ['label' => false, 'class' => 'cc-input', 'id' => 'role']) ?>
                    </div>
                </div>
                <div class="cc-form-row cc-form-row--single">
                    <div>
                        <label class="cc-label" for="bio">Bio</label>
                        <?= $this->Form->control('bio', ['label' => false, 'class' => 'cc-textarea', 'id' => 'bio', 'type' => 'textarea', 'rows' => 4]) ?>
                    </div>
                </div>
                <div class="cc-form-row">
                    <div>
                        <label class="cc-label" for="specialties">Specialties</label>
                        <?= $this->Form->control('specialties', ['label' => false, 'class' => 'cc-input', 'id' => 'specialties']) ?>
                        <div class="cc-field-helper">Comma-separated.</div>
                    </div>
                    <div>
                        <label class="cc-label" for="hourly_rate_cents">Rate (cents)</label>
                        <?= $this->Form->control('hourly_rate_cents', ['label' => false, 'class' => 'cc-input', 'id' => 'hourly_rate_cents', 'type' => 'number']) ?>
                    </div>
                </div>
                <div class="cc-form-row">
                    <div>
                        <label class="cc-label" for="photo_path">Photo path</label>
                        <?= $this->Form->control('photo_path', ['label' => false, 'class' => 'cc-input', 'id' => 'photo_path', 'placeholder' => 'studio/teachers/jane.jpg']) ?>
                    </div>
                    <div>
                        <label class="cc-label" for="sort_order">Sort order</label>
                        <?= $this->Form->control('sort_order', ['label' => false, 'class' => 'cc-input', 'id' => 'sort_order', 'type' => 'number']) ?>
                    </div>
                </div>
                <label class="cc-consent-box">
                    <input type="hidden" name="is_active" value="0">
                    <input type="checkbox" name="is_active" value="1" <?= $teacher->is_active ? 'checked' : '' ?>>
                    <span>
                        <span class="cc-consent-box__label">Active</span>
                        <span class="cc-consent-box__helper">Visible on the public site and bookable.</span>
                    </span>
                </label>
                <div style="display: flex; gap: 10px;">
                    <button class="cc-btn cc-btn--primary" type="submit">Save changes</button>
                    <a class="cc-btn cc-btn--ghost" href="<?= $this->Url->build(['_name' => 'admin_teachers']) ?>">Cancel</a>
                </div>
            <?= $this->Form->end() ?>
        </div>

        <!-- Availability editor -->
        <div>
            <div class="cc-admin-form-card">
                <h2 style="font-size: 18px; margin-bottom: 6px;">Availability</h2>
                <p class="cc-muted" style="font-size: 13px; margin-bottom: 18px;"><?= count($slotList) ?> upcoming slot<?= count($slotList) === 1 ? '' : 's' ?>. Booked slots can't be deleted.</p>

                <!-- Add a single slot -->
                <?= $this->Form->create(null, ['url' => ['_name' => 'admin_teacher_add_slot', 'id' => $teacher->id]]) ?>
                    <div style="display: grid; grid-template-columns: 1.3fr 1fr; gap: 8px;">
                        <input class="cc-input" type="date" name="date" required min="<?= date('Y-m-d') ?>">
                        <input class="cc-input" type="time" name="time" required value="10:00">
                    </div>
                    <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 8px;">
                        <select class="cc-input" name="class_type">
                            <option value="pottery">Pottery</option>
                            <option value="knitting">Knitting</option>
                        </select>
                        <select class="cc-input" name="duration">
                            <option value="60">60 min</option>
                            <option value="90" selected>90 min</option>
                            <option value="120">120 min</option>
                        </select>
                    </div>
                    <button class="cc-btn cc-btn--dark cc-btn--block cc-btn--sm" style="margin-top: 10px;" type="submit">+ Add slot</button>
                <?= $this->Form->end() ?>

                <!-- Generate batch -->
                <div style="margin-top: 18px; padding-top: 18px; border-top: 1px dashed var(--cc-border-soft);">
                    <?= $this->Form->create(null, ['url' => ['_name' => 'admin_teacher_generate', 'id' => $teacher->id]]) ?>
                        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 8px;">
                            <select class="cc-input" name="days">
                                <option value="7">Next 7 days</option>
                                <option value="14" selected>Next 14 days</option>
                                <option value="30">Next 30 days</option>
                            </select>
                            <select class="cc-input" name="class_type">
                                <option value="pottery">Pottery</option>
                                <option value="knitting">Knitting</option>
                            </select>
                        </div>
                        <button class="cc-btn cc-btn--outline cc-btn--block cc-btn--sm" style="margin-top: 10px;" type="submit">↻ Generate slots</button>
                        <div class="cc-field-helper" style="margin-top: 6px;">3 slots per day at 10:00, 13:00, 16:00. Skips Sundays. Won't duplicate existing slots.</div>
                    <?= $this->Form->end() ?>
                </div>
            </div>

            <!-- Slot list -->
            <div class="cc-admin-form-card" style="margin-top: 16px;">
                <h2 style="font-size: 18px; margin-bottom: 12px;">Upcoming slots</h2>
                <?php if (empty($slotList)): ?>
                    <p class="cc-muted" style="font-size: 14px;">No upcoming slots. Add one above, or use Generate.</p>
                <?php else: ?>
                    <div class="cc-slot-list">
                        <?php foreach ($byDay as $day => $daySlots): ?>
                            <div class="cc-day-head" style="margin-top: 12px;"><?= h(date('l, j F', strtotime($day))) ?></div>
                            <?php foreach ($daySlots as $s): ?>
                                <div class="cc-slot-list__row<?= $s->is_booked ? ' is-booked' : '' ?>">
                                    <div><strong><?= h($s->starts_at->format('g:i A')) ?></strong> – <?= h($s->ends_at->format('g:i A')) ?></div>
                                    <div><?= h(ucfirst($s->class_type)) ?></div>
                                    <div>
                                        <?php if ($s->is_booked): ?>
                                            <span class="cc-badge cc-badge--paid">Booked</span>
                                        <?php else: ?>
                                            <span class="cc-badge">Open</span>
                                        <?php endif; ?>
                                    </div>
                                    <div style="text-align: right;">
                                        <?php if (!$s->is_booked): ?>
                                            <?= $this->Form->postLink('Delete',
                                                ['_name' => 'admin_teacher_delete_slot', 'id' => $teacher->id, 'slotId' => $s->id],
                                                ['class' => 'cc-btn cc-btn--ghost cc-btn--sm', 'confirm' => 'Delete this slot?']) ?>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</section>

<?php $this->start('css'); ?>
<style>
@media (max-width: 980px) {
    .cc-admin-page > div[style*="grid-template-columns"] {
        grid-template-columns: 1fr !important;
    }
}
</style>
<?php $this->end(); ?>
