"""
This program has been developed by students from the bachelor Computer Science at Utrecht
University within the Software Project course.
© Copyright Utrecht University (Department of Information and Computing Sciences)
"""

from unittest.mock import MagicMock, patch

import pytest

from control_backend.core.agent_system import _agent_directory


@pytest.fixture(autouse=True)
def reset_agent_directory():
    """
    Automatically clears the global agent directory before and after each test
    to prevent state leakage between tests.
    """
    _agent_directory.clear()
    yield
    _agent_directory.clear()


@pytest.fixture
def mock_settings():
    with patch("control_backend.core.config.settings") as mock:
        # Set default values that match the pydantic model defaults
        # to avoid AttributeErrors during tests
        mock.zmq_settings.internal_pub_address = "tcp://localhost:5560"
        mock.zmq_settings.internal_sub_address = "tcp://localhost:5561"
        mock.zmq_settings.ri_command_address = "tcp://localhost:0000"
        mock.agent_settings.bdi_core_name = "bdi_core_agent"
        mock.agent_settings.llm_name = "llm_agent"
        mock.agent_settings.robot_speech_name = "robot_speech_agent"
        mock.agent_settings.transcription_name = "transcription_agent"
        mock.agent_settings.text_belief_extractor_name = "text_belief_extractor_agent"
        mock.agent_settings.vad_name = "vad_agent"
        mock.behaviour_settings.sleep_s = 0.01  # Speed up tests
        mock.behaviour_settings.comm_setup_max_retries = 1
        mock.behaviour_settings.agentspeak_file = "src/control_backend/agents/bdi/agentspeak.asl"
        yield mock


@pytest.fixture
def mock_zmq_context():
    with patch("zmq.asyncio.Context") as mock:
        mock.instance.return_value = MagicMock()
        yield mock
