"""
Detailed behavior tests for working strategies.

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

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 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 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)
