<?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;
}

$teacherId = isset($_POST['teacher_id']) ? (int) $_POST['teacher_id'] : 0;
$academicYear = trim((string) ($_POST['academic_year'] ?? ''));
$term = trim((string) ($_POST['term'] ?? ''));

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

echo json_encode(timetableFetchTeacherSlots($conn, $teacherId, $academicYear, $term));

