from pathlib import Path


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


def test_reply_assistant_files_exist_and_templates_are_defined():
    templates = read("app/message_templates.py")
    service = read("app/reply_assistant_service.py")
    assert "class MessageTemplate" in templates
    assert "SEND_PROFORMA" in templates
    assert "SEND_INVOICE" in templates
    assert "REQUEST_FISCAL_DATA" in templates
    assert "def validate_reply_readiness" in service
    assert "não pertence à oportunidade desta tarefa" in service


def test_chatwoot_public_reply_methods_are_available():
    chatwoot = read("app/chatwoot_client.py")
    assert "async def send_public_message(" in chatwoot
    assert "async def send_public_message_with_attachments" in chatwoot
    assert "attachments[]" in chatwoot
    assert "CHATWOOT_WRITE_ENABLED=false" in chatwoot


def test_task_page_has_reply_assistant_endpoints_and_controls():
    tasks = read("app/admin_ui/pages/tasks.py")
    assert "Resposta ao cliente" in tasks
    assert "/tasks/{esc(task_id)}/reply-draft" in tasks
    assert "/tasks/{esc(task_id)}/send-reply" in tasks
    assert "Enviar e concluir tarefa" in tasks
    assert "Anexos da oportunidade" in tasks


def test_outbound_communication_and_draft_schema_are_registered():
    comms = read("app/communication_service.py")
    service = read("app/reply_assistant_service.py")
    db = read("app/db.py")
    assert "def record_outbound_communication" in comms
    assert "direction, body, status" in comms
    assert "CREATE TABLE IF NOT EXISTS message_drafts" in service
    assert "ensure_reply_assistant_schema" in db


def test_release_documentation_exists():
    doc = read("docs/CLIENTFLOW_V4928_1_4_9_REPLY_ASSISTANT.md")
    assert "Opportunity Reply Assistant" in doc
    assert "POST /tasks/{task_id}/reply-draft" in doc
    assert "POST /tasks/{task_id}/send-reply" in doc
    assert "CHATWOOT_WRITE_ENABLED=true" in doc
