-- apply_profile_to_user.sql
USE financeiro;

-- Exemplo: aplicar perfil "Financeiro" a um usuário pelo e-mail
SET @p = (SELECT id FROM permission_profiles WHERE name='Financeiro' LIMIT 1);
SET @uid = (SELECT id FROM users WHERE email='financeiro@empresa.com' LIMIT 1);

UPDATE users SET profile_id=@p WHERE id=@uid;

DELETE FROM user_permissions WHERE user_id=@uid;

INSERT INTO user_permissions (user_id, perm_key, can_view, can_create, can_edit, can_delete)
SELECT @uid, perm_key, can_view, can_create, can_edit, can_delete
FROM profile_permissions
WHERE profile_id=@p;
