<?php
declare(strict_types=1);
// Test-only, loopback, GET-only entry. Never install this in a deployed application.
$path = parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH);
$allowed = ['/api/gift.Log/detail', '/api/gift.Log/list'];
if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'GET'
    || !in_array($path, array_merge($allowed, ['/__poc_boundary']), true)) {
    http_response_code(403); echo '{"pilot_denied":true}'; return;
}
$root = (string)getenv('PILOT_TARGET_ROOT');
$runtime = (string)getenv('PILOT_RUNTIME');
// TP6 multi-app uses entry basename to infer application; preserve normal index entry.
$_SERVER['SCRIPT_FILENAME'] = $root . '/public/index.php';
$_SERVER['SCRIPT_NAME'] = '/index.php'; $_SERVER['PHP_SELF'] = '/index.php';
define('TP_ENV', 'poc'); define('BUSINESS', 'aizhct'); define('ENTERPRISE_NO', '10001');
$loader = require $root . '/vendor/autoload.php';
if (is_file(__DIR__ . '/overlay/user/GiftLog.php')) {
    $loader->addPsr4('app\\api\\model\\user\\', __DIR__ . '/overlay/user/', true);
}
require $root . '/external_stubs.php';
if ($path === '/__poc_boundary') {
    header('Content-Type: application/json');
    echo json_encode(['external_stubs' => class_exists(\app\common\Serve::class, false)
        && class_exists(\app\common\UserApi::class, false), 'readonly_get_allowlist' => true]);
    return;
}
$scenario = $_SERVER['HTTP_X_PILOT_SCENARIO'] ?? '';
if (!preg_match('/^PILOT_AI_[A-Z0-9_]{1,60}$/D', $scenario)) {
    http_response_code(403); echo '{"pilot_denied":true}'; return;
}
require is_file(__DIR__ . '/SqlCoverageRecorder.php') ? __DIR__ . '/SqlCoverageRecorder.php'
    : dirname(__DIR__, 3) . '/work/2026-09-28-smart-canteen-dm8-poc/db-coverage-pilot/SqlCoverageRecorder.php';
try {
$app = new \think\App($root);
$app->setEnvName('poc'); $app->setRuntimePath($runtime . '/runtime/'); $app->initialize();
$config = $app->config->get('database');
foreach ($config['connections'] as &$connection) {
    $connection['trigger_sql'] = true;
    // No application SQL logging: only the redacting structural listener below.
    $connection['debug'] = false;
} unset($connection);
$app->config->set($config, 'database');
$app->db->setConfig($app->config);
$app->config->set(['default' => 'file', 'stores' => ['file' => [
    'type' => 'File', 'path' => $runtime . '/cache/', 'prefix' => '', 'expire' => 0,
]]], 'cache');
$actualSource = dirname((new ReflectionClass(\app\api\controller\Enum::class))->getFileName(), 4);
$recorder = new SqlCoverageRecorder($runtime . '/ai_queries.jsonl', $actualSource, $scenario);
$classification = ['connection_events'=>0,'metadata_events'=>0,'unclassified_events'=>0];
$app->db->listen(static function($sql,$elapsed=null)use($recorder,&$classification):void{
    if(strpos((string)$sql,'CONNECT:')===0){++$classification['connection_events'];return;}
    if(preg_match('/^\s*(SHOW|DESCRIBE|EXPLAIN|SP_TABLEDEF)\b/i',(string)$sql)
        || preg_match('/^\s*CALL\s+SP_TABLEDEF\s*\(/i',(string)$sql)
        || preg_match('/^\s*SELECT\b.*\bFROM\s+USER_(TAB_COLUMNS|CONSTRAINTS|CONS_COLUMNS)\b/is',(string)$sql))++$classification['metadata_events'];
    elseif(SqlCoverageRecorder::fingerprint((string)$sql)['operation']==='other')++$classification['unclassified_events'];
    $recorder->observe($sql,$elapsed);
});
register_shutdown_function([$recorder, 'finish']);
register_shutdown_function(static function()use($runtime,$scenario,&$classification):void{
    file_put_contents($runtime.'/ai_queries.jsonl',json_encode(['kind'=>'listener_classification',
        'scenario'=>$scenario]+$classification)."\n",FILE_APPEND|LOCK_EX);
});
$http = $app->http;
$response = $http->run(); $response->send(); $http->end($response);
} catch (Throwable $error) {
    http_response_code(500); header('Content-Type: application/json');
    echo json_encode(['pilot_bootstrap_error'=>get_class($error),
        'file'=>basename($error->getFile()),'line'=>$error->getLine()]);
}
