from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


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


def test_email_reply_agent_service_exists_and_is_draft_only():
    service = read("app/email_reply_agent_service.py")
    assert "generate_email_reply_agent" in service
    assert "file_search" in service
    assert "resposta_sugerida" in service
    assert "never\n    sends" in service or "never sends" in service


def test_reply_assistant_persists_agent_metadata():
    assistant = read("app/reply_assistant_service.py")
    assert "_generate_with_email_agent" in assistant
    assert '"email_agent": email_agent_meta' in assistant
    assert 'generated_by = "openai_email_agent"' in assistant


def test_internal_api_exposes_safe_reply_draft_endpoint():
    internal = read("app/api/internal.py")
    assert '@router.post("/tasks/{task_id}/reply-draft")' in internal
    assert "persist=True" in internal
    assert "message_body" in internal


def test_env_example_documents_agent_flags():
    env = read(".env.example")
    assert "CLIENTFLOW_EMAIL_REPLY_AGENT_ENABLED=false" in env
    assert "OPENAI_VECTOR_STORE_ID=" in env
    assert "OPENAI_API_KEY=" in env


def test_ui_shows_email_agent_status_and_version():
    tasks = read("app/admin_ui/pages/tasks.py")
    assert "Agente de email OpenAI/file_search" in tasks
    assert "v4928.1.5.6" in tasks or "v4928.1.5.7" in tasks or "v4928.1.5.8" in tasks or "v4928.1.5.9" in tasks or "v4928.1.5.10" in tasks or "v4928.1.5.11" in tasks or "v4928.1.5.12" in tasks or "v4928.1.5.13" in tasks
