<?php
session_start();

require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../classes/Database.php';
require_once __DIR__ . '/../../classes/Category.php';

$database = new Database();
$conn = $database->getConnection();
$auth = new Auth($conn);

if (!isset($_SESSION['user_id'])) {
    header('Location: ../../login.php');
    exit();
}

$auth->enforceSuperAdminPrivileges($_SESSION['user_id']);

$category = new Category($conn);
$categories = $category->getAllCategories();
?>

<?php include __DIR__ . '/../../includes/header.php'; ?>
<link rel="stylesheet" href="../../assets/css/simple.css">
    <style>
        .back-link {
            display: inline-block;
            margin-bottom: 15px;
            padding: 10px 15px;
            background-color: #3498db;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            transition: background-color 0.3s ease;
        }

        .back-link:hover {
            background-color: #2980b9;
            color: white;
            text-decoration: none;
        }

        .page-actions {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
            margin: 15px 0 20px;
        }

        .action-link {
            display: inline-block;
            padding: 8px 12px;
            border-radius: 5px;
            color: #fff;
            text-decoration: none;
            background-color: #007bff;
        }

        .action-link.secondary {
            background-color: #6c757d;
        }

        .action-link.warning {
            background-color: #ffc107;
            color: #212529;
        }

        .action-link:hover {
            opacity: 0.9;
            color: #fff;
            text-decoration: none;
        }

        .empty-state {
            text-align: center;
            color: #555;
        }
    </style>

    <a href="../dashboard/admin.php" class="back-link">Back to Dashboard</a>

    <h2>Manage Categories</h2>

    <div class="page-actions">
        <a href="add_category_form.php" class="action-link">Add New Category</a>
    </div>

    <table>
        <thead>
            <tr>
                <th>Category Name</th>
                <th>Description</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php if (!empty($categories)): ?>
                <?php foreach ($categories as $categoryRow): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($categoryRow['category_name'] ?? ''); ?></td>
                        <td><?php echo htmlspecialchars($categoryRow['category_description'] ?? ''); ?></td>
                        <td>
                            <?php if (isset($categoryRow['category_id'])): ?>
                                <a href="edit_category.php?id=<?php echo urlencode($categoryRow['category_id']); ?>" class="action-link warning">Edit</a>
                            <?php else: ?>
                                <span class="empty-state">ID not found</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr>
                    <td colspan="3" class="empty-state">No categories found.</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
<?php include __DIR__ . '/../../includes/footer.php'; ?>
