<?php
$server = $server ?? [];
$systemInfo = $systemInfo ?? [];

$memTotal = (int) ($systemInfo['mem_total'] ?? 0);
$memUsed = (int) ($systemInfo['mem_used'] ?? 0);
$memAvail = (int) ($systemInfo['mem_avail'] ?? 0);
$memPercent = $memTotal > 0 ? round($memUsed / $memTotal * 100, 1) : 0;

$diskTotal = (int) ($systemInfo['disk_total'] ?? 0);
$diskUsed = (int) ($systemInfo['disk_used'] ?? 0);
$diskAvail = (int) ($systemInfo['disk_avail'] ?? 0);
$diskPercent = (int) ($systemInfo['disk_percent'] ?? 0);

$cpuModel = trim(str_replace('model name	: ', '', $systemInfo['cpu_model'] ?? ''));
$osName = trim(str_replace('NAME=', '', $systemInfo['os'] ?? ''), '"');
?>

<div class="page-header">
    <div class="page-header-left">
        <div class="back-link">
            <a href="/servers/<?= $server['id'] ?>"><i class="fas fa-arrow-left"></i> Back to Server</a>
        </div>
        <h1 class="page-title">System Information</h1>
        <p class="page-subtitle"><?= htmlspecialchars($server['name']) ?></p>
    </div>
    <div class="page-header-right">
        <button class="btn btn-secondary" onclick="location.reload()">
            <i class="fas fa-sync-alt"></i> Refresh
        </button>
    </div>
</div>

<div class="dashboard-grid">
    <div class="dashboard-card">
        <div class="card-header">
            <h2><i class="fas fa-server"></i> System</h2>
        </div>
        <div class="card-body">
            <dl class="details-list">
                <dt>Hostname</dt>
                <dd><code><?= htmlspecialchars($systemInfo['hostname'] ?? 'Unknown') ?></code></dd>
                <dt>Operating System</dt>
                <dd><?= htmlspecialchars($osName ?: 'Unknown') ?></dd>
                <dt>Kernel</dt>
                <dd><code><?= htmlspecialchars($systemInfo['kernel'] ?? 'Unknown') ?></code></dd>
                <dt>Uptime</dt>
                <dd><i class="fas fa-clock text-success"></i> <?= htmlspecialchars($systemInfo['uptime'] ?? 'Unknown') ?></dd>
            </dl>
        </div>
    </div>

    <div class="dashboard-card">
        <div class="card-header">
            <h2><i class="fas fa-microchip"></i> CPU</h2>
        </div>
        <div class="card-body">
            <dl class="details-list">
                <dt>Model</dt>
                <dd><code><?= htmlspecialchars($cpuModel ?: 'Unknown') ?></code></dd>
                <dt>Cores</dt>
                <dd>
                    <span class="badge badge-primary badge-lg">
                        <i class="fas fa-microchip"></i>
                        <?= htmlspecialchars($systemInfo['cpu_cores'] ?? '?') ?> cores
                    </span>
                </dd>
            </dl>
        </div>
    </div>

    <div class="dashboard-card">
        <div class="card-header">
            <h2><i class="fas fa-memory"></i> Memory</h2>
        </div>
        <div class="card-body">
            <?php if ($memTotal > 0): ?>
                <div class="metric-block">
                    <div class="metric-info">
                        <span class="metric-label">Usage</span>
                        <span class="metric-value">
                            <?= format_bytes($memUsed) ?> / <?= format_bytes($memTotal) ?>
                            <span class="text-muted">(<?= $memPercent ?>%)</span>
                        </span>
                    </div>
                    <div class="progress-bar">
                        <div class="progress-fill <?= $memPercent > 90 ? 'progress-danger' : ($memPercent > 70 ? 'progress-warning' : 'progress-success') ?>"
                             style="width: <?= min($memPercent, 100) ?>%"></div>
                    </div>
                </div>
                <div class="metric-block" style="margin-top: 0.75rem;">
                    <div class="metric-info">
                        <span class="metric-label">Available</span>
                        <span class="metric-value"><?= format_bytes($memAvail) ?></span>
                    </div>
                </div>
            <?php else: ?>
                <p class="text-muted">Memory information not available</p>
            <?php endif; ?>
        </div>
    </div>

    <div class="dashboard-card">
        <div class="card-header">
            <h2><i class="fas fa-hdd"></i> Storage</h2>
        </div>
        <div class="card-body">
            <?php if ($diskTotal > 0): ?>
                <div class="metric-block">
                    <div class="metric-info">
                        <span class="metric-label">Usage</span>
                        <span class="metric-value">
                            <?= format_bytes($diskUsed) ?> / <?= format_bytes($diskTotal) ?>
                            <span class="text-muted">(<?= $diskPercent ?>%)</span>
                        </span>
                    </div>
                    <div class="progress-bar">
                        <div class="progress-fill <?= $diskPercent > 90 ? 'progress-danger' : ($diskPercent > 70 ? 'progress-warning' : 'progress-success') ?>"
                             style="width: <?= min($diskPercent, 100) ?>%"></div>
                    </div>
                </div>
                <div class="metric-block" style="margin-top: 0.75rem;">
                    <div class="metric-info">
                        <span class="metric-label">Free</span>
                        <span class="metric-value"><?= format_bytes($diskAvail) ?></span>
                    </div>
                </div>
            <?php else: ?>
                <p class="text-muted">Disk information not available</p>
            <?php endif; ?>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <h2><i class="fas fa-chart-pie"></i> Resource Summary</h2>
    </div>
    <div class="card-body">
        <div class="stats-grid">
            <div class="stat-card stat-card-info">
                <div class="stat-icon"><i class="fas fa-microchip"></i></div>
                <div class="stat-info">
                    <span class="stat-value"><?= htmlspecialchars($systemInfo['cpu_cores'] ?? '0') ?></span>
                    <span class="stat-label">CPU Cores</span>
                </div>
            </div>
            <div class="stat-card stat-card-warning">
                <div class="stat-icon"><i class="fas fa-memory"></i></div>
                <div class="stat-info">
                    <span class="stat-value"><?= format_bytes($memTotal) ?></span>
                    <span class="stat-label">Total RAM</span>
                </div>
            </div>
            <div class="stat-card stat-card-purple">
                <div class="stat-icon"><i class="fas fa-hdd"></i></div>
                <div class="stat-info">
                    <span class="stat-value"><?= format_bytes($diskTotal) ?></span>
                    <span class="stat-label">Total Disk</span>
                </div>
            </div>
            <div class="stat-card stat-card-success">
                <div class="stat-icon"><i class="fas fa-clock"></i></div>
                <div class="stat-info">
                    <span class="stat-value" style="font-size:0.8rem"><?= htmlspecialchars($systemInfo['uptime'] ?? '-') ?></span>
                    <span class="stat-label">Uptime</span>
                </div>
            </div>
        </div>
    </div>
</div>
