<?php
require_once 'config.php';

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

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

if (!isset($data['id'])) response(["error" => "ID faltante"], 400);

try {
    $pdo->beginTransaction();
    
    // Eliminar respuestas asociadas primero (por integridad referencial si no hay CASCADE)
    $stmtRes = $pdo->prepare("DELETE FROM data_form_responses WHERE form_id = ?");
    $stmtRes->execute([$data['id']]);

    // Eliminar formulario
    $stmtForm = $pdo->prepare("DELETE FROM data_forms WHERE id = ?");
    $stmtForm->execute([$data['id']]);

    $pdo->commit();
    response(["success" => true]);
} catch (PDOException $e) {
    if ($pdo->inTransaction()) $pdo->rollBack();
    response(["error" => $e->getMessage()], 500);
}
?>