# app/src/trading/market_intelligence/runtime/registry.py

from __future__ import annotations

from src.trading.market_intelligence.common.models import EngineRegistration
from src.trading.market_intelligence.common.types import EngineName
from src.trading.market_intelligence.engine.protocol import EngineProtocol


class RuntimeRegistry:
    """Каталог зарегистрированных Engine."""

    def __init__(self) -> None:
        """Создать пустой каталог Engine."""
        self._engines: dict[EngineName, EngineRegistration] = {}

    def register(
        self,
        engine_type: type[EngineProtocol],
    ) -> None:
        """Зарегистрировать класс Engine."""
        metadata = engine_type.get_metadata()

        self._engines[metadata.name] = EngineRegistration(
            engine_type=engine_type,
            metadata=metadata,
        )

    def unregister(
        self,
        engine_name: EngineName,
    ) -> None:
        """Удалить Engine из каталога."""
        self._engines.pop(engine_name, None)

    def get(
        self,
        engine_name: EngineName,
    ) -> EngineRegistration | None:
        """Вернуть регистрацию Engine по имени."""
        return self._engines.get(engine_name)

    def contains(
        self,
        engine_name: EngineName,
    ) -> bool:
        """Проверить наличие Engine в каталоге."""
        return engine_name in self._engines

    def list(self) -> tuple[EngineRegistration, ...]:
        """Вернуть все зарегистрированные Engine."""
        return tuple(self._engines.values())

    def clear(self) -> None:
        """Очистить каталог Engine."""
        self._engines.clear()