<?php
session_start();
header('Content-Type: application/json');

if (!isset($_SESSION['user_id'])) {
    http_response_code(401);
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit;
}

require_once '../config.php';
require_once '../lib/exam_utils.php';

$examId = (int) ($_POST['exam_id'] ?? 0);
if (!$examId) {
    echo json_encode(['success' => false, 'message' => 'Missing exam id.']);
    exit;
}

$exam = loadExamRecord($examId);
if (!$exam) {
    echo json_encode(['success' => false, 'message' => 'Exam not found.']);
    exit;
}

$saved = saveExamQuestionsToBank($examId, $exam, (int) $_SESSION['user_id']);
echo json_encode(['success' => true, 'message' => $saved . ' questions saved to the bank.', 'saved_count' => $saved]);

