<?php
define('SUPPRESS_ERRORS', true);
include_once('../includes/config.php');

header('Content-Type: application/json');

// Get all permissions grouped by category
$permissionsQuery = "SELECT * FROM tblpermissions ORDER BY Category, PermissionName";
$permissionsResult = mysqli_query($con, $permissionsQuery);

$permissions = [];
$permissionsByCategory = [];

if ($permissionsResult) {
    while ($permission = mysqli_fetch_assoc($permissionsResult)) {
        $permissions[] = $permission;
        $category = $permission['Category'] ?? 'Other';
        if (!isset($permissionsByCategory[$category])) {
            $permissionsByCategory[$category] = [];
        }
        $permissionsByCategory[$category][] = $permission;
    }
}

echo json_encode([
    'success' => true,
    'permissions' => $permissions,
    'permissionsByCategory' => $permissionsByCategory
]);
?>
