<?php ob_start(); ?>
<?php
// Helper: render a status badge
$status = function(bool $ok, string $okLabel = 'OK', string $failLabel = 'FAIL'): string {
    $cls   = $ok ? 'badge-success' : 'badge-danger';
    $label = $ok ? $okLabel : $failLabel;
    return '<span class="badge ' . $cls . '">' . e($label) . '</span>';
};

// Disk usage
$diskFree  = $info['disk_free']  ? $info['disk_free']  : null;
$diskTotal = $info['disk_total'] ? $info['disk_total'] : null;
$diskPct   = ($diskFree && $diskTotal && $diskTotal > 0)
    ? round((1 - $diskFree / $diskTotal) * 100)
    : null;

function fmt_bytes(float $bytes): string {
    if ($bytes >= 1073741824) return round($bytes / 1073741824, 1) . ' GB';
    if ($bytes >= 1048576)    return round($bytes / 1048576, 1) . ' MB';
    return round($bytes / 1024, 1) . ' KB';
}
?>

<?php admin_content_header(__('admin.system.title'), 'monitor', [], __('admin.system.subtitle')); ?>

<div class="system-grid">

    <!-- ─── PHP & Server ──────────────────────── -->
    <div class="sys-card">
        <h3 class="sys-card__title"><?= __('admin.system.php_server') ?></h3>
        <table class="sys-table">
            <tr>
                <td><?= __('admin.system.php_version') ?></td>
                <td>
                    <code><?= e($info['php_version']) ?></code>
                    <?= $status($info['php_ok'], __('admin.system.php_ok'), __('admin.system.php_bad')) ?>
                </td>
            </tr>
            <tr>
                <td><?= __('admin.system.web_server') ?></td>
                <td><code><?= e($info['server_software']) ?></code></td>
            </tr>
            <tr>
                <td><?= __('admin.system.installation') ?></td>
                <td><?= $status($info['installed'], __('admin.system.installed'), __('admin.system.not_installed')) ?></td>
            </tr>
            <tr>
                <td><?= __('admin.system.active_theme') ?></td>
                <td><code><?= e($info['active_theme']) ?></code></td>
            </tr>
            <?php if ($diskPct !== null): ?>
            <tr>
                <td><?= __('admin.system.disk_usage') ?></td>
                <td>
                    <div class="disk-bar">
                        <div class="disk-bar__fill <?= $diskPct > 90 ? 'disk-bar__fill--critical' : ($diskPct > 70 ? 'disk-bar__fill--warn' : '') ?>"
                             style="width:<?= $diskPct ?>%"></div>
                    </div>
                    <small><?= __('admin.system.disk_format', ['percent' => $diskPct, 'free' => fmt_bytes($diskFree), 'total' => fmt_bytes($diskTotal)]) ?></small>
                </td>
            </tr>
            <?php endif; ?>
        </table>
    </div>

    <!-- ─── Database ─────────────────────────── -->
    <div class="sys-card">
        <h3 class="sys-card__title"><?= __('admin.system.database') ?></h3>
        <table class="sys-table">
            <tr>
                <td><?= __('admin.system.connection') ?></td>
                <td><?= $status($info['db_status']['ok'], __('admin.system.connected'), __('admin.system.connection_error')) ?>
                    <?php if (!$info['db_status']['ok']): ?>
                    <br><small class="text-danger"><?= e($info['db_status']['message']) ?></small>
                    <?php endif; ?>
                </td>
            </tr>
            <?php if ($info['db_status']['ok']): ?>
            <tr>
                <td><?= __('admin.system.cms_tables') ?></td>
                <td><?= __('admin.system.tables_found', ['count' => (int) $info['db_status']['table_count']]) ?></td>
            </tr>
            <?php endif; ?>
        </table>
    </div>

    <!-- ─── PHP Extensions ────────────────────── -->
    <div class="sys-card sys-card--wide">
        <h3 class="sys-card__title"><?= __('admin.system.extensions') ?></h3>
        <table class="sys-table">
            <thead>
                <tr>
                    <th><?= __('admin.system.extension') ?></th>
                    <th><?= __('admin.system.ext_description') ?></th>
                    <th><?= __('admin.system.ext_type') ?></th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($info['extensions'] as $ext): ?>
            <tr>
                <td><code><?= e($ext['name']) ?></code></td>
                <td><?= e($ext['label']) ?></td>
                <td>
                    <?php if ($ext['required']): ?>
                    <span class="badge badge-warning" style="font-size:.75rem;"><?= __('admin.system.required') ?></span>
                    <?php else: ?>
                    <span class="badge badge-secondary" style="font-size:.75rem;"><?= __('admin.system.optional') ?></span>
                    <?php endif; ?>
                </td>
                <td>
                    <?php if ($ext['loaded']): ?>
                    <?= $status(true, __('admin.system.loaded')) ?>
                    <?php elseif ($ext['required']): ?>
                    <?= $status(false, '', __('admin.system.missing_required')) ?>
                    <?php else: ?>
                    <span class="badge badge-draft"><?= __('admin.system.not_loaded') ?></span>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <!-- ─── Writable Directories ──────────────── -->
    <div class="sys-card sys-card--wide">
        <h3 class="sys-card__title"><?= __('admin.system.filesystem') ?></h3>
        <table class="sys-table">
            <thead>
                <tr>
                    <th><?= __('admin.system.directory') ?></th>
                    <th><?= __('admin.system.exists') ?></th>
                    <th><?= __('admin.system.writable') ?></th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($info['writable_dirs'] as $dir): ?>
            <tr>
                <td><code><?= e($dir['label']) ?></code></td>
                <td><?= $status($dir['exists'], __('admin.system.yes'), __('admin.system.missing')) ?></td>
                <td>
                    <?php if (!$dir['exists']): ?>
                    <span class="badge badge-draft"><?= __('admin.system.na') ?></span>
                    <?php else: ?>
                    <?= $status($dir['writable'], __('admin.system.writable_yes'), __('admin.system.not_writable')) ?>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <p class="sys-note">
            <?= __('admin.system.filesystem_hint') ?>
        </p>
    </div>

