-- 086_settings_users_manage_package.sql
-- Satukan flow aktif admin user management ke settings.users.manage

INSERT INTO permissions (code, name, module, created_at)
SELECT 'settings.users.manage', 'Kelola Users', 'settings', NOW()
WHERE NOT EXISTS (
  SELECT 1 FROM permissions WHERE code = 'settings.users.manage'
);

UPDATE permissions
SET name = 'Kelola Users',
    module = 'settings'
WHERE code = 'settings.users.manage';

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT DISTINCT rp.role_id, p_manage.id
FROM role_permissions rp
JOIN permissions p_old ON p_old.id = rp.permission_id
JOIN permissions p_manage ON p_manage.code = 'settings.users.manage'
WHERE p_old.code IN ('settings.users.view', 'settings.users.add', 'settings.users.edit', 'settings.users.toggle');
