from connectors.base import PlatformConnector
from connectors.manual.connector import ManualConnector
from connectors.ninjatrader.connector import NinjaTraderConnector
from models.platform import ConnectorStatus, PlatformId


class ConnectorRegistry:
    def __init__(self) -> None:
        self._connectors: dict[PlatformId, PlatformConnector] = {}

    def register(self, connector: PlatformConnector) -> None:
        self._connectors[connector.platform_id] = connector

    def get(self, platform: str | PlatformId) -> PlatformConnector | None:
        try:
            pid = platform if isinstance(platform, PlatformId) else PlatformId(platform.lower())
        except ValueError:
            return None
        return self._connectors.get(pid)

    def all_status(self) -> list[ConnectorStatus]:
        return [c.status() for c in self._connectors.values()]

    def trading_platforms(self) -> list[ConnectorStatus]:
        """Plateformes de trading (hors entree manuelle compagnon)."""
        return [s for s in self.all_status() if s.platform != PlatformId.MANUAL]


def build_registry() -> ConnectorRegistry:
    registry = ConnectorRegistry()
    registry.register(NinjaTraderConnector())
    registry.register(ManualConnector())
    return registry


connector_registry = build_registry()
