<?php

declare(strict_types=1);

set_exception_handler(static function (Throwable $error): void {
    fwrite(STDERR, 'N3_MODEL_NO_DB_FAILED ' . get_class($error) . PHP_EOL);
    exit(2);
});

$variant = (string)getenv('POC_VARIANT');
$roots = [
    'B' => '/tmp/zhct-http-ai-macos.x2CCZX',
    'C' => '/tmp/zhct-ai-http-20260929.3XDj3w',
];
if (!isset($roots[$variant])) throw new RuntimeException('Only B/C isolated roots allowed');
$root = $roots[$variant];
$file = $root . '/app/store/model/weight/StaffWeight.php';
$expectedSha = 'a088d81d1fdebb902d5c52de882f0820efe8021ba70b43c2e436a945b6d93958';
if (!is_file($file) || !hash_equals($expectedSha, (string)hash_file('sha256', $file))) {
    throw new RuntimeException('Runtime model source SHA mismatch');
}
if (!function_exists('app_name')) {
    function app_name(): string { return 'store'; }
}
if (!function_exists('getStoreId')) {
    function getStoreId(): int { return 10001; }
}
require $root . '/vendor/autoload.php';
$actual = (new ReflectionClass('app\\store\\model\\weight\\StaffWeight'))->getFileName();
if (!is_string($actual) || !hash_equals($expectedSha,
        (string)hash_file('sha256', $actual))) {
    throw new RuntimeException('Actual autoload source SHA mismatch');
}
$model = new \app\store\model\weight\StaffWeight([
    'id' => 7,
    'height' => 170,
    'weight' => 60.25,
    'body_fat_rate' => 20,
    'bmi' => 20.76,
    'energy_value' => 123.4,
]);
$row = $model->toArray();
$expected = ['height' => '170.00', 'weight' => '60.25',
    'body_fat_rate' => '20.00', 'bmi' => '20.76',
    'energy_value' => '123.40'];
$fiveStrings = true;
foreach ($expected as $field => $value) {
    if (($row[$field] ?? null) !== $value) $fiveStrings = false;
}
$rawPreserved = $model['weight'] === 60.25 && $model['height'] === 170;
$idPreserved = ($row['id'] ?? null) === 7;
$json = json_decode((string)json_encode($model), true);
$jsonFiveStrings = is_array($json);
foreach ($expected as $field => $value) {
    if (!is_array($json) || ($json[$field] ?? null) !== $value) $jsonFiveStrings = false;
}
$pass = $fiveStrings && $rawPreserved && $idPreserved && $jsonFiveStrings;
echo json_encode(['variant' => $variant, 'runtime_source_ready' => true,
    'actual_autoload_source_ready' => true, 'five_to_array_strings' => $fiveStrings,
    'raw_model_values_preserved' => $rawPreserved,
    'id_integer_preserved' => $idPreserved,
    'five_json_strings' => $jsonFiveStrings, 'pass' => $pass],
    JSON_UNESCAPED_SLASHES) . PHP_EOL;
exit($pass ? 0 : 1);
