<?php
declare(strict_types=1);require_once dirname(__DIR__).'/db_bootstrap.php';require_once dirname(__DIR__).'/output_fleet_lib.php';$conn=ensure_db(true);ua_output_fleet_migrate($conn);$tag='fleet-test-'.bin2hex(random_bytes(5));
function fleet_ok($v,$m){if(!$v)throw new RuntimeException('FAIL '.$m);echo "PASS {$m}\n";}
try{
 $stmt=$conn->prepare("INSERT INTO ua_output_nodes(node_id,instance_type,instance_name,machine_name,agent_url,agent_token,approval_status,is_enabled,last_seen_at) VALUES(?,'branch','Test branch','Test PC','http://127.0.0.1:1',?,'APPROVED',1,NOW())");$token=str_repeat('z',32);$stmt->bind_param('ss',$tag,$token);$stmt->execute();$stmt->close();
 $stmt=$conn->prepare("INSERT INTO ua_output_fleet_printers(node_id,printer_name,approval_status,share_mode,is_online,last_seen_at) VALUES(?,'QA USB Printer','APPROVED','ALL',1,NOW())");$stmt->bind_param('s',$tag);$stmt->execute();$printerId=(int)$stmt->insert_id;$stmt->close();
 $stmt=$conn->prepare("INSERT INTO ua_output_fleet_routes(consumer_scope,command_key,printer_id,paper_size,copies,is_enabled) VALUES('ALL','invoice_thermal',?,'80mm',1,1)");$stmt->bind_param('i',$printerId);$stmt->execute();$stmt->close();
 $resolved=ua_output_fleet_resolve($conn,'invoice_thermal','linked:any-consumer');fleet_ok((int)($resolved['printer_id']??0)===$printerId,'حل المسار المركزي لجميع النسخ');
 $dispatch=ua_output_fleet_dispatch($conn,'invoice_thermal','linked:any-consumer',['title'=>'QA','html'=>'<html><body>QA</body></html>']);fleet_ok(!empty($dispatch['ok'])&&($dispatch['delivery']??'')==='pull_queue','التحويل تلقائياً إلى القناة العكسية دون إعداد جدار ناري');
 $pulled=ua_output_fleet_pull($conn,$tag);fleet_ok(!empty($pulled['job'])&&$pulled['job']['printer']==='QA USB Printer','سحب المهمة من الجهاز المضيف فقط');
 $done=ua_output_fleet_complete($conn,$tag,(int)$pulled['job']['id'],true,'Succeeded');fleet_ok(!empty($done['ok']),'تثبيت نتيجة الطباعة في سجل التدقيق');
 $stmt=$conn->prepare("INSERT INTO ua_output_fleet_routes(consumer_scope,command_key,printer_id,paper_size,copies,is_enabled) VALUES('ALL','contract_print',?,'A4',1,1)");$stmt->bind_param('i',$printerId);$stmt->execute();$stmt->close();
 $file=ua_output_fleet_dispatch_file($conn,'contract_print','linked:any-consumer',['file_name'=>'qa.pdf','file_base64'=>base64_encode('%PDF-QA')]);fleet_ok(!empty($file['ok'])&&($file['delivery']??'')==='pull_queue','إرسال ملفات العقود عبر القناة العكسية');$filePull=ua_output_fleet_pull($conn,$tag);fleet_ok(($filePull['job']['type']??'')==='FILE'&&$filePull['job']['file_name']==='qa.pdf','استلام ملف العقد على عقدة الطباعة');ua_output_fleet_complete($conn,$tag,(int)$filePull['job']['id'],true,'Queued');
 $conn->query("UPDATE ua_output_fleet_printers SET share_mode='NONE' WHERE id={$printerId}");fleet_ok(!ua_output_fleet_resolve($conn,'invoice_thermal','linked:any-consumer'),'منع الطباعة فور إلغاء المشاركة');
 echo "ALL OUTPUT FLEET TESTS PASSED\n";
}finally{$esc=$conn->real_escape_string($tag);@$conn->query("DELETE FROM ua_output_fleet_jobs WHERE node_id='{$esc}'");@$conn->query("DELETE FROM ua_output_fleet_routes WHERE printer_id={$printerId}");@$conn->query("DELETE FROM ua_output_fleet_access WHERE printer_id={$printerId}");@$conn->query("DELETE FROM ua_output_fleet_printers WHERE node_id='{$esc}'");@$conn->query("DELETE FROM ua_output_nodes WHERE node_id='{$esc}'");}
