<?php

declare(strict_types=1);

require dirname(__DIR__) . '/app/bootstrap.php';

use App\Core\Database;

$result=[
    'schema'=>'DIAR4A_LIVE_ACCEPTANCE_V2',
    'captured_at'=>date(DATE_ATOM),
    'status'=>'FAIL','checks'=>[],
    'oracle_writes'=>'ACCEPTANCE_READ_ONLY',
];
try{
    $identity=Database::fetchOne('njch',"SELECT LOWER(SYS_CONTEXT('USERENV','DB_NAME')) db_name,UPPER(SYS_CONTEXT('USERENV','CURRENT_SCHEMA')) current_schema FROM dual");
    $result['checks']['control_database_oranjc']=strtolower((string)($identity['db_name']??''))==='oranjc'?'PASS':'FAIL';
    $result['checks']['control_schema_watany']=strtoupper((string)($identity['current_schema']??''))==='WATANY'?'PASS':'FAIL';
    $acl=Database::fetchOne('njch',"SELECT COUNT(*) c FROM USER_OBJECTS WHERE OBJECT_TYPE IN ('TABLE','VIEW') AND OBJECT_NAME IN ('SECURITY_USERS','DASH_ACL_ROLES','DASH_ACL_USER_ROLES','DASH_ACL_ROLE_OVERRIDES','DASH_ACL_USER_BRANCHES','DASH_ACL_AUDIT')");
    $result['checks']['existing_users_acl_reused']=(int)($acl['c']??0)===6?'PASS':'FAIL';
    $tables=Database::fetchOne('njch',"SELECT COUNT(*) c FROM USER_TABLES WHERE TABLE_NAME IN ('DASH_BRANCH_REGISTRY','DASH_SHARED_SETTINGS','DASH_SHARED_SET_AUDIT','DASH_DIAR_RULESETS','DASH_DIAR_RULE_VERSIONS','DASH_DIAR_RULE_PARAMS','DASH_DIAR_RULE_SCOPES','DASH_DIAR_DRY_RUNS','DASH_DIAR_DRY_RESULTS','DASH_DIAR_AUDIT')");
    $result['checks']['all_control_tables_ready']=(int)($tables['c']??0)===10?'PASS':'FAIL';
    $seq=Database::fetchOne('njch',"SELECT COUNT(*) c FROM USER_SEQUENCES WHERE SEQUENCE_NAME IN ('DASH_SHARED_SET_SEQ','DASH_SHARED_AUD_SEQ','DASH_DIAR_RULESET_SEQ','DASH_DIAR_RVER_SEQ','DASH_DIAR_PARAM_SEQ','DASH_DIAR_SCOPE_SEQ','DASH_DIAR_DRY_SEQ','DASH_DIAR_DRES_SEQ','DASH_DIAR_AUDIT_SEQ')");
    $result['checks']['all_sequences_ready']=(int)($seq['c']??0)===9?'PASS':'FAIL';
    $active=Database::fetchOne('njch',"SELECT COUNT(*) active_count,MAX(v.VERSION_ID) version_id,MAX(v.CONFIG_HASH) config_hash FROM DASH_DIAR_RULE_VERSIONS v JOIN DASH_DIAR_RULESETS r ON r.RULESET_ID=v.RULESET_ID WHERE r.RULESET_CODE='DIAR_CORE' AND v.STATUS_CODE='ACTIVE'");
    $result['checks']['one_active_rule_version']=(int)($active['active_count']??0)===1?'PASS':'FAIL';
    $params=Database::fetchOne('njch',"SELECT COUNT(*) c FROM DASH_DIAR_RULE_PARAMS WHERE VERSION_ID=:version_id",['version_id'=>(int)($active['version_id']??0)]);
    $result['checks']['initial_parameter_contract']=(int)($params['c']??0)>=38?'PASS':'FAIL';
    $scope=Database::fetchOne('njch',"SELECT SUM(CASE WHEN SCOPE_CODE IN ('NJCH','MNH') AND EFFECT_CODE='INCLUDE' THEN 1 ELSE 0 END) include_count,SUM(CASE WHEN SCOPE_CODE='JCKH' AND EFFECT_CODE='EXCLUDE' THEN 1 ELSE 0 END) deferred_count FROM DASH_DIAR_RULE_SCOPES WHERE VERSION_ID=:version_id",['version_id'=>(int)($active['version_id']??0)]);
    $result['checks']['branch_scope_contract']=((int)($scope['include_count']??0)===2&&(int)($scope['deferred_count']??0)===1)?'PASS':'FAIL';
    $boundary=Database::fetchOne('njch',"SELECT TEXT_VALUE text_value FROM DASH_SHARED_SETTINGS WHERE MODULE_CODE='CORE' AND SETTING_CODE='SOURCE_WRITE_BOUNDARY'");
    $result['checks']['source_write_boundary']=((string)($boundary['text_value']??'')==='OPERATIONAL_TABLES_READ_ONLY')?'PASS':'FAIL';
    $result['checks']['config_hash_present']=preg_match('/^[a-f0-9]{64}$/',(string)($active['config_hash']??''))?'PASS':'FAIL';
    $result['checks']['source_operational_writes']='NONE';
    $failed=[];foreach($result['checks'] as $k=>$v){if($v==='FAIL'){$failed[]=$k;}}
    $result['failed']=$failed;$result['status']=$failed===[]?'PASS':'FAIL';
    $result['active_version_id']=(int)($active['version_id']??0);
    $result['parameter_count']=(int)($params['c']??0);
}catch(Throwable $e){$result['error']=preg_replace('/\s+/',' ',(string)$e->getMessage());}
echo json_encode($result,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),PHP_EOL;
exit($result['status']==='PASS'?0:1);
