<?php

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

require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../ai/form_generator_question_validation.php';

$workspace_id = $_GET['workspace_id'] ?? '';

if (empty($workspace_id)) {
    echo json_encode([
        'status' => 'error',
        'message' => 'Missing workspace ID'
    ]);
    exit;
}

$sql = "SELECT * FROM questions WHERE workspace_id = :workspace_id ORDER BY id DESC";

$stmt = $pdo->prepare($sql);
$stmt->execute(['workspace_id' => $workspace_id]);

$questions = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($questions as $i => $row) {
    if (is_array($row)) {
        $questions[$i] = formGeneratorApplyStyleRepairToQuestionRow($row);
    }
}

echo json_encode([
    'status' => 'success',
    'questions' => $questions
]);