"""Load IUVE AI ecosystem principles from canonical documentation."""

from __future__ import annotations

from functools import lru_cache
from pathlib import Path

_CANONICAL_RELATIVE = Path("docs/canonical/IUVE_AI_ECOSYSTEM_PRINCIPLES.md")


def _repo_root() -> Path:
    here = Path(__file__).resolve()
    # .../kai/KAI_AI/iuve_connect_agents/shared/answer_quality/ecosystem_principles.py
    return here.parents[3]


def canonical_principles_path() -> Path:
    return _repo_root() / _CANONICAL_RELATIVE


@lru_cache(maxsize=1)
def load_ecosystem_principles() -> str:
    path = canonical_principles_path()
    if path.is_file():
        return path.read_text(encoding="utf-8").strip()
    return ""


def principles_block_for_prompt() -> str | None:
    text = load_ecosystem_principles()
    if not text:
        return None
    return (
        "IUVE AI ECOSYSTEM PRINCIPLES (canonical reference — follow for architecture answers):\n"
        + text
    )
