<?php
require_once 'config.php';

checkAccess(['admin', 'programmer', 'board']);

$data = json_decode(file_get_contents("php://input"), true);

if (!$data || !isset($data['title'])) {
    response(["error" => "Datos incompletos"], 400);
}

try {
    $stmt = $pdo->prepare("INSERT INTO polls (title, description, questions_json, status) VALUES (?, ?, ?, ?)");
    $stmt->execute([
        $data['title'],
        $data['description'],
        json_encode($data['questions']),
        'Abierta' // Coincide con el ENUM y el frontend
    ]);
    response(["success" => true]);
} catch (PDOException $e) {
    response(["error" => $e->getMessage()], 500);
}
?>