BEGIN;

DELETE FROM role_permissions rp
USING roles r, permissions p
WHERE rp.role_id = r.id
  AND rp.permission_id = p.id
  AND r.name = 'SuperAdmin'
  AND r.tenant_id = '00000000-0000-0000-0000-000000000001'
  AND p.code IN ('iam:tenant:enabled_apps:read', 'iam:tenant:enabled_apps:write');

DELETE FROM roles
WHERE tenant_id = '00000000-0000-0000-0000-000000000001'
  AND name = 'SuperAdmin';

DELETE FROM permissions
WHERE code IN ('iam:tenant:enabled_apps:read', 'iam:tenant:enabled_apps:write');

DROP TABLE IF EXISTS tenant_enabled_apps_history;
DROP TABLE IF EXISTS tenant_entitlements;
DROP TABLE IF EXISTS apps;

DELETE FROM tenants
WHERE id = '00000000-0000-0000-0000-000000000001';

COMMIT;

