<?php
require_once dirname(__DIR__) . '/bootstrap/chairman_v44_migration_bootstrap.php';
require_once dirname(__DIR__) . '/production/ChairmanAutoProductionService.php';

function aph($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']) ? $_GET['mode'] : 'BALANCED';
$action = isset($_GET['action']) ? $_GET['action'] : 'inspect';

$service = new ChairmanAutoProductionService($conn, $V44Engine);
$result = $action === 'build'
    ? $service->ensure($raceDate,$venue,$raceNum,$mode,true)
    : array('status'=>'INSPECT','built'=>false,'after'=>$service->inspect($raceDate,$venue,$raceNum));
?>
<!doctype html>
<html lang="zh-Hant">
<head>
<meta charset="utf-8">
<title>Chairman Auto Production v4.5 / V45.1</title>
<style>
body{background:#050914;color:#e7fbff;font-family:Arial,'Microsoft JhengHei';padding:24px}
.hero,.box{background:#0c1423;border:1px solid #31486b;border-radius:14px;padding:18px;margin-bottom:16px}
.gold{color:#ffc107}.ok{color:#32ffa7}.bad{color:#ff6588}
input,select{background:#091222;color:white;border:1px solid #314966;padding:8px}
button{background:#ffc107;color:#111;border:0;padding:9px 14px;font-weight:900;border-radius:6px}
pre{white-space:pre-wrap;word-break:break-word;background:#060b14;padding:14px;border-radius:8px}
</style>
</head>
<body>
<div class="hero">
<h1>🏆 Chairman Auto Production <span class="gold">v4.5 / V45.1</span></h1>
<p>Detect → Build V44 → Save MySQL → Gateway Ready</p>
</div>
<div class="box">
<form>
Race Date <input type="date" name="race_date" value="<?=aph($raceDate)?>">
Venue <input name="venue" value="<?=aph($venue)?>">
Race # <input type="number" name="race_num" title="0 = ALL RACES" value="<?=aph($raceNum)?>">
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="inspect">Inspect</button>
<button name="action" value="build">Force Build</button>
</form>
<p style="color:#9ed9ff;margin-top:10px;">Race # <b>0</b> = ALL RACES；Race # <b>1</b> = only Race 1。Force Build 會先清除同一範圍舊 V44 rows，再建立最新一份。</p>
</div>
<div class="box">
<h2>Status: <span class="<?=in_array($result['status'],array('BUILT_READY','READY_EXISTING','INSPECT'),true)?'ok':'bad'?>"><?=aph($result['status'])?></span></h2>
<pre><?=aph(json_encode($result,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT))?></pre>
</div>
</body>
</html>
