from typing import Type, Dict
from .engines.translators.translation import translate_all

class EngineFactory:
    _registry: Dict[str, Type] = {}
    _translators: Dict[str, Type] = {}

    @classmethod
    def register(cls, name: str, engine_cls: Type, translator_cls: Type) -> None:
        """Register a new engine by name."""
        cls._registry[name] = engine_cls
        cls._translators[name] = translator_cls

    @classmethod
    def create(cls, name: str, **kwargs):
        if name not in cls._registry:
            raise ValueError(f"Unknown engine '{name}'. Available: {list(cls._registry)}")
        engine_cls = cls._registry[name]
        translator_cls = cls._translators[name]
        translator = translator_cls()

        # Build engine‑specific kwargs
        backend_kwargs, wrapper_kwargs = translate_all(translator, engine_cls._default_backend_kwargs(), kwargs)

        return engine_cls(backend_kwargs, wrapper_kwargs)


def register_all_engines():
    from .engines.rsrm_engine import RSRMEngine
    from .engines.translators.rsrm_translator import RSRMTranslator
    from .engines.pysr_engine import PySREngine
    from .engines.translators.pysr_translator import PySRTranslator
    from .engines.linear_engine import LinearEngine
    from .engines.translators.linear_translator import LinearTranslator
    from .engines.manual_engine import ManualEngine
    from .engines.translators.base import DefaultTranslator

    EngineFactory.register("rsrm", RSRMEngine, RSRMTranslator)
    EngineFactory.register("pysr", PySREngine, PySRTranslator)
    EngineFactory.register("linear", LinearEngine, LinearTranslator)
    EngineFactory.register("manual", ManualEngine, DefaultTranslator)


# Register on import
register_all_engines()