<?php
$stats = $stats ?? [];
$servers = $servers ?? [];
$recentActivity = $recentActivity ?? [];
?>

<div class="page-header">
    <h1 class="page-title">Dashboard</h1>
    <p class="page-subtitle">Server overview and system status</p>
</div>

<div class="stats-grid">
    <div class="stat-card stat-card-primary">
        <div class="stat-icon"><i class="fas fa-hdd"></i></div>
        <div class="stat-info">
            <span class="stat-value" id="statTotalServers"><?= $stats['total_servers'] ?? 0 ?></span>
            <span class="stat-label">Total Servers</span>
        </div>
    </div>
    <div class="stat-card stat-card-success">
        <div class="stat-icon"><i class="fas fa-check-circle"></i></div>
        <div class="stat-info">
            <span class="stat-value" id="statOnlineServers"><?= $stats['online_servers'] ?? 0 ?></span>
            <span class="stat-label">Online</span>
        </div>
    </div>
    <div class="stat-card stat-card-danger">
        <div class="stat-icon"><i class="fas fa-times-circle"></i></div>
        <div class="stat-info">
            <span class="stat-value" id="statOfflineServers"><?= $stats['offline_servers'] ?? 0 ?></span>
            <span class="stat-label">Offline</span>
        </div>
    </div>
    <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" id="statAvgCpu"><?= $stats['avg_cpu'] ?? 0 ?>%</span>
            <span class="stat-label">Avg CPU</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" id="statAvgRam"><?= $stats['avg_ram'] ?? 0 ?>%</span>
            <span class="stat-label">Avg 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" id="statAvgDisk"><?= $stats['avg_disk'] ?? 0 ?>%</span>
            <span class="stat-label">Avg Disk</span>
        </div>
    </div>
</div>

<div class="dashboard-grid">
    <div class="dashboard-card">
        <div class="card-header">
            <h2><i class="fas fa-server"></i> Servers</h2>
            <a href="/servers" class="btn btn-sm">View All</a>
        </div>
        <div class="card-body">
            <?php if (empty($servers)): ?>
                <div class="empty-state">
                    <i class="fas fa-server"></i>
                    <p>No servers configured yet.</p>
                    <?php if (is_admin()): ?>
                    <a href="/servers/create" class="btn btn-primary">Add Server</a>
                    <?php endif; ?>
                </div>
            <?php else: ?>
                <div class="table-responsive">
                    <table class="table table-sm">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>IP</th>
                                <th>Status</th>
                                <th>CPU</th>
                                <th>RAM</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach (array_slice($servers, 0, 8) as $server): ?>
                                <tr>
                                    <td>
                                        <a href="/servers/<?= $server['id'] ?>">
                                            <?= htmlspecialchars($server['name']) ?>
                                        </a>
                                    </td>
                                    <td><code><?= htmlspecialchars($server['ip_address']) ?>:<?= $server['ssh_port'] ?></code></td>
                                    <td>
                                        <span class="status-badge status-<?= $server['current_status'] ?? 'unknown' ?>">
                                            <?= ucfirst($server['current_status'] ?? 'unknown') ?>
                                        </span>
                                    </td>
                                    <td><?= $server['cpu_usage'] ?? '-' ?>%</td>
                                    <td><?= $server['ram_usage'] ?? '-' ?>%</td>
                                    <td>
                                        <a href="/servers/<?= $server['id'] ?>" class="btn btn-xs" title="View">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <a href="/terminal/<?= $server['id'] ?>" class="btn btn-xs" title="Terminal">
                                            <i class="fas fa-terminal"></i>
                                        </a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <div class="dashboard-card">
        <div class="card-header">
            <h2><i class="fas fa-history"></i> Recent Activity</h2>
        </div>
        <div class="card-body">
            <?php if (empty($recentActivity)): ?>
                <div class="empty-state">
                    <i class="fas fa-history"></i>
                    <p>No recent activity.</p>
                </div>
            <?php else: ?>
                <div class="activity-list">
                    <?php foreach ($recentActivity as $activity): ?>
                        <div class="activity-item">
                            <div class="activity-icon activity-<?= str_contains($activity['action'] ?? '', 'failed') ? 'danger' : (str_contains($activity['action'] ?? '', 'created') ? 'success' : 'info') ?>">
                                <i class="fas fa-<?= match(true) {
                                    str_contains($activity['action'] ?? '', 'login') => 'sign-in-alt',
                                    str_contains($activity['action'] ?? '', 'server') => 'server',
                                    str_contains($activity['action'] ?? '', 'command') => 'terminal',
                                    str_contains($activity['action'] ?? '', 'user') => 'user',
                                    default => 'circle',
                                } ?>"></i>
                            </div>
                            <div class="activity-details">
                                <span class="activity-action"><?= htmlspecialchars(str_replace('_', ' ', $activity['action'] ?? '')) ?></span>
                                <span class="activity-meta">
                                    <?= htmlspecialchars($activity['actor_name'] ?? $activity['username'] ?? 'System') ?>
                                    &middot; <?= date('H:i', strtotime($activity['created_at'] ?? '')) ?>
                                </span>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>
