"""Live Stack Exchange 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_stackexchange_live.py -v
"""

import pytest


@pytest.mark.integration
class TestStackExchangeIntegration:
    """Integration tests making real API calls to Stack Exchange."""

    def test_real_search_python(self):
        """Test real search for Python questions."""
        from local_deep_research.web_search_engines.engines.search_engine_stackexchange import (
            StackExchangeSearchEngine,
        )

        engine = StackExchangeSearchEngine(max_results=5)
        results = engine._get_previews("python list comprehension")

        # Verify we got results
        assert len(results) > 0, "Should find Python questions"

        # 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"] == "Stack Overflow", (
                "Source should be Stack Overflow"
            )
            assert "stackoverflow.com" in r["link"], "Link should be SO URL"

    def test_real_search_returns_scores(self):
        """Test that real search returns scores."""
        from local_deep_research.web_search_engines.engines.search_engine_stackexchange import (
            StackExchangeSearchEngine,
        )

        engine = StackExchangeSearchEngine(max_results=5)
        results = engine._get_previews("javascript async await")

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

        # All should have score
        for r in results:
            assert "score" in r, "Should have score"
            assert isinstance(r["score"], int), "Score should be int"

    def test_real_search_returns_tags(self):
        """Test that real search returns tags."""
        from local_deep_research.web_search_engines.engines.search_engine_stackexchange import (
            StackExchangeSearchEngine,
        )

        engine = StackExchangeSearchEngine(max_results=5)
        results = engine._get_previews("react hooks")

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

        # At least some should have tags
        has_tags = any(r.get("tags") for r in results)
        assert has_tags, (
            f"At least one should have tags, got: {[r.get('tags') for r in results]}"
        )

    def test_real_search_different_site(self):
        """Test real search on different Stack Exchange site."""
        from local_deep_research.web_search_engines.engines.search_engine_stackexchange import (
            StackExchangeSearchEngine,
        )

        engine = StackExchangeSearchEngine(max_results=5, site="unix")
        results = engine._get_previews("bash script loop")

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

        # Source should reflect the site
        assert results[0]["source"] == "Unix & Linux", (
            f"Source should be Unix & Linux, got: {results[0]['source']}"
        )
        assert results[0]["site"] == "unix"

    def test_real_search_with_high_score(self):
        """Test real search with minimum score filter."""
        from local_deep_research.web_search_engines.engines.search_engine_stackexchange import (
            StackExchangeSearchEngine,
        )

        engine = StackExchangeSearchEngine(
            max_results=5, min_score=100, sort="votes"
        )
        results = engine._get_previews("python")

        assert len(results) > 0, "Should find high-score Python questions"

        # All should have score >= 100
        for r in results:
            assert r["score"] >= 100, (
                f"Score should be >= 100, got: {r['score']}"
            )

    def test_real_search_returns_answer_count(self):
        """Test that real search returns answer counts."""
        from local_deep_research.web_search_engines.engines.search_engine_stackexchange import (
            StackExchangeSearchEngine,
        )

        engine = StackExchangeSearchEngine(max_results=5, has_answers=True)
        results = engine._get_previews("docker container")

        assert len(results) > 0, "Should find Docker questions with answers"

        # All should have at least one answer
        for r in results:
            assert r["answer_count"] >= 1, (
                f"Should have answers, got: {r['answer_count']}"
            )

    def test_real_search_returns_author_info(self):
        """Test that real search returns author information."""
        from local_deep_research.web_search_engines.engines.search_engine_stackexchange import (
            StackExchangeSearchEngine,
        )

        engine = StackExchangeSearchEngine(max_results=5)
        results = engine._get_previews("git merge conflict")

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

        # At least some should have author
        has_author = any(r.get("author") for r in results)
        assert has_author, "At least one should have author"
