"""
Tests for PubMedSearchEngine._create_enriched_content() edge cases.

Existing tests cover: study type prefix, MeSH terms, grants, and
minimal result (no metadata). This file adds coverage for untested branches:

- Keywords metadata
- Affiliations (single vs multiple)
- Conflict of interest (trivial, non-trivial, "but" exceptions)
- Multiple grants with various fields
- Publication type filtering (non-significant types filtered out)
- Empty/missing metadata fields
- Combined metadata interactions
"""

import pytest

defusedxml = pytest.importorskip(
    "defusedxml", reason="defusedxml not installed"
)


def _make_engine():
    """Import and instantiate PubMedSearchEngine inside function to avoid import issues."""
    from local_deep_research.web_search_engines.engines.search_engine_pubmed import (
        PubMedSearchEngine,
    )

    return PubMedSearchEngine()


class TestEnrichedContentKeywords:
    """Tests for keywords metadata in enriched content."""

    def test_keywords_included(self):
        """Keywords from result are included."""
        engine = _make_engine()
        result = {"keywords": ["machine learning", "neural networks", "AI"]}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Keywords: machine learning, neural networks, AI" in enriched

    def test_empty_keywords_not_shown(self):
        """Empty keywords list is not included."""
        engine = _make_engine()
        result = {"keywords": []}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Keywords" not in enriched

    def test_single_keyword(self):
        """Single keyword is shown."""
        engine = _make_engine()
        result = {"keywords": ["pharmacology"]}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Keywords: pharmacology" in enriched


class TestEnrichedContentAffiliations:
    """Tests for affiliations metadata."""

    def test_single_affiliation_uses_institution(self):
        """Single affiliation uses 'Institution:' label."""
        engine = _make_engine()
        result = {"affiliations": ["Harvard Medical School"]}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Institution: Harvard Medical School" in enriched
        assert "Institutions:" not in enriched

    def test_multiple_affiliations_uses_institutions(self):
        """Multiple affiliations use 'Institutions:' label with list."""
        engine = _make_engine()
        result = {
            "affiliations": [
                "Harvard Medical School",
                "MIT",
                "Stanford University",
            ]
        }
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Institutions:" in enriched
        assert "Harvard Medical School" in enriched
        assert "MIT" in enriched
        assert "Stanford University" in enriched

    def test_empty_affiliations_not_shown(self):
        """Empty affiliations list is not included."""
        engine = _make_engine()
        result = {"affiliations": []}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Institution" not in enriched
        assert "Institutions" not in enriched


class TestEnrichedContentConflictOfInterest:
    """Tests for conflict of interest handling."""

    def test_non_trivial_coi_included(self):
        """Non-trivial conflict of interest is included."""
        engine = _make_engine()
        result = {
            "conflict_of_interest": "Author A received consulting fees from Pharma Corp."
        }
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Conflict of Interest:" in enriched
        assert "consulting fees" in enriched

    def test_trivial_no_conflict_excluded(self):
        """Trivial 'no conflict' statement is excluded."""
        engine = _make_engine()
        result = {
            "conflict_of_interest": "The authors declare no conflict of interest."
        }
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Conflict of Interest" not in enriched

    def test_trivial_no_competing_excluded(self):
        """Trivial 'no competing' statement is excluded."""
        engine = _make_engine()
        result = {"conflict_of_interest": "No competing interests exist."}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Conflict of Interest" not in enriched

    def test_trivial_nothing_to_disclose_excluded(self):
        """Trivial 'nothing to disclose' statement is excluded."""
        engine = _make_engine()
        result = {
            "conflict_of_interest": "The authors have nothing to disclose."
        }
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Conflict of Interest" not in enriched

    def test_trivial_none_declared_excluded(self):
        """Trivial 'none declared' statement is excluded."""
        engine = _make_engine()
        result = {"conflict_of_interest": "None declared."}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Conflict of Interest" not in enriched

    def test_trivial_authors_declare_no_excluded(self):
        """Trivial 'authors declare no' statement is excluded."""
        engine = _make_engine()
        result = {"conflict_of_interest": "The authors declare no conflicts."}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Conflict of Interest" not in enriched

    def test_coi_with_but_exception_included(self):
        """CoI with 'but' exception is still included."""
        engine = _make_engine()
        result = {
            "conflict_of_interest": "The authors declare no conflict of interest, but Author B is on the advisory board of Pharma Inc."
        }
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Conflict of Interest" in enriched
        assert "advisory board" in enriched

    def test_coi_with_except_exception_included(self):
        """CoI with 'except' exception is still included."""
        engine = _make_engine()
        result = {
            "conflict_of_interest": "No competing interests except for grant funding from Industry Corp."
        }
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Conflict of Interest" in enriched

    def test_coi_with_however_exception_included(self):
        """CoI with 'however' exception is still included."""
        engine = _make_engine()
        result = {
            "conflict_of_interest": "Authors declare no conflicts; however, Author C owns stock in BioTech Ltd."
        }
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Conflict of Interest" in enriched

    def test_empty_coi_not_shown(self):
        """Empty string CoI is not included."""
        engine = _make_engine()
        result = {"conflict_of_interest": ""}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Conflict of Interest" not in enriched

    def test_none_coi_not_shown(self):
        """None CoI (key absent) is not included."""
        engine = _make_engine()
        result = {}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Conflict of Interest" not in enriched


