<?php
require_once '../includes/init.php';

// Check admin access
if (!isAuthenticated() || !$_SESSION['is_admin']) {
    redirect(APP_URL . '/login.php');
}

$_SESSION['admin_id'] = $_SESSION['user_id'];

$pageTitle = 'Admin Dashboard';
$db = getDB();

// Get statistics
$stats = [];

// Total users
$stmt = $db->query("SELECT COUNT(*) FROM users WHERE is_admin = 0");
$stats['total_users'] = $stmt->fetchColumn();

// Pending approvals
$stmt = $db->query("SELECT COUNT(*) FROM users WHERE status = 'pending'");
$stats['pending_users'] = $stmt->fetchColumn();

// Active users today
$stmt = $db->query("SELECT COUNT(*) FROM users WHERE last_activity > DATE_SUB(NOW(), INTERVAL 24 HOUR)");
$stats['active_today'] = $stmt->fetchColumn();

// Total messages today
$stmt = $db->query("SELECT COUNT(*) FROM messages WHERE DATE(created_at) = CURDATE()");
$stats['messages_today'] = $stmt->fetchColumn();

// Online users
$stmt = $db->query("SELECT COUNT(*) FROM online_status WHERE is_online = 1");
$stats['online_now'] = $stmt->fetchColumn();

// Recent users
$stmt = $db->query("SELECT * FROM users WHERE is_admin = 0 ORDER BY created_at DESC LIMIT 10");
$recentUsers = $stmt->fetchAll();

// Recent activity
$stmt = $db->query("SELECT al.*, u.name, u.unique_id FROM activity_logs al LEFT JOIN users u ON al.user_id = u.id ORDER BY al.created_at DESC LIMIT 20");
$recentActivity = $stmt->fetchAll();

// Pending users list
$stmt = $db->query("SELECT * FROM users WHERE status = 'pending' ORDER BY created_at DESC");
$pendingUsers = $stmt->fetchAll();

include '../includes/header.php';
?>

