import logging
import pytest
from seekify import Search

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Engines to test
TEXT_ENGINES = [
    "bing", "brave", "duckduckgo", "google", 
    "grokipedia", "mojeek", "yandex", "yahoo", "wikipedia"
]
IMAGE_ENGINES = ["duckduckgo"]
VIDEO_ENGINES = ["duckduckgo"]
NEWS_ENGINES = ["bing", "duckduckgo", "yahoo"]
BOOK_ENGINES = ["annasarchive"]

@pytest.mark.parametrize("engine", TEXT_ENGINES)
def test_text_search(engine):
    logger.info(f"Testing text search with backend: {engine}")
    with Search() as s:
        results = s.text("python programming", backend=engine, max_results=3)
        assert results is not None
        # Some engines might legitimately return 0 results or be blocked/rate-limited. 
        # We warn instead of failing strictly if empty, but we expect a list.
        if not results:
            logger.warning(f"No results returned for {engine}")
        else:
            assert len(results) > 0
            assert "title" in results[0]
            assert "href" in results[0]

@pytest.mark.parametrize("engine", IMAGE_ENGINES)
def test_image_search(engine):
    logger.info(f"Testing image search with backend: {engine}")
    with Search() as s:
        results = s.images("cute cat", backend=engine, max_results=3)
        assert results is not None
        if not results:
            logger.warning(f"No results returned for {engine}")
        else:
            assert len(results) > 0
            assert "image" in results[0]

@pytest.mark.parametrize("engine", VIDEO_ENGINES)
def test_video_search(engine):
    logger.info(f"Testing video search with backend: {engine}")
    with Search() as s:
        results = s.videos("python tutorial", backend=engine, max_results=3)
        assert results is not None
        if not results:
            logger.warning(f"No results returned for {engine}")
        else:
            assert len(results) > 0
            assert "content" in results[0]

@pytest.mark.parametrize("engine", NEWS_ENGINES)
def test_news_search(engine):
    logger.info(f"Testing news search with backend: {engine}")
    try:
        with Search() as s:
            results = s.news("technology", backend=engine, max_results=3)
            assert results is not None
            if not results:
                logger.warning(f"No results returned for {engine}")
            else:
                assert len(results) > 0
                assert "title" in results[0]
    except Exception as e:
        logger.warning(f"News search failed for {engine}: {e}")
        # Mark as skip or pass with warning to avoid failing the whole suite for one flaky provider
        pytest.skip(f"Provider {engine} failed: {e}")

@pytest.mark.parametrize("engine", BOOK_ENGINES)
def test_book_search(engine):
    logger.info(f"Testing book search with backend: {engine}")
    try:
        with Search() as s:
            results = s.books("python", backend=engine, max_results=3)
            assert results is not None
            if not results:
                logger.warning(f"No results returned for {engine}")
            else:
                assert len(results) > 0
    except Exception as e:
        # Anna's archive can be unstable or blocked
        logger.warning(f"Book search failed for {engine}: {e}")
