<?php
// Offline finite-oracle experiment only. No PDO, no app bootstrap, no writes.
$input = json_decode(file_get_contents($argv[1]), true);
if (!is_array($input) || !isset($input['physical'], $input['computed'])) throw new RuntimeException('input');
function fromHex($hex) {
    if ($hex === null) return null;
    if (!is_string($hex) || !preg_match('/^[0-9a-f]{16}$/D', $hex)) throw new RuntimeException('hex');
    return unpack('e', hex2bin($hex))[1];
}
function fmtOnlyPhysical($x, $context) {
    if ($x === null || $context !== 'explicit_physical_FLOAT8_2') return $x;
    return (float)sprintf('%.2F', $x);
}
function asHex($x) { return $x === null ? null : bin2hex(pack('e', $x)); }
$out = ['status'=>'OFFLINE_FORMATTER_FINITE_ORACLE_NOT_BUSINESS_HELPER', 'PHP_VERSION'=>PHP_VERSION,
    'sql'=>0,'physical_count'=>0,'physical_null'=>0,'physical_mismatches'=>[],
    'computed_count'=>0,'computed_mismatches'=>[], 'global_round_counterexamples'=>[], 'rows'=>[]];
foreach (['physical','computed'] as $group) {
    foreach ($input[$group] as $case) {
        $x = fromHex($case['stored_double_hex']);
        $got = fmtOnlyPhysical($x, $case['context']);
        $bits = asHex($got);
        $out[$group.'_count']++;
        if ($group==='physical' && $x===null) $out['physical_null']++;
        if ($bits !== $case['expected_getter_hex']) $out[$group.'_mismatches'][] = ['case_id'=>$case['case_id'],'expected_hex'=>$case['expected_getter_hex'],'got_hex'=>$bits];
        if ($group==='computed' && asHex((float)sprintf('%.2F',$x)) !== $case['expected_getter_hex']) {
            $out['global_round_counterexamples'][] = ['case_id'=>$case['case_id'],'actual_hex'=>$case['expected_getter_hex'],'globally_formatted_hex'=>asHex((float)sprintf('%.2F',$x))];
        }
        $out['rows'][] = ['case_id'=>$case['case_id'],'input_hex'=>$case['stored_double_hex'],'output_hex'=>$bits,
                          'output_float17g'=>$got===null?null:sprintf('%.17g',$got),'expected_hex'=>$case['expected_getter_hex']];
    }
}
$out['scope_warning']='Formatter applies only after correct storage mapping, only with explicitly known physical FLOAT(8,2) provenance. Does not fix original C FLOAT53 storage or SUM/computed values.';
echo json_encode($out, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES), "\n";
