<?php
declare(strict_types=1);ob_start();require_once dirname(__DIR__).'/db_bootstrap.php';require_once dirname(__DIR__).'/output_fleet_lib.php';if(ob_get_length())ob_clean();header('Content-Type: application/json; charset=utf-8');$conn=ensure_db(true);$raw=(string)file_get_contents('php://input');$auth=ua_output_fleet_authenticate($conn,$raw);if(empty($auth['ok'])){http_response_code(401);echo json_encode($auth);exit;}$data=json_decode($raw,true);if(!is_array($data))$data=[];$node=substr((string)($data['node_id']??''),0,100);$nodeEsc=$conn->real_escape_string($node);$rs=$conn->query("SELECT node_id FROM ua_output_nodes WHERE node_id='{$nodeEsc}' AND approval_status='APPROVED' AND is_enabled=1 LIMIT 1");if(!$rs||!$rs->num_rows){http_response_code(403);echo json_encode(['ok'=>false,'error'=>'NODE_NOT_APPROVED']);exit;}if(isset($data['complete_job_id']))$result=ua_output_fleet_complete($conn,$node,(int)$data['complete_job_id'],!empty($data['success']),(string)($data['message']??''));else $result=ua_output_fleet_pull($conn,$node);echo json_encode($result,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
