"""Project-local read-only teaching server; no arbitrary file or network access."""
import json
from pathlib import Path

from mcp.server.fastmcp import FastMCP
from mcp.types import ToolAnnotations

FIXTURE = Path(__file__).resolve().parents[1] / "assets/examples/teacher-prep/course-materials.json"
mcp = FastMCP("AgentLearn course materials")


def catalog():
    return json.loads(FIXTURE.read_text(encoding="utf-8"))


@mcp.tool(annotations=ToolAnnotations(readOnlyHint=True, destructiveHint=False, openWorldHint=False))
def get_course_material(course_id: str) -> dict:
    """读取虚构课程资料，只接受资料目录中的课程 ID；不提供修改或发送操作。"""
    for course in catalog()["courses"]:
        if course["id"] == course_id:
            return {"fixtureVersion": 1, "notice": catalog()["notice"], "course": course}
    raise ValueError("课程不存在；请先查看 course://catalog 中的可用课程 ID。")


@mcp.resource("course://catalog")
def course_catalog() -> str:
    """虚构课程目录，可按 ID 选择资料。"""
    return json.dumps({"courses": [{"id": c["id"], "title": c["title"]} for c in catalog()["courses"]]}, ensure_ascii=False)


@mcp.prompt()
def prepare_lesson(course_id: str) -> str:
    """提供备课要求模板，不执行资料读取或模型生成。"""
    if course_id not in {c["id"] for c in catalog()["courses"]}:
        raise ValueError("课程不存在。")
    return f"请读取课程 {course_id} 的资料，按其年级、学生基础、40分钟课时与目标拟定教案。区分资料依据与待核对项；不要将模板获取当作教案已生成。"


if __name__ == "__main__":
    mcp.run(transport="stdio")
