<?php
declare(strict_types=1);define('CDX_CATEGORY_CONTEXT',true);require __DIR__.'/preflight.php';
$original=json_decode(file_get_contents(__DIR__.'/private-baseline.json'),true);$crud=json_decode(file_get_contents(__DIR__.'/private-crud.json'),true);$id=$crud['retained']['id'];$marker=$crud['retained']['marker'];$row=$q('SELECT * FROM '.$p.'dishes_custom_category WHERE ID=?',[$id])[0];$refs=(int)$q('SELECT COUNT(*) AS N FROM '.$p.'dishes WHERE CUSTOM_CATEGORY_ID=?',[$id])[0]['n'];
$table=$p.'dishes_custom_category';$foreign=$lane==='B'?$q('SELECT TABLE_NAME,CONSTRAINT_NAME,REFERENCED_TABLE_NAME FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA=DATABASE() AND REFERENCED_TABLE_NAME IS NOT NULL AND (TABLE_NAME=? OR REFERENCED_TABLE_NAME=?) ORDER BY TABLE_NAME,CONSTRAINT_NAME',[$table,$table]):$q("SELECT C.TABLE_NAME,C.CONSTRAINT_NAME,P.TABLE_NAME AS REFERENCED_TABLE_NAME FROM USER_CONSTRAINTS C LEFT JOIN USER_CONSTRAINTS P ON C.R_CONSTRAINT_NAME=P.CONSTRAINT_NAME WHERE C.CONSTRAINT_TYPE='R' AND (C.TABLE_NAME=? OR P.TABLE_NAME=?) ORDER BY C.TABLE_NAME,C.CONSTRAINT_NAME",[strtoupper($table),strtoupper($table)]);
// Persisted metadata JSON turns PDO integer-valued floats into ints. Compare identical
// JSON metadata values, not those in-memory numeric types. Business/source/wire stay strict.
$metadataEqual=static function(array $a,array $b):bool{return json_encode($a)===json_encode($b);};
$checks=['natural_category_all_original_columns_unchanged'=>$hashRows('SELECT * FROM '.$table.' WHERE ID<>? ORDER BY ID',[$id])===$original['category_source'],'dishes_all_rows_classification_projection_unchanged'=>$report['dishes_source']===$original['dishes_source'],'columns_unchanged'=>$metadataEqual($report['columns'],$original['columns']),'indexes_unchanged'=>$metadataEqual($report['indexes'],$original['indexes']),'constraints_unchanged'=>$report['constraints']===$original['constraints'],'triggers_unchanged'=>$report['triggers']===$original['triggers'],'no_foreign_key_dependency'=>$foreign===[],'exact_owned_marker'=>strpos($row['name'],$marker)===0&&$row['create_by']==='CDXCAT10ADMIN','owned_disabled_soft_deleted'=>(int)$row['status']===2&&(int)$row['is_delete']===1,'owned_zero_dish_references'=>$refs===0,'owned_code_null'=>$row['code']===null,'owned_raw_seconds_zero_fraction'=>preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.0{1,6})?$/D',$row['create_time'])===1&&preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.0{1,6})?$/D',$row['update_time'])===1];
$out=['lane'=>$lane,'independent_new_connection'=>true,'business_dml'=>0,'ddl'=>0,'checks'=>$checks,'foreign_keys'=>$foreign,'selected_owned_full_column_types'=>array_map('gettype',$row),'selected_owned_row_sha256'=>hash('sha256',serialize($row)),'retained'=>['id'=>$id,'marker'=>$marker,'status'=>(int)$row['status'],'is_delete'=>(int)$row['is_delete'],'dish_references'=>$refs],'source_rows'=>$original['category_source']['rows'],'dishes_projection_rows'=>$original['dishes_source']['rows'],'business_source_sha256'=>hash_file('sha256',__DIR__.'/source/application/p/logic/DishCustomCategory.php'),'all_passed'=>!in_array(false,$checks,true)];if(!$checks['columns_unchanged']||!$checks['indexes_unchanged'])$out['metadata_diagnostic']=['columns_before'=>$original['columns'],'columns_after'=>$report['columns'],'indexes_before'=>$original['indexes'],'indexes_after'=>$report['indexes']];file_put_contents(__DIR__.'/private-audit.json',json_encode($out,JSON_PRETTY_PRINT));chmod(__DIR__.'/private-audit.json',0600);echo json_encode($out),"\n";exit($out['all_passed']?0:2);
