import importlib
import logging

logger = logging.getLogger(__name__)

# Track loaded plugins to avoid double module loading overhead
_loaded_plugins = set()

def load_plugin(plugin_name: str):
    """Dynamically load an AIForge plugin to register its operators."""
    if plugin_name in _loaded_plugins:
        return
        
    try:
        importlib.import_module(f"aiforge.plugins.{plugin_name}")
        _loaded_plugins.add(plugin_name)
        logger.info(f"Successfully loaded plugin: {plugin_name}")
    except ImportError as e:
        logger.error(f"Failed to load plugin '{plugin_name}': {e}")
        raise
