<?php
require_auth();
require_any_permission(['jobs.run', 'ota.manage', 'settings.manage']);
$method = $_SERVER['REQUEST_METHOD'];

if ($method === 'GET') {
    $health = ['v5' => true, 'ota' => function_exists('hv5_ota_health_snapshot') ? hv5_ota_health_snapshot() : []];
    if (hv5_table_exists('background_jobs')) {
        $health['pending_jobs'] = (int)db()->query("SELECT COUNT(*) FROM background_jobs WHERE status='pending'")->fetchColumn();
    }
    json_ok($health);
}

csrf_check();
if ($method === 'POST') {
    if (!function_exists('hv5_run_jobs')) json_error('V5 not loaded', 503);
    json_ok(hv5_run_jobs(25), 'Jobs processed');
}
json_error('Method not allowed', 405);
