"""
Tests for uncovered code paths in NASAADSSearchEngine._format_doc_preview.

Targets:
- bibstem as string vs list
- DOI as string vs list
- missing abstract
- non-arXiv paper
- empty keywords
- exception in formatting returns None
- journal from bibstem fallback
"""

from unittest.mock import Mock, patch

import pytest

from local_deep_research.web_search_engines.engines.search_engine_nasa_ads import (
    NasaAdsSearchEngine,
)

MODULE = "local_deep_research.web_search_engines.engines.search_engine_nasa_ads"


@pytest.fixture
def engine():
    """Create engine with mocked init to avoid settings context requirement."""
    with patch(f"{MODULE}.JournalReputationFilter"):
        eng = NasaAdsSearchEngine(
            api_key="test-key",
            max_results=5,
            settings_snapshot={
                "llm.ollama.url": {"value": "http://localhost:11434"},
            },
        )
    eng.rate_tracker = Mock()
    eng.rate_tracker.apply_rate_limit.return_value = 0
    return eng


class TestFormatDocPreview:
    def test_basic_document(self, engine):
        """Formats basic document correctly."""
        doc = {
            "bibcode": "2024ApJ...123...45S",
            "title": ["A Study of Stars"],
            "abstract": "We study stars in detail.",
            "year": "2024",
            "pubdate": "2024-01-15",
            "pub": "The Astrophysical Journal",
            "author": ["Smith, J.", "Doe, A."],
            "citation_count": 42,
            "doi": ["10.1234/test"],
            "keyword": ["stars", "astronomy"],
        }

        preview = engine._format_doc_preview(doc)

        assert preview is not None
        assert preview["title"] == "A Study of Stars"
        assert preview["year"] == "2024"
        assert preview["journal"] == "The Astrophysical Journal"
        assert preview["citations"] == 42
        assert "Smith, J., Doe, A." in preview["authors"]
        assert preview["keywords"] == ["stars", "astronomy"]
        assert "doi.org" in preview["link"]

    def test_bibstem_as_string(self, engine):
        """Handles bibstem as string instead of list."""
        doc = {
            "bibcode": "2024Test",
            "title": ["Test"],
            "bibstem": "ApJ",
        }
        preview = engine._format_doc_preview(doc)
        assert preview is not None
        assert preview["journal"] == "ApJ"

    def test_bibstem_as_list(self, engine):
        """Handles bibstem as list."""
        doc = {
            "bibcode": "2024Test",
            "title": ["Test"],
            "bibstem": ["MNRAS"],
        }
        preview = engine._format_doc_preview(doc)
        assert preview is not None
        assert preview["journal"] == "MNRAS"

    def test_empty_bibstem(self, engine):
        """Handles empty bibstem list."""
        doc = {
            "bibcode": "2024Test",
            "title": ["Test"],
            "bibstem": [],
        }
        preview = engine._format_doc_preview(doc)
        assert preview is not None
        assert preview["journal"] == "unknown"

    def test_doi_as_string(self, engine):
        """Handles DOI as string."""
        doc = {
            "bibcode": "2024Test",
            "title": ["Test"],
            "doi": "10.1234/test",
        }
        preview = engine._format_doc_preview(doc)
        assert preview is not None
        assert "doi.org/10.1234/test" in preview["link"]

    def test_doi_as_list(self, engine):
        """Handles DOI as list."""
        doc = {
            "bibcode": "2024Test",
            "title": ["Test"],
            "doi": ["10.5678/test"],
        }
        preview = engine._format_doc_preview(doc)
        assert preview is not None
        assert "doi.org/10.5678/test" in preview["link"]

    def test_no_doi_uses_ads_url(self, engine):
        """Falls back to ADS URL when no DOI."""
        doc = {
            "bibcode": "2024Test",
            "title": ["Test"],
        }
        preview = engine._format_doc_preview(doc)
        assert preview is not None
        assert "adsabs.harvard.edu" in preview["link"]
        assert "2024Test" in preview["link"]

    def test_no_abstract_uses_title(self, engine):
        """Snippet uses title when no abstract."""
        doc = {
            "bibcode": "2024Test",
            "title": ["Important Discovery"],
        }
        preview = engine._format_doc_preview(doc)
        assert preview is not None
        assert "Important Discovery" in preview["snippet"]

    def test_arxiv_detection(self, engine):
        """Detects arXiv papers from bibstem."""
        doc = {
            "bibcode": "2024arXiv",
            "title": ["arXiv Paper"],
            "bibstem": ["arXiv"],
        }
        preview = engine._format_doc_preview(doc)
        assert preview is not None
        assert preview["is_arxiv"] is True

    def test_non_arxiv(self, engine):
        """Non-arXiv papers are correctly identified."""
        doc = {
            "bibcode": "2024ApJ",
            "title": ["Journal Paper"],
            "bibstem": ["ApJ"],
        }
        preview = engine._format_doc_preview(doc)
        assert preview is not None
        assert preview["is_arxiv"] is False

    def test_empty_keywords(self, engine):
        """Handles missing keywords."""
        doc = {
            "bibcode": "2024Test",
            "title": ["Test"],
        }
        preview = engine._format_doc_preview(doc)
        assert preview is not None
        assert preview["keywords"] == []

    def test_many_authors_truncated(self, engine):
        """Authors list truncated to 5 with 'et al.'."""
        doc = {
            "bibcode": "2024Test",
            "title": ["Test"],
            "author": [f"Author{i}" for i in range(10)],
        }
        preview = engine._format_doc_preview(doc)
        assert preview is not None
        assert "et al." in preview["authors"]

    def test_empty_title_list(self, engine):
        """Handles empty title list."""
        doc = {
            "bibcode": "2024Test",
            "title": [],
        }
        preview = engine._format_doc_preview(doc)
        assert preview is not None
        assert preview["title"] == "No title"

    def test_no_title_key(self, engine):
        """Handles document with no title key at all."""
        doc = {"bibcode": "2024Test"}
        preview = engine._format_doc_preview(doc)
        assert preview is not None
        assert preview["title"] == "No title"
