from types import SimpleNamespace
from unittest.mock import patch

from agent.system_prompt import build_system_prompt_parts
from hermes_constants import reset_hermes_home_override, set_hermes_home_override


def _make_agent(**overrides):
    base = dict(
        load_soul_identity=False,
        skip_context_files=False,
        valid_tool_names=[],
        _task_completion_guidance=False,
        _parallel_tool_call_guidance=False,
        _tool_use_enforcement=False,
        _environment_probe=False,
        _kanban_worker_guidance="",
        _memory_store=None,
        _memory_enabled=False,
        _user_profile_enabled=False,
        _memory_manager=None,
        _platform_hint_overrides={},
        _shared_knowledge_enabled=True,
        _shared_knowledge_path=None,
        _shared_knowledge_max_chars=4000,
        model="",
        provider="",
        platform="telegram",
        pass_session_id=False,
        session_id="",
    )
    base.update(overrides)
    return SimpleNamespace(**base)


def _build_parts(agent):
    with (
        patch("run_agent.load_soul_md", return_value=""),
        patch("run_agent.build_nous_subscription_prompt", return_value=""),
        patch("run_agent.build_environment_hints", return_value=""),
        patch("run_agent.build_context_files_prompt", return_value=""),
    ):
        return build_system_prompt_parts(agent)


def test_shared_knowledge_block_is_injected_from_root_even_under_profile_scope(tmp_path, monkeypatch):
    root = tmp_path / ".hermes"
    profile_home = root / "profiles" / "principal_alpha001"
    shared_dir = root / "shared_knowledge"
    shared_dir.mkdir(parents=True)
    profile_home.mkdir(parents=True)
    (shared_dir / "faq.md").write_text("Global FAQ: shipping cutoff is 5pm.", encoding="utf-8")
    monkeypatch.setenv("HERMES_HOME", str(root))

    token = set_hermes_home_override(profile_home)
    try:
        parts = _build_parts(_make_agent())
    finally:
        reset_hermes_home_override(token)

    volatile = parts["volatile"]
    assert "SHARED KNOWLEDGE" in volatile
    assert "Global FAQ: shipping cutoff is 5pm." in volatile
    assert str(shared_dir) in volatile


def test_shared_knowledge_is_shared_across_profiles_but_can_be_disabled(tmp_path, monkeypatch):
    root = tmp_path / ".hermes"
    alpha_home = root / "profiles" / "principal_alpha001"
    beta_home = root / "profiles" / "principal_beta0002"
    shared_dir = root / "shared_knowledge"
    shared_dir.mkdir(parents=True)
    alpha_home.mkdir(parents=True)
    beta_home.mkdir(parents=True)
    (shared_dir / "common.md").write_text("Company FAQ: refund window is 7 days.", encoding="utf-8")
    monkeypatch.setenv("HERMES_HOME", str(root))

    token_a = set_hermes_home_override(alpha_home)
    try:
        alpha_parts = _build_parts(_make_agent())
    finally:
        reset_hermes_home_override(token_a)

    token_b = set_hermes_home_override(beta_home)
    try:
        beta_parts = _build_parts(_make_agent())
        disabled_parts = _build_parts(_make_agent(_shared_knowledge_enabled=False))
    finally:
        reset_hermes_home_override(token_b)

    assert "Company FAQ: refund window is 7 days." in alpha_parts["volatile"]
    assert "Company FAQ: refund window is 7 days." in beta_parts["volatile"]
    assert "SHARED KNOWLEDGE" not in disabled_parts["volatile"]
