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:app:read', 'iam:app:write', 'iam:app:approve', 'iam:app:delete');

DELETE FROM permissions
WHERE code IN ('iam:app:read', 'iam:app:write', 'iam:app:approve', 'iam:app:delete');

DROP TABLE IF EXISTS app_status_change_requests;
DROP TABLE IF EXISTS app_change_logs;

ALTER TABLE apps
    DROP COLUMN IF EXISTS app_type,
    DROP COLUMN IF EXISTS owner,
    DROP COLUMN IF EXISTS owner_user_id,
    DROP COLUMN IF EXISTS updated_at;

COMMIT;

