<?php
require_once __DIR__ . '/runtime/wr_bootstrap.php';
require_once __DIR__ . '/config/config.php';

$dbOk = wr_db_guard_normalize();
$tz = isset($GLOBALS['WR_TIMEZONE_STATUS']) ? $GLOBALS['WR_TIMEZONE_STATUS'] : array();
$hs = wr_output_guard_headers_sent();
$logDir = wr_runtime_log_dir();
if (!is_dir($logDir)) {
    @mkdir($logDir, 0775, true);
}
$loggerWritable = is_dir($logDir) && is_writable($logDir);

$configured = isset($tz['configured']) && $tz['configured'] !== ''
    ? $tz['configured']
    : '(php.ini empty)';
$configuredState = !empty($tz['configured_valid']) ? 'VALID' : 'INVALID';
$correction = !empty($tz['corrected'])
    ? 'YES → ' . (isset($tz['effective']) ? $tz['effective'] : 'Asia/Hong_Kong')
    : 'NO';

$checks = array(
    'Runtime Version' => WR_RUNTIME_V503,
    'Request ID' => wr_runtime_request_id(),
    'Effective Timezone' => isset($tz['effective']) ? $tz['effective'] : 'Asia/Hong_Kong',
    'Configured Timezone' => $configured,
    'Configured Timezone State' => $configuredState,
    'Timezone Auto Corrected' => $correction,
    'Timezone Valid' => !empty($tz['valid']) ? 'YES' : 'NO',
    'Output Buffer Level' => ob_get_level(),
    'Headers Sent' => $hs['sent'] ? 'YES' : 'NO',
    'PHP Version' => PHP_VERSION,
    'Memory Usage' => wr_runtime_memory_mb() . ' MB',
    'Execution Time' => wr_runtime_execution_ms() . ' ms',
    'DB Ping' => ($dbOk && wr_db_guard_ping()) ? 'OK' : 'FAILED',
    'UTF8MB4' => ($dbOk && isset($conn) && strtolower($conn->character_set_name()) === 'utf8mb4') ? 'YES' : 'NO',
    'Logger Writable' => $loggerWritable ? 'YES' : 'NO',
    'Recursion Guard' => 'ACTIVE',
    'Error Handler Status' => !empty($GLOBALS['WR_ERROR_HANDLER_ACTIVE']) ? 'ACTIVE' : 'INACTIVE'
);
?><!doctype html>
<html lang="zh-Hant">
<head>
<meta charset="utf-8">
<title>WinRacing Runtime V50.3a Diagnostic</title>
<style>
body{background:#07111f;color:#eaf4ff;font-family:Arial,"Microsoft JhengHei";padding:24px}.wrap{max-width:1000px;margin:auto}.card{background:#0d1b2e;border:1px solid #304c70;border-radius:14px;padding:18px}table{width:100%;border-collapse:collapse}td{padding:10px;border-bottom:1px solid #253c59}td:first-child{font-weight:700;color:#8ed8ff}.ok{color:#4dffa7}
</style>
</head>
<body><div class="wrap"><div class="card"><h1>WinRacing Runtime Foundation V50.3a</h1><table>
<?php foreach ($checks as $k => $v): ?>
<tr><td><?=htmlspecialchars($k, ENT_QUOTES, 'UTF-8')?></td><td class="ok"><?=htmlspecialchars((string) $v, ENT_QUOTES, 'UTF-8')?></td></tr>
<?php endforeach; ?>
</table></div></div></body></html>
