from __future__ import annotations

import json
from typing import TYPE_CHECKING

from niannian.mcp.mcp_space import resolve_mcp_memory_space

if TYPE_CHECKING:
    from mcp.server.fastmcp import FastMCP

    from niannian.mcp.service import NianNianService


def register_memory_tools(mcp: FastMCP, svc: NianNianService) -> None:
    @mcp.tool()
    def niannian_ingest(text: str, project: str, tags: str = "", source: str = "mcp") -> str:
        """小忆：记入简单碎片。project=记忆空间（必填）。loop 节点 write/review/adopt 请用 niannian_record。"""
        tag_list = [t.strip() for t in tags.split(",") if t.strip()]
        space = resolve_mcp_memory_space(svc, project)
        return json.dumps(svc.ingest(text, tags=tag_list, source=source, project=space), ensure_ascii=False)

    @mcp.tool()
    def niannian_dream(project: str) -> str:
        """小海：执行记忆提炼（伪清除）。project=记忆空间（必填）。"""
        space = resolve_mcp_memory_space(svc, project)
        return json.dumps(svc.dream(project=space), ensure_ascii=False)
