<?php

declare(strict_types=1);

namespace think;

$root = (string)getenv('POC_TARGET_ROOT');
define('TP_ENV', 'poc');
define('BUSINESS', 'aizhct');
define('ENTERPRISE_NO', $_SERVER['HTTP_ENTERPRISE_NO'] ?? '10001');
require $root . '/vendor/autoload.php';
if (getenv('POC_EXTERNAL_STUBS') === '1') {
    require $root . '/external_stubs.php';
}
if (($_SERVER['REQUEST_URI'] ?? '') === '/__poc_boundary') {
    header('Content-Type: application/json');
    echo json_encode(['external_stubs' => getenv('POC_EXTERNAL_STUBS') === '1'
        && class_exists(\app\common\Serve::class, false)
        && class_exists(\app\common\UserApi::class, false)]);
    return;
}
$app = (new App($root))->setEnvName('poc');
$app->initialize();
$http = $app->http;
$response = $http->run();
$response->send();
$http->end($response);
if (strpos((string)($_SERVER['REQUEST_URI'] ?? ''), '/api/mealCheckout/submit') !== false) {
    foreach (['mysql_zhct' => 'store', 'mysql' => 'ai'] as $connection => $label) {
        try {
            $sql = (string)\think\facade\Db::connect($connection)->getLastSql();
            $sql = preg_replace("/'(?:''|[^'])*'/", "'?'", $sql);
            $sql = preg_replace('/[0-9]{7,}/', '?', (string)$sql);
            error_log('CDXPARMOB_SQL_PROBE_' . $label . ' ' . substr((string)$sql, 0, 2500));
        } catch (\Throwable $error) {
            error_log('CDXPARMOB_SQL_PROBE_' . $label . ' unavailable: '
                . get_class($error));
        }
    }
}
