#!/usr/bin/env php
<?php
// weather.php - Dispatcher-style CLI for VEVOR station

$options = getopt("", ["station:", "color", "debug"]);
$stationId = $options['station'] ?? 'YOUR_STATION_ID';
$useColor  = isset($options['color']);
$debug     = isset($options['debug']);

// Fetch JSON from Weather Underground API
$url = "https://api.weather.com/v2/pws/observations/current?stationId={$stationId}&format=json&units=m&apiKey=YOUR_API_KEY";
$json = file_get_contents($url);
$data = json_decode($json, true);

// Dispatcher: route events
function dispatch($data, $useColor) {
    $temp = $data['observations'][0]['metric']['temp'];
    $wind = $data['observations'][0]['metric']['windspeed'];
    $rain = $data['observations'][0]['metric']['precipRate'];

    $alerts = [];
    if ($temp < 0) $alerts[] = "Frost Alert";
    if ($rain > 10) $alerts[] = "Heavy Rain";
    if ($wind > 40) $alerts[] = "Storm Warning";

    foreach ($alerts as $alert) {
        echo $useColor ? "\033[31m$alert\033[0m\n" : "$alert\n";
    }
}

// Run dispatcher
dispatch($data, $useColor);

if ($debug) {
    print_r($data);
}
