<?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';
require_once __DIR__ . '/Chairman/audit/wr_chairman_final_release_certifier_v65_0.php';
function v65h($v){return htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8');}
function v65date(){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=v65date();$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);$rehearsal=(new ChairmanGoLiveRehearsalV64())->rehearse($source,$os,$leak,$ready,$shadow,$date,$venue);$data=(new ChairmanFinalReleaseCertifierV65())->certify($source,$os,$leak,$ready,$shadow,$rehearsal,$date,$venue);$s=$data['release_summary'];
?><!doctype html><html lang="zh-Hant"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>WinRacing V65 Final Production Release</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}.release{border:2px solid var(--gr);box-shadow:0 0 24px rgba(54,242,163,.12)}@media(max-width:900px){.grid,.g3{grid-template-columns:1fr}}
</style></head><body><div class="w"><div class="p release"><h1>🏁 V65.0 — <span class="gold">Final Race-Day Production Release</span></h1><div class="muted">真正上場成品：完整串連 V61→V64 認證鏈，鎖定來源指紋、決策覆蓋、結果隔離、deterministic replay、Fail-Closed、監控及 Rollback。全程只讀。</div></div>
<div class="p"><form>Race Date <input type="date" name="race_date" value="<?=v65h($date)?>"> Venue <input name="venue" value="<?=v65h($venue)?>"> <button name="action" value="build">Build + Final Certify</button></form></div>
<div class="p release"><div class="grid"><div class="c"><small>Release State</small><div class="k <?=$s['release_state']==='BLOCKED'?'red':'green'?>"><?=v65h($s['release_state'])?></div></div><div class="c"><small>Release Score</small><div class="k cyan"><?=number_format((float)$s['release_score'],1)?></div></div><div class="c"><small>Critical / Warning</small><div class="k"><span class="red"><?=v65h($s['critical_count'])?></span> / <span class="gold"><?=v65h($s['warning_count'])?></span></div></div><div class="c"><small>Production Gate</small><div class="k <?=$s['production_gate']==='PRODUCTION_GO_LIVE'?'green':'red'?>" style="font-size:18px"><?=v65h($s['production_gate'])?></div></div></div></div>
<div class="p"><h2>Final Certification Chain</h2><div class="grid"><div class="c"><small>V61 Leakage</small><div class="k cyan" style="font-size:20px"><?=v65h($s['v61_state'])?></div></div><div class="c"><small>V62 Gate</small><div class="k cyan" style="font-size:20px"><?=v65h($s['v62_gate'])?></div></div><div class="c"><small>V63 Shadow</small><div class="k cyan" style="font-size:20px"><?=v65h($s['v63_state'])?></div></div><div class="c"><small>V64 Rehearsal</small><div class="k cyan" style="font-size:20px"><?=v65h($s['v64_state'])?></div></div></div></div>
<div class="p"><h2>Release Lock</h2><div class="grid"><div class="c"><small>Fingerprint Chain</small><div class="k <?=$s['fingerprint_chain_locked']?'green':'red'?>"><?=$s['fingerprint_chain_locked']?'LOCKED':'DRIFT'?></div></div><div class="c"><small>Decision Count</small><div class="k"><?=v65h($s['decision_count'])?></div></div><div class="c" style="grid-column:span 2"><small>Release Token</small><div class="fp cyan"><?=v65h($s['release_token']?:'NOT ISSUED')?></div></div></div></div>
<div class="p"><h2>Final Release Checks</h2><div class="g3"><?php foreach($data['checks'] as $x):?><div class="c"><h3 class="gold"><?=v65h($x['check'])?></h3><span class="b <?=$x['status']==='PASS'?'green':'red'?>"><?=v65h($x['status'])?></span> <span class="muted"><?=v65h($x['severity'])?></span></div><?php endforeach;?></div></div>
<div class="p"><h2>Race-Day Runbook</h2><div class="g3"><?php foreach($data['race_day_runbook'] as $k=>$v):?><div class="c"><h3 class="gold"><?=v65h(strtoupper($k))?></h3><div><?=v65h($v)?></div></div><?php endforeach;?></div></div>
<div class="p"><h2>Findings</h2><?php if(!$data['findings']):?><div class="c green">All final production-release checks passed. WinRacing V65 is certified for monitored pre-race use.</div><?php else:?><div class="g3"><?php foreach($data['findings'] as $f):?><div class="c <?=strtolower(v65h($f['severity']))?>"><h3><?=v65h($f['severity'])?> · <?=v65h($f['type'])?></h3><p><?=v65h($f['message'])?></p></div><?php endforeach;?></div><?php endif;?></div>
<div class="p muted">FINAL SAFETY CONTRACT: PRE-RACE ONLY ｜ V61→V64 REQUIRED ｜ FINGERPRINT CHAIN LOCK ｜ DETERMINISTIC ｜ RESULTS ISOLATED ｜ FAIL CLOSED ｜ ROLLBACK READY ｜ NO DB WRITE ｜ NO SCHEMA CHANGE ｜ NO LIVE-WEIGHT CHANGE</div></div></body></html>
