<?php

// Set the content type to JSON
header('Content-Type: application/json');

// Get the raw POST data
$raw_post_data = file_get_contents('php://input');

// Define the path for the JSON file to store the raw request
$file_path = 'database/connection-status.json';

// Prepare the data to be saved
$data_to_save = array(
    'received_at' => date('Y-m-d H:i:s'), // Timestamp when the data is received
    'raw_data'    => json_decode($raw_post_data, true), // Decode the raw data (optional for easier readability)
);

// Save the new data to the file, replacing the existing content
file_put_contents($file_path, json_encode($data_to_save, JSON_PRETTY_PRINT));

// Respond with a success message
$response = array('status' => 'success', 'message' => 'Status received and saved successfully');

// Send the JSON response back
echo json_encode($response);

?>