from __future__ import annotations

from unittest.mock import MagicMock, patch

from nees import NEES, __version__
from nees.validation import run_demo


def _mock_response(status_code: int, body: dict) -> MagicMock:
    response = MagicMock()
    response.status_code = status_code
    response.json.return_value = body
    response.text = str(body)
    return response


def test_import_and_local_process() -> None:
    assert __version__ == "2.0.0"

    sdk = NEES()
    response = sdk.process(
        {"message": "Hello", "history": [], "session_id": "123"},
        enable_trace=True,
    )

    assert response.mode == "local"
    assert response.session_id == "123"
    assert "Hello" in response.text
    assert response.trace.summary["intent"] == "greeting"
    assert any(step.name == "intent" for step in response.trace.steps)


def test_validation_demo() -> None:
    results = run_demo()

    assert len(results) == 3
    assert all(result["trace_id"] for result in results)
    assert all("summary" in result for result in results)


def test_remote_mode_uses_transport() -> None:
    raw = {
        "response": "Remote hello",
        "intent_type": "greeting",
        "emotional_state": "open",
        "framework_used": "warm_opening",
        "urgency_level": "low",
        "governance_clean": True,
        "trace_id": "trace-123",
    }

    with patch("requests.Session.request") as mock_request:
        mock_request.return_value = _mock_response(200, raw)

        sdk = NEES(mode="remote", api_key="test-key")
        response = sdk.process(
            {"message": "Hello", "history": [], "session_id": "remote-1"},
            enable_trace=True,
        )
        sdk.close()

    assert response.mode == "remote"
    assert response.text == "Remote hello"
    assert response.trace.trace_id == "trace-123"
    assert response.trace.steps[0].name == "remote_request"
