-- =============================================================================
-- Migration 006: Superuser Hierarchy - ROLLBACK
-- =============================================================================

-- Drop triggers
DROP TRIGGER IF EXISTS prevent_initial_superuser_demotion ON user_roles;
DROP TRIGGER IF EXISTS prevent_delete_initial_superuser ON users;
DROP TRIGGER IF EXISTS check_initial_superuser ON users;

-- Drop functions
DROP FUNCTION IF EXISTS transfer_initial_superuser(INTEGER, INTEGER, TEXT);
DROP FUNCTION IF EXISTS prevent_initial_superuser_role_removal();
DROP FUNCTION IF EXISTS prevent_initial_superuser_deletion();
DROP FUNCTION IF EXISTS ensure_single_initial_superuser();

-- Drop superuser_transfers table
DROP TABLE IF EXISTS superuser_transfers;

-- Drop indexes
DROP INDEX IF EXISTS idx_users_initial_superuser;

-- Remove columns from users
ALTER TABLE users DROP COLUMN IF EXISTS is_protected;
ALTER TABLE users DROP COLUMN IF EXISTS is_initial_superuser;

-- Note: Cannot remove enum value in PostgreSQL
-- SUPERUSER will remain in user_role enum but can be ignored
-- To fully remove, you'd need to recreate the enum type
