<?php
$root=dirname(__DIR__);$checks=[];
function c(&$a,$name,$ok){$a[]=['name'=>$name,'status'=>$ok?'PASS':'FAIL'];}
$v=json_decode(file_get_contents($root.'/DDB_VERSION.json'),true);
c($checks,'version',($v['version']??'')==='11.0.11.59');
c($checks,'phase',($v['phase']??'')==='H11.0-P11.1-R45-P2.2');
$r=file_get_contents($root.'/app/Repositories/RevenueRepository.php');
c($checks,'optimized method',strpos($r,'monthlyRevenueBundleYearMonthRange')!==false);
c($checks,'direct YEARID predicate',strpos($r,'WHERE YEARID IN (:year, :compare_year)')!==false);
c($checks,'direct month predicate',strpos($r,'VISIT_MONTH BETWEEN :start_month AND :end_month')!==false);
c($checks,'group direct month',strpos($r,'GROUP BY YEARID, VISIT_MONTH')!==false);
c($checks,'plan profiler',is_file($root.'/tools/revenue-sql-plan-profile.php'));
c($checks,'dbms xplan',strpos(file_get_contents($root.'/tools/revenue-sql-plan-profile.php'),'DBMS_XPLAN.DISPLAY')!==false);
c($checks,'numeric parity',strpos(file_get_contents($root.'/tools/revenue-sql-plan-profile.php'),'numeric_parity')!==false);
c($checks,'no DML',!preg_match('/\b(insert|update|delete|merge|commit|rollback)\b/i',file_get_contents($root.'/tools/revenue-sql-plan-profile.php')));
$fail=array_filter($checks,fn($x)=>$x['status']==='FAIL');
echo json_encode(['phase'=>'R45-P2.2','checks'=>$checks,'pass'=>count($checks)-count($fail),'fail'=>count($fail)],JSON_PRETTY_PRINT).PHP_EOL;exit($fail?1:0);
