"""Docs router — serves markdown files from the docs/ directory."""
from __future__ import annotations

from pathlib import Path

from fastapi import APIRouter, HTTPException

router = APIRouter(prefix="/docs", tags=["docs"])

_DOCS_DIR = Path("docs")


@router.get("/{filename}")
async def get_doc(filename: str) -> dict:
    # Prevent path traversal — strip to bare filename only
    safe_name = Path(filename).name
    if not safe_name.endswith(".md"):
        safe_name += ".md"
    path = _DOCS_DIR / safe_name
    if not path.exists() or not path.is_file():
        raise HTTPException(status_code=404, detail=f"Doc '{safe_name}' not found")
    return {"content": path.read_text(encoding="utf-8"), "filename": safe_name}
