"""Integration tests for Chat with Research system.

These tests verify the end-to-end flow from chat messages to research.
"""


class TestResearchIntegration:
    """Tests for chat-research integration."""

    def test_research_context_built_from_conversation_history(self):
        """Test that research context reflects the conversation turns."""
        from src.local_deep_research.chat.context import ChatContextManager

        messages = [
            {
                "id": "msg-1",
                "role": "user",
                "content": "What is quantum computing?",
                "message_type": "query",
            },
            {
                "id": "msg-2",
                "role": "assistant",
                "content": "Quantum computing uses quantum mechanics...",
                "message_type": "response",
                "research_id": "research-1",
            },
            {
                "id": "msg-3",
                "role": "user",
                "content": "What about practical applications?",
                "message_type": "query",
            },
        ]

        accumulated_context = {
            "key_entities": ["quantum computing", "qubits"],
            "topics": ["physics", "computing"],
            "summary": "Discussion about quantum computing basics.",
        }

        manager = ChatContextManager(
            session_id="test-session",
            messages=messages,
            accumulated_context=accumulated_context,
        )

        context = manager.build_research_context()

        # Should reflect the multi-turn conversation
        assert context["is_multi_turn"] is True
        assert context["turn_count"] == 3
        # Should include accumulated data
        assert "quantum computing" in context["key_entities"]
        assert "physics" in context["topics"]

    def test_context_includes_previous_research_findings(self):
        """Test that context includes findings from previous research."""
        from src.local_deep_research.chat.context import ChatContextManager

        messages = [
            {
                "id": "msg-1",
                "role": "user",
                "content": "What is quantum computing?",
                "message_type": "query",
            },
            {
                "id": "msg-2",
                "role": "assistant",
                "content": "Quantum computing is a type of computing that uses quantum bits (qubits) instead of classical bits. It leverages quantum mechanical phenomena like superposition and entanglement.",
                "message_type": "response",
                "research_id": "research-1",
            },
        ]

        manager = ChatContextManager(
            session_id="test-session",
            messages=messages,
            accumulated_context={},
        )

        findings = manager._extract_findings_from_history()

        # Should extract content from assistant messages with research_id
        assert "quantum" in findings.lower()
        assert (
            "qubits" in findings.lower() or "superposition" in findings.lower()
        )

    def test_multi_turn_flag_correct_for_first_message(self):
        """Test that is_multi_turn is False for first message."""
        from src.local_deep_research.chat.context import ChatContextManager

        manager = ChatContextManager(
            session_id="test-session",
            messages=[],  # No previous messages
            accumulated_context={},
        )

        context = manager.build_research_context()

        assert context["is_multi_turn"] is False
        assert context["turn_count"] == 0

    def test_multi_turn_flag_correct_for_followup(self):
        """Test that is_multi_turn is True for follow-up messages."""
        from src.local_deep_research.chat.context import ChatContextManager

        messages = [
            {
                "id": "msg-1",
                "role": "user",
                "content": "Initial question",
                "message_type": "query",
            },
            {
                "id": "msg-2",
                "role": "assistant",
                "content": "Initial response",
                "message_type": "response",
                "research_id": "research-1",
            },
        ]

        manager = ChatContextManager(
            session_id="test-session",
            messages=messages,
            accumulated_context={},
        )

        context = manager.build_research_context()

        assert context["is_multi_turn"] is True
        assert context["turn_count"] == 2


class TestContextExtraction:
    """Tests for context extraction from research results."""

    def test_extract_context_updates_from_new_research(self):
        """Test extracting context updates from new research content."""
        from src.local_deep_research.chat.context import ChatContextManager

        manager = ChatContextManager(
            session_id="test-session",
            messages=[],
            accumulated_context={},
        )

        new_content = """
        Quantum computing represents a fundamental shift in computing paradigms.

        Key findings:
        - Qubits can exist in multiple states simultaneously
        - Quantum entanglement enables faster processing
        - Applications include cryptography and drug discovery
        """

        updates = manager.extract_context_updates(new_content)

        # Should have summary
        assert len(updates["summary_addition"]) > 0
