<?php
// Permission management helpers
// Requires: config/auth.php loaded

function getPermissionList() {
    $pdo = getDB();
    return $pdo->query("SELECT * FROM permissions ORDER BY module_name, permission_name")->fetchAll();
}

function getGroupedPermissions() {
    $perms = getPermissionList();
    $grouped = [];
    foreach ($perms as $p) {
        $grouped[$p['module_name']][] = $p;
    }
    return $grouped;
}

function getRolePermissions($role_id) {
    $pdo = getDB();
    $stmt = $pdo->prepare("SELECT permission_id FROM role_permissions WHERE role_id = ?");
    $stmt->execute([$role_id]);
    return $stmt->fetchAll(PDO::FETCH_COLUMN);
}
