<?php
session_start();
require_once __DIR__ . '/db/config.php';

if (!isset($_SESSION['user_id']) || ($_SESSION['role'] ?? '') !== 'admin') {
    header('Location: login.php');
    exit;
}

$pdo = db();

if (isset($_GET['action']) && isset($_GET['id'])) {
    $id = $_GET['id'];
    if ($_GET['action'] === 'activate') {
        $pdo->prepare("UPDATE users SET status = 'active' WHERE id = ?")->execute([$id]);
    } elseif ($_GET['action'] === 'deactivate') {
        $pdo->prepare("UPDATE users SET status = 'inactive' WHERE id = ?")->execute([$id]);
    } elseif ($_GET['action'] === 'delete') {
        $pdo->prepare("UPDATE users SET deleted_at = NOW() WHERE id = ?")->execute([$id]);
    }
    header('Location: admin_users.php');
    exit;
}

$users = $pdo->query("SELECT * FROM users WHERE deleted_at IS NULL ORDER BY created_at DESC")->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Users | Admin</title>
    <link rel="stylesheet" href="assets/css/style.css?v=<?= time() ?>">
</head>
<body style="background: #050505;">
    <div class="dashboard-container">
        <aside class="sidebar">
            <a href="index.php" class="sidebar-brand">AFGCARS</a>
            <ul class="sidebar-menu">
                <li><a href="admin_dashboard.php"><span>Dashboard</span></a></li>
                <li><a href="admin_cars.php"><span>Manage Cars</span></a></li>
                <li><a href="admin_users.php" class="active"><span>Users</span></a></li>
                <li><a href="admin_messages.php"><span>Messages</span></a></li>
            </ul>
            <div class="sidebar-footer">
                <a href="logout.php" style="color: var(--danger); text-decoration: none; font-weight: 600;">Logout</a>
            </div>
        </aside>

        <main class="main-content">
            <h1 style="margin-bottom: 2rem; font-weight: 900;">User Management</h1>
            
            <div class="glass" style="padding: 2rem;">
                <div class="table-container">
                    <table>
                        <thead>
                            <tr>
                                <th>User Info</th>
                                <th>Role</th>
                                <th>Status</th>
                                <th>Joined</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($users as $u): ?>
                            <tr>
                                <td>
                                    <div style="font-weight: 700;"><?= htmlspecialchars($u['name']) ?></div>
                                    <div style="font-size: 0.8rem; color: var(--text-secondary);"><?= htmlspecialchars($u['email']) ?></div>
                                </td>
                                <td><span class="badge badge-<?= $u['role'] === 'admin' ? 'danger' : 'success' ?>"><?= strtoupper($u['role']) ?></span></td>
                                <td><span class="badge badge-<?= $u['status'] === 'active' ? 'success' : 'warning' ?>"><?= ucfirst($u['status']) ?></span></td>
                                <td><?= date('M d, Y', strtotime($u['created_at'])) ?></td>
                                <td>
                                    <div style="display: flex; gap: 0.8rem;">
                                        <?php if ($u['status'] === 'active'): ?>
                                            <a href="admin_users.php?action=deactivate&id=<?= $u['id'] ?>" style="color: var(--warning); text-decoration: none; font-weight: 700; font-size: 0.8rem;">Deactivate</a>
                                        <?php else: ?>
                                            <a href="admin_users.php?action=activate&id=<?= $u['id'] ?>" style="color: var(--success); text-decoration: none; font-weight: 700; font-size: 0.8rem;">Activate</a>
                                        <?php endif; ?>
                                        <a href="admin_users.php?action=delete&id=<?= $u['id'] ?>" onclick="return confirm('Delete this user?')" style="color: var(--danger); text-decoration: none; font-weight: 700; font-size: 0.8rem;">Delete</a>
                                    </div>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </main>
    </div>
</body>
</html>