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

header('Content-Type: application/json');

if (!isset($_SESSION['staff_id'])) {
    echo json_encode(['status'=>0, 'message'=>'Unauthorized']);
    exit;
}

$ids = isset($_POST['ids']) ? $_POST['ids'] : [];

if (!empty($ids)) {
    // Make sure all IDs are integers
    $ids = array_map('intval', $ids);
    $placeholders = implode(',', array_fill(0, count($ids), '?'));
    
    $types = str_repeat('i', count($ids));
    $stmt = $conn->prepare("DELETE FROM questions WHERE id IN ($placeholders)");

    $stmt->bind_param($types, ...$ids);

    if ($stmt->execute()) {
        echo json_encode(['status'=>1, 'message'=>'Selected questions deleted successfully.']);
    } else {
        echo json_encode(['status'=>0, 'message'=>'Failed to delete selected questions.']);
    }

    $stmt->close();
} else {
    echo json_encode(['status'=>0, 'message'=>'No questions selected.']);
}
