<?php
include('db_connection.php');

$teacher_id = isset($_GET['teacher_id']) ? (int)$_GET['teacher_id'] : null;

try {
    $classesStmt = $pdo->prepare("SELECT id, class_name FROM classes ORDER BY class_name ASC");
    $classesStmt->execute();
    $classes = $classesStmt->fetchAll(PDO::FETCH_ASSOC);

    if ($teacher_id) {
        $testsStmt = $pdo->prepare("SELECT id, name FROM tests WHERE teacher_id = :teacher_id ORDER BY name ASC");
        $testsStmt->bindParam(':teacher_id', $teacher_id, PDO::PARAM_INT);
    } else {
        $testsStmt = $pdo->prepare("SELECT id, name FROM tests ORDER BY name ASC");
    }
    $testsStmt->execute();
    $tests = $testsStmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode(['success' => true, 'classes' => $classes, 'tests' => $tests]);
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}
?>
