import pytest
import respx
from httpx import Response
from app.agents import classify_intent as ci


def test_clear_approve_no_llm(monkeypatch):
    # Patch _llm_classify so we can assert it wasn't called
    called = {"n": 0}
    def fake(text):
        called["n"] += 1
        return ("unclear", None)
    monkeypatch.setattr(ci, "_llm_classify", fake)
    assert ci.classify("승인") == ("approve", None)
    assert ci.classify("OK") == ("approve", None)
    assert ci.classify("진행") == ("approve", None)
    assert ci.classify("agree") == ("approve", None)
    assert called["n"] == 0


def test_clear_reject_only_no_llm(monkeypatch):
    monkeypatch.setattr(ci, "_llm_classify", lambda t: ("unclear", None))
    assert ci.classify("반려") == ("reject", None)
    assert ci.classify("거절") == ("reject", None)


def test_reject_with_text_split(monkeypatch):
    monkeypatch.setattr(ci, "_llm_classify", lambda t: ("unclear", None))
    intent, fb = ci.classify("반려, 제목 짧게")
    assert intent == "reject"
    assert "제목 짧게" in fb


@respx.mock
def test_ambiguous_calls_llm(monkeypatch):
    monkeypatch.setenv("ANTHROPIC_API_KEY", "k")
    respx.post("https://api.anthropic.com/v1/messages").mock(
        return_value=Response(200, json={"content": [{"type": "text",
            "text": '{"intent":"reject","feedback":"좀 더 화려하게"}'}]})
    )
    intent, fb = ci.classify("음... 좀 더 화려한 분위기가 좋겠어")
    assert intent == "reject"
    assert "화려하게" in fb


def test_empty_text_returns_unclear():
    assert ci.classify("") == ("unclear", None)
    assert ci.classify(None) == ("unclear", None)
