<?php
/**
 * ekosistem.php - Dashboard Portal Ekosistem Xieqa AI
 * Menampilkan ringkasan dan pintasan ke semua modul dalam satu halaman.
 */
session_set_cookie_params(['lifetime' => 31536000, 'path' => '/']);
ini_set('session.gc_maxlifetime', 31536000);
session_start();

include 'config/database.php';

// Cek Login (Re-use dari index.php)
if (!isset($_SESSION['user_id'])) {
    header("Location: auth/login_page.php");
    exit;
}

$user_id = $_SESSION['user_id'];
$nama_siswa = $_SESSION['nama'] ?? 'User';

$stmt_user = mysqli_prepare($conn, "SELECT username, status_akun, role, status_broadcast FROM users WHERE id = ?");
mysqli_stmt_bind_param($stmt_user, "s", $user_id); 
mysqli_stmt_execute($stmt_user);
$res_user = mysqli_stmt_get_result($stmt_user);
$u_data = mysqli_fetch_assoc($res_user);

$role_user = $u_data['role'] ?? 'siswa';
$raw_status = strtolower(trim($u_data['status_akun'] ?? 'free')); 
$status_broadcast = $u_data['status_broadcast'] ?? 'none'; 
mysqli_stmt_close($stmt_user);

// Ambil beberapa statisik dasar (contoh)
// Anda bisa mengganti query ini sesuai tabel asli nanti
$total_iot = 0;
$stmt_iot = mysqli_query($conn, "SELECT COUNT(*) as tot FROM iot_devices WHERE user_id = " . (int)$user_id);
if ($stmt_iot && $row = mysqli_fetch_assoc($stmt_iot)) {
    $total_iot = $row['tot'];
}

