<?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/fonts.css">
    <link rel="stylesheet" href="assets/css/style.css?v=<?= time() ?>">
</head>
<body>
    <div class="dashboard-container">
        <!-- Sidebar -->
        <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_purchases.php"><span>Purchase Requests</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" class="btn btn-danger btn-sm" style="width: 100%;">Logout</a>
            </div>
        </aside>

        <!-- Main Content -->
        <main class="main-content">
            <header class="mb-3">
                <h1 class="fw-bold" style="font-size: 2.5rem;">User Management</h1>
                <p class="text-secondary">Manage access levels and monitor activity of marketplace users.</p>
            </header>

            <div class="glass" style="padding: 2.5rem;">
                <div class="table-container">
                    <table>
                        <thead>
                            <tr>
                                <th>User Info</th>
                                <th>Phone/Address</th>
                                <th>Role</th>
                                <th>Status</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($users as $u): ?>
                            <tr>
                                <td>
                                    <div class="fw-bold"><?= htmlspecialchars($u['name']) ?></div>
                                    <div class="text-sm"><?= htmlspecialchars($u['email']) ?></div>
                                </td>
                                <td>
                                    <div class="fw-bold text-sm"><?= htmlspecialchars($u['phone'] ?: 'N/A') ?></div>
                                    <div class="text-sm text-secondary" style="max-width: 250px; overflow: hidden; text-overflow: ellipsis;"><?= htmlspecialchars($u['address'] ?: 'N/A') ?></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>
                                    <div style="display: flex; gap: 1rem;">
                                        <?php if ($u['status'] === 'active'): ?>
                                            <a href="admin_users.php?action=deactivate&id=<?= $u['id'] ?>" class="text-gold text-sm fw-bold" style="text-decoration: none;">Deactivate</a>
                                        <?php else: ?>
                                            <a href="admin_users.php?action=activate&id=<?= $u['id'] ?>" class="text-gold text-sm fw-bold" style="text-decoration: none;">Activate</a>
                                        <?php endif; ?>
                                        <a href="admin_users.php?action=delete&id=<?= $u['id'] ?>" onclick="return confirm('Delete this user?')" class="text-sm fw-bold" style="color: var(--danger); text-decoration: none;">Delete</a>
                                    </div>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </main>
    </div>
</body>
</html>