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

require_once 'db_config.php';

$pdo = getDatabaseConnection();

$parentId = isset($_GET['parent_id']) ? $_GET['parent_id'] : null;

if ($parentId === 'null' || $parentId === '0' || $parentId === '') {
    $parentId = null;
}

$sql = "SELECT id, label_pl, is_final, ticket_type FROM wizard_steps WHERE parent_id " . ($parentId === null ? "IS NULL" : "= :parent_id");
$stmt = $pdo->prepare($sql);

if ($parentId !== null) {
    $stmt->bindParam(':parent_id', $parentId, PDO::PARAM_INT);
}

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

echo json_encode($steps);
?>