"""Access to the canonical scaffolding template (in ``mgf-standard/templates/``).

The template is the single source of truth for the shared scaffolding: the drift policy
(``policy.toml``), byte-exact shared files (e.g. ``verify_codeberg.py``), and the
``cookiecutter-mgf-project`` generator. ``mgf-fed drift-check`` verifies against it;
``mgf-fed scaffold`` (later) applies it.
"""

from __future__ import annotations

import tomllib
from pathlib import Path
from typing import Any

from mgf.fed._roster import federation_root


def template_dir(root: Path | None = None) -> Path:
    return (root or federation_root()) / "mgf-standard" / "templates"


def load_policy(root: Path | None = None) -> dict[str, Any]:
    path = template_dir(root) / "policy.toml"
    if not path.is_file():
        raise FileNotFoundError(f"drift policy not found at {path}")
    data: dict[str, Any] = tomllib.loads(path.read_text(encoding="utf-8"))
    return data


def canonical_text(name: str, root: Path | None = None) -> str:
    return (template_dir(root) / name).read_text(encoding="utf-8")
