<?php
@set_time_limit(1200);
@ini_set('memory_limit','1536M');

require_once __DIR__ . '/config/config.php';
if (!isset($conn) && isset($db)) $conn=$db;
if (!isset($conn) || !($conn instanceof mysqli)) die('DB connection not found');
$conn->set_charset('utf8mb4');

require_once __DIR__ . '/Chairman/decision/ChairmanPreRaceDecisionEngineV47.php';

function h47($s){return htmlspecialchars((string)$s,ENT_QUOTES,'UTF-8');}

$date=isset($_GET['race_date'])?$_GET['race_date']:(isset($_GET['date'])?$_GET['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']:'latest';

$engine=new ChairmanPreRaceDecisionEngineV47($conn);
$build=null;
if($action==='build') $build=$engine->build($date,$venue,$raceNum,$mode);
$data=$engine->latest($date,$venue,$raceNum);
?>
<!doctype html>
<html lang="zh-Hant">
<head>
<meta charset="utf-8">
<title>V47 Chairman Pre-Race Decision Engine</title>
<style>
body{margin:0;background:#050914;color:#eef;font-family:Arial,'Microsoft JhengHei';font-size:13px}
.wrap{max-width:1800px;margin:auto;padding:22px}
.hero,.panel{background:#0b1424;border:1px solid #31486b;border-radius:14px;padding:16px;margin-bottom:14px}
.gold{color:#ffc107}.green{color:#32ffa7}.red{color:#ff657f}.cyan{color:#52d9ff}
.notice{background:#281d05;border:1px solid #806900;color:#ffe693;padding:12px;border-radius:10px;margin-top:10px}
input,select{background:#07111f;color:#fff;border:1px solid #345;padding:8px}
button{background:#ffc107;border:0;padding:9px 14px;font-weight:900;border-radius:6px}
.grid{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:12px}
.card{background:#08101d;border:1px solid #2c405c;border-radius:12px;padding:13px}
table{width:100%;border-collapse:collapse}td,th{border-bottom:1px solid #243650;padding:7px;text-align:left;font-size:12px}
.badge{padding:3px 8px;border-radius:999px;border:1px solid #715f12;color:#ffe378;display:inline-block}
@media(max-width:1100px){.grid{grid-template-columns:1fr 1fr}}
@media(max-width:700px){.grid{grid-template-columns:1fr}}
</style>
</head>
<body><div class="wrap">
<div class="hero">
<h1>🏆 V47 — Chairman <span class="gold">Pre-Race Ensemble Decision Engine</span></h1>
<div>V44 + P1 + 7分表 + Signal + Bias + Risk → 賽前決策</div>
<div class="notice">⚠️ 本版本是 <b>賽前決策版</b>：賽果未齊亦可使用；程式不讀 racing_results，亦不使用 finish_position。</div>
</div>

<div class="panel">
<form>
Race Date <input type="date" name="race_date" value="<?=h47($date)?>">
Venue <input name="venue" value="<?=h47($venue)?>">
Race # <input type="number" name="race_num" value="<?=h47($raceNum)?>" title="0 = ALL RACES">
Mode <select 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 name="action" value="build">Build V47 Pre-Race</button>
</form>
</div>

<?php if($build): ?>
<div class="panel green"><?=h47($build['message'])?></div>
<?php endif; ?>

<div class="panel">
<h2>Race Summary</h2>
<div class="grid">
<?php foreach($data['summary'] as $r): ?>
<div class="card">
<h3 class="gold">R<?=h47($r['race_num'])?>｜膽 #<?=h47($r['banker_horse'])?></h3>
<p>信心 <span class="cyan"><?=number_format((float)$r['race_confidence'],1)?></span>｜<?=h47($r['race_mode'])?></p>
<p>Q／六環：<span class="badge"><?=h47($r['q_top2'])?></span></p>
<p>單T／Top4：<span class="badge"><?=h47($r['trio_top4'])?></span></p>
<p>冷腳：<?=h47($r['cold_horses'])?>｜避：<?=h47($r['avoid_horses'])?></p>
</div>
<?php endforeach; ?>
</div>
</div>

<div class="panel">
<h2>Horse Ranking</h2>
<table>
<tr><th>Race</th><th>Rank</th><th>Horse</th><th>Name</th><th>Final</th><th>V44</th><th>P1</th><th>7分</th><th>Signal</th><th>Risk</th><th>Role</th><th>Action</th></tr>
<?php foreach($data['ranking'] as $r): ?>
<tr>
<td>R<?=h47($r['race_num'])?></td>
<td class="gold">#<?=h47($r['pre_race_rank'])?></td>
<td class="gold"><?=h47($r['horse_num'])?></td>
<td><?=h47($r['horse_name'])?></td>
<td class="green"><?=number_format((float)$r['ensemble_score'],1)?></td>
<td><?=number_format((float)$r['v44_score'],1)?></td>
<td><?=number_format((float)$r['p1_score'],1)?></td>
<td><?=number_format((float)$r['db7_score'],1)?></td>
<td><?=number_format((float)$r['signal_score'],1)?></td>
<td class="red"><?=number_format((float)$r['risk_score'],1)?></td>
<td><?=h47($r['decision_role'])?></td>
<td><?=h47($r['decision_action'])?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div></body></html>
