<?php
declare(strict_types=1);

// Isolated HTTP info-log timing only. Never print body, params, SQL or messages.
$target = (string)getenv('CDX_LOG_TARGET');
$paths = [
    'A' => '/tmp/zhct-http-ai-macos.zjaU8h/runtime/api/log/20261002_info.log',
    'B' => '/tmp/zhct-http-ai-macos.x2CCZX/runtime/api/log/20261002_info.log',
    'C' => '/tmp/zhct-ai-http-20260929.3XDj3w/runtime/api/log/20261002_info.log',
];
if (!isset($paths[$target]) || !is_file($paths[$target])) {
    echo json_encode(['status' => 'log-unavailable', 'target' => $target]), PHP_EOL;
    exit(2);
}
$result = [];
foreach (file($paths[$target], FILE_IGNORE_NEW_LINES) ?: [] as $line) {
    $entry = json_decode($line, true);
    if (!is_array($entry)) continue;
    $url = (string)($entry['url'] ?? '');
    if (strtolower((string)parse_url($url, PHP_URL_PATH))
        !== '/api/dishes.dishes/list') continue;
    $runtime = $entry['runtime'] ?? null;
    if (!is_numeric($runtime)) continue;
    $result[] = [
        'time' => (string)($entry['time'] ?? ''),
        'method' => (string)($entry['request_method'] ?? ''),
        'runtime_seconds' => round((float)$runtime, 3),
        'log_type' => (string)($entry['log_type'] ?? ''),
    ];
}
echo json_encode(['status' => 'catalog-list-timing-only',
    'target' => $target, 'matching_log_entries' => count($result),
    'recent_entries' => array_slice($result, -8),
    'business_values_output' => false], JSON_UNESCAPED_SLASHES), PHP_EOL;