<div class="min-h-screen bg-slate-50 dark:bg-slate-900">
    <!-- Admin Header -->
    <header class="bg-white dark:bg-slate-800 border-b border-slate-200 dark:border-slate-700 sticky top-0 z-20">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex items-center justify-between h-16">
                <div class="flex items-center gap-4">
                    <div class="text-primary flex items-center gap-2">
                        <span class="material-symbols-outlined text-2xl">admin_panel_settings</span>
                        <span class="font-bold text-lg">Admin Panel</span>
                    </div>
                </div>
                <div class="flex items-center gap-4">
                    <a href="../chats.php" class="text-slate-600 dark:text-slate-400 hover:text-primary transition-colors">
                        <span class="material-symbols-outlined">chat</span>
                    </a>
                    <a href="../logout.php" class="text-slate-600 dark:text-slate-400 hover:text-red-500 transition-colors">
                        <span class="material-symbols-outlined">logout</span>
                    </a>
                </div>
            </div>
        </div>
    </header>

    <!-- Navigation -->
    <nav class="bg-white dark:bg-slate-800 border-b border-slate-200 dark:border-slate-700">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex gap-6 overflow-x-auto no-scrollbar">
                <a href="dashboard.php" class="flex items-center gap-2 py-4 border-b-2 border-primary text-primary font-medium whitespace-nowrap">
                    <span class="material-symbols-outlined text-sm">dashboard</span>
                    Dashboard
                </a>
                <a href="users.php" class="flex items-center gap-2 py-4 border-b-2 border-transparent text-slate-600 dark:text-slate-400 hover:text-primary font-medium whitespace-nowrap">
                    <span class="material-symbols-outlined text-sm">people</span>
                    Users
                    <?php if ($stats['pending_users'] > 0): ?>
                    <span class="bg-red-500 text-white text-xs px-2 py-0.5 rounded-full"><?php echo $stats['pending_users']; ?></span>
                    <?php endif; ?>
                </a>
                <a href="chats.php" class="flex items-center gap-2 py-4 border-b-2 border-transparent text-slate-600 dark:text-slate-400 hover:text-primary font-medium whitespace-nowrap">
                    <span class="material-symbols-outlined text-sm">chat</span>
                    Chats
                </a>
                <a href="announcements.php" class="flex items-center gap-2 py-4 border-b-2 border-transparent text-slate-600 dark:text-slate-400 hover:text-primary font-medium whitespace-nowrap">
                    <span class="material-symbols-outlined text-sm">campaign</span>
                    Announcements
                </a>
                <a href="settings.php" class="flex items-center gap-2 py-4 border-b-2 border-transparent text-slate-600 dark:text-slate-400 hover:text-primary font-medium whitespace-nowrap">
                    <span class="material-symbols-outlined text-sm">settings</span>
                    Settings
                </a>
            </div>
        </div>
    </nav>

    <!-- Main Content -->
    <main class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
        <!-- Stats Grid -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
            <a href="auto-logout-settings.php" class="bg-white dark:bg-slate-800 rounded-xl border border-slate-200 dark:border-slate-700 p-6 hover:shadow-lg transition-shadow">
                <div class="flex items-center justify-between mb-4">
                    <div class="p-3 bg-blue-100 dark:bg-blue-900/20 rounded-lg">
                        <span class="material-symbols-outlined text-blue-600 dark:text-blue-400">timer</span>
                    </div>
                    <span class="text-sm text-slate-500 dark:text-slate-400">Configure</span>
                </div>
                <h3 class="text-lg font-semibold text-slate-900 dark:text-slate-100">Auto-Logout</h3>
                <p class="text-sm text-slate-600 dark:text-slate-400">Manage automatic logout settings</p>
            </a>
            <a href="users.php" class="bg-white dark:bg-slate-800 rounded-xl border border-slate-200 dark:border-slate-700 p-6 hover:shadow-lg transition-shadow">
                <div class="flex items-center justify-between mb-4">
                    <div class="p-3 bg-green-100 dark:bg-green-900/20 rounded-lg">
                        <span class="material-symbols-outlined text-green-600 dark:text-green-400">people</span>
                    </div>
                    <span class="text-sm text-slate-500 dark:text-slate-400">Manage</span>
                </div>
                <h3 class="text-lg font-semibold text-slate-900 dark:text-slate-100">Users</h3>
                <p class="text-sm text-slate-600 dark:text-slate-400">Manage user accounts</p>
            </a>
            <a href="chats.php" class="bg-white dark:bg-slate-800 rounded-xl border border-slate-200 dark:border-slate-700 p-6 hover:shadow-lg transition-shadow">
                <div class="flex items-center justify-between mb-4">
                    <div class="p-3 bg-purple-100 dark:bg-purple-900/20 rounded-lg">
                        <span class="material-symbols-outlined text-purple-600 dark:text-purple-400">chat</span>
                    </div>
                    <span class="text-sm text-slate-500 dark:text-slate-400">Monitor</span>
                </div>
                <h3 class="text-lg font-semibold text-slate-900 dark:text-slate-100">Chats</h3>
                <p class="text-sm text-slate-600 dark:text-slate-400">Monitor chat activity</p>
            </a>
            <a href="settings.php" class="bg-white dark:bg-slate-800 rounded-xl border border-slate-200 dark:border-slate-700 p-6 hover:shadow-lg transition-shadow">
                <div class="flex items-center justify-between mb-4">
                    <div class="p-3 bg-orange-100 dark:bg-orange-900/20 rounded-lg">
                        <span class="material-symbols-outlined text-orange-600 dark:text-orange-400">settings</span>
                    </div>
                    <span class="text-sm text-slate-500 dark:text-slate-400">Configure</span>
                </div>
                <h3 class="text-lg font-semibold text-slate-900 dark:text-slate-100">Settings</h3>
                <p class="text-sm text-slate-600 dark:text-slate-400">System settings</p>
            </a>
        </div>

        <div class="grid lg:grid-cols-2 gap-8">
            <!-- Pending Approvals -->
            <div class="bg-white dark:bg-slate-800 rounded-xl border border-slate-200 dark:border-slate-700 overflow-hidden">
                <div class="p-4 border-b border-slate-200 dark:border-slate-700 flex items-center justify-between">
                    <h3 class="font-semibold text-slate-900 dark:text-white flex items-center gap-2">
                        <span class="material-symbols-outlined text-amber-500">pending_actions</span>
                        Pending Approvals
                    </h3>
                    <?php if (!empty($pendingUsers)): ?>
                    <a href="users.php?filter=pending" class="text-sm text-primary hover:underline">View All</a>
                    <?php endif; ?>
                </div>
                <div class="divide-y divide-slate-200 dark:divide-slate-700">
                    <?php if (empty($pendingUsers)): ?>
                    <div class="p-8 text-center">
                        <span class="material-symbols-outlined text-4xl text-slate-300 mb-2">check_circle</span>
                        <p class="text-slate-500">No pending approvals</p>
                    </div>
                    <?php else: ?>
                        <?php foreach (array_slice($pendingUsers, 0, 5) as $user): ?>
                        <div class="p-4 flex items-center justify-between hover:bg-slate-50 dark:hover:bg-slate-700/50">
                            <div class="flex items-center gap-3">
                                <?php if ($user['profile_photo']): ?>
                                <img src="../uploads/images/<?php echo htmlspecialchars($user['profile_photo']); ?>" class="w-10 h-10 rounded-full object-cover">
                                <?php else: ?>
                                <div class="w-10 h-10 rounded-full bg-primary/10 flex items-center justify-center">
                                    <span class="material-symbols-outlined text-primary">person</span>
                                </div>
                                <?php endif; ?>
                                <div>
                                    <p class="font-medium text-slate-900 dark:text-white"><?php echo htmlspecialchars($user['name']); ?></p>
                                    <p class="text-sm text-slate-500">@<?php echo htmlspecialchars($user['unique_id']); ?></p>
                                </div>
                            </div>
                            <div class="flex gap-2">
                                <form method="POST" action="user-action.php" class="inline">
                                    <input type="hidden" name="action" value="approve">
                                    <input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
                                    <button type="submit" class="px-3 py-1.5 bg-green-500 text-white rounded-lg text-sm font-medium hover:bg-green-600 transition-colors">Approve</button>
                                </form>
                                <form method="POST" action="user-action.php" class="inline">
                                    <input type="hidden" name="action" value="reject">
                                    <input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
                                    <button type="submit" class="px-3 py-1.5 bg-red-500 text-white rounded-lg text-sm font-medium hover:bg-red-600 transition-colors">Reject</button>
                                </form>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Recent Activity -->
            <div class="bg-white dark:bg-slate-800 rounded-xl border border-slate-200 dark:border-slate-700 overflow-hidden">
                <div class="p-4 border-b border-slate-200 dark:border-slate-700">
                    <h3 class="font-semibold text-slate-900 dark:text-white flex items-center gap-2">
                        <span class="material-symbols-outlined text-blue-500">history</span>
                        Recent Activity
                    </h3>
                </div>
                <div class="max-h-[400px] overflow-y-auto">
                    <div class="divide-y divide-slate-200 dark:divide-slate-700">
                        <?php foreach ($recentActivity as $activity): ?>
                        <div class="p-4 hover:bg-slate-50 dark:hover:bg-slate-700/50">
                            <div class="flex items-start gap-3">
                                <span class="material-symbols-outlined text-slate-400 mt-0.5">
                                    <?php echo $activity['action'] === 'login' ? 'login' : ($activity['action'] === 'signup' ? 'person_add' : 'info'); ?>
                                </span>
                                <div class="flex-1">
                                    <p class="text-sm text-slate-900 dark:text-white">
                                        <span class="font-medium"><?php echo $activity['name'] ?: 'System'; ?></span>
                                        <?php echo $activity['action'] === 'login' ? 'logged in' : ($activity['action'] === 'signup' ? 'signed up' : $activity['action']); ?>
                                    </p>
                                    <p class="text-xs text-slate-500"><?php echo formatDateTime($activity['created_at']); ?></p>
                                    <?php if ($activity['ip_address']): ?>
                                    <p class="text-xs text-slate-400">IP: <?php echo $activity['ip_address']; ?></p>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- Quick Actions -->
        <div class="mt-8 bg-white dark:bg-slate-800 rounded-xl border border-slate-200 dark:border-slate-700 p-6">
            <h3 class="font-semibold text-slate-900 dark:text-white mb-4">Quick Actions</h3>
            <div class="flex flex-wrap gap-3">
                <a href="users.php" class="flex items-center gap-2 px-4 py-2 bg-primary text-white rounded-lg font-medium hover:bg-primary/90 transition-colors">
                    <span class="material-symbols-outlined">manage_accounts</span>
                    Manage Users
                </a>
                <a href="announcements.php" class="flex items-center gap-2 px-4 py-2 bg-slate-100 dark:bg-slate-700 text-slate-700 dark:text-slate-300 rounded-lg font-medium hover:bg-slate-200 transition-colors">
                    <span class="material-symbols-outlined">campaign</span>
                    Send Announcement
                </a>
                <a href="export.php" class="flex items-center gap-2 px-4 py-2 bg-slate-100 dark:bg-slate-700 text-slate-700 dark:text-slate-300 rounded-lg font-medium hover:bg-slate-200 transition-colors">
                    <span class="material-symbols-outlined">download</span>
                    Export Data
                </a>
            </div>
        </div>
    </main>
</div>

<?php $hideFooter = true; include '../includes/footer.php'; ?>
