<?php
require __DIR__ . '/../env.php';
if (!function_exists('json')) {
    function json($data = [], $code = 200, array $header = [], $options = [])
    {
        return \think\Response::create($data, 'json', $code, $header, $options);
    }
}
if (getenv('POC_EXTERNAL_STUBS') === '1') {
    require __DIR__ . '/../external_stubs.php';
}
if (($_SERVER['REQUEST_URI'] ?? '') === '/__poc_boundary') {
    header('Content-Type: application/json');
    $pocConfig = require __DIR__ . '/../application/config/poc/config.php';
    $redisHost = (string)getenv('POC_REDIS_HOST');
    $redisPort = (int)getenv('POC_REDIS_PORT');
    $isolatedRedis = (strpos($redisHost, 'codex_dm8_redis_') === 0 && $redisPort === 6379)
        || ($redisHost === '127.0.0.1' && $redisPort === 16379);
    echo json_encode(['external_stubs' => (getenv('POC_REAL_QUEUE') === '1'
            || class_exists(\app\common\Serve::class, false))
        && class_exists(\app\api\service\xpyun\Xpyun::class, false)
        && class_exists(\app\common\service\wanyisheng\Dispatcher::class, false)
        && class_exists(\app\common\QwRobot::class, false),
        'real_queue' => getenv('POC_REAL_QUEUE') === '1',
        'queue_push_fault' => getenv('POC_FAIL_QUEUE_PUSH') === '1',
        'ai_commit_fault' => getenv('POC_FAIL_AI_COMMIT') === '1',
        'mysql_utf8mb4' => getenv('POC_MYSQL_UTF8MB4') === '1',
        'redis_isolated' => $isolatedRedis
            && ($pocConfig['redis']['host'] ?? '') === $redisHost
            && (int)($pocConfig['redis']['port'] ?? 0) === $redisPort,
        'account_fault' => getenv('POC_FAIL_ACCOUNT_SETTLEMENT') === '1'
            && class_exists(\app\common\service\account\AccountSettlementService::class, false),
        'refund_fault' => getenv('POC_FAIL_ACCOUNT_REFUND') === '1'
            && class_exists(\app\common\service\account\AccountSettlementService::class, false),
        'store_order_fault' => getenv('POC_FAIL_STORE_ORDER_INSERT') === '1'
            && class_exists(\app\api\model\meal\Order::class, false)]);
    return;
}
define('APP_PATH', __DIR__ . '/../application/');
require __DIR__ . '/../thinkphp/base.php';
if (getenv('POC_EXTERNAL_STUBS') === '1' && !empty($_SERVER['HTTP_X_POC_ORDER_NO'])) {
    \app\common\service\Order::forceNextOrderNo((string)$_SERVER['HTTP_X_POC_ORDER_NO']);
}
\think\App::run()->send();
