<?php
require __DIR__.'/db.php';$lease=json_decode(file_get_contents(__DIR__.'/private-lease.json'),true);$report=['lane'=>$lane,'passed'=>false,'DML'=>0,'DDL'=>0,'HTTP'=>0];try{
$staff=$q('SELECT * FROM '.$p.'staff WHERE UUID=?',[$lease['scopes']['marked_empty']['uuid']]);$check(count($staff)===1&&$staff[0]['name']===$lease['scopes']['marked_empty']['name'],'Existing exact marked Staff');$users=$q('SELECT * FROM '.$p.'user WHERE STAFF_UUID=? AND IS_DELETE=0',[$staff[0]['uuid']]);$report['mapping']=['users_count'=>count($users),'type'=>isset($users[0])?(int)$users[0]['type']:null,'account_matches_marked_name'=>isset($users[0])&&$users[0]['account']===$staff[0]['name'],'user_UUID_length'=>isset($users[0])?strlen($users[0]['uuid']):null,'user_UUID_sha256'=>isset($users[0])?hash('sha256',$users[0]['uuid']):null];$userSha=$lane==='B'?'9b565723bd37ca530630b04f0c6689324436ec782b1686ae9eb75d8278658e94':'01b328f20167b97d988fdb835498f8fcb2de45f6fa3cba70313b136fff0696da';$check(count($users)===1&&(int)$users[0]['type']===1&&$users[0]['account']===$staff[0]['name']&&strlen($users[0]['uuid'])===34&&hash('sha256',$users[0]['uuid'])===$userSha,'Exact approved existing marked User34 mapping');$actor=$q('SELECT * FROM '.$p.'user WHERE ID=?',[$lease['actor']['id']]);$check(count($actor)===1&&$actor[0]['uuid']===$lease['actor']['uuid']&&(int)$actor[0]['type']===0&&(int)$actor[0]['is_delete']===0,'Actual actor unchanged');
$natural=[];$ddlAll=[];$inv=json_decode(file_get_contents(__DIR__.'/private-inventory.json'),true);foreach(['member_weight','member_weight_status']as $t){$table=$p.$t;$triggers=$lane==='B'?$q('SELECT TRIGGER_NAME,EVENT_MANIPULATION FROM information_schema.TRIGGERS WHERE TRIGGER_SCHEMA=DATABASE() AND EVENT_OBJECT_TABLE=?',[$table]):$q('SELECT TRIGGER_NAME,STATUS,TRIGGERING_EVENT FROM USER_TRIGGERS WHERE TABLE_NAME=?',[strtoupper($table)]);$fks=$lane==='B'?$q('SELECT TABLE_NAME,CONSTRAINT_NAME,REFERENCED_TABLE_NAME FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA=DATABASE() AND REFERENCED_TABLE_NAME IS NOT NULL AND (TABLE_NAME=? OR REFERENCED_TABLE_NAME=?)',[$table,$table]):$q("SELECT C.TABLE_NAME,C.CONSTRAINT_NAME,P.TABLE_NAME AS REFERENCED_TABLE_NAME FROM USER_CONSTRAINTS C LEFT JOIN USER_CONSTRAINTS P ON C.R_CONSTRAINT_NAME=P.CONSTRAINT_NAME WHERE C.CONSTRAINT_TYPE='R' AND (C.TABLE_NAME=? OR P.TABLE_NAME=?)",[strtoupper($table),strtoupper($table)]);$check($triggers===[]&&$fks===[],'No unknown trigger/FK');$indexes=$lane==='B'?$q('SELECT INDEX_NAME,NON_UNIQUE,SEQ_IN_INDEX,COLUMN_NAME FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=? ORDER BY INDEX_NAME,SEQ_IN_INDEX',[$table]):$q('SELECT I.INDEX_NAME,I.UNIQUENESS,C.COLUMN_POSITION,C.COLUMN_NAME FROM USER_INDEXES I JOIN USER_IND_COLUMNS C ON I.INDEX_NAME=C.INDEX_NAME WHERE I.TABLE_NAME=? ORDER BY I.INDEX_NAME,C.COLUMN_POSITION',[strtoupper($table)]);
if($lane==='B')$ddl=$q('SHOW CREATE TABLE '.$table)[0]['create table'];else{$ddl=$db->query("SELECT DBMS_METADATA.GET_DDL('TABLE','".strtoupper($table)."','ZHCT_APP') FROM DUAL")->fetchColumn();if(is_resource($ddl))$ddl=stream_get_contents($ddl);}$ddlAll[$t]=$ddl;$check(preg_match('/["`]?ID["`]?\s+(?:BIGINT|int\(10\) unsigned)\s+(?:NOT NULL\s+)?(?:IDENTITY\s*\(|AUTO_INCREMENT\b)/i',$ddl)===1,'Generated ID proven');$check((int)$q('SELECT COUNT(*) AS CNT FROM '.$table.' WHERE STAFF_UUID=?',[$staff[0]['uuid']])[0]['cnt']===0,'Exact marked scope0 before fixture');$rows=[];$last=0;for($page=0;$page<3;$page++){$sql='SELECT * FROM '.$table.' WHERE ID>? ORDER BY ID';$part=$q($lane==='B'?$sql.' LIMIT 100':'SELECT * FROM ('.$sql.') WHERE ROWNUM<=100',[$last]);$rows=array_merge($rows,$part);if(count($part)<100)break;$last=(int)$part[count($part)-1]['id'];}$check(count($rows)===$inv['tables'][$t]['total_count']&&count($rows)<=200,'Complete original natural count cap200');$check(strlen(json_encode($rows))<=2097152,'Natural source cap2MiB');foreach($rows as $row)foreach($row as $v)$check(!is_string($v)||strlen($v)<=262144,'Natural field cap256KiB');$natural[$t]=$rows;$report['tables'][$t]=['columns'=>$inv['tables'][$t]['columns'],'indexes'=>$indexes,'triggers'=>$triggers,'foreign_keys'=>$fks,'ddl_sha256'=>hash('sha256',$ddl),'natural_count'=>count($rows),'natural_sha256'=>$hash($rows)];}
$uuid=static function():string{$x=strtoupper(bin2hex(random_bytes(16)));return substr($x,0,8).'-'.substr($x,8,4).'-'.substr($x,12,4).'-'.substr($x,16,4).'-'.substr($x,20);};$write=['member_weight'=>[],'member_weight_status'=>[]];foreach([1,2]as $i){$write['member_weight'][]=['staff_uuid'=>$staff[0]['uuid'],'weight'=>$i===1?'0.00':'63.25','pbf'=>$i===1?'0.00':'18.50','create_time'=>'2001-03-0'.$i.' 08:00:00','ymd'=>'2001-03-0'.$i];$u=$uuid();$check((int)$q('SELECT COUNT(*) AS CNT FROM '.$p.'member_weight_status WHERE UUID=?',[$u])[0]['cnt']===0,'New fixture GUID36 absent');$write['member_weight_status'][]=['uuid'=>$u,'staff_uuid'=>$staff[0]['uuid'],'status'=>$i===1?'1':'3','energy_value'=>$i===1?0:1800,'weight'=>$i===1?0:63,'years'=>2001,'create_time'=>'2001-03-0'.$i.' 09:00:00','create_by'=>$users[0]['uuid']];}
foreach($write as $t=>$rows)foreach($rows as $r){$cols=array_map(static function($c){return strtolower($c['column_name']);},$inv['tables'][$t]['columns']);$check(count($cols)===count($r)+1&&array_diff($cols,array_keys($r),['id'])===[],'All nonID columns explicit');}
$save('private-fixture-lease.json',['staff'=>$staff[0],'user'=>$users[0],'actor'=>$actor[0],'natural'=>$natural,'write'=>$write]);$save('private-full-ddl.json',$ddlAll);$report['reference']=['staff_sha256'=>$hash($staff),'user_sha256'=>$hash($users),'actor_sha256'=>$hash($actor),'fixture_author_UUID_length'=>strlen($users[0]['uuid']),'fixture_author_name_sha256'=>hash('sha256',$users[0]['name'])];$report['write_count']=['member_weight'=>2,'member_weight_status'=>2];$report['passed']=true;
}catch(Throwable $e){$save('private-fixture-gate-error.json',['class'=>get_class($e),'message'=>$e->getMessage()]);$report['failure']=['class'=>get_class($e),'reason_sha256'=>hash('sha256',$e->getMessage())];}$save('private-fixture-gate.json',$report);echo json_encode($report,JSON_UNESCAPED_UNICODE),"\n";exit($report['passed']?0:2);
