"""Live Project Gutenberg via Gutendex API integration tests.

Hit real network APIs — excluded from CI via `-m 'not integration'`.
Run locally with:

    pdm run pytest tests/performance/search_engines/test_search_engine_gutenberg_live.py -v
"""

import pytest


@pytest.mark.integration
class TestGutenbergIntegration:
    """Integration tests making real API calls to Gutendex."""

    def test_real_search_sherlock_holmes(self):
        """Test real search for Sherlock Holmes."""
        from local_deep_research.web_search_engines.engines.search_engine_gutenberg import (
            GutenbergSearchEngine,
        )

        engine = GutenbergSearchEngine(max_results=5)
        results = engine._get_previews("sherlock holmes")

        # Verify we got results
        assert len(results) > 0, "Should find Sherlock Holmes books"

        # Verify result structure
        for r in results:
            assert "title" in r, "Each result should have a title"
            assert "link" in r, "Each result should have a link"
            assert r["source"] == "Project Gutenberg"
            assert "gutenberg.org" in r["link"], "Link should be Gutenberg URL"

        # Verify at least one is actually Sherlock Holmes
        titles = [r["title"].lower() for r in results]
        assert any("sherlock" in t or "holmes" in t for t in titles), (
            f"Should find Sherlock Holmes, got: {titles}"
        )

    def test_real_search_finds_doyle(self):
        """Test that Sherlock Holmes search finds Arthur Conan Doyle."""
        from local_deep_research.web_search_engines.engines.search_engine_gutenberg import (
            GutenbergSearchEngine,
        )

        engine = GutenbergSearchEngine(max_results=5)
        results = engine._get_previews("sherlock holmes doyle")

        assert len(results) > 0, "Should find results"

        # At least one should have Doyle as author
        found_doyle = False
        for r in results:
            for author in r.get("authors", []):
                if "doyle" in author.lower():
                    found_doyle = True
                    break
        assert found_doyle, (
            f"Should find Doyle as author, got: {[r.get('authors') for r in results]}"
        )

    def test_real_search_pride_and_prejudice(self):
        """Test real search for Pride and Prejudice."""
        from local_deep_research.web_search_engines.engines.search_engine_gutenberg import (
            GutenbergSearchEngine,
        )

        engine = GutenbergSearchEngine(max_results=5)
        results = engine._get_previews("pride and prejudice austen")

        assert len(results) > 0, "Should find Pride and Prejudice"

        # Check for Jane Austen
        found_austen = False
        for r in results:
            for author in r.get("authors", []):
                if "austen" in author.lower():
                    found_austen = True
                    break
        assert found_austen, (
            f"Should find Austen, got: {[r.get('authors') for r in results]}"
        )

    def test_real_search_returns_download_count(self):
        """Test that real search returns download counts."""
        from local_deep_research.web_search_engines.engines.search_engine_gutenberg import (
            GutenbergSearchEngine,
        )

        engine = GutenbergSearchEngine(max_results=3)
        results = engine._get_previews("moby dick")

        assert len(results) > 0, "Should find Moby Dick"

        # Popular books should have download counts
        first = results[0]
        assert "download_count" in first, "Should have download_count"
        assert first["download_count"] > 0, "Popular book should have downloads"

    def test_real_search_with_language_filter(self):
        """Test real search with English language filter."""
        from local_deep_research.web_search_engines.engines.search_engine_gutenberg import (
            GutenbergSearchEngine,
        )

        engine = GutenbergSearchEngine(max_results=5, languages="en")
        results = engine._get_previews("sherlock holmes")

        assert len(results) > 0, "Should find English Sherlock Holmes books"

        # All results should have English
        for r in results:
            assert "en" in r.get("languages", []), (
                f"Should be English, got languages: {r.get('languages')}"
            )

    def test_real_search_has_read_url(self):
        """Test that results include read URLs."""
        from local_deep_research.web_search_engines.engines.search_engine_gutenberg import (
            GutenbergSearchEngine,
        )

        engine = GutenbergSearchEngine(max_results=3)
        results = engine._get_previews("frankenstein")

        assert len(results) > 0, "Should find Frankenstein"

        # At least one should have a read URL
        has_read_url = any(r.get("read_url") for r in results)
        assert has_read_url, "At least one result should have read_url"
