<?php
require_once __DIR__ . '/runtime/wr_bootstrap.php';
@set_time_limit(1200);
@ini_set('memory_limit','1536M');

require_once __DIR__ . '/config/config.php';
wr_db_guard_normalize();
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/ultimate/ChairmanUltimateDecisionEngineV50.php';

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

$engine=new ChairmanUltimateDecisionEngineV50($conn);
$build=null;
if($action==='build') $build=$engine->build($date,$venue);
$data=$engine->latest($date,$venue);
$day=isset($data['day'])?$data['day']:array();
?>
<!doctype html>
<html lang="zh-Hant">
<head>
<meta charset="utf-8">
<title>V50 Ultimate Chairman Decision</title>
<style>
body{margin:0;background:#050914;color:#edf6ff;font-family:Arial,'Microsoft JhengHei';font-size:13px}
.wrap{max-width:1880px;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}
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}
.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}
.stars{font-size:18px;color:#ffc107;letter-spacing:2px}
@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>🏆 V50 — <span class="gold">Ultimate Chairman Decision</span></h1>
<div>V49 Consensus → Final Race Decision → Daily Portfolio</div>
</div>

<div class="panel">
<form>
Race Date <input type="date" name="race_date" value="<?=h50($date)?>">
Venue <input name="venue" value="<?=h50($venue)?>">
<button name="action" value="build">Build V50 Ultimate</button>
</form>
</div>

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

<?php if($day): ?>
<div class="panel">
<h2>Daily Ultimate Summary</h2>
<div class="grid">
<div class="card"><small>Best Banker</small><h2 class="gold">R<?=h50($day['best_banker_race'])?> #<?=h50($day['best_banker_horse'])?></h2><p><?=h50($day['best_banker_name'])?>｜<?=number_format((float)$day['best_banker_score'],1)?></p></div>
<div class="card"><small>Portfolio</small><h2 class="green"><?=number_format((float)$day['portfolio_confidence'],1)?></h2><p><?=h50($day['portfolio_state'])?></p></div>
<div class="card"><small>Top 3 Bankers</small><h2 class="cyan"><?=h50($day['top3_bankers'])?></h2></div>
</div>
</div>
<?php endif; ?>

<div class="panel">
<h2>Ultimate Race Decisions</h2>
<div class="grid">
<?php foreach($data['races'] as $r): ?>
<div class="card">
<h3 class="gold">R<?=h50($r['race_num'])?>｜膽 #<?=h50($r['banker_horse'])?> <?=h50($r['banker_name'])?></h3>
<div class="stars"><?=str_repeat('★',(int)$r['star_rating'])?><?=str_repeat('☆',5-(int)$r['star_rating'])?></div>
<p>Ultimate <?=number_format((float)$r['ultimate_confidence'],1)?>｜<?=h50($r['race_state'])?></p>
<p>Consensus <?=number_format((float)$r['consensus_pct'],1)?>｜RSI <?=number_format((float)$r['stability_index'],1)?></p>
<p>Q <?=h50($r['q_top2'])?>｜Top4 <?=h50($r['trio_top4'])?></p>
<p>Value <?=h50($r['value_horses'])?>｜Avoid <?=h50($r['avoid_horses'])?></p>
</div>
<?php endforeach; ?>
</div>
</div>
</div></body></html>
