<?php
// =========================================================================
// admin/dashboard_llm.php - PUSAT KENDALI XIEQA LLM (MODERN VERSION)
// Developer: Sikawarsito, S.Kom
// Fungsi: Hub utama (NOC, CRM, Cache, & Configuration)
// =========================================================================

session_start();
include '../../config/database.php';

// 1. PROTEKSI ADMIN
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../auth/login_page.php");
    exit;
}

// 2. AMBIL STATISTIK REAL-TIME
$total_llm_users = 0; $total_api_keys = 0; $total_api_hits = 0; $total_cache = 0;

$q_api = @mysqli_query($conn, "SELECT COUNT(DISTINCT user_id) as total_users, COUNT(id) as total_keys, SUM(usage_count) as total_hits FROM api_key_bot");
if ($q_api) {
    $data_api = mysqli_fetch_assoc($q_api);
    $total_llm_users = $data_api['total_users'] ?? 0;
    $total_api_keys = $data_api['total_keys'] ?? 0;
    $total_api_hits = $data_api['total_hits'] ?? 0;
}

$q_cache = @mysqli_query($conn, "SELECT COUNT(id) as total_data FROM cache_llm");
if ($q_cache) {
    $total_cache = mysqli_fetch_assoc($q_cache)['total_data'] ?? 0;
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Xieqa Control Center</title>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <style>
        body { background-color: #f8fafc; font-family: 'Plus Jakarta Sans', sans-serif; color: #1e293b; }
        .card-menu { background: #ffffff; border: 1px solid #e2e8f0; border-radius: 24px; padding: 25px; transition: all 0.3s ease; text-decoration: none; display: flex; flex-direction: column; height: 100%; box-shadow: 0 10px 30px rgba(0,0,0,0.02); }
        .card-menu:hover { transform: translateY(-7px); box-shadow: 0 20px 40px rgba(0,0,0,0.06); border-color: #cbd5e1; }
        .icon-wrapper { width: 55px; height: 55px; border-radius: 16px; display: flex; align-items: center; justify-content: center; font-size: 24px; margin-bottom: 20px; }
        
        .bg-blue { background: #eff6ff; color: #3b82f6; }
        .bg-green { background: #f0fdf4; color: #10b981; }
        .bg-purple { background: #faf5ff; color: #a855f7; }
        .bg-amber { background: #fffbeb; color: #f59e0b; }
        
        .stat-box { background: white; border-radius: 20px; padding: 20px; border: 1px solid #e2e8f0; display: flex; align-items: center; gap: 15px; }
        .stat-icon { width: 45px; height: 45px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 20px; }
    </style>
</head>
<body>

    <div class="container py-5">
        <div class="d-flex align-items-center justify-content-between mb-5 flex-wrap gap-3">
            <div class="d-flex align-items-center gap-3">
                <div class="bg-dark text-white rounded-4 d-flex align-items-center justify-content-center shadow" style="width: 55px; height: 55px;">
                    <i class="bi bi-cpu-fill fs-3"></i>
                </div>
                <div>
                    <h2 class="fw-bolder m-0 text-dark">Xieqa LLM Hub</h2>
                    <p class="text-secondary m-0 fw-medium">Sistem Manajemen Backend & API Gateway</p>
                </div>
            </div>
            <div class="d-flex gap-2">
                <a href="ai_gateway_traffic_flow.php" target="_blank" class="btn btn-outline-primary border rounded-pill fw-bold shadow-sm px-4 bg-white">
                    <i class="bi bi-diagram-3 me-1"></i> Visualisasi Flow
                </a>
                <a href="index.php" class="btn btn-white border rounded-pill fw-bold text-secondary shadow-sm px-4 bg-white">
                    <i class="bi bi-house-door me-1"></i> Dashboard Utama
                </a>
            </div>
        </div>

        <div class="row g-3 mb-5">
            <div class="col-6 col-md-3">
                <div class="stat-box">
                    <div class="stat-icon bg-primary bg-opacity-10 text-primary"><i class="bi bi-people"></i></div>
                    <div><small class="text-muted fw-bold d-block">USERS</small><h5 class="fw-bolder m-0"><?= number_format($total_llm_users) ?></h5></div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="stat-box">
                    <div class="stat-icon bg-warning bg-opacity-10 text-warning"><i class="bi bi-key"></i></div>
                    <div><small class="text-muted fw-bold d-block">KEYS</small><h5 class="fw-bolder m-0"><?= number_format($total_api_keys) ?></h5></div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="stat-box">
                    <div class="stat-icon bg-danger bg-opacity-10 text-danger"><i class="bi bi-lightning"></i></div>
                    <div><small class="text-muted fw-bold d-block">HITS</small><h5 class="fw-bolder m-0"><?= number_format($total_api_hits) ?></h5></div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="stat-box">
                    <div class="stat-icon bg-success bg-opacity-10 text-success"><i class="bi bi-database"></i></div>
                    <div><small class="text-muted fw-bold d-block">CACHE</small><h5 class="fw-bolder m-0"><?= number_format($total_cache) ?></h5></div>
                </div>
            </div>
        </div>

        <h5 class="fw-bold mb-4 text-dark"><i class="bi bi-grid-fill text-primary me-2"></i> Modul Manajemen Sistem</h5>
        <div class="row g-4">
            
            <div class="col-md-6 col-lg-3">
                <a href="monitoring_user_llm.php" class="card-menu">
                    <div class="icon-wrapper bg-blue"><i class="bi bi-person-badge-fill"></i></div>
                    <h5 class="fw-bold text-dark">User CRM</h5>
                    <p class="text-secondary small mb-4">Pantau kuota, kirim pesan pop-up, dan kelola hak akses pengguna API.</p>
                    <div class="mt-auto text-primary fw-bold small">Manage Users <i class="bi bi-chevron-right"></i></div>
                </a>
            </div>

            <div class="col-md-6 col-lg-3">
                <a href="monitor_cache.php" class="card-menu">
                    <div class="icon-wrapper bg-green"><i class="bi bi-database-fill-check"></i></div>
                    <h5 class="fw-bold text-dark">Cache Engine</h5>
                    <p class="text-secondary small mb-4">Optimasi kecepatan respons dan hemat kuota API dengan sistem caching.</p>
                    <div class="mt-auto text-success fw-bold small">System Engine <i class="bi bi-chevron-right"></i></div>
                </a>
            </div>

            <div class="col-md-6 col-lg-3">
                <a href="monitor_api.php" target="_blank" class="card-menu">
                    <div class="icon-wrapper bg-purple"><i class="bi bi-activity"></i></div>
                    <h5 class="fw-bold text-dark">Router NOC</h5>
                    <p class="text-secondary small mb-4">Monitor real-time Load Balancer dan status kesehatan setiap API Key.</p>
                    <div class="mt-auto text-purple fw-bold small" style="color: #a855f7;">Live Monitor <i class="bi bi-box-arrow-up-right"></i></div>
                </a>
            </div>

            <div class="col-md-6 col-lg-3">
                <a href="konfigurasi.php" class="card-menu border-primary border-opacity-25">
                    <div class="icon-wrapper bg-amber"><i class="bi bi-gear-wide-connected"></i></div>
                    <h5 class="fw-bold text-dark">Konfigurasi API</h5>
                    <p class="text-secondary small mb-4">Input & ganti 8 Slot API Key serta atur model Gemini/Groq secara dinamis.</p>
                    <div class="mt-auto text-warning fw-bold small">Setup Engine <i class="bi bi-chevron-right"></i></div>
                </a>
            </div>

        </div>

        <div class="text-center mt-5">
            <small class="text-secondary fw-bold" style="letter-spacing: 1px;">&copy; <?= date('Y') ?> DEVELOPED BY SIKAWARSITO, S.KOM</small>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>