"""Tests for the AAL session."""

from unittest.mock import MagicMock

from aal_sdk.session import AALSession


def test_session_id_and_agent_id_are_set():
    """Test that session id and agent_id are accessible."""
    push_fn = MagicMock()
    session = AALSession("sess-123", "agent-456", push_fn)

    assert session.id == "sess-123"
    assert session.agent_id == "agent-456"


def test_log_action_with_all_fields():
    """Test log_action creates correct QueuedEvent with all fields."""
    push_fn = MagicMock()
    session = AALSession("sess-1", "agent-1", push_fn)

    session.log_action(
        {
            "action_type": "TOOL_CALL",
            "tool_name": "get_weather",
            "parameters": {"city": "London"},
            "response": {"temp": 15},
            "status": "SUCCESS",
            "latency_ms": 42.5,
        }
    )

    push_fn.assert_called_once()
    event = push_fn.call_args[0][0]
    assert event["session_id"] == "sess-1"
    assert event["agent_id"] == "agent-1"
    assert event["action_type"] == "TOOL_CALL"
    assert event["tool_name"] == "get_weather"
    assert event["parameters"] == {"city": "London"}
    assert event["response"] == {"temp": 15}
    assert event["status"] == "SUCCESS"
    assert event["latency_ms"] == 42.5


def test_log_action_with_minimal_fields():
    """Test log_action with only required fields."""
    push_fn = MagicMock()
    session = AALSession("sess-1", "agent-1", push_fn)

    session.log_action(
        {
            "action_type": "MESSAGE",
            "status": "SUCCESS",
        }
    )

    push_fn.assert_called_once()
    event = push_fn.call_args[0][0]
    assert event["session_id"] == "sess-1"
    assert event["action_type"] == "MESSAGE"
    assert event["status"] == "SUCCESS"
    assert "tool_name" not in event
    assert "parameters" not in event
    assert "response" not in event
    assert "latency_ms" not in event


def test_optional_fields_omitted_when_not_provided():
    """Test that optional fields are not included when not in input."""
    push_fn = MagicMock()
    session = AALSession("sess-1", "agent-1", push_fn)

    session.log_action(
        {
            "action_type": "API_REQUEST",
            "status": "ERROR",
            "tool_name": "search_api",
        }
    )

    event = push_fn.call_args[0][0]
    assert event["tool_name"] == "search_api"
    assert "parameters" not in event
    assert "response" not in event
    assert "latency_ms" not in event
