<?php
declare(strict_types=1);

session_start();
require_once '../../../config.php';
require_once dirname(__DIR__) . '/helpers.php';

header('Content-Type: application/json');

if (!isset($_SESSION['user_id'])) {
    http_response_code(401);
    echo json_encode([]);
    exit;
}

$conn = getDBConnection();
if (!$conn) {
    echo json_encode([]);
    exit;
}

$className = trim((string) ($_POST['class_name'] ?? ''));
$academicYear = trim((string) ($_POST['academic_year'] ?? ''));
$term = trim((string) ($_POST['term'] ?? ''));

if ($className === '' || $academicYear === '' || $term === '') {
    echo json_encode([]);
    exit;
}

echo json_encode(timetableFetchClassSlots($conn, $className, $academicYear, $term));

