-- Migration: Grant all modules to admin@cybexpte.com user and dev group
-- Date: 2025-11-27
-- Description: Delete existing module permissions for admin user and dev group, then grant access to all modules

-- Delete existing user module permissions for admin user
DELETE FROM user_module_permissions 
WHERE user_id = '0194a8b2-1111-7123-8123-123456789ac0'::uuid;

-- Grant access to all modules in the modules table for admin user
INSERT INTO user_module_permissions (user_id, module_id, action)
SELECT 
    '0194a8b2-1111-7123-8123-123456789ac0'::uuid,
    m.module_id,
    'allow'
FROM modules m
WHERE NOT EXISTS (
    SELECT 1 FROM user_module_permissions ump
    WHERE ump.user_id = '0194a8b2-1111-7123-8123-123456789ac0'::uuid
    AND ump.module_id = m.module_id
);

-- Delete existing group module permissions for dev group
DELETE FROM group_module_permissions 
WHERE group_id = '0194a8b2-eeee-7123-8123-123456789ace'::uuid;

-- Grant access to all modules in the modules table for dev group
INSERT INTO group_module_permissions (group_id, module_id, action)
SELECT 
    '0194a8b2-eeee-7123-8123-123456789ace'::uuid,
    m.module_id,
    'allow'
FROM modules m
WHERE NOT EXISTS (
    SELECT 1 FROM group_module_permissions gmp
    WHERE gmp.group_id = '0194a8b2-eeee-7123-8123-123456789ace'::uuid
    AND gmp.module_id = m.module_id
);

-- Rollback section (commented out)
/*
-- To rollback, delete the granted permissions
DELETE FROM user_module_permissions 
WHERE user_id = '0194a8b2-1111-7123-8123-123456789ac0'::uuid;

DELETE FROM group_module_permissions 
WHERE group_id = '0194a8b2-eeee-7123-8123-123456789ace'::uuid;
*/