class TestEnrichedContentGrants:
    """Tests for grant/funding metadata."""

    def test_single_grant_with_agency_and_id(self):
        """Single grant with both agency and ID."""
        engine = _make_engine()
        result = {"grants": [{"agency": "NIH", "id": "R01-12345"}]}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Funded by: NIH (Grant ID: R01-12345)" in enriched

    def test_single_grant_agency_only(self):
        """Single grant with agency only."""
        engine = _make_engine()
        result = {"grants": [{"agency": "NSF"}]}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Funded by: NSF" in enriched
        assert "Grant ID" not in enriched

    def test_single_grant_id_only(self):
        """Single grant with ID only."""
        engine = _make_engine()
        result = {"grants": [{"id": "ABC-789"}]}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Funded by:" in enriched
        assert "ABC-789" in enriched

    def test_multiple_grants(self):
        """Multiple grants shown as a list."""
        engine = _make_engine()
        result = {
            "grants": [
                {"agency": "NIH", "id": "R01-111"},
                {"agency": "NSF", "id": "DMS-222"},
            ]
        }
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Funding Sources:" in enriched
        assert "NIH" in enriched
        assert "NSF" in enriched

    def test_empty_grants_not_shown(self):
        """Empty grants list is not shown."""
        engine = _make_engine()
        result = {"grants": []}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Fund" not in enriched

    def test_grant_with_empty_fields(self):
        """Grant with no agency or id fields is skipped."""
        engine = _make_engine()
        result = {"grants": [{"country": "USA"}]}
        enriched = engine._create_enriched_content(result, "Abstract.")
        # No "Funded by" because the grant_text list is empty
        assert "Fund" not in enriched


class TestEnrichedContentPublicationTypes:
    """Tests for publication type filtering."""

    def test_clinical_trial_is_significant(self):
        """Clinical trial is a significant publication type."""
        engine = _make_engine()
        result = {"publication_types": ["Clinical Trial", "Journal Article"]}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "[Study Type: Clinical Trial]" in enriched

    def test_meta_analysis_is_significant(self):
        """Meta-analysis is a significant publication type."""
        engine = _make_engine()
        result = {"publication_types": ["Meta-Analysis"]}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "[Study Type: Meta-Analysis]" in enriched

    def test_systematic_review_is_significant(self):
        """Systematic review is a significant publication type."""
        engine = _make_engine()
        result = {"publication_types": ["Systematic Review"]}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "[Study Type: Systematic Review]" in enriched

    def test_case_report_is_significant(self):
        """Case report is a significant publication type."""
        engine = _make_engine()
        result = {"publication_types": ["Case Report"]}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "[Study Type: Case Report]" in enriched

    def test_guideline_is_significant(self):
        """Practice guideline is a significant publication type."""
        engine = _make_engine()
        result = {"publication_types": ["Practice Guideline"]}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Guideline" in enriched

    def test_journal_article_not_significant(self):
        """Plain 'Journal Article' is not a significant type."""
        engine = _make_engine()
        result = {"publication_types": ["Journal Article"]}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "[Study Type:" not in enriched

    def test_multiple_significant_types(self):
        """Multiple significant types are joined."""
        engine = _make_engine()
        result = {
            "publication_types": [
                "Randomized Controlled Trial",
                "Multicenter Study",
                "Journal Article",
            ]
        }
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Randomized Controlled Trial" in enriched
        assert "Multicenter Study" in enriched

    def test_empty_publication_types_not_shown(self):
        """Empty publication_types list doesn't add study type."""
        engine = _make_engine()
        result = {"publication_types": []}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "[Study Type:" not in enriched


class TestEnrichedContentMetadataFooter:
    """Tests for the metadata footer section."""

    def test_metadata_footer_has_separator(self):
        """Metadata footer is preceded by a separator."""
        engine = _make_engine()
        result = {"keywords": ["test"]}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "---\nStudy Metadata:" in enriched

    def test_no_metadata_no_footer(self):
        """No metadata produces no footer separator."""
        engine = _make_engine()
        result = {}
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Study Metadata" not in enriched

    def test_base_content_always_present(self):
        """Base content is always present in output."""
        engine = _make_engine()
        result = {}
        enriched = engine._create_enriched_content(
            result, "The full abstract text."
        )
        assert "The full abstract text." in enriched

    def test_combined_metadata(self):
        """Multiple metadata types combined in footer."""
        engine = _make_engine()
        result = {
            "keywords": ["drug", "therapy"],
            "mesh_terms": ["Humans", "Neoplasms"],
            "affiliations": ["MGH"],
        }
        enriched = engine._create_enriched_content(result, "Abstract.")
        assert "Keywords:" in enriched
        assert "Medical Topics (MeSH):" in enriched
        assert "Institution: MGH" in enriched
