"""Tests for OpenAlex source_id/type extraction and S2 journal_ref mapping."""

from unittest.mock import patch


MODULE_OA = (
    "local_deep_research.web_search_engines.engines.search_engine_openalex"
)
MODULE_S2 = (
    "local_deep_research.web_search_engines.engines"
    ".search_engine_semantic_scholar"
)


def _make_openalex_work(
    *,
    source_id="https://openalex.org/S137773608",
    source_name="Nature",
    source_type="journal",
):
    """Build a minimal OpenAlex work dict."""
    source = {}
    if source_id is not None:
        source["id"] = source_id
    if source_name is not None:
        source["display_name"] = source_name
    if source_type is not None:
        source["type"] = source_type

    return {
        "id": "https://openalex.org/W12345",
        "display_name": "Test Paper",
        "publication_year": 2024,
        "publication_date": "2024-01-15",
        "doi": "https://doi.org/10.1234/test",
        "primary_location": {"source": source} if source else {},
        "authorships": [],
        "cited_by_count": 42,
        "open_access": {"is_oa": False},
        "best_oa_location": None,
        "abstract_inverted_index": None,
    }


class TestOpenAlexSourceIdExtraction:
    def test_source_id_extracted_from_url(self):
        """source.id URL → short openalex_source_id."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        with patch(
            "local_deep_research.advanced_search_system.filters.journal_reputation_filter.JournalReputationFilter.create_default",
            return_value=None,
        ):
            engine = OpenAlexSearchEngine()

        work = _make_openalex_work(source_id="https://openalex.org/S137773608")
        preview = engine._format_work_preview(work)

        assert preview["openalex_source_id"] == "S137773608"

    def test_source_type_extracted(self):
        """source.type → source_type field."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        with patch(
            "local_deep_research.advanced_search_system.filters.journal_reputation_filter.JournalReputationFilter.create_default",
            return_value=None,
        ):
            engine = OpenAlexSearchEngine()

        work = _make_openalex_work(source_type="conference")
        preview = engine._format_work_preview(work)

        assert preview["source_type"] == "conference"

    def test_journal_ref_from_display_name(self):
        """source.display_name → journal_ref."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        with patch(
            "local_deep_research.advanced_search_system.filters.journal_reputation_filter.JournalReputationFilter.create_default",
            return_value=None,
        ):
            engine = OpenAlexSearchEngine()

        work = _make_openalex_work(source_name="Nature")
        preview = engine._format_work_preview(work)

        assert preview["journal_ref"] == "Nature"

    def test_journal_ref_none_when_no_source(self):
        """No source in primary_location → journal_ref is None."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        with patch(
            "local_deep_research.advanced_search_system.filters.journal_reputation_filter.JournalReputationFilter.create_default",
            return_value=None,
        ):
            engine = OpenAlexSearchEngine()

        work = _make_openalex_work(
            source_id=None, source_name=None, source_type=None
        )
        work["primary_location"] = {}
        preview = engine._format_work_preview(work)

        assert preview["journal_ref"] is None

    def test_full_content_forwards_new_fields(self):
        """_get_full_content preserves journal_ref, source_id, source_type."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        with patch(
            "local_deep_research.advanced_search_system.filters.journal_reputation_filter.JournalReputationFilter.create_default",
            return_value=None,
        ):
            engine = OpenAlexSearchEngine()

        preview = {
            "id": "W12345",
            "title": "Test",
            "link": "https://doi.org/10.1234/test",
            "snippet": "Abstract text",
            "authors": "Smith J",
            "year": 2024,
            "date": "2024-01-15",
            "journal": "Nature",
            "journal_ref": "Nature",
            "openalex_source_id": "S137773608",
            "source_type": "journal",
            "citations": 42,
            "is_open_access": False,
            "oa_url": None,
            "abstract": "Abstract text",
            "type": "academic_paper",
        }

        results = engine._get_full_content([preview])
        assert len(results) == 1
        assert results[0]["journal_ref"] == "Nature"
        assert results[0]["openalex_source_id"] == "S137773608"
        assert results[0]["source_type"] == "journal"


class TestSemanticScholarJournalRef:
    """Test the venue → journal_ref mapping logic used by S2 engine."""

    def test_venue_maps_to_journal_ref(self):
        """Non-empty venue → journal_ref = venue."""
        venue = "NeurIPS"
        journal_ref = venue if venue else None
        assert journal_ref == "NeurIPS"

    def test_empty_venue_maps_to_none(self):
        """Empty venue → journal_ref = None."""
        venue = ""
        journal_ref = venue if venue else None
        assert journal_ref is None

    def test_none_venue_maps_to_none(self):
        """None venue → journal_ref = None."""
        venue = None
        journal_ref = venue if venue else None
        assert journal_ref is None
