"""
Behavioral tests for followup_research models.

Tests FollowUpRequest and FollowUpResponse dataclasses.
"""


class TestFollowUpRequestInit:
    """Tests for FollowUpRequest initialization."""

    def test_requires_parent_research_id(self):
        """Requires parent_research_id."""
        from local_deep_research.followup_research.models import FollowUpRequest

        request = FollowUpRequest(
            parent_research_id="abc123", question="What about X?"
        )
        assert request.parent_research_id == "abc123"

    def test_requires_question(self):
        """Requires question."""
        from local_deep_research.followup_research.models import FollowUpRequest

        request = FollowUpRequest(
            parent_research_id="abc123", question="What about X?"
        )
        assert request.question == "What about X?"

    def test_default_strategy_is_source_based(self):
        """Default strategy is source-based."""
        from local_deep_research.followup_research.models import FollowUpRequest

        request = FollowUpRequest(
            parent_research_id="abc123", question="Question"
        )
        assert request.strategy == "source-based"

    def test_accepts_custom_strategy(self):
        """Accepts custom strategy."""
        from local_deep_research.followup_research.models import FollowUpRequest

        request = FollowUpRequest(
            parent_research_id="abc123",
            question="Question",
            strategy="deep-dive",
        )
        assert request.strategy == "deep-dive"

    def test_default_max_iterations_is_1(self):
        """Default max_iterations is 1."""
        from local_deep_research.followup_research.models import FollowUpRequest

        request = FollowUpRequest(
            parent_research_id="abc123", question="Question"
        )
        assert request.max_iterations == 1

    def test_accepts_custom_max_iterations(self):
        """Accepts custom max_iterations."""
        from local_deep_research.followup_research.models import FollowUpRequest

        request = FollowUpRequest(
            parent_research_id="abc123",
            question="Question",
            max_iterations=5,
        )
        assert request.max_iterations == 5

    def test_default_questions_per_iteration_is_3(self):
        """Default questions_per_iteration is 3."""
        from local_deep_research.followup_research.models import FollowUpRequest

        request = FollowUpRequest(
            parent_research_id="abc123", question="Question"
        )
        assert request.questions_per_iteration == 3

    def test_accepts_custom_questions_per_iteration(self):
        """Accepts custom questions_per_iteration."""
        from local_deep_research.followup_research.models import FollowUpRequest

        request = FollowUpRequest(
            parent_research_id="abc123",
            question="Question",
            questions_per_iteration=5,
        )
        assert request.questions_per_iteration == 5


class TestFollowUpRequestToDict:
    """Tests for FollowUpRequest.to_dict method."""

    def test_returns_dict(self):
        """to_dict returns a dictionary."""
        from local_deep_research.followup_research.models import FollowUpRequest

        request = FollowUpRequest(
            parent_research_id="abc123", question="Question"
        )
        result = request.to_dict()
        assert isinstance(result, dict)

    def test_includes_parent_research_id(self):
        """Includes parent_research_id in dict."""
        from local_deep_research.followup_research.models import FollowUpRequest

        request = FollowUpRequest(
            parent_research_id="abc123", question="Question"
        )
        result = request.to_dict()
        assert result["parent_research_id"] == "abc123"

    def test_includes_question(self):
        """Includes question in dict."""
        from local_deep_research.followup_research.models import FollowUpRequest

        request = FollowUpRequest(
            parent_research_id="abc123", question="What about X?"
        )
        result = request.to_dict()
        assert result["question"] == "What about X?"

    def test_includes_strategy(self):
        """Includes strategy in dict."""
        from local_deep_research.followup_research.models import FollowUpRequest

        request = FollowUpRequest(
            parent_research_id="abc123",
            question="Question",
            strategy="deep-dive",
        )
        result = request.to_dict()
        assert result["strategy"] == "deep-dive"

    def test_includes_max_iterations(self):
        """Includes max_iterations in dict."""
        from local_deep_research.followup_research.models import FollowUpRequest

        request = FollowUpRequest(
            parent_research_id="abc123", question="Question", max_iterations=5
        )
        result = request.to_dict()
        assert result["max_iterations"] == 5

    def test_includes_questions_per_iteration(self):
        """Includes questions_per_iteration in dict."""
        from local_deep_research.followup_research.models import FollowUpRequest

        request = FollowUpRequest(
            parent_research_id="abc123",
            question="Question",
            questions_per_iteration=10,
        )
        result = request.to_dict()
        assert result["questions_per_iteration"] == 10

    def test_dict_has_all_fields(self):
        """Dict has all expected fields."""
        from local_deep_research.followup_research.models import FollowUpRequest

        request = FollowUpRequest(
            parent_research_id="abc123", question="Question"
        )
        result = request.to_dict()
        expected_keys = {
            "parent_research_id",
            "question",
            "strategy",
            "max_iterations",
            "questions_per_iteration",
        }
        assert set(result.keys()) == expected_keys


