<?php
require_once dirname(__DIR__) . '/bootstrap/chairman_v44_migration_bootstrap.php';
function v44h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }
$raceDate = isset($_GET['race_date']) ? $_GET['race_date'] : date('Y-m-d');
$venue = isset($_GET['venue']) ? $_GET['venue'] : '';
$raceNum = isset($_GET['race_num']) ? intval($_GET['race_num']) : 0;
$mode = isset($_GET['mode']) ? strtoupper($_GET['mode']) : 'BALANCED';
$action = isset($_GET['action']) ? $_GET['action'] : 'preview';
$result = ($action==='build') ? $V44Engine->build($raceDate,$venue,$raceNum,$mode) : $V44Engine->preview($raceDate,$venue,$raceNum,$mode);
?>
<!doctype html>
<html lang="zh-Hant">
<head>
<meta charset="utf-8">
<title>Chairman Framework v3.0 — V44 Migration</title>
<style>
body{margin:0;background:#050914;color:#e7fbff;font-family:Arial,'Microsoft JhengHei';font-size:13px}.wrap{padding:22px;max-width:1700px;margin:auto}
.hero{border:1px solid #31486b;border-radius:16px;background:#0b1325;padding:20px;margin-bottom:16px}.title{font-size:28px;font-weight:900}.gold{color:#ffc107}.ok{color:#32ffa7}.bad{color:#ff5a86}
.panel{border:1px solid #283c59;border-radius:12px;background:#0c1423;margin-bottom:16px;overflow:hidden}.panel h3{margin:0;padding:12px;border-bottom:1px solid #283c59}.body{padding:12px}
.inp{background:#091222;color:#fff;border:1px solid #314966;padding:8px}.btn{background:#ffc107;color:#111;text-decoration:none;border:0;padding:9px 12px;font-weight:900}
table{width:100%;border-collapse:collapse}th,td{border-bottom:1px solid #243650;padding:7px;font-size:12px;vertical-align:top}th{background:#101a2c;position:sticky;top:0}.scroll{max-height:620px;overflow:auto}
</style>
</head>
<body>
<div class="wrap">
<div class="hero"><div class="title">🏆 Chairman Framework <span class="gold">v3.0 Production Migration</span></div><div>V44 Ranking Optimizer migrated into Framework Engine.</div></div>

<div class="panel"><h3>Build / Preview</h3><div class="body">
<form method="get">
Race Date <input class="inp" type="date" name="race_date" value="<?=v44h($raceDate)?>">
Venue <input class="inp" name="venue" value="<?=v44h($venue)?>">
Race # <input class="inp" type="number" name="race_num" value="<?=v44h($raceNum)?>">
Mode <select class="inp" name="mode"><option <?=$mode==='BALANCED'?'selected':''?>>BALANCED</option><option <?=$mode==='AGGRESSIVE'?'selected':''?>>AGGRESSIVE</option><option <?=$mode==='CONSERVATIVE'?'selected':''?>>CONSERVATIVE</option><option <?=$mode==='DEFENSIVE'?'selected':''?>>DEFENSIVE</option></select>
<button class="btn" name="action" value="preview">Preview</button>
<button class="btn" name="action" value="build">Build & Save</button>
</form>
</div></div>

<div class="panel"><h3>Status</h3><div class="body">
<p>Status: <span class="gold"><?=v44h($result['status'])?></span></p>
<p>Run Key: <?=v44h(isset($result['run_key'])?$result['run_key']:'')?></p>
<p>Source Rows: <?=v44h(isset($result['source_rows'])?$result['source_rows']:0)?></p>
<p>Production Rows: <?=v44h(isset($result['production_rows'])?$result['production_rows']:(isset($result['counts']['prod'])?$result['counts']['prod']:0))?></p>
<?php if(isset($result['message'])): ?><p class="ok"><?=v44h($result['message'])?></p><?php endif; ?>
</div></div>

<?php if(isset($result['production'])): ?>
<div class="panel"><h3>Preview Production Output</h3><div class="scroll"><table>
<tr><th>Race</th><th>Rank</th><th>Horse</th><th>Name</th><th>Role</th><th>Decision</th><th>Final</th><th>Win</th><th>Place</th><th>Q</th><th>Trio</th><th>Risk</th><th>Reason</th></tr>
<?php foreach($result['production'] as $r): ?>
<tr>
<td>R<?=v44h($r['race_num'])?></td><td class="gold">#<?=v44h($r['race_rank'])?></td><td class="gold"><?=v44h($r['horse_num'])?></td><td><?=v44h($r['horse_name'])?></td>
<td><?=v44h($r['role_tag'])?></td><td><?=v44h($r['final_decision'])?></td><td class="gold"><?=number_format((float)$r['calibrated_score'],1)?></td>
<td><?=number_format((float)$r['win_score'],1)?></td><td><?=number_format((float)$r['place_score'],1)?></td><td><?=number_format((float)$r['q_score'],1)?></td><td><?=number_format((float)$r['trio_score'],1)?></td><td class="bad"><?=number_format((float)$r['risk_score'],1)?></td><td><?=v44h($r['reason_zh'])?></td>
</tr>
<?php endforeach; ?>
</table></div></div>
<?php endif; ?>
</div>
</body>
</html>
