<?php
declare(strict_types=1);

namespace App\Controller;

use Cake\Http\Exception\NotFoundException;
use Cake\ORM\TableRegistry;

class TeachersController extends AppController
{
    public function index(): void
    {
        $teachers = TableRegistry::getTableLocator()->get('Teachers')->find()
            ->where(['is_active' => true])
            ->orderByAsc('sort_order')
            ->all();
        $this->set(compact('teachers'));
    }

    public function profile(string $slug): void
    {
        $teacher = TableRegistry::getTableLocator()->get('Teachers')->find()
            ->where(['slug' => $slug, 'is_active' => true])
            ->first();
        if (!$teacher) {
            throw new NotFoundException('Teacher not found.');
        }

        $slots = TableRegistry::getTableLocator()->get('AvailabilitySlots')->find()
            ->where([
                'teacher_id' => $teacher->id,
                'is_booked' => false,
                'starts_at >=' => date('Y-m-d H:i:s'),
            ])
            ->orderByAsc('starts_at')
            ->limit(8)
            ->all();

        $this->set(compact('teacher', 'slots'));
    }
}
