"""Live Open Library 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_openlibrary_live.py -v
"""

import pytest


@pytest.mark.integration
class TestOpenLibraryIntegration:
    """Integration tests making real API calls to Open Library."""

    def test_real_search_harry_potter(self):
        """Test real search for Harry Potter books."""
        from local_deep_research.web_search_engines.engines.search_engine_openlibrary import (
            OpenLibrarySearchEngine,
        )

        engine = OpenLibrarySearchEngine(max_results=5)
        results = engine._get_previews("harry potter")

        # Verify we got results
        assert len(results) > 0, "Should find Harry Potter 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"] == "Open Library", (
                "Source should be Open Library"
            )
            assert r["link"].startswith("https://openlibrary.org"), (
                "Link should be valid"
            )

        # Verify at least one result mentions Harry Potter
        titles = [r["title"].lower() for r in results]
        assert any("harry potter" in t or "potter" in t for t in titles), (
            f"At least one result should contain 'Harry Potter', got: {titles}"
        )

    def test_real_author_search_stephen_king(self):
        """Test real author search for Stephen King."""
        from local_deep_research.web_search_engines.engines.search_engine_openlibrary import (
            OpenLibrarySearchEngine,
        )

        engine = OpenLibrarySearchEngine(max_results=5, search_field="author")
        results = engine._get_previews("Stephen King")

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

        # Verify at least one result has Stephen King as author
        found_king = False
        for r in results:
            authors = r.get("authors", [])
            for author in authors:
                if "king" in author.lower():
                    found_king = True
                    break
        assert found_king, (
            f"At least one result should have King as author, got: {[r.get('authors') for r in results]}"
        )

    def test_real_title_search_1984(self):
        """Test real title search for 1984."""
        from local_deep_research.web_search_engines.engines.search_engine_openlibrary import (
            OpenLibrarySearchEngine,
        )

        engine = OpenLibrarySearchEngine(max_results=5, search_field="title")
        results = engine._get_previews("1984")

        # Verify we got results
        assert len(results) > 0, "Should find books with 1984 in title"

        # Verify result has expected fields
        first = results[0]
        assert "title" in first
        assert "authors" in first
        assert "link" in first

    def test_real_search_returns_metadata(self):
        """Test that real search returns proper metadata."""
        from local_deep_research.web_search_engines.engines.search_engine_openlibrary import (
            OpenLibrarySearchEngine,
        )

        engine = OpenLibrarySearchEngine(max_results=3)
        results = engine._get_previews("lord of the rings tolkien")

        assert len(results) > 0, "Should find Lord of the Rings"

        # Check first result has expected metadata
        first = results[0]
        assert first.get("title"), "Should have title"
        assert first.get("authors"), "Should have authors"
        assert first.get("link"), "Should have link"
        assert first.get("id"), "Should have id"

        # Verify it's actually Tolkien
        authors = first.get("authors", [])
        assert any("tolkien" in a.lower() for a in authors), (
            f"Should find Tolkien as author, got: {authors}"
        )

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

        engine = OpenLibrarySearchEngine(max_results=5, language="eng")
        results = engine._get_previews("science fiction")

        assert len(results) > 0, "Should find science fiction books in English"
        assert all(r["source"] == "Open Library" for r in results)
