<?php
require_once __DIR__ . '/../api/lib.php';
require_once __DIR__ . '/../partials/admin-sidebar.php';
hw_require_auth(['superadmin', 'admin']);
$authUser = hw_get_auth_user();

$db = hw_db();

// Get website stats
$stats = [];
$stats['total_users'] = $db->query("SELECT COUNT(*) FROM users")->fetchColumn();
$stats['total_coaches'] = $db->query("SELECT COUNT(*) FROM coaches")->fetchColumn();
$stats['total_appointments'] = $db->query("SELECT COUNT(*) FROM appointments")->fetchColumn();
$stats['pending_appointments'] = $db->query("SELECT COUNT(*) FROM appointments WHERE status = 'scheduled'")->fetchColumn();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Superadmin Dashboard | Herbal Wellness</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body {
            background: linear-gradient(135deg, #f0fdf4 0%, #f0fdf4 50%, #f9fafb 100%);
        }
        
        .stat-card {
            background: white;
            border-radius: 16px;
            padding: 28px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
            transition: all 0.3s;
            border-left: 4px solid transparent;
        }
        
        .stat-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
        }
        
        .stat-card.users { border-left-color: #3b82f6; }
        .stat-card.coaches { border-left-color: #10b981; }
        .stat-card.appointments { border-left-color: #8b5cf6; }
        .stat-card.scheduled { border-left-color: #f59e0b; }
        
        .stat-icon {
            width: 60px;
            height: 60px;
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 28px;
            margin-bottom: 16px;
        }
        
        .stat-icon.users { background: #dbeafe; color: #3b82f6; }
        .stat-icon.coaches { background: #dcfce7; color: #10b981; }
        .stat-icon.appointments { background: #ede9fe; color: #8b5cf6; }
        .stat-icon.scheduled { background: #fef3c7; color: #f59e0b; }
        
        .stat-number {
            font-size: 32px;
            font-weight: 700;
            color: #1f2937;
            margin-bottom: 8px;
        }
        
        .stat-label {
            font-size: 14px;
            color: #6b7280;
            font-weight: 500;
        }

        .config-card {
            background: white;
            border-radius: 12px;
            padding: 20px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
            transition: all 0.3s;
            border-left: 4px solid #e5e7eb;
            cursor: pointer;
        }

        .config-card:hover {
            box-shadow: 0 4px 16px rgba(0, 0, 0, 0.10);
            transform: translateY(-2px);
        }

        .config-card.security { border-left-color: #dc2626; }
        .config-card.integration { border-left-color: #2563eb; }
        .config-card.user { border-left-color: #7c3aed; }
        .config-card.emergency { border-left-color: #ea580c; }
        .config-card.offboard { border-left-color: #d946ef; }
        .config-card.audit { border-left-color: #0891b2; }
        .config-card.monitoring { border-left-color: #f43f5e; }
        .config-card.encryption { border-left-color: #06b6d4; }
        .config-card.subscription { border-left-color: #8b5cf6; }
        .config-card.billing { border-left-color: #16a34a; }

        .config-title {
            font-weight: 700;
            color: #1f2937;
            font-size: 15px;
            margin-bottom: 6px;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .config-desc {
            font-size: 13px;
            color: #6b7280;
            line-height: 1.4;
        }

        .section-header {
            display: flex;
            align-items: center;
            gap: 12px;
            margin-bottom: 20px;
            padding-bottom: 16px;
            border-bottom: 2px solid #e5e7eb;
        }

        .section-header h3 {
            font-size: 20px;
            font-weight: 700;
            color: #1f2937;
            margin: 0;
        }

        .section-header i {
            font-size: 24px;
        }
    </style>
</head>
<body>
    <div class="min-h-screen flex">
        <!-- Sidebar -->
        <?php renderAdminSidebar('dashboard'); ?>
        
        <!-- Main Content -->
        <div class="flex-1 p-8">
            <div class="mb-12">
                <h2 class="text-4xl font-bold text-gray-900 mb-2">Dashboard Overview</h2>
                <p class="text-gray-600">Welcome back, <?= htmlspecialchars($authUser['first_name']) ?>! Manage your platform and advanced settings.</p>
            </div>
            
            <!-- Stats Cards -->
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-12">
                <div class="stat-card users">
                    <div class="stat-icon users">
                        <i class="fas fa-users"></i>
                    </div>
                    <div class="stat-number"><?= $stats['total_users'] ?></div>
                    <div class="stat-label">Total Users</div>
                    <a href="/files/atasteofafricanmarketplace/superadmin/users.php" class="text-blue-600 hover:text-blue-800 text-sm font-semibold mt-4 inline-flex items-center gap-1">
                        View All <i class="fas fa-arrow-right"></i>
                    </a>
                </div>
                
                <div class="stat-card coaches">
                    <div class="stat-icon coaches">
                        <i class="fas fa-user-tie"></i>
                    </div>
                    <div class="stat-number"><?= $stats['total_coaches'] ?></div>
                    <div class="stat-label">Total Coaches</div>
                    <a href="/files/atasteofafricanmarketplace/superadmin/coaches.php" class="text-emerald-600 hover:text-emerald-800 text-sm font-semibold mt-4 inline-flex items-center gap-1">
                        Manage <i class="fas fa-arrow-right"></i>
                    </a>
                </div>
                
                <div class="stat-card appointments">
                    <div class="stat-icon appointments">
                        <i class="fas fa-calendar"></i>
                    </div>
                    <div class="stat-number"><?= $stats['total_appointments'] ?></div>
                    <div class="stat-label">Total Appointments</div>
                    <a href="/files/atasteofafricanmarketplace/superadmin/appointments.php" class="text-purple-600 hover:text-purple-800 text-sm font-semibold mt-4 inline-flex items-center gap-1">
                        View <i class="fas fa-arrow-right"></i>
                    </a>
                </div>
                
                <div class="stat-card scheduled">
                    <div class="stat-icon scheduled">
                        <i class="fas fa-clock"></i>
                    </div>
                    <div class="stat-number"><?= $stats['pending_appointments'] ?></div>
                    <div class="stat-label">Scheduled Today</div>
                    <a href="/files/atasteofafricanmarketplace/superadmin/appointments.php?status=scheduled" class="text-orange-600 hover:text-orange-800 text-sm font-semibold mt-4 inline-flex items-center gap-1">
                        View <i class="fas fa-arrow-right"></i>
                    </a>
                </div>
            </div>

            <!-- Quick Actions & System Status -->
            <div class="grid grid-cols-1 lg:grid-cols-3 gap-6 mb-12">
                <!-- Quick Actions -->
                <div class="stat-card">
                    <div class="section-header" style="border: none; padding: 0;">
                        <i class="fas fa-bolt text-emerald-600" style="font-size: 22px;"></i>
                        <h3 style="margin: 0;">Quick Actions</h3>
                    </div>
                    <div class="space-y-3">
                        <a href="/files/atasteofafricanmarketplace/superadmin/coaches.php?action=add" class="block w-full bg-gradient-to-r from-emerald-500 to-emerald-600 text-white text-center py-3 px-4 rounded-lg hover:shadow-lg transition font-semibold text-sm">
                            <i class="fas fa-plus mr-2"></i>Add Coach
                        </a>
                        <a href="/files/atasteofafricanmarketplace/superadmin/users.php" class="block w-full bg-gradient-to-r from-blue-500 to-blue-600 text-white text-center py-3 px-4 rounded-lg hover:shadow-lg transition font-semibold text-sm">
                            <i class="fas fa-users mr-2"></i>View Users
                        </a>
                        <a href="/files/atasteofafricanmarketplace/superadmin/settings.php" class="block w-full bg-gradient-to-r from-purple-500 to-purple-600 text-white text-center py-3 px-4 rounded-lg hover:shadow-lg transition font-semibold text-sm">
                            <i class="fas fa-cog mr-2"></i>Settings
                        </a>
                    </div>
                </div>

                <!-- System Status -->
                <div class="stat-card">
                    <div class="section-header" style="border: none; padding: 0;">
                        <i class="fas fa-heartbeat text-green-600" style="font-size: 22px;"></i>
                        <h3 style="margin: 0;">System Status</h3>
                    </div>
                    <div class="space-y-2">
                        <div style="padding: 8px 0; display: flex; align-items: center; gap: 8px;">
                            <div style="width: 8px; height: 8px; background: #22c55e; border-radius: 50%; animation: pulse 2s infinite;"></div>
                            <span style="font-size: 13px; color: #6b7280;">Website Online</span>
                        </div>
                        <div style="padding: 8px 0; display: flex; align-items: center; gap: 8px;">
                            <div style="width: 8px; height: 8px; background: #22c55e; border-radius: 50%; animation: pulse 2s infinite;"></div>
                            <span style="font-size: 13px; color: #6b7280;">Database Connected</span>
                        </div>
                        <div style="padding: 8px 0; display: flex; align-items: center; gap: 8px;">
                            <div style="width: 8px; height: 8px; background: #22c55e; border-radius: 50%; animation: pulse 2s infinite;"></div>
                            <span style="font-size: 13px; color: #6b7280;">All Services Active</span>
                        </div>
                    </div>
                </div>

                <!-- Admin Shortcuts -->
                <div class="stat-card">
                    <div class="section-header" style="border: none; padding: 0;">
                        <i class="fas fa-link text-orange-600" style="font-size: 22px;"></i>
                        <h3 style="margin: 0;">Admin Shortcuts</h3>
                    </div>
                    <div class="space-y-2">
                        <a href="/files/atasteofafricanmarketplace/superadmin/coach-applications.php" style="padding: 8px 0; display: block; font-size: 13px; color: #3b82f6; font-weight: 500; text-decoration: none;">
                            <i class="fas fa-file-alt mr-2"></i>Coach Applications
                        </a>
                        <a href="/files/atasteofafricanmarketplace/superadmin/toa-members.php" style="padding: 8px 0; display: block; font-size: 13px; color: #10b981; font-weight: 500; text-decoration: none;">
                            <i class="fas fa-leaf mr-2"></i>TOA Members
                        </a>
                        <a href="/files/atasteofafricanmarketplace/superadmin/content.php" style="padding: 8px 0; display: block; font-size: 13px; color: #8b5cf6; font-weight: 500; text-decoration: none;">
                            <i class="fas fa-newspaper mr-2"></i>Content Management
                        </a>
                    </div>
                </div>
            </div>

            <!-- System Configuration Section -->
            <div class="mb-12">
                <div class="section-header">
                    <i class="fas fa-cogs text-emerald-600"></i>
                    <h3>System Configuration</h3>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                    <a href="/files/atasteofafricanmarketplace/superadmin/settings.php#security" class="config-card security">
                        <div class="config-title"><i class="fas fa-lock"></i>Global Security</div>
                        <div class="config-desc">2FA, password policies, session timeout</div>
                    </a>
                    <a href="/files/atasteofafricanmarketplace/superadmin/settings.php#integration" class="config-card integration">
                        <div class="config-title"><i class="fas fa-plug"></i>Integrations</div>
                        <div class="config-desc">PayPal, email service, analytics</div>
                    </a>
                    <a href="/files/atasteofafricanmarketplace/superadmin/settings.php#retention" class="config-card security" style="border-left-color: #f59e0b;">
                        <div class="config-title"><i class="fas fa-database"></i>Data Retention</div>
                        <div class="config-desc">Backup schedules, data purging</div>
                    </a>
                </div>
            </div>

            <!-- User & Role Management Section -->
            <div class="mb-12">
                <div class="section-header">
                    <i class="fas fa-user-shield text-purple-600"></i>
                    <h3>User & Role Management</h3>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                    <a href="/files/atasteofafricanmarketplace/superadmin/settings.php#roles" class="config-card user">
                        <div class="config-title"><i class="fas fa-user-tie"></i>Admin Roles</div>
                        <div class="config-desc">Assign limited admin rights to users</div>
                    </a>
                    <a href="/files/atasteofafricanmarketplace/superadmin/settings.php#recovery" class="config-card emergency">
                        <div class="config-title"><i class="fas fa-unlock"></i>Access Recovery</div>
                        <div class="config-desc">Unlock accounts, restore access</div>
                    </a>
                    <a href="/files/atasteofafricanmarketplace/superadmin/settings.php#offboard" class="config-card offboard">
                        <div class="config-title"><i class="fas fa-sign-out-alt"></i>Offboarding</div>
                        <div class="config-desc">User deletion and data handling</div>
                    </a>
                </div>
            </div>

            <!-- Security & Compliance Section -->
            <div class="mb-12">
                <div class="section-header">
                    <i class="fas fa-shield-alt text-red-600"></i>
                    <h3>Security & Compliance</h3>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                    <a href="/files/atasteofafricanmarketplace/superadmin/settings.php#audit" class="config-card audit">
                        <div class="config-title"><i class="fas fa-file-audit"></i>Audit Logging</div>
                        <div class="config-desc">Activity logs and audit reports</div>
                    </a>
                    <a href="/files/atasteofafricanmarketplace/superadmin/settings.php#monitoring" class="config-card monitoring">
                        <div class="config-title"><i class="fas fa-shield-alt"></i>Monitoring</div>
                        <div class="config-desc">Suspicious activity detection</div>
                    </a>
                    <a href="/files/atasteofafricanmarketplace/superadmin/settings.php#encryption" class="config-card encryption">
                        <div class="config-title"><i class="fas fa-key"></i>Encryption</div>
                        <div class="config-desc">SSL certificates and encryption keys</div>
                    </a>
                </div>
            </div>

            <!-- Billing & Subscriptions Section -->
            <div class="mb-12">
                <div class="section-header">
                    <i class="fas fa-credit-card text-green-600"></i>
                    <h3>Billing & Subscriptions</h3>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                    <a href="/files/atasteofafricanmarketplace/superadmin/subscriptions.php" class="config-card subscription">
                        <div class="config-title"><i class="fas fa-credit-card"></i>Subscriptions</div>
                        <div class="config-desc">Manage plans and upgrades</div>
                    </a>
                    <a href="/files/atasteofafricanmarketplace/superadmin/subscriptions.php" class="config-card billing">
                        <div class="config-title"><i class="fas fa-money-bill-wave"></i>Payment Methods</div>
                        <div class="config-desc">Payment gateways and invoices</div>
                    </a>
                    <a href="/files/atasteofafricanmarketplace/superadmin/subscriptions.php" class="config-card billing">
                        <div class="config-title"><i class="fas fa-chart-line"></i>Analytics</div>
                        <div class="config-desc">Revenue reports and metrics</div>
                    </a>
                </div>
            </div>

            <!-- Footer -->
            <div class="mt-12 pt-8 border-t border-gray-200 text-center text-gray-600">
                <p style="font-size: 13px;">Last updated: <span id="last-update"><?= date('M d, Y H:i'); ?></span></p>
            </div>
        </div>
    </div>

    <style>
        @keyframes pulse {
            0%, 100% {
                opacity: 1;
            }
            50% {
                opacity: 0.5;
            }
        }
    </style>

    <script>
        function openModal(modalId) {
            document.getElementById(modalId).classList.remove('hidden');
        }
        
        function closeModal(modalId) {
            document.getElementById(modalId).classList.add('hidden');
        }
    </script>
</body>
</html>