<?php
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/session_check.php';
checkSession();

if (!isAdmin()) {
    header('Location: /index.php');
    exit;
}

require_once __DIR__ . '/../includes/i18n.php';

$pageTitle = t('Sistema') . ' - Admin';

$dbHost = $_ENV['DB_HOST'] ?? getenv('DB_HOST') ?? 'localhost';
$dbPort = $_ENV['DB_PORT'] ?? getenv('DB_PORT') ?? '3306';
$dbName = $_ENV['DB_NAME'] ?? getenv('DB_NAME') ?? 'bot';

try {
    $pdo = getDbConnection();
    $dbVersion = $pdo->query('SELECT VERSION()')->fetchColumn();
    $dbSize = $pdo->query("SELECT ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS size_mb FROM information_schema.tables WHERE table_schema = '$dbName'")->fetchColumn();
    $tableCount = $pdo->query("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '$dbName'")->fetchColumn();
} catch (Exception $e) {
    $dbError = $e->getMessage();
}

require_once __DIR__ . '/../templates/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-hdd-stack"></i> <?= t('Sistema') ?></h2>
</div>

<div class="row">
    <div class="col-md-6">
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-header border-0">
                <h5 class="mb-0"><i class="bi bi-database"></i> <?= t('Base de Datos') ?></h5>
            </div>
            <div class="card-body">
                <?php if (isset($dbError)): ?>
                    <div class="alert alert-danger"><?= htmlspecialchars($dbError) ?></div>
                <?php else: ?>
                    <table class="table table-borderless mb-0">
                        <tr>
                            <td class="text-muted text-uppercase" style="font-size: 0.75rem; letter-spacing: 1px;"><?= t('Host') ?> / IP</td>
                            <td style="font-family: 'Share Tech Mono', monospace; color: var(--hud-cyan);"><?= htmlspecialchars($dbHost) ?></td>
                        </tr>
                        <tr>
                            <td class="text-muted text-uppercase" style="font-size: 0.75rem; letter-spacing: 1px;"><?= t('Puerto') ?></td>
                            <td style="font-family: 'Share Tech Mono', monospace; color: var(--hud-cyan);"><?= htmlspecialchars($dbPort) ?></td>
                        </tr>
                        <tr>
                            <td class="text-muted text-uppercase" style="font-size: 0.75rem; letter-spacing: 1px;"><?= t('Base de Datos') ?></td>
                            <td style="font-family: 'Share Tech Mono', monospace; color: var(--accent-orange);"><?= htmlspecialchars($dbName) ?></td>
                        </tr>
                        <tr>
                            <td class="text-muted text-uppercase" style="font-size: 0.75rem; letter-spacing: 1px;"><?= t('Versión MySQL') ?></td>
                            <td style="font-family: 'Share Tech Mono', monospace;"><?= htmlspecialchars($dbVersion ?? 'N/A') ?></td>
                        </tr>
                        <tr>
                            <td class="text-muted text-uppercase" style="font-size: 0.75rem; letter-spacing: 1px;"><?= t('Tamaño') ?></td>
                            <td style="font-family: 'Share Tech Mono', monospace;"><?= $dbSize ?? '0' ?> MB</td>
                        </tr>
                        <tr>
                            <td class="text-muted text-uppercase" style="font-size: 0.75rem; letter-spacing: 1px;"><?= t('Tablas') ?></td>
                            <td style="font-family: 'Share Tech Mono', monospace;"><?= $tableCount ?? '0' ?></td>
                        </tr>
                    </table>
                <?php endif; ?>
            </div>
        </div>
    </div>
    
    <div class="col-md-6">
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-header border-0">
                <h5 class="mb-0"><i class="bi bi-server"></i> <?= t('Servidor') ?></h5>
            </div>
            <div class="card-body">
                <table class="table table-borderless mb-0">
                    <tr>
                        <td class="text-muted text-uppercase" style="font-size: 0.75rem; letter-spacing: 1px;"><?= t('Versión PHP') ?></td>
                        <td style="font-family: 'Share Tech Mono', monospace;"><?= phpversion() ?></td>
                    </tr>
                    <tr>
                        <td class="text-muted text-uppercase" style="font-size: 0.75rem; letter-spacing: 1px;"><?= t('Software Servidor') ?></td>
                        <td style="font-family: 'Share Tech Mono', monospace;"><?= $_SERVER['SERVER_SOFTWARE'] ?? 'N/A' ?></td>
                    </tr>
                    <tr>
                        <td class="text-muted text-uppercase" style="font-size: 0.75rem; letter-spacing: 1px;"><?= t('Nombre Servidor') ?></td>
                        <td style="font-family: 'Share Tech Mono', monospace;"><?= $_SERVER['SERVER_NAME'] ?? 'N/A' ?></td>
                    </tr>
                    <tr>
                        <td class="text-muted text-uppercase" style="font-size: 0.75rem; letter-spacing: 1px;"><?= t('Raíz Documentos') ?></td>
                        <td style="font-family: 'Share Tech Mono', monospace; font-size: 0.85rem;"><?= $_SERVER['DOCUMENT_ROOT'] ?? 'N/A' ?></td>
                    </tr>
                    <tr>
                        <td class="text-muted text-uppercase" style="font-size: 0.75rem; letter-spacing: 1px;"><?= t('Zona Horaria') ?></td>
                        <td style="font-family: 'Share Tech Mono', monospace;"><?= date_default_timezone_get() ?></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>

<?php require_once __DIR__ . '/../templates/footer.php'; ?>
