<?php
declare(strict_types=1);
function actor(){global $runtime;ck(file_exists($runtime.'/owned-actor.json'),'Prepared exact new actor');return json_decode(file_get_contents($runtime.'/owned-actor.json'),true);}
function ownRows(){ $a=actor();$out=[];foreach(['user'=>'user_id','staff'=>'id','user_weight'=>'id','user_weight_status'=>'id','user_course'=>'id','user_score'=>'id','user_score_log'=>'id','user_bind'=>'bind_id','user_health_metric_record'=>'id'] as $t=>$pk)$out[$t]=readNative('AI','SELECT * FROM yoshop_'.$t.' WHERE user_id=? ORDER BY '.$pk,[$a['user_id']]);return $out;}
function protect(){global $runtime;$old=oldProtection();$core=ownSnapshot();$prior=json_decode(file_get_contents($runtime.'/prior-persistence.json'),true);unset($prior['own_fullrows']['STORE_P1'],$prior['old_fullrows']['ZHCT.prepay_order']);ck($core===$prior['own_fullrows']&&$old===$prior['old_fullrows'],'All prior funds/actor fullrows except Root-released Prepay raw');return ['old'=>$old,'core'=>$core,'Prepay_raw_guard'=>'Root_released_no_query_no_write'];}
function tableHashes($exclude=null){$out=[];foreach(['user'=>'user_id','staff'=>'id','user_weight'=>'id','user_weight_status'=>'id','user_course'=>'id','user_score'=>'id','user_score_log'=>'id','user_bind'=>'bind_id','user_health_metric_record'=>'id'] as $t=>$pk){$sql='SELECT * FROM yoshop_'.$t.($exclude===null?'':' WHERE (user_id<>? OR user_id IS NULL)').' ORDER BY '.$pk;consumeSQL('independent.SELECT.streaming_hash');$s=nativePdo('AI')->prepare($sql);$b=[];if($exclude!==null){$s->bindValue(1,(string)$exclude,2);$b[]=['position'=>1,'value'=>(string)$exclude,'pdo_type'=>2];}$ok=$s->execute();$hash=hash_init('sha256');hash_update($hash,'[');$n=0;while($r=$s->fetch(PDO::FETCH_ASSOC)){foreach($r as &$v)if(is_resource($v))$v=stream_get_contents($v);unset($v);ksort($r);if($n++)hash_update($hash,',');hash_update($hash,json_encode($r,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRESERVE_ZERO_FRACTION));}hash_update($hash,']');$h=hash_final($hash);event('independent-native.jsonl',['connection'=>'AI','queryString'=>$s->queryString,'bindValue'=>$b,'execute'=>$ok,'rowCount'=>$s->rowCount(),'rows'=>'PROTECTED_FULLROW_STREAMING_HASH_ONLY','fullrow_sha256'=>$h,'fetched_count'=>$n]);$out[$t]=['count'=>$n,'sha256'=>$h];}return $out;}
function decimals(){global $runtime,$dm;$a=actor();$cast=$dm?'VARCHAR(80)':'CHAR';$out=[];foreach(['user'=>'user_id','staff'=>'id','user_health_metric_record'=>'id'] as $t=>$pk){$fields=[];foreach(json_decode(file_get_contents($runtime.'/schema.json'),true)[$t]['columns'] as $c){$n=strtolower($c['column_name']);if(strtolower($c['data_type'])==='decimal')$fields[]='CAST('.$n.' AS '.$cast.') '.$n.'_text';}if($fields)$out[$t]=readNative('AI','SELECT '.$pk.','.implode(',',$fields).' FROM yoshop_'.$t.' WHERE user_id=? ORDER BY '.$pk,[$a['user_id']]);}return $out;}
