<?php
require_once __DIR__ . '/runtime/wr_bootstrap.php';
@set_time_limit(1200); @ini_set('memory_limit','1536M');
require_once __DIR__ . '/config/config.php';
if (!wr_db_guard_normalize()) { throw new RuntimeException('DB connection unavailable'); }
require_once __DIR__ . '/Chairman/memory/ChairmanPatternMemoryEngineV53.php';
require_once __DIR__ . '/Chairman/runtime/wr_chairman_os_scheduler_v60_0.php';
require_once __DIR__ . '/Chairman/audit/wr_chairman_pre_race_leakage_auditor_v61_0.php';
require_once __DIR__ . '/Chairman/audit/wr_chairman_production_readiness_gate_v62_0.php';
require_once __DIR__ . '/Chairman/audit/wr_chairman_shadow_run_validator_v63_0.php';
require_once __DIR__ . '/Chairman/audit/wr_chairman_go_live_rehearsal_v64_0.php';
function v64h($v){return htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8');}
function v64date(){foreach(array('race_date','date','review_date') as $k){if(!isset($_GET[$k]))continue;$v=trim((string)$_GET[$k]);$d=DateTimeImmutable::createFromFormat('!Y-m-d',$v);$e=DateTimeImmutable::getLastErrors();if($d instanceof DateTimeImmutable&&($e===false||((int)$e['warning_count']===0&&(int)$e['error_count']===0))&&$d->format('Y-m-d')===$v)return $v;}return(new DateTimeImmutable('now',new DateTimeZone('Asia/Hong_Kong')))->format('Y-m-d');}
$date=v64date();$venue=isset($_GET['venue'])?preg_replace('/[^A-Za-z0-9_\-]/','',trim((string)$_GET['venue'])):'';$action=isset($_GET['action'])?trim((string)$_GET['action']):'latest';
$engine=new ChairmanPatternMemoryEngineV53($conn);$source=$action==='build'?$engine->build($date,$venue):$engine->latest($date,$venue);if(!is_array($source))$source=array();
$os=(new ChairmanOSSchedulerV60())->build($source,$date,$venue);$leak=(new ChairmanPreRaceLeakageAuditorV61())->audit($source,$date,$venue);$ready=(new ChairmanProductionReadinessGateV62())->certify($source,$os,$leak,$date,$venue);$shadow=(new ChairmanShadowRunValidatorV63())->validate($source,$ready,$date,$venue);$data=(new ChairmanGoLiveRehearsalV64())->rehearse($source,$os,$leak,$ready,$shadow,$date,$venue);$s=$data['rehearsal_summary'];
?><!doctype html><html lang="zh-Hant"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>WinRacing V64 Go-Live Rehearsal</title><style>
:root{--bg:#050914;--p:#0b1424;--c:#08101d;--l:#304765;--g:#ffc107;--gr:#36f2a3;--cy:#5edbff;--r:#ff7589;--m:#9eb0c7}*{box-sizing:border-box}body{margin:0;background:var(--bg);color:#eef6ff;font:13px Arial,'Microsoft JhengHei',sans-serif}.w{max-width:1600px;margin:auto;padding:18px}.p{background:var(--p);border:1px solid #31486b;border-radius:14px;padding:15px;margin-bottom:12px}.grid{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:10px}.g3{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:10px}.c{background:var(--c);border:1px solid var(--l);border-radius:12px;padding:12px}.k{font-size:27px;font-weight:900}.gold{color:var(--g)}.green{color:var(--gr)}.cyan{color:var(--cy)}.red{color:var(--r)}.muted{color:var(--m)}.b{display:inline-block;padding:3px 8px;border-radius:999px;background:#162942;border:1px solid #36577e;font-weight:700}input{background:#07111f;color:#fff;border:1px solid #345;padding:8px}button{background:#ffc107;border:0;padding:9px 14px;font-weight:900;border-radius:6px}.critical{border-left:4px solid var(--r)}.warning{border-left:4px solid var(--g)}.fp{word-break:break-all;font-family:Consolas,monospace;font-size:11px}@media(max-width:900px){.grid,.g3{grid-template-columns:1fr}}
</style></head><body><div class="w"><div class="p"><h1>🚦 V64.0 — <span class="gold">Controlled Go-Live Rehearsal</span></h1><div class="muted">正式上場前最後演練：串連 V61、V62、V63，鎖定來源指紋、決策覆蓋、結果隔離、Fail-Closed 與 Rollback Runbook。全程只讀。</div></div>
<div class="p"><form>Race Date <input type="date" name="race_date" value="<?=v64h($date)?>"> Venue <input name="venue" value="<?=v64h($venue)?>"> <button name="action" value="build">Build + Rehearse</button></form></div>
<div class="p"><div class="grid"><div class="c"><small>Rehearsal State</small><div class="k <?=$s['rehearsal_state']==='PASS'?'green':($s['rehearsal_state']==='FAIL'?'red':'gold')?>"><?=v64h($s['rehearsal_state'])?></div></div><div class="c"><small>Rehearsal Score</small><div class="k cyan"><?=number_format((float)$s['rehearsal_score'],1)?></div></div><div class="c"><small>Critical / Warning</small><div class="k"><span class="red"><?=v64h($s['critical_count'])?></span> / <span class="gold"><?=v64h($s['warning_count'])?></span></div></div><div class="c"><small>Launch Gate</small><div class="k <?=$s['rehearsal_gate']==='BLOCKED'?'red':'green'?>" style="font-size:18px"><?=v64h($s['rehearsal_gate'])?></div></div></div></div>
<div class="p"><h2>Certification Chain</h2><div class="grid"><div class="c"><small>V61 Leakage</small><div class="k cyan" style="font-size:20px"><?=v64h($s['v61_audit_state'])?></div></div><div class="c"><small>V62 Production Gate</small><div class="k cyan" style="font-size:20px"><?=v64h($s['v62_production_gate'])?></div></div><div class="c"><small>V63 Shadow</small><div class="k cyan" style="font-size:20px"><?=v64h($s['v63_shadow_state'])?></div></div><div class="c"><small>Decision Count</small><div class="k"><?=v64h($s['decision_count'])?></div></div></div></div>
<div class="p"><h2>Source Lock</h2><div class="grid"><div class="c"><small>Fingerprint Match</small><div class="k <?=$s['fingerprint_match']?'green':'red'?>"><?=$s['fingerprint_match']?'YES':'NO'?></div></div><div class="c" style="grid-column:span 3"><small>Source Fingerprint</small><div class="fp cyan"><?=v64h($s['source_fingerprint'])?></div></div></div></div>
<div class="p"><h2>Rehearsal Checks</h2><div class="g3"><?php foreach($data['checks'] as $x):?><div class="c"><h3 class="gold"><?=v64h($x['check'])?></h3><span class="b <?=$x['status']==='PASS'?'green':'red'?>"><?=v64h($x['status'])?></span> <span class="muted"><?=v64h($x['severity'])?></span></div><?php endforeach;?></div></div>
<div class="p"><h2>Launch Runbook</h2><div class="g3"><?php foreach($data['launch_runbook'] as $k=>$v):?><div class="c"><h3 class="gold"><?=v64h(strtoupper($k))?></h3><div><?=v64h($v)?></div></div><?php endforeach;?></div></div>
<div class="p"><h2>Findings</h2><?php if(!$data['findings']):?><div class="c green">All controlled go-live rehearsal checks passed.</div><?php else:?><div class="g3"><?php foreach($data['findings'] as $f):?><div class="c <?=strtolower(v64h($f['severity']))?>"><h3><?=v64h($f['severity'])?> · <?=v64h($f['type'])?></h3><p><?=v64h($f['message'])?></p></div><?php endforeach;?></div><?php endif;?></div>
<div class="p muted">Safety Contract: PRE-RACE ONLY ｜ V61+V62+V63 REQUIRED ｜ SOURCE FINGERPRINT LOCK ｜ FAIL CLOSED ｜ ROLLBACK READY ｜ NO DB WRITE ｜ NO SCHEMA CHANGE ｜ NO LIVE-WEIGHT CHANGE</div></div></body></html>
