<?php
chdir('../');
session_start();
require_once('public/connection.php');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $student_id = intval($_POST['student_id'] ?? 0);
    $exam_id = intval($_POST['exam_id'] ?? 0);
    $count = intval($_POST['count'] ?? 0);

    if ($student_id && $exam_id) {
        // 1. Insert a malpractice log
        $stmt = $conn->prepare("INSERT INTO malpractice_logs (student_id, exam_id, warning_count, log_time) VALUES (?, ?, ?, NOW())");
        $stmt->bind_param("iii", $student_id, $exam_id, $count);
        $stmt->execute();

        // 2. Flag student if count reaches 3
        if ($count >= 3) {
            $conn->query("UPDATE students SET flagged = 1 WHERE id = $student_id");
        }

        http_response_code(200);
        echo "Recorded";
    } else {
        http_response_code(400);
        echo "Missing data";
    }
} else {
    http_response_code(405);
    echo "Method not allowed";
}
?>