class TestFollowUpResponseInit:
    """Tests for FollowUpResponse initialization."""

    def test_requires_all_fields(self):
        """Requires all fields."""
        from local_deep_research.followup_research.models import (
            FollowUpResponse,
        )

        response = FollowUpResponse(
            research_id="res123",
            question="What about X?",
            answer="The answer is Y.",
            sources_used=[{"url": "https://example.com"}],
            parent_context_used=True,
            reused_links_count=3,
            new_links_count=2,
        )
        assert response.research_id == "res123"
        assert response.question == "What about X?"
        assert response.answer == "The answer is Y."

    def test_sources_used_is_list(self):
        """sources_used is a list."""
        from local_deep_research.followup_research.models import (
            FollowUpResponse,
        )

        response = FollowUpResponse(
            research_id="res123",
            question="Question",
            answer="Answer",
            sources_used=[{"url": "url1"}, {"url": "url2"}],
            parent_context_used=True,
            reused_links_count=1,
            new_links_count=1,
        )
        assert isinstance(response.sources_used, list)
        assert len(response.sources_used) == 2

    def test_parent_context_used_is_bool(self):
        """parent_context_used is boolean."""
        from local_deep_research.followup_research.models import (
            FollowUpResponse,
        )

        response = FollowUpResponse(
            research_id="res123",
            question="Question",
            answer="Answer",
            sources_used=[],
            parent_context_used=False,
            reused_links_count=0,
            new_links_count=0,
        )
        assert response.parent_context_used is False

    def test_link_counts_are_ints(self):
        """Link counts are integers."""
        from local_deep_research.followup_research.models import (
            FollowUpResponse,
        )

        response = FollowUpResponse(
            research_id="res123",
            question="Question",
            answer="Answer",
            sources_used=[],
            parent_context_used=True,
            reused_links_count=5,
            new_links_count=10,
        )
        assert response.reused_links_count == 5
        assert response.new_links_count == 10


