<?php
declare(strict_types=1);
// New loopback-only test worker; target application code/config are unchanged.
$root = (string)getenv('PILOT_TARGET_ROOT');
$lane = (string)getenv('PILOT_LANE');
$expected = $lane === 'B' ? '/tmp/zhct-http-store-mysql.dQTTBP'
    : '/tmp/zhct-dm-poc-20260928.fcpiiN/store';
if (!in_array($lane, ['B', 'C'], true) || $root !== $expected || is_link($root)) {
    http_response_code(503); exit;
}
$path = parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH);
if ($path === '/__poc_boundary') { require $root . '/public/index.php'; return; }
$allowed = ['/p/dateMenu/getRecipes', '/p/mealOrder/detail',
    '/api/device/recipe/addDishes', '/api/device/recipe/copy', '/api/device/recipe/deleteDishes'];
if (!in_array($path, $allowed, true) || ($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
    http_response_code(403); exit;
}
require $root . '/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') { http_response_code(503); exit; }
require $root . '/external_stubs.php';
define('APP_PATH', $root . '/application/');
require $root . '/thinkphp/base.php';
require __DIR__ . '/SqlCoverageRecorder.php';
$scenario = 'PILOT_' . strtoupper(str_replace(['/', '.'], '_', ltrim($path, '/')));
if (($_SERVER['HTTP_X_DB_PILOT_CASE'] ?? '') === 'ROLLBACK_SECOND_SLOT') {
    $scenario .= '_ROLLBACK_SECOND_SLOT';
}
$observer = new SqlCoverageRecorder(__DIR__ . '/events-' . $lane . '.jsonl', $root, $scenario);
\think\App::initCommon();
\think\Request::instance()->pathinfo();
$database = require $root . '/application/database.php';
$database['debug'] = true;
$database['sql_explain'] = false;
\think\Config::set($database, 'database');
\think\Db::clear();
\think\Db::listen([$observer, 'observe']);
register_shutdown_function([$observer, 'finish']);
\think\App::run()->send();
