<?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=[];$html=(string)($data['html']??'');if($html===''||strlen($html)>12*1024*1024){http_response_code(413);echo json_encode(['ok'=>false,'error'=>'INVALID_PRINT_DOCUMENT']);exit;}$scope=(string)($auth['instance_id']==='server-local'?ua_install_instance_key():'linked:'.$auth['instance_id']);$result=ua_output_fleet_dispatch($conn,(string)($data['command_key']??''),$scope,$data);if(empty($result['ok']))http_response_code((int)($result['status']??502)?:502);echo !empty($result['body'])?$result['body']:json_encode(['ok'=>(bool)($result['ok']??false),'error'=>(string)($result['error']??'PRINT_FAILED')],JSON_UNESCAPED_UNICODE);
