<?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/audit/wr_chairman_pre_race_leakage_auditor_v61_0.php';

function v61h($v){ return htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8'); }
function v61date(){
    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=v61date();
$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);
$auditor=new ChairmanPreRaceLeakageAuditorV61();
$data=$auditor->audit(is_array($source)?$source:array(),$date,$venue);
$s=$data['leakage_summary']; $checks=$data['checks']; $findings=$data['findings'];
?><!doctype html><html lang="zh-Hant"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>WinRacing V61 Pre-Race Leakage Audit</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)}.info{border-left:4px solid var(--cy)}
@media(max-width:900px){.grid,.g3{grid-template-columns:1fr}}
</style></head><body><div class="w">
<div class="p"><h1>🛡️ V61.0 — <span class="gold">Pre-Race Data Leakage Auditor</span></h1>
<div class="muted">檢查賽果欄位、results_used、未來日期及賽前資料契約。Read-only / Advisory-only。</div></div>
<div class="p"><form>Race Date <input type="date" name="race_date" value="<?=v61h($date)?>"> Venue <input name="venue" value="<?=v61h($venue)?>"> <button name="action" value="build">Build + Audit</button></form></div>
<div class="p"><div class="grid">
<div class="c"><small>Audit State</small><div class="k <?=$s['audit_state']==='PASS'?'green':($s['audit_state']==='BLOCK'?'red':'gold')?>"><?=v61h($s['audit_state'])?></div></div>
<div class="c"><small>Leakage Score</small><div class="k cyan"><?=number_format((float)$s['leakage_score'],1)?></div></div>
<div class="c"><small>Critical / Warning</small><div class="k"><span class="red"><?=v61h($s['critical_count'])?></span> / <span class="gold"><?=v61h($s['warning_count'])?></span></div></div>
<div class="c"><small>Production Gate</small><div class="k <?=$s['production_gate']==='CLOSED'?'red':'green'?>" style="font-size:20px"><?=v61h($s['production_gate'])?></div></div>
</div></div>
<div class="p"><h2>Audit Checks</h2><div class="grid"><?php foreach($checks as $x):?>
<div class="c"><h3 class="gold"><?=v61h($x['check'])?></h3><span class="b <?=$x['status']==='PASS'?'green':'red'?>"><?=v61h($x['status'])?></span></div>
<?php endforeach;?></div></div>
<div class="p"><h2>Findings</h2>
<?php if(!$findings):?><div class="c green">No leakage finding. Pre-race payload passed this audit.</div>
<?php else:?><div class="g3"><?php foreach($findings as $f):?>
<div class="c <?=strtolower(v61h($f['severity']))?>"><h3><?=v61h($f['severity'])?> · <?=v61h($f['type'])?></h3>
<p class="cyan"><?=v61h($f['path'])?></p><p><?=v61h($f['message'])?></p></div>
<?php endforeach;?></div><?php endif;?></div>
<div class="p muted">Safety Contract: PRE-RACE ONLY ｜ RESULTS MUST BE UNAVAILABLE ｜ NO DB WRITE ｜ NO SCHEMA CHANGE ｜ NO LIVE-WEIGHT CHANGE</div>
</div></body></html>
