"""Minimal tests for the Chox Python SDK."""

import pytest
from unittest.mock import patch

from chox import ChoxClient
from chox.errors import ChoxError, ChoxNetworkError
from chox.client import _build_params


# ---------------------------------------------------------------------------
# Error classes
# ---------------------------------------------------------------------------


def test_chox_error_attributes():
    err = ChoxError(404, "not found")
    assert err.status == 404
    assert err.body == "not found"
    assert "404" in str(err)


def test_chox_network_error_cause():
    cause = ConnectionRefusedError("refused")
    err = ChoxNetworkError("request failed", cause=cause)
    assert err.cause is cause
    assert str(err) == "request failed"


def test_chox_network_error_no_cause():
    err = ChoxNetworkError("timeout")
    assert err.cause is None


# ---------------------------------------------------------------------------
# _build_params helper
# ---------------------------------------------------------------------------


def test_build_params_excludes_none():
    result = _build_params(limit=10, offset=None, action_type="read")
    assert result == {"limit": "10", "action_type": "read"}
    assert "offset" not in result


def test_build_params_stringifies_values():
    result = _build_params(limit=5, start="2024-01-01")
    assert result == {"limit": "5", "start": "2024-01-01"}


# ---------------------------------------------------------------------------
# evaluate()
# ---------------------------------------------------------------------------

VALID_RESPONSE = {
    "request_id": "req_123",
    "verdict": "allow",
    "action_type": "read",
    "risk_score": 0.1,
    "reason": "Low-risk read operation",
    "evaluated_at": "2025-01-01T00:00:00+00:00",
}


def make_client(fail_open=True):
    return ChoxClient(
        base_url="https://chox.example.com",
        token="chox_token_test",
        fail_open=fail_open,
    )


def test_evaluate_happy_path():
    client = make_client()
    with patch.object(client, "_request", return_value=VALID_RESPONSE):
        result = client.evaluate(tool="stripe.list_charges")
    assert result.verdict == "allow"
    assert result.request_id == "req_123"
    assert result.risk_score == 0.1


def test_evaluate_fail_open_returns_allow():
    client = make_client(fail_open=True)
    with patch.object(client, "_request", side_effect=ChoxNetworkError("unreachable")):
        result = client.evaluate(tool="stripe.create_charge")
    assert result.verdict == "allow"
    assert result.request_id == "chox_fail_open"


def test_evaluate_fail_closed_raises():
    client = make_client(fail_open=False)
    with patch.object(client, "_request", side_effect=ChoxNetworkError("unreachable")):
        with pytest.raises(ChoxNetworkError):
            client.evaluate(tool="stripe.create_charge")


def test_evaluate_fail_open_on_chox_error():
    client = make_client(fail_open=True)
    with patch.object(client, "_request", side_effect=ChoxError(500, "internal error")):
        result = client.evaluate(tool="stripe.create_charge")
    assert result.verdict == "allow"
