<?php
session_start();
require_once 'conexao.php';

if (!isset($_SESSION['logado']) || $_SESSION['logado'] !== true) {
    header('HTTP/1.1 401 Unauthorized');
    exit;
}

$pergunta_id = $_GET['pergunta_id'] ?? 0;

if ($pergunta_id) {
    $stmt = $conn->prepare("SELECT id, texto FROM questionario_opcoes WHERE pergunta_id = ? ORDER BY ordem ASC");
    $stmt->bind_param("i", $pergunta_id);
    $stmt->execute();
    $result = $stmt->get_result();
    $opcoes = $result->fetch_all(MYSQLI_ASSOC);
    
    header('Content-Type: application/json');
    echo json_encode($opcoes);
} else {
    header('HTTP/1.1 400 Bad Request');
    echo json_encode([]);
}
?>