<?php
if (ob_get_level() === 0) { ob_start(); }

require_once __DIR__ . '/Chairman/safety/ChairmanRuntimeLoggerV502.php';
ChairmanRuntimeLoggerV502::install('header_safe_diagnostic_v50_2', false);

require_once __DIR__ . '/config/config.php';
if (!isset($conn) && isset($db)) $conn=$db;

require_once __DIR__ . '/Chairman/safety/ChairmanSchemaCompatibilityGuardV501.php';

$guardData=array('status'=>'ERROR','issues'=>array('DB unavailable'));
if (isset($conn) && $conn instanceof mysqli) {
    $guard=new ChairmanSchemaCompatibilityGuardV501($conn);
    $guardData=$guard->inspect();
}

$headerInfo=ChairmanRuntimeLoggerV502::headersSentInfo();

function h502($v){ return htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8'); }

if (!headers_sent()) {
    header('Content-Type: text/html; charset=utf-8');
    header('X-WinRacing-Version: V50.2-Header-Safe');
}
?>
<!doctype html>
<html lang="zh-Hant">
<head>
<meta charset="utf-8">
<title>WinRacing Header Safe Diagnostic V50.2</title>
<style>
body{margin:0;background:#050914;color:#edf6ff;font-family:Arial,'Microsoft JhengHei';font-size:14px}
.wrap{max-width:1500px;margin:auto;padding:22px}.panel{background:#0b1424;border:1px solid #31486b;border-radius:14px;padding:16px;margin-bottom:14px}
.ok{color:#32ffa7}.warn{color:#ffc857}.bad{color:#ff657f}table{width:100%;border-collapse:collapse}td,th{border-bottom:1px solid #263a55;padding:8px;text-align:left}
code{color:#52d9ff}.badge{display:inline-block;padding:3px 8px;border:1px solid #8b7411;border-radius:12px;color:#ffd941;margin-left:6px}
</style>
</head>
<body>
<div class="wrap">
<div class="panel">
<h1>🛡️ Header Safe Diagnostic <span class="badge">V50.2</span></h1>
<p>Audit Fix Knife 2｜Buffer Safe｜Header Safe｜AJAX Safe｜API Safe</p>
</div>

<div class="panel">
<h2>Header / Buffer Status</h2>
<table>
<tr><th>Run ID</th><td><code><?=h502(ChairmanRuntimeLoggerV502::runId())?></code></td></tr>
<tr><th>Execution</th><td><?=h502(ChairmanRuntimeLoggerV502::executionMs())?> ms</td></tr>
<tr><th>Output Buffer Level</th><td><?=h502(ChairmanRuntimeLoggerV502::bufferLevel())?></td></tr>
<tr><th>Headers Sent</th><td class="<?=$headerInfo['sent']?'bad':'ok'?>"><?=$headerInfo['sent']?'YES':'NO'?></td></tr>
<tr><th>Header Source</th><td><?=h502($headerInfo['file'])?> : <?=h502($headerInfo['line'])?></td></tr>
<tr><th>Log File</th><td><code><?=h502(ChairmanRuntimeLoggerV502::logFile())?></code></td></tr>
</table>
</div>

<div class="panel">
<h2>Schema Guard</h2>
<p>Status：
<span class="<?=($guardData['status']==='OK'?'ok':'warn')?>"><?=h502($guardData['status'])?></span>
｜ALTER Executed：<?=!empty($guardData['alter_executed'])?'YES':'NO'?></p>
<table>
<tr><th>Table</th><th>Profile</th><th>Columns</th></tr>
<tr><td>wr_ai_decision</td><td><?=h502($guardData['decision_profile']??'')?></td><td><?=count($guardData['decision_columns']??array())?></td></tr>
<tr><td>wr_ai_engine_vote</td><td><?=h502($guardData['vote_profile']??'')?></td><td><?=count($guardData['vote_columns']??array())?></td></tr>
</table>
</div>

<div class="panel">
<h2>Knife 2 Fixes</h2>
<ul>
<li>UI-014：non-fatal warning / notice 只寫入 log，不再在 header 前輸出。</li>
<li>頁面最前啟動 output buffer，保護後續 header()。</li>
<li>Runtime API 加入 Header / Buffer 狀態。</li>
<li>不修改任何 V44–V50 分數或決策。</li>
</ul>
</div>
</div>
</body>
</html>
