<?php
require 'config.php';
session_start();

$teacher_id = $_SESSION['teacher_id'] ?? null;
if (!$teacher_id) {
    echo "Unauthorized.";
    exit;
}

$id       = $_POST['id'] ?? null;
$qText    = $_POST['question_text'] ?? null;
$answer   = $_POST['correct_answer'] ?? null;
$options  = $_POST['options'] ?? null;

if(!$id || !$qText){ echo "Missing data."; exit; }

$stmt = $dbh->prepare("UPDATE questions 
    SET question_text=:qText, correct_answer=:answer, options=:options 
    WHERE id=:id AND teacher_id=:teacher_id");
if($stmt->execute([
    ':qText'=>$qText,
    ':answer'=>$answer,
    ':options'=>$options,
    ':id'=>$id,
    ':teacher_id'=>$teacher_id
])){
    echo "Question updated.";
} else {
    echo "Error updating question.";
}
