<?php
require_once dirname(__DIR__) . '/airmax_monitor_lib.php';

function health_assert_same($expected, $actual, string $message): void {
    if ($expected !== $actual) {
        fwrite(STDERR, "FAIL: {$message}\nExpected: " . var_export($expected, true) . "\nActual: " . var_export($actual, true) . "\n");
        exit(1);
    }
}

$classic = airmax_parse_status_body(json_encode([
    'wireless'=>[
        'signal'=>-58,
        'polling'=>['quality'=>86, 'capacity'=>65],
    ],
    'host'=>[
        'devmodel'=>'PowerBeam M5 400',
        'cpuload'=>54,
        'totalram'=>1000,
        'freeram'=>660,
    ],
]));
health_assert_same(86, $classic['quality'], 'classic airOS quality must remain authoritative');
health_assert_same(65, $classic['capacity'], 'classic airOS capacity must remain authoritative');
health_assert_same(54.0, $classic['cpu'], 'classic airOS CPU must remain authoritative');
health_assert_same(34.0, $classic['memory'], 'classic airOS memory calculation must remain authoritative');
health_assert_same(-58, $classic['signal'], 'unrelated successful signal parsing must not change');

$modern = airmax_parse_status_body(json_encode([
    'wireless'=>[
        'signal'=>-61,
        'airmax'=>['link_quality_percent'=>'92%', 'link_capacity_percent'=>78],
    ],
    'system'=>[
        'cpu_usage_percent'=>'27%',
        'memory'=>['total_memory'=>2048, 'free_memory'=>512],
    ],
    'host'=>['devmodel'=>'PowerBeam 5AC'],
]));
health_assert_same(92.0, $modern['quality'], 'modern nested quality must be detected');
health_assert_same(78.0, $modern['capacity'], 'modern nested capacity must be detected');
health_assert_same(27.0, $modern['cpu'], 'modern nested CPU must be detected');
health_assert_same(75.0, $modern['memory'], 'modern total/free memory must be calculated');
health_assert_same(-61, $modern['signal'], 'modern enrichment must not alter signal parsing');

$mimosa = airmax_extract_live_health_metrics([
    'data'=>json_encode([
        'radio'=>['LinkQuality'=>88, 'LinkCapacityPercent'=>'71%'],
        'system'=>['CPUUtilization'=>19, 'MemoryUsagePercent'=>43],
    ]),
]);
health_assert_same(88.0, $mimosa['quality'], 'Mimosa JSON-string quality must be detected');
health_assert_same(71.0, $mimosa['capacity'], 'Mimosa JSON-string capacity must be detected');
health_assert_same(19.0, $mimosa['cpu'], 'Mimosa JSON-string CPU must be detected');
health_assert_same(43.0, $mimosa['memory'], 'Mimosa JSON-string memory must be detected');

$preserved = airmax_fill_missing_live_health_metrics(
    ['quality'=>99, 'capacity'=>98, 'cpu'=>12, 'memory'=>34, 'signal'=>-55],
    ['LinkQuality'=>10, 'LinkCapacity'=>20, 'CPULoad'=>30, 'MemoryUsage'=>40]
);
health_assert_same(99, $preserved['quality'], 'enrichment must never overwrite quality');
health_assert_same(98, $preserved['capacity'], 'enrichment must never overwrite capacity');
health_assert_same(12, $preserved['cpu'], 'enrichment must never overwrite CPU');
health_assert_same(34, $preserved['memory'], 'enrichment must never overwrite memory');
health_assert_same(-55, $preserved['signal'], 'enrichment must not touch unrelated metrics');

$rejected = airmax_extract_live_health_metrics(['LinkCapacityPercent'=>650, 'MemoryUsagePercent'=>4096]);
health_assert_same(null, $rejected['capacity'], 'PHY rate must not be mislabeled as capacity percentage');
health_assert_same(null, $rejected['memory'], 'byte/counter value must not be mislabeled as memory percentage');

echo "OK airmax modern health metrics\n";
