"""FastMCP server instance for Mnemosyne. Imported by asgi.py at startup."""

from __future__ import annotations

from fastmcp import FastMCP

from .auth import MCPAuthMiddleware
from .tools import (
    register_discovery_tools,
    register_health_tools,
    register_search_tools,
)

INSTRUCTIONS = """\
Mnemosyne is a content-type-aware, multimodal knowledge base. It indexes
documents (text, images, audio metadata) into a Neo4j knowledge graph and
exposes them through hybrid search (vector + full-text + concept-graph) with
optional Synesis re-ranking.

Content is organized into Libraries; each library has a `library_type` that
shapes how content is chunked, embedded, and re-ranked:

- fiction      — Novels, short stories. Cover art available.
- nonfiction   — General non-fiction prose.
- technical    — Manuals, textbooks, docs. Diagrams and code-like content.
- music        — Lyrics, liner notes, album artwork.
- film         — Scripts, synopses, stills.
- art          — Catalogs, descriptions, artwork itself.
- journal      — Personal entries; temporal/reflective.
- business     — Proposals, marketing, sales, strategy. Commercial context.
- finance      — Statements, tax, market commentary. Quote figures exactly.

Tools:
- search                      Hybrid retrieval. Filter by library_uid, library_type,
                              or collection_uid. Returns ranked chunks with
                              text_preview (~500 chars) and chunk_uid.
- get_chunk                   Fetch the full text of a chunk by its chunk_uid.
- list_libraries              Discover libraries (and their library_type).
- list_collections            Discover collections, optionally per library.
- list_items                  Discover indexed items (documents).
- get_health                  Health check (used by Pallas/Daedalus pollers).

Workflow: list_libraries → search(query, library_type=...) → get_chunk(chunk_uid)
when the preview isn't enough. The calling LLM is responsible for synthesis
and citation — Mnemosyne returns evidence, not answers.
"""


def build_server() -> FastMCP:
    mcp = FastMCP(
        "mnemosyne",
        instructions=INSTRUCTIONS,
    )
    mcp.add_middleware(MCPAuthMiddleware())
    register_search_tools(mcp)
    register_discovery_tools(mcp)
    register_health_tools(mcp)
    return mcp


mcp = build_server()
