# Shared State Module
# This module holds global state that is shared across all routers

from pathlib import Path

# Project root for path resolution in routers
PROJECT_ROOT = Path(__file__).parent.parent

# === Lazy-loaded dependencies ===
# These will be initialized when first accessed or during api.py lifespan

# Global state - shared across routers
active_loops = {}

# MCP instance - will be started in api.py lifespan
_multi_mcp = None

def get_multi_mcp():
    """Get the MultiMCP instance, creating it if needed."""
    global _multi_mcp
    if _multi_mcp is None:
        from mcp_servers.multi_mcp import MultiMCP
        _multi_mcp = MultiMCP()
    return _multi_mcp

# RemMe store instance
_remme_store = None

def get_remme_store():
    """Get the RemmeStore instance, creating it if needed."""
    global _remme_store
    if _remme_store is None:
        from remme.store import RemmeStore
        _remme_store = RemmeStore()
    return _remme_store

# RemMe extractor instance
_remme_extractor = None

def get_remme_extractor():
    """Get the RemmeExtractor instance, creating it if needed."""
    global _remme_extractor
    if _remme_extractor is None:
        from remme.extractor import RemmeExtractor
        _remme_extractor = RemmeExtractor()
    return _remme_extractor


# Harness runtime instance
_harness_runtime = None


def get_harness_runtime():
    """Get the HarnessRuntime instance, creating it if needed."""
    global _harness_runtime
    if _harness_runtime is None:
        from harness.runtime import HarnessRuntime

        _harness_runtime = HarnessRuntime(project_root=PROJECT_ROOT)
    return _harness_runtime

# Global settings state
settings = {}