$has_api_key = false;
$stmt_api = mysqli_query($conn, "SELECT id FROM user_api_keys WHERE user_id = " . (int)$user_id . " LIMIT 1");
if ($stmt_api && mysqli_num_rows($stmt_api) > 0) {
    $has_api_key = true;
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Portal Ekosistem - XIEQA AI</title>
    
    <link href="https://fonts.googleapis.com/css2?family=Inter: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>
        :root {
            --bg-body: #f8fafc;
            --card-bg: #ffffff;
            --text-main: #1e293b;
            --text-muted: #64748b;
            --primary: #3b82f6;
        }
        body {
            font-family: 'Inter', sans-serif;
            background-color: var(--bg-body);
            color: var(--text-main);
            padding-bottom: 50px;
        }
        .navbar-custom {
            background: rgba(255, 255, 255, 0.9);
            backdrop-filter: blur(10px);
            border-bottom: 1px solid #e2e8f0;
            position: sticky;
            top: 0;
            z-index: 1000;
        }
        .ekosistem-card {
            background: var(--card-bg);
            border: 1px solid rgba(0,0,0,0.05);
            border-radius: 20px;
            padding: 24px;
            height: 100%;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            text-decoration: none;
            color: inherit;
            display: flex;
            flex-direction: column;
            position: relative;
            overflow: hidden;
        }
        .ekosistem-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 30px rgba(0,0,0,0.08);
            border-color: rgba(0,0,0,0.1);
        }
        .icon-box {
            width: 50px;
            height: 50px;
            border-radius: 14px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            margin-bottom: 16px;
        }
        .card-title {
            font-weight: 700;
            font-size: 18px;
            margin-bottom: 8px;
        }
        .card-desc {
            font-size: 13px;
            color: var(--text-muted);
            line-height: 1.5;
            margin-bottom: 20px;
            flex-grow: 1;
        }
        .card-footer-custom {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-top: 1px solid #f1f5f9;
            padding-top: 16px;
            font-size: 12px;
            font-weight: 600;
        }
        .badge-pro {
            background: linear-gradient(90deg, #f59e0b, #d97706);
            color: white;
            font-size: 10px;
            padding: 4px 8px;
            border-radius: 12px;
            position: absolute;
            top: 20px;
            right: 20px;
        }
        
        /* Gradients for Icons */
        .ic-analisis { background: #f0fdf4; color: #10b981; }
        .ic-llm { background: #fffbeb; color: #f59e0b; }
        .ic-iot { background: #ecfeff; color: #06b6d4; }
        .ic-sim { background: #eff6ff; color: #3b82f6; }
        .ic-agent { background: #faf5ff; color: #a855f7; }
        .ic-broadcast { background: #fdf2f8; color: #ec4899; }
        
        /* Dashboard Stats Widget */
        .stat-card {
            background: linear-gradient(135deg, #1e293b, #0f172a);
            color: white;
            border-radius: 24px;
            padding: 30px;
            margin-bottom: 30px;
            box-shadow: 0 10px 25px rgba(15, 23, 42, 0.2);
            position: relative;
            overflow: hidden;
        }
        .stat-card::after {
            content: '';
            position: absolute;
            top: -50%;
            right: -10%;
            width: 300px;
            height: 300px;
            background: radial-gradient(circle, rgba(59,130,246,0.3) 0%, rgba(0,0,0,0) 70%);
            border-radius: 50%;
        }
    </style>
</head>
<body>

<nav class="navbar navbar-custom py-3 shadow-sm">
    <div class="container d-flex justify-content-between align-items-center">
        <a href="index.php" class="text-decoration-none d-flex align-items-center text-dark">
            <i class="bi bi-arrow-left me-2 fs-5"></i>
            <span class="fw-bold fs-5">Kembali ke Chat</span>
        </a>
        <div class="d-flex align-items-center gap-3">
            <div class="text-end d-none d-md-block">
                <div class="fw-bold fs-6"><?= htmlspecialchars($nama_siswa) ?></div>
                <div class="text-muted" style="font-size: 11px;"><?= strtoupper($raw_status) ?> ACCOUNT</div>
            </div>
            <div class="bg-primary text-white rounded-circle d-flex align-items-center justify-content-center" style="width: 40px; height: 40px;">
                <i class="bi bi-person-fill"></i>
            </div>
        </div>
    </div>
</nav>

<div class="container mt-4 pt-2">
    <!-- Header Widget -->
    <div class="stat-card">
        <div class="row align-items-center position-relative" style="z-index: 10;">
            <div class="col-md-7">
                <h2 class="fw-bold mb-2">Portal Ekosistem Terpadu</h2>
                <p class="text-white-50 mb-4" style="font-size: 15px;">Akses seluruh modul kecerdasan buatan, integrasi perangkat IoT, dan analitik data dari satu pusat kontrol.</p>
                <div class="d-flex gap-4">
                    <div>
                        <div class="text-white-50" style="font-size: 11px; text-transform: uppercase; letter-spacing: 1px;">IoT Terhubung</div>
                        <div class="fs-4 fw-bold"><?= $total_iot ?> <span class="fs-6 fw-normal text-white-50">Alat</span></div>
                    </div>
                    <div>
                        <div class="text-white-50" style="font-size: 11px; text-transform: uppercase; letter-spacing: 1px;">Status API</div>
                        <div class="fs-4 fw-bold text-success"><i class="bi bi-check-circle-fill fs-5 me-1"></i> <?= $has_api_key ? 'Aktif' : 'Standby' ?></div>
                    </div>
                </div>
            </div>
            <div class="col-md-5 text-end d-none d-md-block">
                <i class="bi bi-grid-fill text-white-50" style="font-size: 120px; opacity: 0.2;"></i>
            </div>
        </div>
    </div>

    <!-- Modul Grid -->
    <div class="row g-4">
        
        <!-- Analisis Trend -->
        <div class="col-md-4">
            <a href="analisis/index.php" class="ekosistem-card">
                <?php if ($raw_status !== 'pro' && $role_user !== 'admin'): ?>
                    <span class="badge-pro">PRO</span>
                <?php endif; ?>
                <div class="icon-box ic-analisis"><i class="bi bi-graph-up-arrow"></i></div>
                <div class="card-title">Analisis Trend</div>
                <div class="card-desc">Visualisasi analitik berbasis AI. Memantau pertumbuhan data dan memprediksi tren masa depan dengan algoritma cerdas.</div>
                <div class="card-footer-custom">
                    <span class="text-success"><i class="bi bi-activity"></i> Sistem Normal</span>
                    <i class="bi bi-arrow-right text-muted"></i>
                </div>
            </a>
        </div>

        <!-- LLM API Key -->
        <div class="col-md-4">
            <a href="llm/index.php" class="ekosistem-card">
                <div class="icon-box ic-llm"><i class="bi bi-key-fill"></i></div>
                <div class="card-title">LLM API Key</div>
                <div class="card-desc">Atur kredensial model bahasamu (Xieqa LLM) untuk mendapatkan respons kustom sesuai kebutuhan ruang kerja.</div>
                <div class="card-footer-custom">
                    <span class="text-warning"><i class="bi bi-shield-lock-fill"></i> Konfigurasi Privat</span>
                    <i class="bi bi-arrow-right text-muted"></i>
                </div>
            </a>
        </div>

        <!-- IoT Integrasi -->
        <div class="col-md-4">
            <a href="apikey/index.php" class="ekosistem-card">
                <div class="icon-box ic-iot"><i class="bi bi-cpu-fill"></i></div>
                <div class="card-title">IoT Integrasi</div>
                <div class="card-desc">Manajemen perangkat keras (Hardware). Sambungkan mikrokontroler (ESP32/Arduino) dengan AI melalui token akses aman.</div>
                <div class="card-footer-custom">
                    <span class="text-info"><i class="bi bi-router-fill"></i> <?= $total_iot ?> Perangkat Aktif</span>
                    <i class="bi bi-arrow-right text-muted"></i>
                </div>
            </a>
        </div>

        <!-- IoT Simulator -->
        <div class="col-md-4">
            <a href="robot_simulator/index.php" class="ekosistem-card">
                <div class="icon-box ic-sim"><i class="bi bi-display"></i></div>
                <div class="card-title">IoT Simulator 3D</div>
                <div class="card-desc">Uji coba interaksi alat secara virtual. Modul ruang simulasi visual 3D untuk melihat pergerakan robot dan sensor sebelum diimplementasi.</div>
                <div class="card-footer-custom">
                    <span class="text-primary"><i class="bi bi-box-fill"></i> Virtual Reality</span>
                    <i class="bi bi-arrow-right text-muted"></i>
                </div>
            </a>
        </div>

        <!-- Agent Koding -->
        <div class="col-md-4">
            <a href="autokoding/index.php" class="ekosistem-card">
                <?php if ($raw_status !== 'pro' && $role_user !== 'admin'): ?>
                    <span class="badge-pro">PRO</span>
                <?php endif; ?>
                <div class="icon-box ic-agent"><i class="bi bi-robot"></i></div>
                <div class="card-title">Agent Koding</div>
                <div class="card-desc">Asisten pembuat aplikasi *fullstack* otomatis. Masukkan spesifikasi, dan agen akan membangun kode di ruang *sandbox* yang aman.</div>
                <div class="card-footer-custom">
                    <span class="text-purple" style="color: #a855f7;"><i class="bi bi-code-slash"></i> Advanced AI</span>
                    <i class="bi bi-arrow-right text-muted"></i>
                </div>
            </a>
        </div>

        <!-- Broadcast -->
        <div class="col-md-4">
            <a href="generate_share.php" class="ekosistem-card">
                <div class="icon-box ic-broadcast"><i class="bi bi-broadcast"></i></div>
                <div class="card-title">Pusat Broadcast</div>
                <div class="card-desc">Sistem distribusi tugas masal. Kirimkan *prompt* rahasia untuk langsung ditangkap oleh seluruh jaringan *user* terkait.</div>
                <div class="card-footer-custom">
                    <?php if ($role_user === 'admin' || $status_broadcast === 'approved'): ?>
                        <span class="text-success"><i class="bi bi-check-circle-fill"></i> Akses Diizinkan</span>
                    <?php else: ?>
                        <span class="text-danger"><i class="bi bi-lock-fill"></i> Perlu Persetujuan</span>
                    <?php endif; ?>
                    <i class="bi bi-arrow-right text-muted"></i>
                </div>
            </a>
        </div>

    </div>
</div>

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