<?php

declare(strict_types=1);
$options=getopt('', ['target::']);
$root=rtrim((string)($options['target']??'C:\\xampp\\htdocs\\ddb-new'),"\\/");
$bootstrap=$root.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'bootstrap.php';
if(!is_file($bootstrap)){fwrite(STDERR,"TARGET_BOOTSTRAP_NOT_FOUND|$bootstrap\n");exit(10);}require $bootstrap;
use App\Core\Database;
try{
 $id=Database::fetchOne('njch',"SELECT UPPER(SYS_CONTEXT('USERENV','DB_NAME')) DB_NAME,UPPER(SYS_CONTEXT('USERENV','CURRENT_SCHEMA')) CURRENT_SCHEMA FROM dual");
 if(strtoupper((string)($id['db_name']??''))!=='ORANJC'||strtoupper((string)($id['current_schema']??''))!=='WATANY')throw new RuntimeException('IDENTITY');
 $objects=Database::fetchOne('njch',"SELECT (SELECT COUNT(*) FROM USER_TABLES WHERE TABLE_NAME IN ('DASH_DIAR_CASE_INFO_REQUESTS','DASH_DIAR_CASE_FINDINGS','DASH_DIAR_CASE_APPROVALS','DASH_DIAR_CORRECTIVE_ACTIONS')) TABLES_COUNT,(SELECT COUNT(*) FROM USER_SEQUENCES WHERE SEQUENCE_NAME IN ('DASH_DIAR_INFO_REQ_SEQ','DASH_DIAR_FINDING_SEQ','DASH_DIAR_APPROVAL_SEQ','DASH_DIAR_CORR_ACTION_SEQ')) SEQ_COUNT,(SELECT COUNT(*) FROM USER_TAB_COLUMNS WHERE TABLE_NAME='DASH_DIAR_CASES' AND COLUMN_NAME IN ('WORKFLOW_STAGE_CODE','SUPERVISOR_USERNAME','MANAGEMENT_APPROVER','REOPEN_COUNT','LAST_ACTION_AT')) COL_COUNT FROM dual");
 if((int)($objects['tables_count']??0)!==4||(int)($objects['seq_count']??0)!==4||(int)($objects['col_count']??0)!==5)throw new RuntimeException('OBJECT_COUNTS');
 $integrity=Database::fetchOne('njch',"SELECT COUNT(*) TOTAL_CASES,SUM(CASE WHEN WORKFLOW_STAGE_CODE IS NULL THEN 1 ELSE 0 END) NULL_STAGES,SUM(CASE WHEN WORKFLOW_STAGE_CODE NOT IN ('NEW','ASSIGNED','UNDER_REVIEW','INFORMATION_REQUIRED','FINDING_PROPOSED','SUPERVISOR_REVIEW','MANAGEMENT_APPROVAL','CORRECTIVE_ACTION','CLOSED') THEN 1 ELSE 0 END) BAD_STAGES,SUM(CASE WHEN STORE_CODE IN (56,57) THEN 1 ELSE 0 END) LEGACY_EXCLUDED_CASES,SUM(CASE WHEN STORE_CODE IS NULL OR STORE_CODE NOT IN (56,57) THEN 1 ELSE 0 END) GOVERNED_CASES FROM DASH_DIAR_CASES");
 $total=(int)($integrity['total_cases']??0);$nullStages=(int)($integrity['null_stages']??-1);$badStages=(int)($integrity['bad_stages']??-1);$excluded=(int)($integrity['legacy_excluded_cases']??0);$governed=(int)($integrity['governed_cases']??0);
 if($total<1||$nullStages!==0||$badStages!==0||$governed<1)throw new RuntimeException('CASE_INTEGRITY|TOTAL='.$total.'|NULL_STAGES='.$nullStages.'|BAD_STAGES='.$badStages.'|GOVERNED='.$governed.'|EXCLUDED='.$excluded);
 $repoPath=$root.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'Repositories'.DIRECTORY_SEPARATOR.'DiarInvestigationRepository.php';
 $repo=is_file($repoPath)?(string)file_get_contents($repoPath):'';
 if(strpos($repo,'STORE_CODE IS NULL')===false||strpos($repo,'STORE_CODE NOT IN (56,57)')===false)throw new RuntimeException('EXCLUDED_STORE_SCOPE_FILTER_MISSING');
 $orphans=Database::fetchOne('njch',"SELECT (SELECT COUNT(*) FROM DASH_DIAR_CASE_INFO_REQUESTS x WHERE NOT EXISTS (SELECT 1 FROM DASH_DIAR_CASES c WHERE c.CASE_ID=x.CASE_ID)) INFO_ORPHANS,(SELECT COUNT(*) FROM DASH_DIAR_CASE_FINDINGS x WHERE NOT EXISTS (SELECT 1 FROM DASH_DIAR_CASES c WHERE c.CASE_ID=x.CASE_ID)) FIND_ORPHANS,(SELECT COUNT(*) FROM DASH_DIAR_CASE_APPROVALS x WHERE NOT EXISTS (SELECT 1 FROM DASH_DIAR_CASES c WHERE c.CASE_ID=x.CASE_ID)) APPR_ORPHANS,(SELECT COUNT(*) FROM DASH_DIAR_CORRECTIVE_ACTIONS x WHERE NOT EXISTS (SELECT 1 FROM DASH_DIAR_CASES c WHERE c.CASE_ID=x.CASE_ID)) CORR_ORPHANS FROM dual");
 foreach($orphans as $v)if((int)$v!==0)throw new RuntimeException('ORPHAN_ROWS');
 echo 'DIAR_6B_4_R1_1_LIVE_ACCEPTANCE|PASS|DB=ORANJC|SCHEMA=WATANY|TABLES=4|SEQUENCES=4|CASE_COLUMNS=5|RAW_TOTAL_CASES='.$total.'|GOVERNED_CASES='.$governed.'|LEGACY_EXCLUDED_CASES='.$excluded.'|NULL_STAGES=0|BAD_STAGES=0|VISIBLE_BAD_STORES=0|ORPHANS=0|SOURCE_MM_WRITES=NONE|DB_LINK=NONE'.PHP_EOL;exit(0);
}catch(Throwable $e){fwrite(STDERR,'DIAR_6B_4_R1_1_LIVE_ACCEPTANCE|FAIL|ERROR='.$e->getMessage().PHP_EOL);exit(22);}
