"""
Mainline stage component registration.

This module registers all Mainline-specific stage components with the Sideline framework.
It should be called during application startup to ensure all components are available.

Terminology:
- Stage: A pipeline component (source, effect, display, camera, overlay)
- Plugin: A distributable package containing one or more stages
- This module registers stage components, not plugins themselves
"""

import logging

logger = logging.getLogger(__name__)


def register_stages(registry):
    """Register Mainline-specific stage components with the Sideline registry.

    This function is called by Sideline's plugin discovery system.

    Args:
        registry: StageRegistry instance from Sideline
    """
    logger.info("Registering Mainline stage components")

    # Register data sources
    _register_data_sources(registry)

    # Register effects
    _register_effects(registry)

    # Register any other Mainline-specific stages
    _register_other_stages(registry)


def _register_data_sources(registry):
    """Register Mainline data source stages."""
    try:
        from engine.data_sources.sources import HeadlinesDataSource, PoetryDataSource
        from engine.data_sources.pipeline_introspection import (
            PipelineIntrospectionSource,
        )

        registry.register("source", HeadlinesDataSource)
        registry.register("source", PoetryDataSource)
        registry.register("source", PipelineIntrospectionSource)

        # Register with friendly aliases
        registry._categories["source"]["headlines"] = HeadlinesDataSource
        registry._categories["source"]["poetry"] = PoetryDataSource
        registry._categories["source"]["pipeline-inspect"] = PipelineIntrospectionSource

        logger.info("Registered Mainline data sources")
    except ImportError as e:
        logger.warning(f"Failed to register data sources: {e}")


def _register_effects(registry):
    """Register Mainline effect stages."""
    try:
        # Register effects
        from sideline.effects import EffectRegistry
        from sideline.effects.registry import get_registry

        # Get the global effect registry instance
        effect_registry = get_registry()

        # Note: EffectRegistry stores effect instances, not classes
        # For now, skip effect registration since it requires more refactoring
        logger.info("Effect registration skipped (requires effect refactoring)")
    except ImportError as e:
        logger.warning(f"Failed to register effects: {e}")


def _register_other_stages(registry):
    """Register other Mainline-specific stage components."""
    try:
        # Register buffer stages
        from sideline.pipeline.stages.framebuffer import FrameBufferStage

        registry.register("effect", FrameBufferStage)
        logger.info("Registered Mainline buffer stages")
    except ImportError as e:
        logger.warning(f"Failed to register buffer stages: {e}")


# Convenience function for explicit registration
def register_all_stages():
    """Explicitly register all Mainline stages.

    This can be called directly instead of using plugin discovery.
    """
    from sideline.pipeline import StageRegistry

    register_stages(StageRegistry)
