<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, X-Current-Version');

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit();
}

$current_version = $_SERVER['HTTP_X_CURRENT_VERSION'] ?? 'unknown';

$latest_firmware = [
    'version' => 'v1.0.1',
    'file_path' => '/var/www/firmware/dev/climate-controller/firmware.bin',
    'download_url' => 'https://firmware.ryzer.one/dev/climate-controller/firmware.bin'
];

$file_exists = file_exists($latest_firmware['file_path']);
$file_size = $file_exists ? filesize($latest_firmware['file_path']) : 0;
$update_available = ($current_version !== $latest_firmware['version']) && $file_exists;

$response = [
    'update_available' => $update_available,
    'latest_version' => $latest_firmware['version'],
    'current_version' => $current_version,
    'download_url' => $latest_firmware['download_url'],
    'file_size' => $file_size,
    'timestamp' => time()
];

echo json_encode($response, JSON_PRETTY_PRINT);
?>
