"""
Test that all strategy classes can be imported without errors.

This is Level 1 testing - verifying imports work before testing functionality.
"""

import pytest
from loguru import logger

from .conftest import STRATEGY_IMPORTS, FACTORY_STRATEGY_NAMES


class TestStrategyImports:
    """Test that all strategy classes can be imported."""

    @pytest.mark.parametrize("module_name,class_name", STRATEGY_IMPORTS)
    def test_strategy_class_import(self, module_name: str, class_name: str):
        """Test that each strategy class can be imported from its module."""
        try:
            module = __import__(
                f"local_deep_research.advanced_search_system.strategies.{module_name}",
                fromlist=[class_name],
            )
            strategy_class = getattr(module, class_name)

            # Verify it's a class
            assert isinstance(strategy_class, type), (
                f"{class_name} is not a class"
            )

            # Verify it has analyze_topic method
            assert hasattr(strategy_class, "analyze_topic"), (
                f"{class_name} missing analyze_topic method"
            )

            logger.info(
                f"Successfully imported {class_name} from {module_name}"
            )

        except ImportError as e:
            pytest.fail(
                f"Failed to import {class_name} from {module_name}: {e}"
            )
        except AttributeError as e:
            pytest.fail(f"Class {class_name} not found in {module_name}: {e}")

    def test_base_strategy_import(self):
        """Test that BaseSearchStrategy can be imported."""
        from local_deep_research.advanced_search_system.strategies.base_strategy import (
            BaseSearchStrategy,
        )

        assert BaseSearchStrategy is not None
        assert hasattr(BaseSearchStrategy, "analyze_topic")
        assert hasattr(BaseSearchStrategy, "set_progress_callback")

    def test_strategies_init_exports(self):
        """Test that the strategies __init__.py exports key classes."""
        try:
            from local_deep_research.advanced_search_system import (
                strategies,
            )

            # Check if at least some strategies are exported
            # Not all may be exported via __init__, but the module should be importable
            logger.info(f"Strategies module dir: {dir(strategies)}")

        except ImportError as e:
            pytest.fail(f"Failed to import strategies module: {e}")


class TestFactoryImports:
    """Test that the factory can import and create strategies."""

    def test_factory_import(self):
        """Test that the search_system_factory can be imported."""
        try:
            from local_deep_research.search_system_factory import (
                create_strategy,
            )

            assert create_strategy is not None
            assert callable(create_strategy)

        except ImportError as e:
            pytest.fail(f"Failed to import create_strategy from factory: {e}")

    def test_search_system_import(self):
        """Test that AdvancedSearchSystem can be imported."""
        try:
            from local_deep_research.search_system import (
                AdvancedSearchSystem,
            )

            assert AdvancedSearchSystem is not None
            assert hasattr(AdvancedSearchSystem, "analyze_topic")

        except ImportError as e:
            pytest.fail(f"Failed to import AdvancedSearchSystem: {e}")

    @pytest.mark.parametrize("strategy_name", FACTORY_STRATEGY_NAMES)
    def test_factory_strategy_import_path(self, strategy_name: str):
        """
        Test that each factory strategy name can be resolved to an import.

        This tests the import paths inside the factory without actually creating instances.
        """
        from local_deep_research.search_system_factory import (
            create_strategy,
        )
        from unittest.mock import Mock

        # Create minimal mocks
        mock_model = Mock()
        mock_search = Mock()

        # Try to create the strategy - this will attempt the import
        try:
            strategy = create_strategy(
                strategy_name=strategy_name,
                model=mock_model,
                search=mock_search,
            )

            # Verify we got something back
            assert strategy is not None, (
                f"Factory returned None for {strategy_name}"
            )

            # Verify it has the required method
            assert hasattr(strategy, "analyze_topic"), (
                f"Strategy {strategy_name} missing analyze_topic method"
            )

            logger.info(
                f"Factory successfully created: {strategy_name} -> {strategy.__class__.__name__}"
            )

        except ImportError as e:
            pytest.fail(f"Factory import failed for {strategy_name}: {e}")
        except Exception as e:
            # Log but don't fail - instantiation errors are tested elsewhere
            logger.warning(
                f"Factory creation failed for {strategy_name}: {type(e).__name__}: {e}"
            )
            pytest.skip(
                f"Strategy {strategy_name} has instantiation issues (tested elsewhere)"
            )


class TestSupportingModuleImports:
    """Test imports of supporting modules used by strategies."""

    def test_citation_handler_import(self):
        """Test CitationHandler import."""
        from local_deep_research.citation_handler import CitationHandler

        assert CitationHandler is not None

    def test_findings_repository_import(self):
        """Test FindingsRepository import."""
        from local_deep_research.advanced_search_system.findings.repository import (
            FindingsRepository,
        )

        assert FindingsRepository is not None

    def test_question_generators_import(self):
        """Test question generator imports."""
        from local_deep_research.advanced_search_system.questions.standard_question import (
            StandardQuestionGenerator,
        )
        from local_deep_research.advanced_search_system.questions.atomic_fact_question import (
            AtomicFactQuestionGenerator,
        )

        assert StandardQuestionGenerator is not None
        assert AtomicFactQuestionGenerator is not None

    def test_cross_engine_filter_import(self):
        """Test CrossEngineFilter import."""
        from local_deep_research.advanced_search_system.filters.cross_engine_filter import (
            CrossEngineFilter,
        )

        assert CrossEngineFilter is not None

    def test_search_utilities_import(self):
        """Test search utilities import."""
        from local_deep_research.utilities.search_utilities import (
            extract_links_from_search_results,
        )

        assert extract_links_from_search_results is not None
