from __future__ import annotations

import json
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
    from mcp.server.fastmcp import FastMCP

    from niannian.mcp.service import NianNianService


def register_vault_tools(mcp: FastMCP, svc: NianNianService) -> None:
    @mcp.tool()
    def niannian_vault_import(
        md_body: str,
        namespace: str = "user",
        hints_json: str = "",
    ) -> str:
        """本地 MD 入库 vault（Codex/Cursor 主路径）。hints_json 为可选 JSON 字符串。"""
        hints: dict[str, Any] | None = None
        if hints_json.strip():
            hints = json.loads(hints_json)
        return json.dumps(
            svc.vault_import(md_body, namespace=namespace, hints=hints),
            ensure_ascii=False,
        )

    @mcp.tool()
    def niannian_extract_status(vault_id: str = "", atom_id: str = "") -> str:
        """查询 vault_id / atom_id 对应资料状态（路径、打分、批次）。"""
        return json.dumps(
            svc.extract_status(vault_id=vault_id or None, atom_id=atom_id or None),
            ensure_ascii=False,
        )
