<?php
require_once __DIR__ . '/../../config/session.php';
require_once __DIR__ . '/../../config/db.php';
require_once __DIR__ . '/../../config/response.php';

require_login();

$raw = json_decode(file_get_contents('php://input'), true);
$role_id     = (int)($raw['role_id'] ?? 0);
$permissions = $raw['permissions'] ?? [];

if (!$role_id) {
    jsonResponse(['success' => false, 'message' => 'role_id দিন'], 422);
}

// আগের সব permission মুছো
$pdo->prepare("DELETE FROM role_permissions WHERE role_id = ?")->execute([$role_id]);

// নতুন permission যোগ করো
$stmt = $pdo->prepare("INSERT IGNORE INTO role_permissions (role_id, permission_id) VALUES (?, ?)");
foreach ($permissions as $perm_id) {
    $stmt->execute([$role_id, (int)$perm_id]);
}

jsonResponse(['success' => true, 'message' => 'Permission সেট হয়েছে']);