<?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/explain/ChairmanExplainableAIEngineV48.php';

function h48($s){return htmlspecialchars((string)$s,ENT_QUOTES,'UTF-8');}
function labelClass48($s){
    if($s==='VERY_HIGH'||$s==='HIGH') return 'green';
    if($s==='MEDIUM') return 'gold';
    return 'red';
}

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

$engine=new ChairmanExplainableAIEngineV48($conn);
$build=null;
if($action==='build') $build=$engine->build($date,$venue,$raceNum);
$data=$engine->latest($date,$venue,$raceNum);
?>
<!doctype html>
<html lang="zh-Hant">
<head>
<meta charset="utf-8">
<title>V48 Chairman Explainable AI</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}
.notice{background:#281d05;border:1px solid #806900;color:#ffe693;padding:12px;border-radius:10px;margin-top:10px}
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}
table{width:100%;border-collapse:collapse}td,th{border-bottom:1px solid #243650;padding:7px;text-align:left;font-size:12px;vertical-align:top}
.badge{padding:3px 8px;border-radius:999px;border:1px solid #715f12;color:#ffe378;display:inline-block}
.pos{color:#32ffa7}.warn{color:#ffae57}
.contrib{font-family:Consolas,monospace}
@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>🧠 V48 — Chairman <span class="gold">Explainable AI</span></h1>
<div>Feature Contribution → Positive Reasons → Warning Reasons → Confidence Audit</div>
<div class="notice">⚠️ 純賽前解釋：本版本只解釋 V47 賽前決策，完全不讀賽果。</div>
</div>

<div class="panel">
<form>
Race Date <input type="date" name="race_date" value="<?=h48($date)?>">
Venue <input name="venue" value="<?=h48($venue)?>">
Race # <input type="number" name="race_num" value="<?=h48($raceNum)?>" title="0 = ALL RACES">
<button name="action" value="build">Build V48 XAI</button>
</form>
</div>

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

<div class="panel">
<h2>Race Explanation Summary</h2>
<div class="grid">
<?php foreach($data['race_summary'] as $r): ?>
<div class="card">
<h3 class="gold">R<?=h48($r['race_num'])?>｜膽 #<?=h48($r['banker_horse'])?></h3>
<p>Score <?=number_format((float)$r['banker_score'],1)?>｜Race Confidence <span class="cyan"><?=number_format((float)$r['race_confidence'],1)?></span></p>
<p>Clarity：<b><?=h48($r['race_clarity'])?></b></p>
<p class="pos">✓ <?=h48($r['support_summary'])?></p>
<p class="warn">⚠ <?=h48($r['warning_summary'])?></p>
</div>
<?php endforeach; ?>
</div>
</div>

<div class="panel">
<h2>Horse Explanation Audit</h2>
<table>
<tr>
<th>Race</th><th>Rank</th><th>Horse</th><th>Name</th><th>Final</th><th>Grade</th><th>Confidence</th>
<th>Contribution</th><th>Positive Reasons</th><th>Warnings</th>
</tr>
<?php foreach($data['explanations'] as $r): ?>
<tr>
<td>R<?=h48($r['race_num'])?></td>
<td class="gold">#<?=h48($r['pre_race_rank'])?></td>
<td class="gold"><?=h48($r['horse_num'])?></td>
<td><?=h48($r['horse_name'])?></td>
<td class="green"><?=number_format((float)$r['ensemble_score'],1)?></td>
<td><?=h48($r['explanation_grade'])?></td>
<td class="<?=labelClass48($r['confidence_label'])?>"><?=h48($r['confidence_label'])?></td>
<td class="contrib">
V44 +<?=number_format((float)$r['contribution_v44'],1)?><br>
P1 +<?=number_format((float)$r['contribution_p1'],1)?><br>
7分 +<?=number_format((float)$r['contribution_db7'],1)?><br>
Signal +<?=number_format((float)$r['contribution_signal'],1)?><br>
Bias +<?=number_format((float)$r['contribution_bias'],1)?><br>
Conf +<?=number_format((float)$r['contribution_confidence'],1)?><br>
Risk <?=number_format((float)$r['contribution_risk'],1)?>
</td>
<td class="pos"><?=h48($r['positive_reasons'])?></td>
<td class="warn"><?=h48($r['warning_reasons'])?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div></body></html>
