from typing import Dict
from .session import ShellSession
from health.utils.logging_config import setup_logger

logger = setup_logger(__name__)

class ShellManager:
    _instance = None
    _sessions: Dict[str, ShellSession] = {}

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(ShellManager, cls).__new__(cls)
        return cls._instance

    def get_session(self, channel_id: str) -> ShellSession:
        if channel_id not in self._sessions or not self._sessions[channel_id].is_alive():
            logger.info(f"Creating new ShellSession for channel {channel_id}")
            self._sessions[channel_id] = ShellSession()
        return self._sessions[channel_id]

    def close_session(self, channel_id: str):
        if channel_id in self._sessions:
            logger.info(f"Closing ShellSession for channel {channel_id}")
            self._sessions[channel_id].close()
            del self._sessions[channel_id]
