<?php
/**
 * Nexora — diagnóstico de conexiones Legado + Seasonal.
 * Muestra únicamente estado lógico; no imprime host, base, usuario ni errores PDO.
 */

if(!defined('access') || access !== 'admincp') die();

$results = array();
foreach(NexoraRealm::publicList() as $realmId => $realmInfo) {
	$checks = array('game' => false, 'account' => false);
	try {
		$gameDb = Connection::Database('MuOnline', $realmId);
		$gameTest = $gameDb->query_fetch_single('SELECT 1 AS nexora_connection_test');
		$checks['game'] = is_array($gameTest) && isset($gameTest['nexora_connection_test']);
	} catch(Exception $ex) {
		$checks['game'] = false;
	}

	try {
		$accountDb = Connection::Database('Me_MuOnline', $realmId);
		$accountTest = $accountDb->query_fetch_single('SELECT 1 AS nexora_connection_test');
		$checks['account'] = is_array($accountTest) && isset($accountTest['nexora_connection_test']);
	} catch(Exception $ex) {
		$checks['account'] = false;
	}

	$cacheSummary = array('missing'=>0, 'not_writable'=>0, 'latest'=>0);
	try {
		$cacheManager = new CacheManager();
		$cacheHealth = $cacheManager->getRealmCacheHealth($realmId);
		foreach($cacheHealth as $cacheRow) {
			if(!$cacheRow['exists']) $cacheSummary['missing']++;
			if(!$cacheRow['writable']) $cacheSummary['not_writable']++;
			if($cacheRow['mtime'] > $cacheSummary['latest']) $cacheSummary['latest'] = $cacheRow['mtime'];
		}
	} catch(Exception $ex) {
		$cacheSummary['missing'] = 1;
		$cacheSummary['not_writable'] = 1;
	}
	$cronState = function_exists('nexoraCronStateLoad') ? nexoraCronStateLoad($realmId) : array();
	$cronLatest = 0;
	if(is_array($cronState)) foreach($cronState as $cronTimestamp) if((int)$cronTimestamp > $cronLatest) $cronLatest = (int)$cronTimestamp;

	$results[$realmId] = array('info' => $realmInfo, 'checks' => $checks, 'cache' => $cacheSummary, 'cron_latest' => $cronLatest);
}
?>

<div class="row">
	<div class="col-lg-12">
		<h1 class="page-header">Nexora · Legado + Seasonal</h1>
		<div class="alert alert-info">
			Esta pantalla valida las conexiones configuradas sin revelar infraestructura. La ranura nativa “Database (2)” de WebEngine no se utiliza como reino.
		</div>
	</div>
</div>

<div class="row">
	<?php foreach($results as $realmId => $result) { ?>
	<div class="col-md-6">
		<div class="panel panel-default">
			<div class="panel-heading"><strong><?php echo htmlspecialchars($result['info']['label'], ENT_QUOTES, 'UTF-8'); ?></strong></div>
			<div class="panel-body">
				<p><?php echo htmlspecialchars($result['info']['short_label'], ENT_QUOTES, 'UTF-8'); ?></p>
				<table class="table table-condensed">
					<tr><th>Datos del juego</th><td><?php echo $result['checks']['game'] ? '<span class="label label-success">Conectado</span>' : '<span class="label label-danger">Sin conexión</span>'; ?></td></tr>
					<tr><th>Cuentas y membresía</th><td><?php echo $result['checks']['account'] ? '<span class="label label-success">Conectado</span>' : '<span class="label label-danger">Sin conexión</span>'; ?></td></tr>
					<tr><th>Caché del reino</th><td><?php echo ($result['cache']['missing'] === 0 && $result['cache']['not_writable'] === 0) ? '<span class="label label-success">Operativa</span>' : '<span class="label label-warning">Revisar</span>'; ?><?php if($result['cache']['missing'] > 0) echo ' <small>'.(int)$result['cache']['missing'].' faltantes</small>'; ?><?php if($result['cache']['not_writable'] > 0) echo ' <small>'.(int)$result['cache']['not_writable'].' sin escritura</small>'; ?></td></tr>
					<tr><th>Último cron del reino</th><td><?php echo $result['cron_latest'] ? date('Y/m/d H:i', $result['cron_latest']) : '<em>Pendiente de primera ejecución</em>'; ?></td></tr>
				</table>
			</div>
		</div>
	</div>
	<?php } ?>
</div>

<div class="alert alert-warning">
	Si Seasonal aparece sin conexión, concede al usuario SQL actual acceso a esa base o define credenciales exclusivas en <code>includes/config/nexora.realms.php</code>.
</div>
