<?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 protection(){global $runtime;$rows=ownRows();$baseline=json_decode(file_get_contents($runtime.'/owned-baseline.json'),true);ck(protectedOwn($rows)===protectedOwn($baseline),'All own Weight9 except exact approved fields unchanged');$related=relatedRows();ck($related===json_decode(file_get_contents($runtime.'/disease-baseline.json'),true),'Disease/Habit/OtherInfo whole rows unchanged');return ['protected_own'=>protectedOwn($rows),'related'=>$related,'old'=>protect()];}
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;}
