<?php
require_once __DIR__.'/db.php';
$priorRoot=$lane==='B'?'/tmp/cdx-store-category9-B.cVux2z':'/tmp/cdx-store-category9-C.Hr1w83';
$prior=json_decode(file_get_contents($priorRoot.'/reference-'.$lane.'.json'),true);
$actorId=$prior['reference']['actor_id'];
$source=static function()use($q,$p,$actorId):array {
 $sets=$q('SELECT * FROM '.$p.'nutrition_set ORDER BY ID');$times=$q('SELECT * FROM '.$p.'nutrition_time ORDER BY ID');
 $refs=[];foreach($sets as $r)$refs['set:'.$r['uuid']]=(int)$q('SELECT COUNT(*) AS CNT FROM '.$p.'nutrition WHERE IS_DEL=1 AND SET_UUID=?',[$r['uuid']])[0]['cnt'];
 foreach($times as $r){$refs['time-nutrition:'.$r['uuid']]=(int)$q('SELECT COUNT(*) AS CNT FROM '.$p.'nutrition WHERE IS_DEL=1 AND USE_TIME LIKE ?',['%'.$r['uuid'].'%'])[0]['cnt'];$refs['time-set:'.$r['uuid']]=(int)$q('SELECT COUNT(*) AS CNT FROM '.$p.'nutrition_set WHERE IS_DEL=1 AND USE_TIME=?',[$r['uuid']])[0]['cnt'];}
 $actor=$q('SELECT * FROM '.$p.'user WHERE ID=?',[$actorId]);if(count($actor)!==1||(int)$actor[0]['type']!==0||(int)$actor[0]['is_delete']!==0)throw new RuntimeException('Actor identity');
 return ['sets'=>$sets,'times'=>$times,'references'=>$refs,'actor_sha256'=>hash('sha256',json_encode($actor,JSON_UNESCAPED_UNICODE|JSON_PRESERVE_ZERO_FRACTION)),
 'sets_page_order'=>$q('SELECT * FROM '.$p.'nutrition_set WHERE IS_DEL=1 ORDER BY ORDER_NO ASC'.($GLOBALS['lane']==='B'?' LIMIT 10':' LIMIT 10')),
 'set_all_order'=>$q('SELECT NAME,UUID FROM '.$p.'nutrition_set WHERE IS_DEL=1 AND STATUS=1 ORDER BY ORDER_NO ASC'),
 'times_page_order'=>$q('SELECT * FROM '.$p.'nutrition_time WHERE DEL_FLAG=0 ORDER BY ORDER_NO ASC LIMIT 10')];
};
$guard=static function(array $now):void {
 $baseline=json_decode(file_get_contents(__DIR__.'/private-http-baseline.json'),true);
 if($now!==$baseline)throw new RuntimeException('Complete source drift');
};
