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

/**
 * PowerGrid OS - Shutdown Handler
 * Executed when the timer reaches zero.
 */

// Trigger Home Assistant Failure Webhook
trigger_ha_webhook('grid_failure');

echo json_encode(['status' => 'SYSTEM_HALTED', 'message' => 'Total shutdown initiated.']);

function trigger_ha_webhook($event)
{
    // Placeholder for actual HA webhook URL
    $url = "http://your-ha-instance:8123/api/webhook/" . $event;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_exec($ch);
    curl_close($ch);
}
