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_chatwoot_auto_complete_excludes_payment_and_review_by_default():
    service = read("app/task_service.py")
    start = service.index("# Guardrail v4.6.7")
    end = service.index('configured = os.getenv("CHATWOOT_AUTO_COMPLETE_ACTION_CODES"', start)
    defaults = service[start:end]
    assert '"SEND_INFO"' in defaults
    assert '"SEND_QUOTE"' in defaults
    assert '"SUPPORT"' in defaults
    assert '"CONFIRM_PAYMENT"' not in defaults
    assert '"MARK_NO_INTEREST"' not in defaults
    assert '"REVIEW_MANUALLY"' not in defaults


def test_chatwoot_auto_complete_blocks_ambiguous_opportunity_linking():
    service = read("app/task_service.py")
    assert "blocked_ambiguous_opportunity" in service
    assert "operator_must_confirm_opportunity_before_auto_complete" in service
    assert "opportunity_linking_status" in service


def test_task_done_records_timeline_event_after_completion():
    service = read("app/task_service.py")
    assert "task_auto_completed" in service
    assert "Task concluída automaticamente" in service
    assert "_record_task_timeline_event(task_id, event_type=timeline_event_type)" in service


def test_v467_documentation_exists():
    doc = read("docs/CLIENTFLOW_V467_CHATWOOT_AUTOCOMPLETE_GUARDRAILS.md")
    assert "Chatwoot Auto-complete Guardrails" in doc
    assert "CONFIRM_PAYMENT" in doc
