<?php
// Local verification only: exercise the actual orderPrint exception handlers without DB or network writes.
require '/workspace/zhct/zhctproject/store/env.php';
define('APP_PATH', '/workspace/zhct/zhctproject/store/application/');
require '/workspace/zhct/zhctproject/store/thinkphp/base.php';
require APP_PATH.'common.php';
class ReceiptStabilityExceptionProbe extends \app\p\logic\MealOrder {
    public $mode;
    public $released = false;
    public $row = ['id'=>991,'order_no'=>'QA-STABILITY','restaurant_id'=>1,'source'=>2,'equipment_code'=>'QA',
        'pay_type'=>20,'pay_status'=>20,'order_status'=>30,'refund_status'=>0,'total_price'=>'3.00','pay_price'=>'3.00','print_status'=>1];
    public function __construct(string $mode) { $this->mode=$mode; }
    protected function loadReceiptOrder(int $orderId): array { return $this->row; }
    protected function acquireReceiptPrintLock(int $orderId, bool $manualReprint): array { return ['allowed'=>true]; }
    protected function releaseReceiptPrintLock(array $lock): void { $this->released=true; }
    protected function markOrderPrinting(int $orderId): bool { $this->row['print_status']=2; return true; }
    protected function markOrderPrintPending(int $orderId): void { $this->row['print_status']=1; }
    protected function markOrderPrintNone(int $orderId): void { $this->row['print_status']=0; }
    protected function isAutomaticReceiptAllowed(array $order): bool {
        if ($this->mode==='policy-query-exception') throw new \RuntimeException('QA policy query exception');
        return true;
    }
    protected function prepareReceiptPrintJob(array $order): array {
        return ['ready'=>true,'printer_sns'=>['QA-NO-NETWORK'],'template'=>1,'copies'=>1,'content'=>'QA','mode'=>'probe'];
    }
    protected function printReceiptToPlatform(string $sn, string $content, int $copies): array {
        throw new \RuntimeException('QA platform exception');
    }
}
foreach (['policy-query-exception','platform-exception'] as $mode) {
    $probe=new ReceiptStabilityExceptionProbe($mode);
    $before=$probe->row;
    if ($probe->orderPrint(991)!==false || !$probe->released || $probe->row!==$before) {
        throw new \RuntimeException('Failure isolation probe failed: '.$mode);
    }
    echo $mode.': PASS (pending restored, transaction fields unchanged, lock released)'.PHP_EOL;
}
