<?php
function relatedRows(){return ['user_disease'=>readNative('AI','SELECT * FROM yoshop_user_disease WHERE user_id=? ORDER BY id',[actor()['user_id']]),'habit'=>readNative('AI','SELECT * FROM yoshop_habit WHERE staff_id=? ORDER BY id',[actor()['staff_id']]),'user_other_info'=>readNative('AI','SELECT * FROM yoshop_user_other_info WHERE user_id=? ORDER BY id',[actor()['user_id']])];}
function scaleRows(){global $dm;$a=actor();$sessions=readNative('ZHCT','SELECT * FROM ydy_measurement_session WHERE ai_user_id=? ORDER BY session_id',[$a['user_id']]);$ids=array_column($sessions,'session_id');$raw=[];$records=[];if($ids){$q=implode(',',array_fill(0,count($ids),'?'));$raw=readNative('ZHCT','SELECT * FROM ydy_measurement_session_raw WHERE session_id IN ('.$q.') ORDER BY id',$ids);$records=readNative('ZHCT','SELECT * FROM ydy_health_record WHERE session_id IN ('.$q.') ORDER BY record_id',$ids);}return ['sessions'=>$sessions,'raw'=>$raw,'records'=>$records];}
function recordText(){global $runtime,$dm;$a=actor();$cols=['CAST(value AS '.($dm?'VARCHAR(80)':'CHAR').') value_text'];foreach(json_decode(file_get_contents($runtime.'/store-schema.json'),true)['health_record']['columns'] as $c){$name=strtolower($c['column_name']);if($name!=='value'&&preg_match('/decimal|numeric|number/',strtolower($c['data_type'])))$cols[]='CAST('.$name.' AS '.($dm?'VARCHAR(80)':'CHAR').') '.$name.'_text';}return readNative('ZHCT','SELECT record_id,'.implode(',',$cols).' FROM ydy_health_record WHERE ai_user_id=? ORDER BY record_id',[$a['user_id']]);}
function protectedOwn($rows){foreach($rows['staff'] as &$r)unset($r['weight'],$r['update_time']);unset($r);foreach($rows['user_weight'] as &$r)unset($r['weight'],$r['height'],$r['bmi'],$r['update_time']);unset($r);return $rows;}
function funds(){global $runtime;$ids=json_decode(file_get_contents($runtime.'/prepared.json'),true)['ids'];$out=[];foreach([['AI','user','user_id','AI_U1'],['AI','user','user_id','AI_U2'],['ZHCT','user','id','STORE_U1'],['ZHCT','user','id','STORE_U2']] as [$db,$t,$pk,$key])$out[$key]=readNative($db,'SELECT * FROM '.($db==='AI'?'yoshop_':'ydy_').$t.' WHERE '.$pk.'=?',[$ids[$key]]);foreach(['account_settlement'=>'settlement_id','user_balance_log'=>'log_id','user_subsidy'=>'id','user_gift_log'=>'log_id'] as $t=>$pk)$out[$t]=readNative('AI','SELECT * FROM yoshop_'.$t.' WHERE user_id IN (?,?) ORDER BY '.$pk,[$ids['AI_U1'],$ids['AI_U2']]);$out['meal_order']=readNative('ZHCT','SELECT * FROM ydy_meal_order WHERE user_id IN (?,?) ORDER BY id',[$ids['STORE_U1'],$ids['STORE_U2']]);$orderIds=array_column($out['meal_order'],'id');foreach(['initial_menu','meal_order_pickup'] as $t)$out[$t]=$orderIds?readNative('ZHCT','SELECT * FROM ydy_'.$t.' WHERE meal_order_id IN ('.implode(',',array_fill(0,count($orderIds),'?')).') ORDER BY id',$orderIds):[];ck($out===json_decode(file_get_contents($runtime.'/fund-baseline.json'),true),'All original own funds/paid chain fullrows unchanged');return $out;}
function protection(){global $runtime;$rows=ownRows();ck($rows===json_decode(file_get_contents($runtime.'/AI-baseline.json'),true),'All own AI9 fullrows unchanged - no mutable exceptions');$related=relatedRows();ck($related===json_decode(file_get_contents($runtime.'/disease-baseline.json'),true),'Disease/Habit/OtherInfo fullrows unchanged');return ['AI'=>$rows,'related'=>$related,'funds'=>funds(),'scope_note'=>'No global StoreStaff claim; unrelated old Read8 drift remains unattributed; Prepay oldraw released'];}
function scaleHashes(){global $runtime;$a=actor();$out=[];foreach(['measurement_session'=>'session_id','measurement_session_raw'=>'id','health_record'=>'record_id'] as $t=>$pk){$where=$t==='measurement_session_raw'?' WHERE session_id NOT IN (SELECT session_id FROM ydy_measurement_session WHERE ai_user_id=?)':' WHERE (ai_user_id<>? OR ai_user_id IS NULL)';$sql='SELECT * FROM ydy_'.$t.$where.' ORDER BY '.$pk;consumeSQL('independent.SELECT.streaming_hash');$s=nativePdo('ZHCT')->prepare($sql);$s->bindValue(1,(string)$a['user_id'],2);$ok=$s->execute();$h=hash_init('sha256');hash_update($h,'[');$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($h,',');hash_update($h,json_encode($r,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRESERVE_ZERO_FRACTION));}hash_update($h,']');$digest=hash_final($h);event('independent-native.jsonl',['connection'=>'ZHCT','queryString'=>$sql,'bindValue'=>[['position'=>1,'value'=>(string)$a['user_id'],'pdo_type'=>2]],'execute'=>$ok,'rows'=>'PROTECTED_FULLROW_STREAMING_HASH_ONLY','fetched_count'=>$n,'fullrow_sha256'=>$digest]);$out[$t]=['count'=>$n,'sha256'=>$digest];}return $out;}
