<?php
/**
 * AJAX: Add Rubric to Session
 * Temporarily add rubric for case creation
 * Method: POST
 */

session_start();
require_once '../config/config.php';
require_once '../includes/auth.php';

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS');

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    http_response_code(200);
    exit;
}

if (!isLoggedIn()) {
    echo json_encode(['success' => false, 'error' => 'Unauthorized']);
    exit;
}

if ($_SERVER['REQUEST_METHOD'] != 'POST') {
    echo json_encode(['success' => false, 'error' => 'Method not allowed']);
    exit;
}

$rubricId = (int)($_POST['rubric_id'] ?? 0);

if ($rubricId <= 0) {
    echo json_encode(['success' => false, 'error' => 'Invalid rubric ID']);
    exit;
}

if (!isset($_SESSION['temp_rubrics'])) {
    $_SESSION['temp_rubrics'] = [];
}

if (!in_array($rubricId, $_SESSION['temp_rubrics'])) {
    $_SESSION['temp_rubrics'][] = $rubricId;
}

echo json_encode(['success' => true, 'rubric_id' => $rubricId]);
?>