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

require_once __DIR__ . '/Chairman/safety/ChairmanRuntimeLoggerV501.php';
ChairmanRuntimeLoggerV501::install('safety_foundation_dashboard');

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();
}
function h501($v){return htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8');}
?>
<!doctype html><html lang="zh-Hant"><head><meta charset="utf-8">
<title>WinRacing Safety Foundation V50.1</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>🛡️ WinRacing Safety Foundation <span class="badge">V50.1</span></h1>
<p>Audit Fix Knife 1｜只加不減｜不改計分｜不改 V44–V50 Decision Logic</p>
</div>
<div class="panel">
<h2>Runtime</h2>
<table>
<tr><th>Version</th><td>V50.1 Safety Foundation</td></tr>
<tr><th>Run ID</th><td><code><?=h501(ChairmanRuntimeLoggerV501::runId())?></code></td></tr>
<tr><th>Execution</th><td><?=h501(ChairmanRuntimeLoggerV501::executionMs())?> ms</td></tr>
<tr><th>PHP</th><td><?=h501(PHP_VERSION)?></td></tr>
<tr><th>Log File</th><td><code><?=h501(ChairmanRuntimeLoggerV501::logFile())?></code></td></tr>
</table>
</div>
<div class="panel">
<h2>AI Decision Schema Guard</h2>
<p>Status：
<span class="<?=($guardData['status']==='OK'?'ok':'warn')?>"><?=h501($guardData['status'])?></span>
｜Safe Mode：<?=h501($guardData['safe_mode']??'')?>
｜ALTER Executed：<?=!empty($guardData['alter_executed'])?'YES':'NO'?></p>
<table>
<tr><th>Table</th><th>Profile</th><th>Column Count</th></tr>
<tr><td>wr_ai_decision</td><td><?=h501($guardData['decision_profile']??'')?></td><td><?=count($guardData['decision_columns']??array())?></td></tr>
<tr><td>wr_ai_engine_vote</td><td><?=h501($guardData['vote_profile']??'')?></td><td><?=count($guardData['vote_columns']??array())?></td></tr>
</table>
<?php if(!empty($guardData['issues'])): ?>
<h3 class="warn">Warnings</h3><ul><?php foreach($guardData['issues'] as $x): ?><li><?=h501($x)?></li><?php endforeach; ?></ul>
<?php else: ?><p class="ok">Schema compatibility check passed.</p><?php endif; ?>
</div>
<div class="panel">
<h2>Fixes Included</h2>
<ul>
<li>DEAD-001：V4.1 duplicate Gateway endpoint disabled in the new safety copy.</li>
<li>DB-001：AI Decision schema inspected before future writes; this knife does not ALTER DB.</li>
<li>ERROR-001：PHP warnings, suppressed errors, uncaught exceptions and fatal shutdown are logged.</li>
<li>UI-009～013：Run ID、Execution Time、Version、Cache／Schema status、Debug information available.</li>
</ul>
</div>
</div></body></html>
