<?php
declare(strict_types=1);
const LR_PLAN_SHA='b9957993cdfaa2053d4fae87f77e74de58d25331b523576ca166f93fc518c345';
const LR_LITERAL_SHA='a80a70c2b2b3d63a176b6e4befcc04fcb2d410453b1fb94c38bcc1a068c92757';
function lr_assert(bool $test,string $message):void{if(!$test)throw new RuntimeException($message);}
function lr_load(string $file):array{
    lr_assert(is_file($file)&&filesize($file)<200000,'Exact finite plan file');
    lr_assert(hash_file('sha256',$file)===LR_PLAN_SHA,'Approved plan SHA only');
    $p=json_decode(file_get_contents($file),true,512,JSON_THROW_ON_ERROR);
    lr_assert($p['connection']['lane']==='C'&&$p['connection']['host']==='127.0.0.1'&&$p['connection']['port']===5237&&$p['connection']['schema']==='ZHCT_APP','C-only endpoint');
    lr_assert(count($p['cases'])===47&&count($p['oracles'])===43,'Frozen cardinality');
    lr_assert(hash('sha256',$p['templates']['literal_rint'])===LR_LITERAL_SHA,'Exact WITH only');
    lr_assert($p['budget']['C_total_diagnostic_attempts']===50&&$p['budget']['ceiling']===64,'Exact budget');
    foreach(['DDL','DML','natural','HTTP','B_SQL','retry','begin_commit_rollback']as $k)lr_assert($p['budget'][$k]===0,'Zero '.$k);
    return $p;
}
function lr_sequence(array $p):array{
    $out=[];
    foreach(['identity_schema','identity_version']as $role)$out[]=['kind'=>'select','role'=>$role,'case_id'=>null,'sql'=>$p['templates'][$role],'parameters'=>[]];
    $out[]=['kind'=>'prepare_only','role'=>'literal_rint','case_id'=>null,'sql'=>$p['templates']['literal_rint'],'parameters'=>[]];
    foreach($p['cases']as $c)$out[]=['kind'=>'select','role'=>'literal_rint','case_id'=>$c['case_id'],'sql'=>$p['templates'][$c['template']],'parameters'=>$c['parameters']];
    return $out;
}
function lr_step_gate(array $p,array $seq,int $index,array $step,int $attempts):void{
    lr_assert($attempts===$index&&$index>=0&&$index<50&&$attempts<64,'No skip/retry/budget expansion');
    lr_assert(isset($seq[$index])&&$seq[$index]===$step,'Exact ordered step and bind');
    lr_assert(isset($p['templates'][$step['role']])&&$p['templates'][$step['role']]===$step['sql'],'No alternate SQL');
    lr_assert(in_array(hash('sha256',$step['sql']),['8eaca5e08cc964dc2e79ed09c413d50324dd438de1400e24b71f7a41ec030914','8899a5988b18244dba04533adf90b70b9d1945a1d691faf7116a271af748eef3',LR_LITERAL_SHA],true),'Exact SQL hash whitelist');
    foreach($step['parameters']as $b){
        lr_assert($b['position']===1&&$b['original_value']===$b['runtime_value'],'Original bound value not transformed');
        lr_assert(gettype($b['runtime_value'])===$b['php_type'],'Original host type');
        lr_assert($b['runtime_value']===null?$b['pdo_type_code']===0:(is_string($b['runtime_value'])&&in_array($b['pdo_type_code'],[1,2],true)),'Exact permitted bind');
    }
}
function lr_result_gate(array $step,array $rows,array $meta,int $bytes):void{
    lr_assert($bytes<=65536,'Result64KB limit');
    lr_assert(($meta['supported']??false)===true,'Unknown metadata stops');
    $role=$step['role'];
    if($role==='identity_schema'){
        lr_assert(count($rows)===1&&$rows[0]===['schema_name'=>'ZHCT_APP'],'Exact current schema');return;
    }
    if($role==='identity_version'){
        $version=[['banner'=>'DM Database Server 64 V8'],['banner'=>'8.4'],['banner'=>'企业版'],['banner'=>'DB Version: 0x7000d'],['banner'=>'03134284488-20260212-314192-20200'],['banner'=>'Msg Version: 1'],['banner'=>'Gsu level(5) cnt: 102']];
        lr_assert(count($rows)<=16&&$rows===$version,'Unchanged previously observed exact server identity');return;
    }
    lr_assert($role==='literal_rint'&&count($rows)===1,'Exactly one literal result');
    $row=$rows[0];$expected=['x','whole','scaled','lower_i','rounded_i','quantized','cap','clipped','input_is_null','half_side','was_clipped','stored_real','stored_double'];
    $keys=array_keys($row);sort($keys);sort($expected);lr_assert($keys===$expected,'Exact literal columns');
    lr_assert(count($meta['columns'])===13,'Exact metadata column count');
    $isNull=$step['parameters'][0]['original_value']===null;
    foreach(['x','whole','scaled','lower_i','rounded_i','quantized','clipped','stored_real','stored_double']as $k){
        lr_assert($isNull?$row[$k]===null:(is_float($row[$k])&&is_finite($row[$k])),'Native literal double/NULL '.$k);
    }
    lr_assert(is_float($row['cap'])&&is_finite($row['cap']),'Finite double cap');
    lr_assert($row['input_is_null']===($isNull?1:0),'Exact NULL flag');
    lr_assert($isNull?($row['half_side']===null&&$row['was_clipped']===null):(is_int($row['half_side'])&&in_array($row['half_side'],[-1,0,1],true)&&is_int($row['was_clipped'])&&in_array($row['was_clipped'],[0,1],true)),'Native integer diagnostic flags');
}
