"""Fixtures for MCP server tests."""

from __future__ import annotations

from collections.abc import Generator
from pathlib import Path

import pytest

from filigree.core import DB_FILENAME, FILIGREE_DIR_NAME, SUMMARY_FILENAME, FiligreeDB, write_config

# Re-export _parse so existing ``from tests.mcp.conftest import _parse``
# imports continue to work during migration.  New code should import from
# ``tests.mcp._helpers`` instead.
from tests.mcp._helpers import _parse as _parse


@pytest.fixture
def mcp_db(tmp_path: Path) -> Generator[FiligreeDB, None, None]:
    """Set up a FiligreeDB and patch the MCP module globals."""
    filigree_dir = tmp_path / FILIGREE_DIR_NAME
    filigree_dir.mkdir()
    write_config(filigree_dir, {"prefix": "mcp", "version": 1})
    (filigree_dir / SUMMARY_FILENAME).write_text("# test\n")

    d = FiligreeDB(filigree_dir / DB_FILENAME, prefix="mcp")
    d.initialize()

    import filigree.mcp_server as mcp_mod

    original_db = mcp_mod.db
    original_dir = mcp_mod._filigree_dir
    mcp_mod.db = d
    mcp_mod._filigree_dir = filigree_dir

    yield d

    mcp_mod.db = original_db
    mcp_mod._filigree_dir = original_dir
    d.close()
