"""Tests for LangChain integration."""

import pytest
from unittest.mock import Mock, patch


class TestAgentStatusCallbackHandler:
    """Test LangChain callback handler."""

    def test_import_without_langchain(self):
        """Test graceful handling when LangChain not installed."""
        pass

    def test_handler_creation(self):
        """Test handler creation with langchain installed."""
        try:
            from agent_status.integrations.langchain import AgentStatusCallbackHandler, LANGCHAIN_AVAILABLE

            if not LANGCHAIN_AVAILABLE:
                pytest.skip("LangChain not installed")

            handler = AgentStatusCallbackHandler(
                api_key="test_key",
                agent_name="Test Agent",
            )

            assert handler.api_key == "test_key"
            assert handler.agent_name == "Test Agent"
            assert handler.agent_id is None
            assert handler.auto_register is True

        except ImportError:
            pytest.skip("LangChain not installed")

    def test_metrics_tracking(self):
        """Test metrics are properly initialized."""
        try:
            from agent_status.integrations.langchain import AgentStatusCallbackHandler, LANGCHAIN_AVAILABLE

            if not LANGCHAIN_AVAILABLE:
                pytest.skip("LangChain not installed")

            handler = AgentStatusCallbackHandler(
                api_key="test_key",
                agent_name="Test Agent",
            )

            metrics = handler.metrics
            assert metrics["total_calls"] == 0
            assert metrics["llm_calls"] == 0
            assert metrics["errors"] == 0
            assert metrics["avg_latency_ms"] == 0

        except ImportError:
            pytest.skip("LangChain not installed")

    def test_reset_metrics(self):
        """Test metrics reset."""
        try:
            from agent_status.integrations.langchain import AgentStatusCallbackHandler, LANGCHAIN_AVAILABLE

            if not LANGCHAIN_AVAILABLE:
                pytest.skip("LangChain not installed")

            handler = AgentStatusCallbackHandler(
                api_key="test_key",
                agent_name="Test Agent",
            )

            handler._metrics["total_calls"] = 10
            handler._metrics["errors"] = 2

            handler.reset_metrics()

            assert handler.metrics["total_calls"] == 0
            assert handler.metrics["errors"] == 0

        except ImportError:
            pytest.skip("LangChain not installed")

    def test_report_generation(self):
        """Test report generation."""
        try:
            from agent_status.integrations.langchain import AgentStatusCallbackHandler, LANGCHAIN_AVAILABLE

            if not LANGCHAIN_AVAILABLE:
                pytest.skip("LangChain not installed")

            handler = AgentStatusCallbackHandler(
                api_key="test_key",
                agent_name="Test Agent",
            )

            report = handler.report()

            assert report["agent_name"] == "Test Agent"
            assert report["agent_id"] is None
            assert "timestamp" in report
            assert "metrics" in report

        except ImportError:
            pytest.skip("LangChain not installed")

    def test_backward_compat_alias(self):
        """Test backward compatibility alias."""
        try:
            from agent_status.integrations.langchain import RoraCallbackHandler, AgentStatusCallbackHandler
            assert RoraCallbackHandler is AgentStatusCallbackHandler
        except ImportError:
            pytest.skip("LangChain not installed")


class TestIntegrationsInit:
    """Test integrations module initialization."""

    def test_integrations_import(self):
        """Test integrations module can be imported."""
        try:
            from agent_status import integrations
            from agent_status.integrations import AgentStatusCallbackHandler
        except ImportError as e:
            if "langchain" not in str(e).lower():
                raise
