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

$filePath = __DIR__ . '/../database/whatsapp_settings.json';

// Default structure
$defaultEvents = [
    "Create" => ["enabled" => false, "message" => ""],
    "Cancelled" => ["enabled" => false, "message" => ""],
    "Failed" => ["enabled" => false, "message" => ""],
    "Processing" => ["enabled" => false, "message" => ""],
    "Pending" => ["enabled" => false, "message" => ""],
    "Completed" => ["enabled" => false, "message" => ""]
];

try {
    if (file_exists($filePath)) {
        $data = file_get_contents($filePath);
        $json = json_decode($data, true);

        if ($json === null || !is_array($json)) {
            $json = $defaultEvents;
        } else {
            $json = array_merge($defaultEvents, $json);
        }

        echo json_encode([
            "success" => true,
            "data" => $json
        ]);
    } else {
        echo json_encode([
            "success" => true,
            "data" => $defaultEvents
        ]);
    }
} catch (Exception $e) {
    echo json_encode([
        "success" => false,
        "message" => "Error reading settings: " . $e->getMessage(),
        "data" => new stdClass()
    ]);
}
?>
