"""
Detailed behavior tests for working strategies.

Tests specific features and behaviors of strategies beyond basic functionality.
"""

import pytest
from loguru import logger


class TestSourceBasedStrategy:
    """Detailed tests for SourceBasedSearchStrategy."""

    def test_finds_sources_from_search_results(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that source-based strategy extracts sources from search results."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="source-based",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        strategy.analyze_topic("Test query")

        # Should have accumulated links (or at least not crash)
        assert isinstance(strategy.all_links_of_system, (list, set, dict))
        logger.info(
            f"Source-based found {len(strategy.all_links_of_system)} sources"
        )

    def test_generates_questions_for_sources(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that source-based strategy generates questions."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="source-based",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        strategy.analyze_topic("Test query")

        # Should have generated questions
        assert len(strategy.questions_by_iteration) > 0
        logger.info(f"Generated questions: {strategy.questions_by_iteration}")

    def test_returns_formatted_findings(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that result includes formatted_findings."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="source-based",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        result = strategy.analyze_topic("Test query")

        assert "formatted_findings" in result or "findings" in result


class TestRapidStrategy:
    """Detailed tests for RapidSearchStrategy."""

    def test_completes_in_single_iteration(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that rapid strategy completes quickly (single iteration)."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="rapid",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        result = strategy.analyze_topic("Quick test query")

        # Rapid should complete in 1 iteration
        iterations = result.get("iterations", 1)
        assert iterations <= 2, f"Rapid strategy took {iterations} iterations"
        logger.info(f"Rapid strategy completed in {iterations} iteration(s)")

    def test_uses_original_query_directly(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that rapid strategy searches with original query."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="rapid",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        query = "Test original query"
        strategy.analyze_topic(query)

        # Check that search was called
        assert strategy_mock_search.run.called


class TestParallelStrategy:
    """Detailed tests for ParallelSearchStrategy."""

    def test_executes_multiple_searches(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that parallel strategy executes multiple searches."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="parallel",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        strategy.analyze_topic("Test query for parallel")

        # Should have made multiple search calls
        call_count = strategy_mock_search.run.call_count
        logger.info(f"Parallel strategy made {call_count} search calls")


class TestIterDRAGStrategy:
    """Detailed tests for IterDRAGStrategy."""

    def test_builds_knowledge_iteratively(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that IterDRAG builds knowledge through iterations."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="iterdrag",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        result = strategy.analyze_topic("Complex research topic")

        # Should have current_knowledge
        assert "current_knowledge" in result
        assert result["current_knowledge"] is not None


class TestNewsStrategy:
    """Detailed tests for NewsAggregationStrategy."""

    def test_handles_news_queries(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that news strategy handles news-specific queries."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="news",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        result = strategy.analyze_topic("Latest developments in AI")

        assert isinstance(result, dict)
        logger.info(f"News strategy returned keys: {list(result.keys())}")


class TestFocusedIterationStrategy:
    """Detailed tests for FocusedIterationStrategy."""

    def test_uses_knowledge_accumulation(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that focused iteration accumulates knowledge."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="focused-iteration",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        result = strategy.analyze_topic("Deep research topic")

        # Should have findings
        assert "findings" in result or "current_knowledge" in result

    def test_tracks_previous_searches(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that focused iteration tracks previous searches."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="focused-iteration",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        strategy.analyze_topic("Research topic")

        # Should have questions tracked
        assert strategy.questions_by_iteration is not None


class TestConstrainedStrategies:
    """Tests for constraint-based strategies."""

    @pytest.mark.parametrize(
        "strategy_name",
        [
            "constrained",
            "parallel-constrained",
            "early-stop-constrained",
        ],
    )
    def test_constrained_strategy_works(
        self,
        strategy_name,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that constrained strategies can analyze topics."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name=strategy_name,
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        result = strategy.analyze_topic("Find X that matches Y constraint")

        assert isinstance(result, dict)
        logger.info(f"{strategy_name} returned: {list(result.keys())}")


class TestDualConfidenceStrategies:
    """Tests for dual confidence strategies."""

    @pytest.mark.parametrize(
        "strategy_name",
        [
            "dual-confidence",
            "dual-confidence-with-rejection",
            "concurrent-dual-confidence",
        ],
    )
    def test_dual_confidence_strategy_works(
        self,
        strategy_name,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that dual confidence strategies work."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name=strategy_name,
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        result = strategy.analyze_topic(
            "Question requiring confidence assessment"
        )

        assert isinstance(result, dict)
        logger.info(f"{strategy_name} keys: {list(result.keys())}")


class TestModularStrategies:
    """Tests for modular strategies."""

    @pytest.mark.parametrize("strategy_name", ["modular", "modular-parallel"])
    def test_modular_strategy_works(
        self,
        strategy_name,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that modular strategies work."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name=strategy_name,
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        result = strategy.analyze_topic("Modular research query")

        assert isinstance(result, dict)


class TestBrowseCompStrategy:
    """Tests for BrowseComp strategy."""

    def test_browsecomp_handles_puzzle_queries(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test BrowseComp with puzzle-style queries."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="browsecomp",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        puzzle_query = "What is the name of the location that was formed during the ice age?"
        result = strategy.analyze_topic(puzzle_query)

        assert isinstance(result, dict)
        logger.info(f"BrowseComp result keys: {list(result.keys())}")


class TestSmartQueryStrategy:
    """Tests for SmartQueryStrategy."""

    def test_smart_query_selects_strategy(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that smart-query selects appropriate strategy."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="smart-query",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        result = strategy.analyze_topic("Research question")

        assert isinstance(result, dict)


class TestTopicOrganizationStrategy:
    """Tests for TopicOrganizationStrategy."""

    def test_topic_organization_works(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test topic organization strategy."""
        from loguru import logger
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        # Initialize MILESTONE log level if not already defined
        # This is normally done during web app initialization
        try:
            logger.level("MILESTONE")
        except ValueError:
            logger.level("MILESTONE", no=26, color="<magenta><bold>")

        strategy = create_strategy(
            strategy_name="topic-organization",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        result = strategy.analyze_topic("Multi-topic research question")

        assert isinstance(result, dict)


class TestIterativeRefinementStrategy:
    """Tests for IterativeRefinementStrategy."""

    def test_iterative_refinement_works(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test iterative refinement strategy."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="iterative-refinement",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        result = strategy.analyze_topic("Question to refine iteratively")

        assert isinstance(result, dict)
        logger.info(f"Iterative refinement keys: {list(result.keys())}")
