"""Resources 模块."""
from ..tools.connections import get_current_connection, get_session_state


def get_es_connections_resource() -> dict:
    """获取连接列表资源."""
    from ..config import load_connections

    connections = load_connections()
    return {
        "connections": [
            {
                "name": conn.name,
                "env": conn.env,
                "description": conn.description,
            }
            for conn in connections
        ]
    }


def get_es_current_resource() -> dict:
    """获取当前连接资源."""
    state = get_session_state()
    current = get_current_connection()

    if not current:
        return {"current": None, "message": "当前没有选中的连接"}

    return {
        "current": current,
        "env": state.env,
        "es_version": state.es_version,
        "es_version_info": (
            f"{state.es_version_info.major}.{state.es_version_info.minor}.{state.es_version_info.patch}"
            if state.es_version_info else None
        ),
        "safety": {
            "read_only": state.safety.read_only if state.safety else None,
            "max_search_size": state.safety.max_search_size if state.safety else None,
        },
    }


def get_es_status_resource() -> dict:
    """获取 ES MCP 状态资源."""
    from ..config import PROFILE_FILE

    state = get_session_state()

    return {
        "status": "running" if state.current_connection else "idle",
        "profile_file": str(PROFILE_FILE),
        "config_source": "ES_CONNECTIONS environment variable",
        "current_connection": state.current_connection,
    }
