<?php

namespace app\common {
    if (getenv('POC_REAL_QUEUE') !== '1') {
        class Serve
        {
            public static function push(string $action, array $data = [], int $delay = 0, bool $notify = true, ?string $jobQueueName = null): bool
            {
                if (getenv('POC_FAIL_QUEUE_PUSH') === '1'
                    && in_array($action, ['consumeOrderPay', 'syncMealOrder'], true)) {
                    $redis = new \Redis();
                    $redis->connect((string)getenv('POC_REDIS_HOST'), (int)getenv('POC_REDIS_PORT'), 1.0);
                    if ($action === 'consumeOrderPay') {
                        $redis->setex('poc:queue_push_fault:' . (string)($data['order_no'] ?? ''), 300, '1');
                    } else {
                        $redis->setex('poc:queue_sync_fault:' . (string)($data['order_id'] ?? ''), 300, '1');
                    }
                    throw new \RuntimeException('POC_QUEUE_PUSH_FAIL_' . $action);
                }
                return true;
            }
        }
    }

    class QwRobot
    {
        public static function pushMsg($params, $robotType = '', bool $useQueue = true, ?string $jobQueueName = null): bool
        {
            return true;
        }

        public static function pushMsgFormat($msg, $title = '消息提醒', $robotType = 'msg', bool $useQueue = true, ?string $jobQueueName = null): bool
        {
            return true;
        }

        public static function getRobot(): array
        {
            return [];
        }
    }
}

namespace app\api\service\xpyun {
    class Xpyun
    {
        public function printReceipt(string $sn, string $content, int $copies = 1, array $options = []): array
        {
            return ['code' => 0, 'data' => 'POC_PRINT_STUB'];
        }
    }
}

namespace app\common\service\wanyisheng {
    class Dispatcher
    {
        public static function order(string $orderNo, $sourceId = ''): void {}
        public static function refund(string $refundNo, $sourceId = ''): void {}
        public static function healthProfile(int $aiUserId, string $sessionId): void
        {
            $redis = new \Redis();
            $redis->connect((string)getenv('POC_REDIS_HOST'), (int)getenv('POC_REDIS_PORT'), 1.0);
            $redis->setex('poc:mocatch_health_profile:' . $sessionId, 300, '1');
        }
    }
}

namespace app\common\service\account {
    if (getenv('POC_FAIL_ACCOUNT_SETTLEMENT') === '1' || getenv('POC_FAIL_ACCOUNT_REFUND') === '1') {
        class AccountSettlementService
        {
            public function getDeductionOrder(): array
            {
                return ['subsidy', 'cash', 'gift'];
            }

            public function deduct($accountDb, array $params): array
            {
                if (getenv('POC_FAIL_ACCOUNT_SETTLEMENT') !== '1') {
                    throw new \RuntimeException('POC_UNEXPECTED_ACCOUNT_DEDUCT');
                }
                $redis = new \Redis();
                $redis->connect((string)getenv('POC_REDIS_HOST'), (int)getenv('POC_REDIS_PORT'), 1.0);
                $redis->setex('poc:account_fault:' . (string)($params['order_no'] ?? ''), 300, '1');
                throw new \RuntimeException('POC_FAULT_AI_SETTLEMENT');
            }

            public function refund($accountDb, array $params): array
            {
                if (getenv('POC_FAIL_ACCOUNT_REFUND') !== '1') {
                    throw new \RuntimeException('POC_UNEXPECTED_ACCOUNT_REFUND');
                }
                $redis = new \Redis();
                $redis->connect((string)getenv('POC_REDIS_HOST'), (int)getenv('POC_REDIS_PORT'), 1.0);
                $redis->setex('poc:refund_fault:' . (string)($params['refund_no'] ?? ''), 300, '1');
                throw new \RuntimeException('POC_FAULT_AI_REFUND');
            }
        }
    }
}

namespace app\api\model\meal {
    if (getenv('POC_FAIL_STORE_ORDER_INSERT') === '1') {
        class Order
        {
            public static function detail($where)
            {
                return null;
            }

            public function save($data)
            {
                $redis = new \Redis();
                $redis->connect((string)getenv('POC_REDIS_HOST'), (int)getenv('POC_REDIS_PORT'), 1.0);
                $redis->setex('poc:store_order_fault:' . (string)($data['order_no'] ?? ''), 300, '1');
                throw new \RuntimeException('POC_FAULT_STORE_ORDER_INSERT');
            }
        }
    }
}