</div>

<style>
.content-subtitle { color: var(--text-muted, #6c757d); margin-top: -.25rem; margin-bottom: 1.5rem; }

.system-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
    gap: 1.5rem;
}
.sys-card--wide { grid-column: 1 / -1; }

.sys-card {
    background: #fff;
    border: 1px solid var(--border-color, #e0e0e0);
    border-radius: 8px;
    overflow: hidden;
}
.sys-card__title {
    font-size: .95rem;
    font-weight: 600;
    margin: 0;
    padding: .75rem 1rem;
    background: #f8f9fa;
    border-bottom: 1px solid var(--border-color, #e0e0e0);
}

.sys-table {
    width: 100%;
    border-collapse: collapse;
    font-size: .88rem;
}
.sys-table th {
    text-align: left;
    padding: .5rem 1rem;
    font-weight: 600;
    font-size: .8rem;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--text-muted, #6c757d);
    background: #fafafa;
    border-bottom: 1px solid var(--border-color, #e0e0e0);
}
.sys-table td {
    padding: .55rem 1rem;
    border-bottom: 1px solid #f0f0f0;
    vertical-align: middle;
}
.sys-table tr:last-child td { border-bottom: none; }
.sys-table tr:hover td { background: #fafafa; }

.sys-note {
    margin: 0;
    padding: .75rem 1rem;
    background: #fffbeb;
    border-top: 1px solid #fde68a;
    font-size: .82rem;
    color: #92400e;
}

.badge-danger  { background: #dc2626; color: #fff; }
.badge-warning { background: #f59e0b; color: #fff; }
.badge-secondary { background: #6c757d; color: #fff; }
.text-danger   { color: #dc2626; }

.disk-bar {
    height: 8px;
    background: #e5e7eb;
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: .3rem;
    width: 180px;
}
.disk-bar__fill { height: 100%; background: #22c55e; border-radius: 4px; transition: width .3s; }
.disk-bar__fill--warn     { background: #f59e0b; }
.disk-bar__fill--critical { background: #dc2626; }
</style>

<?php $content = ob_get_clean(); include dirname(__DIR__) . '/layouts/admin.php'; ?>
