<?php
/**
 * AJAX: Remove Rubric from Session
 * Remove rubric from temporary selection
 * 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 (isset($_SESSION['temp_rubrics'])) {
    $key = array_search($rubricId, $_SESSION['temp_rubrics']);
    if ($key !== false) {
        unset($_SESSION['temp_rubrics'][$key]);
        $_SESSION['temp_rubrics'] = array_values($_SESSION['temp_rubrics']);
    }
}

echo json_encode(['success' => true]);
?>