class TestFollowUpResponseToDict:
    """Tests for FollowUpResponse.to_dict method."""

    def test_returns_dict(self):
        """to_dict returns a dictionary."""
        from local_deep_research.followup_research.models import (
            FollowUpResponse,
        )

        response = FollowUpResponse(
            research_id="res123",
            question="Question",
            answer="Answer",
            sources_used=[],
            parent_context_used=True,
            reused_links_count=0,
            new_links_count=0,
        )
        result = response.to_dict()
        assert isinstance(result, dict)

    def test_includes_research_id(self):
        """Includes research_id in dict."""
        from local_deep_research.followup_research.models import (
            FollowUpResponse,
        )

        response = FollowUpResponse(
            research_id="res123",
            question="Question",
            answer="Answer",
            sources_used=[],
            parent_context_used=True,
            reused_links_count=0,
            new_links_count=0,
        )
        result = response.to_dict()
        assert result["research_id"] == "res123"

    def test_includes_question(self):
        """Includes question in dict."""
        from local_deep_research.followup_research.models import (
            FollowUpResponse,
        )

        response = FollowUpResponse(
            research_id="res123",
            question="What about X?",
            answer="Answer",
            sources_used=[],
            parent_context_used=True,
            reused_links_count=0,
            new_links_count=0,
        )
        result = response.to_dict()
        assert result["question"] == "What about X?"

    def test_includes_answer(self):
        """Includes answer in dict."""
        from local_deep_research.followup_research.models import (
            FollowUpResponse,
        )

        response = FollowUpResponse(
            research_id="res123",
            question="Question",
            answer="The detailed answer.",
            sources_used=[],
            parent_context_used=True,
            reused_links_count=0,
            new_links_count=0,
        )
        result = response.to_dict()
        assert result["answer"] == "The detailed answer."

    def test_includes_sources_used(self):
        """Includes sources_used in dict."""
        from local_deep_research.followup_research.models import (
            FollowUpResponse,
        )

        sources = [{"url": "url1", "title": "Source 1"}]
        response = FollowUpResponse(
            research_id="res123",
            question="Question",
            answer="Answer",
            sources_used=sources,
            parent_context_used=True,
            reused_links_count=1,
            new_links_count=0,
        )
        result = response.to_dict()
        assert result["sources_used"] == sources

    def test_includes_parent_context_used(self):
        """Includes parent_context_used in dict."""
        from local_deep_research.followup_research.models import (
            FollowUpResponse,
        )

        response = FollowUpResponse(
            research_id="res123",
            question="Question",
            answer="Answer",
            sources_used=[],
            parent_context_used=True,
            reused_links_count=0,
            new_links_count=0,
        )
        result = response.to_dict()
        assert result["parent_context_used"] is True

    def test_includes_link_counts(self):
        """Includes link counts in dict."""
        from local_deep_research.followup_research.models import (
            FollowUpResponse,
        )

        response = FollowUpResponse(
            research_id="res123",
            question="Question",
            answer="Answer",
            sources_used=[],
            parent_context_used=True,
            reused_links_count=5,
            new_links_count=10,
        )
        result = response.to_dict()
        assert result["reused_links_count"] == 5
        assert result["new_links_count"] == 10

    def test_dict_has_all_fields(self):
        """Dict has all expected fields."""
        from local_deep_research.followup_research.models import (
            FollowUpResponse,
        )

        response = FollowUpResponse(
            research_id="res123",
            question="Question",
            answer="Answer",
            sources_used=[],
            parent_context_used=True,
            reused_links_count=0,
            new_links_count=0,
        )
        result = response.to_dict()
        expected_keys = {
            "research_id",
            "question",
            "answer",
            "sources_used",
            "parent_context_used",
            "reused_links_count",
            "new_links_count",
        }
        assert set(result.keys()) == expected_keys


class TestFollowUpModelsDataclass:
    """Tests for dataclass behavior."""

    def test_request_is_dataclass(self):
        """FollowUpRequest is a dataclass."""
        from dataclasses import is_dataclass

        from local_deep_research.followup_research.models import FollowUpRequest

        assert is_dataclass(FollowUpRequest)

    def test_response_is_dataclass(self):
        """FollowUpResponse is a dataclass."""
        from dataclasses import is_dataclass

        from local_deep_research.followup_research.models import (
            FollowUpResponse,
        )

        assert is_dataclass(FollowUpResponse)

    def test_request_equality(self):
        """FollowUpRequest supports equality comparison."""
        from local_deep_research.followup_research.models import FollowUpRequest

        req1 = FollowUpRequest(
            parent_research_id="abc", question="Q", strategy="s"
        )
        req2 = FollowUpRequest(
            parent_research_id="abc", question="Q", strategy="s"
        )
        assert req1 == req2

    def test_response_equality(self):
        """FollowUpResponse supports equality comparison."""
        from local_deep_research.followup_research.models import (
            FollowUpResponse,
        )

        resp1 = FollowUpResponse(
            research_id="r",
            question="Q",
            answer="A",
            sources_used=[],
            parent_context_used=True,
            reused_links_count=0,
            new_links_count=0,
        )
        resp2 = FollowUpResponse(
            research_id="r",
            question="Q",
            answer="A",
            sources_used=[],
            parent_context_used=True,
            reused_links_count=0,
            new_links_count=0,
        )
        assert resp1 == resp2
