<?php
header('Content-Type: application/json');
require_once '../db/config.php';

try {
    $pdo = db();
    $stmt = $pdo->query("SELECT id, event_name, probability, track_data FROM stochastic_events");
    $events = $stmt->fetchAll();

    // Decode the JSON track_data for each event
    foreach ($events as &$event) {
        $event['track_data'] = json_decode($event['track_data']);
    }

    echo json_encode($events);

} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
}
