<?php
include 'db_connection.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['success' => false, 'message' => 'Invalid request method.']);
    exit();
}

$test_name = isset($_POST['test_name']) ? trim($_POST['test_name']) : null;
$teacher_id = isset($_POST['teacher_id']) ? (int)$_POST['teacher_id'] : null;

if (!$test_name || !$teacher_id) {
    echo json_encode(['success' => false, 'message' => 'Pārbaudes darba nosaukums nav ievadīts!']);
    exit();
}

try {
    $stmt = $pdo->prepare("INSERT INTO tests (name, teacher_id) VALUES (:name, :teacher_id)");
    $stmt->bindParam(':name', $test_name);
    $stmt->bindParam(':teacher_id', $teacher_id, PDO::PARAM_INT);
    $stmt->execute();

    echo json_encode(['success' => true, 'message' => 'Pārbaudes darbs pievienots!']);
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}
?>
