from pathlib import Path


def read(path: str) -> str:
    return Path(path).read_text(encoding="utf-8")


def test_blif_knowledge_files_and_retriever_exist():
    knowledge = read("app/business_knowledge/blif_knowledge.json")
    service = read("app/business_knowledge_service.py")
    assert "installation_policy" in knowledge
    assert "vat_policy" in knowledge
    assert "load_balancer" in knowledge
    assert "retrieve_business_knowledge" in service
    assert "default_template_code" in service


def test_business_aware_templates_without_attachment_exist():
    templates = read("app/message_templates.py")
    assert "CLARIFY_INSTALLATION_SCOPE" in templates
    assert "CLARIFY_VAT_POLICY" in templates
    assert "CLARIFY_LOAD_BALANCER" in templates
    assert "BUSINESS_KNOWLEDGE_REPLY" in templates
    assert "reply_type=\"answer_without_attachment\"" in templates


def test_openrouter_llm_generator_is_optional_and_guarded():
    generator = read("app/llm_reply_generator.py")
    config = read("app/config.py")
    env = read(".env.example")
    assert "generate_reply_with_openrouter" in generator
    assert "build_reply_system_prompt" in generator
    assert "Não inventes preços" in generator
    assert "clientflow_reply_llm_enabled" in config
    assert "CLIENTFLOW_REPLY_LLM_ENABLED=false" in env


def test_reply_assistant_uses_knowledge_and_safety_validator():
    assistant = read("app/reply_assistant_service.py")
    validator = read("app/reply_safety_validator.py")
    tasks = read("app/admin_ui/pages/tasks.py")
    assert "_choose_template_for_task" in assistant
    assert "_generate_business_aware_body" in assistant
    assert "validate_generated_reply" in assistant
    assert "A BLIF não presta serviço direto de instalação" in validator
    assert "Conhecimento BLIF usado" in tasks
    assert "v4928.1.5.0" in tasks


def test_release_documentation_exists():
    doc = read("docs/CLIENTFLOW_V4928_1_5_0_BLIF_KNOWLEDGE_REPLY_ASSISTANT.md")
    assert "BLIF Knowledge Reply Assistant" in doc
    assert "CLIENTFLOW_REPLY_LLM_ENABLED" in doc
    assert "Varisom" in doc
