<?php
require __DIR__.'/common.php';$expected=json_decode(file_get_contents(__DIR__.'/private/lease-expected-'.$lane.'.json'),true)['source_after'];$current=$source();
if($current!==$expected)throw new RuntimeException('Full prior final ownscope lease drift');
$r=['lane'=>$lane,'source_lease_equal'=>true,'source'=>$current,'HTTP'=>0,'DML'=>0,'DDL'=>0];
$target=array_values(array_filter($current['nutrition'],static function($x){return $x['name']==='CDXNF11F5-candidate-product';}));if(count($target)!==1||$target[0]['use_weight']!==1.25)throw new RuntimeException('Exact candidate row and previous weight');$r['target']=$target[0];
$r['metadata']=$lane==='B'?$q('SELECT COLUMN_NAME,COLUMN_TYPE,IS_NULLABLE,COLUMN_DEFAULT FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=? ORDER BY ORDINAL_POSITION',['ydy_nutrition']):$q('SELECT COLUMN_NAME,DATA_TYPE,DATA_PRECISION,DATA_SCALE,NULLABLE,DATA_DEFAULT FROM USER_TAB_COLUMNS WHERE TABLE_NAME=? ORDER BY COLUMN_ID',['YDY_NUTRITION']);
$r['physical']=$q($lane==='B'?'SELECT use_weight,use_weight+0e0 AS computed_weight FROM ydy_nutrition WHERE uuid=?':'SELECT use_weight,CAST(use_weight AS DOUBLE) AS computed_weight FROM ydy_nutrition WHERE uuid=?',[$target[0]['uuid']]);$r['native']=[];foreach($r['physical'][0]as $k=>$v)$r['native'][$k]=['value'=>$v,'type'=>gettype($v),'bits64'=>is_float($v)?bin2hex(pack('d',$v)):null];
$keys=['uuid','name','set_uuid','use_weight','use_number','use_weight_unit','use_number_unit','use_time','use_method','employ','is_nutrition_station','supplier','nutrition_category_uuid'];$r['proposed_params']=[];foreach($keys as $k)$r['proposed_params'][$k]=$target[0][$k];$r['proposed_params']['use_weight']='12.345';
foreach([18921,18922]as $port){$s=@fsockopen('127.0.0.1',$port,$n,$e,0.5);$r['ports'][(string)$port]=$s!==false;if($s)fclose($s);}if(array_filter($r['ports']))throw new RuntimeException('Port collision');
$r['oracle']=$GLOBALS['NF_ORACLE'];$save('preflight-'.$lane.'.json',$r);echo json_encode(['lane'=>$lane,'full_ownscope_lease'=>true,'own_target_id'=>$target[0]['id'],'SELECT'=>count($GLOBALS['NF_ORACLE']),'ports'=>$r['ports'],'DML'=>0,'DDL'=>0,'HTTP'=>0]),"\n";
