<?php
include '../db.php';

$roleId = intval($_GET['role_id'] ?? 0);

// Get all rights grouped by category
$rightsResult = $conn->query("SELECT id, name, category FROM rights ORDER BY category, name");
$rightsByCategory = [];

while ($row = $rightsResult->fetch_assoc()) {
    $cat = $row['category'] ?: 'Uncategorized';
    $rightsByCategory[$cat][] = [
        'id' => $row['id'],
        'name' => $row['name']
    ];
}

// Get assigned rights
$assignedResult = $conn->query("SELECT right_id FROM role_rights WHERE role_id = $roleId");
$assignedRights = array_column($assignedResult->fetch_all(MYSQLI_ASSOC), 'right_id');

// Return as JSON
header('Content-Type: application/json');
echo json_encode([
  'grouped' => $rightsByCategory,
  'assigned' => $assignedRights
]);
