"""The privacy policy and the terms of use, as the site reads them: `GET /api/legal/{name}`.

Sources of truth: `services/process_doc.py` (`legal_page`), `docs/legal/privacy.md` and
`docs/legal/terms.md` (the copy), `tests/test_legal_pages.py`. Read-only: each document is read
per request and nothing is written or cached. An unknown name, or a document that is absent or
carries no version line, is a 404 with a code, the same answer `/privacy` and `/terms` give.
"""

from __future__ import annotations

from fastapi import APIRouter, HTTPException

from app.models.schemas import LegalPageOut
from app.services import process_doc

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


@router.get("/{name}", response_model=LegalPageOut)
def read_legal(name: str) -> dict:
    if name not in process_doc.LEGAL:
        raise HTTPException(status_code=404, detail={"error_code": "LEGAL_UNKNOWN",
                                                     "summary": "There is no legal page by that name."})
    out = process_doc.legal_page(name)
    if out is None:
        raise HTTPException(status_code=404, detail={"error_code": "DOC_MISSING",
                                                     "summary": f"docs/legal/{name}.md is absent or carries no version line"})
    return out
