<?php
declare(strict_types=1);
date_default_timezone_set('Asia/Shanghai');require __DIR__.'/oracle.php';
$db=smPdo();$sha=catSha();$runtime=getenv('SM7_RUNTIME');
define('APP_PATH',getenv('SM7_ROOT').'/application/');require APP_PATH.'common/enum/EnumBasics.php';require APP_PATH.'common/enum/order/OrderSource.php';
$spec=json_decode(file_get_contents($runtime.'/green-spec-private.json'),false,512,JSON_THROW_ON_ERROR);$boundary=smLoad('boundary-private.json');$events=[];
foreach(file($runtime.'/queries.jsonl',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES)?:[] as $line){$event=json_decode($line,true);if(strpos($event['scenario']??'','PILOT_CAT_GREEN_')===0){$events[]=$event;}}
$end=[];$dml=0;foreach($events as $event){if($event['kind']==='request_end'){$end[$event['scenario']]=$event;}if($event['kind']==='query_observed'&&in_array($event['operation'],['insert','update','delete','replace','merge'],true)){$dml++;}}
$cases=[];$afterProof=[];
foreach($spec as $entry){$name=$entry->name;$case=json_decode(json_encode($entry,JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR),true);unset($case['before']);$after=catSnapshot($db,$case,$boundary);
 $raw=file_get_contents($runtime.'/raw-green-'.$name.'-private.json');$body=json_decode($raw,false,512,JSON_THROW_ON_ERROR);$diff=[];catStrict($entry->before->body,$body,'$',$diff);
 $scope=$entry->before->source_digest===$after['source_digest'];$scenario='PILOT_CAT_GREEN_'.$name;$status=$end[$scenario]['http_status']??0;$fatal=$end[$scenario]['fatal']??true;
 $positive=false;if(in_array($case['kind'],['dishes','orders','recognize'],true)){$positive=!empty($body->data->data);}elseif($case['kind']==='face'){$positive=!empty($body->resultObject->data);}elseif($case['kind']==='card'){$positive=isset($body->code)&&$body->code===0&&!empty($body->data);}
 $cases[]=['case'=>$name,'method'=>$case['method'],'path'=>$case['path'],'http_status'=>$status,'fatal'=>$fatal,'code'=>$body->code??$body->resultCode??null,'positive'=>$positive,'source_pass'=>$status===200&&!$fatal&&$diff===[]&&$scope,'source_window_unchanged'=>$scope,'differences'=>$diff,'wire_bytes'=>strlen($raw),'wire_sha256'=>hash('sha256',$raw)];$afterProof[$name]=$after;
}
$result=['lane'=>getenv('SM7_LANE'),'source_sha256'=>$sha,'cases'=>$cases,'events'=>$events,'http_dml'=>$dml,'new_fixture_dml'=>0,'ddl'=>0,'actor_unchanged'=>(bool)catActor($db)];
smSave('green-after-private.json',$afterProof);smSave('green-report-private.json',$result);
echo json_encode(['lane'=>$result['lane'],'cases'=>$cases,'http_dml'=>$dml,'new_fixture_dml'=>0,'ddl'=>0]),PHP_EOL;
