-- Ensure demo-seeded users have baseline platform roles for permission checks.

-- Give all demo-seeded users the base 'user' platform role
INSERT INTO user_roles (user_id, role_id, community_id, granted_by)
SELECT u.id, r.id, NULL, u.id
FROM users u
JOIN roles r ON r.name = 'user' AND r.community_id IS NULL
WHERE u.id::text LIKE 'd000%'
ON CONFLICT (user_id, role_id, community_id) DO NOTHING;

-- Make demo moderator a platform admin
INSERT INTO user_roles (user_id, role_id, community_id, granted_by)
SELECT 'd0000001-0000-0000-0000-000000000002'::uuid, r.id, NULL, 'd0000001-0000-0000-0000-000000000002'::uuid
FROM roles r
WHERE r.name = 'platform_admin' AND r.community_id IS NULL
ON CONFLICT (user_id, role_id, community_id) DO NOTHING;
