<?php
namespace app\cores\database { class Dm {} }
namespace {
function config($key){return $GLOBALS['database_type'];}
function getImgFullPath($path='',$exist=false){return $path?'media:'.$path:'';}
class StrictListRows {
 public $rows=[],$connection;
 public function inquiryCount(...$a){return count($this->rows);}
 public function inquiryNutrition(...$a){return $this->rows;}
 public function db($unused=false){return $this;}
 public function getConnection(){return $this->connection;}
}
$root=$argv[1];$variant=$argv[4]??'original';if(!in_array($variant,['original','strict-candidate'],true))throw new \RuntimeException('Variant');require $root.'/application/common/service/NutritionFloat82Write.php';require $argv[2]??$root.'/application/p/logic/Nutrition.php';require $argv[3]??$root.'/application/m/logic/Nutrition.php';
$p=(new \ReflectionClass(\app\p\logic\Nutrition::class))->newInstanceWithoutConstructor();$m=(new \ReflectionClass(\app\m\logic\Nutrition::class))->newInstanceWithoutConstructor();$model=new StrictListRows();foreach([$p,$m]as $logic){$prop=new \ReflectionProperty($logic,'model');$prop->setAccessible(true);$prop->setValue($logic,$model);}
$source=json_decode(file_get_contents($root.'/tests/Nutrition/Float82SourceOracles.json'),true);$checks=0;$failures=[];$computedControls=0;$affinityControls=0;$fields=['a.*','b.name as set_name'];
$check=static function($a,$b,$label)use(&$checks,&$failures){$checks++;if($a!==$b)$failures[]=$label;};
foreach(['mysql','app\\cores\\database\\Dm']as $actual){$model->connection=$actual==='mysql'?new \stdClass():new \app\cores\database\Dm();
 foreach(['mysql','app\\cores\\database\\Dm','\\app\\cores\\database\\Dm']as $global){$GLOBALS['database_type']=$global;
  foreach($source['cases']as $case){if(strpos($case['case_id'],'ORACLE-')!==0||$case['expected_stored_double_hex']===null)continue;$physical=unpack('e',hex2bin($case['expected_stored_double_hex']))[1];$getter=unpack('e',hex2bin($case['expected_direct_getter_hex']))[1];
   foreach(['power','carbohydrate','protein','fat','sodium']as $field){$row=['id'=>1,'uuid'=>'OWN-STRICT-LIST','name'=>'CDXOWN-STRICT','create_time'=>'2001-01-01 01:01:01','img_path'=>'','source_img_path'=>'','app_img'=>'','set_name'=>'CDXOWN-SET','use_weight'=>123.4567,$field=>$actual==='mysql'?$getter:$physical];$model->rows=[$row];$expected=$row;$expected[$field]=$getter;
    foreach(['p'=>$p,'m'=>$m]as $label=>$logic){$out=$logic->lists(['is_del'=>1],1,1,[],$fields)['data'];$check($out,['total'=>1,'rows'=>[$expected],'nutrition_sum'=>1],$label.'/'.$actual.'/'.$global.'/'.$case['case_id'].'/'.$field.' strict full DTO');$check($out['rows'][0]['use_weight'],123.4567,'M,D formatter excludes plain FLOAT');}
   }
  }
  $physical=12.350000381469727;$row=['id'=>1,'uuid'=>'CONTROL','name'=>'CONTROL','create_time'=>'2001-01-01 01:01:01','img_path'=>'','source_img_path'=>'','app_img'=>'','power'=>$physical,'use_weight'=>123.4567];$model->rows=[$row];
  foreach([['SUM(a.power) AS power'],['a.*'],['a.*','b.name as set_name','SUM(a.power) AS power'],['b.name as set_name','a.*'],['a.*','b.name as other_name']]as $dynamic){foreach(['p'=>$p,'m'=>$m]as $label=>$logic){$check($logic->lists(['is_del'=>1],1,1,[],$dynamic)['data'],['total'=>1,'rows'=>[$row],'nutrition_sum'=>1],$label.' exact projection rejects dynamic/computed');$computedControls++;}}
  foreach(['p'=>$p,'m'=>$m]as $label=>$logic){$want=$row;if($actual!=='mysql')$want['power']=12.35;$check($logic->lists(['is_del'=>1],1,1,[],$fields)['data'],['total'=>1,'rows'=>[$want],'nutrition_sum'=>1],$label.' model affinity overrides opposite global, Mysql raw promoted value unchanged');$affinityControls++;}
 }
}
$result=['status'=>$failures?'RED':'GREEN','variant'=>$variant,'assertions'=>$checks,'failures'=>$failures,'differences'=>count($failures),'computed_dynamic_controls'=>$computedControls,'actual_model_affinity_controls'=>$affinityControls,'HTTP'=>0,'DB_connections'=>0,'DML'=>0,'limitations'=>'Pure real logic with injected original PDO shapes; not candidate HTTP'];$file=__DIR__.'/strict-projection-'.$variant.'.json';$bytes=json_encode($result,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);if(is_file($file)&&file_get_contents($file)!==$bytes)throw new \RuntimeException('Existing evidence differs: use the recorded original/runtime or strict candidate source; never overwrite');if(!is_file($file))file_put_contents($file,$bytes);unset($result['failures']);echo json_encode($result),"\n";exit($failures?2:0);
}
