"""Live PubChem 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_pubchem_live.py -v
"""

import pytest


@pytest.mark.integration
class TestPubChemIntegration:
    """Integration tests making real API calls to PubChem."""

    def test_real_search_aspirin(self):
        """Test real search for aspirin."""
        from local_deep_research.web_search_engines.engines.search_engine_pubchem import (
            PubChemSearchEngine,
        )

        engine = PubChemSearchEngine(max_results=3)
        results = engine._get_previews("aspirin")

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

        # Verify first result
        first = results[0]
        assert "title" in first, "Should have title"
        assert "link" in first, "Should have link"
        assert first["source"] == "PubChem", "Source should be PubChem"
        assert "pubchem.ncbi.nlm.nih.gov" in first["link"], (
            "Link should be PubChem URL"
        )

    def test_real_search_returns_molecular_formula(self):
        """Test that real search returns molecular formula."""
        from local_deep_research.web_search_engines.engines.search_engine_pubchem import (
            PubChemSearchEngine,
        )

        engine = PubChemSearchEngine(max_results=1)
        results = engine._get_previews("caffeine")

        assert len(results) > 0, "Should find caffeine"

        first = results[0]
        assert first.get("molecular_formula"), "Should have molecular_formula"
        # Caffeine is C8H10N4O2
        assert "C" in first["molecular_formula"], "Formula should contain C"

    def test_real_search_returns_molecular_weight(self):
        """Test that real search returns molecular weight."""
        from local_deep_research.web_search_engines.engines.search_engine_pubchem import (
            PubChemSearchEngine,
        )

        engine = PubChemSearchEngine(max_results=1)
        results = engine._get_previews("glucose")

        assert len(results) > 0, "Should find glucose"

        first = results[0]
        assert first.get("molecular_weight"), "Should have molecular_weight"
        # Just verify molecular weight is a positive number
        # (PubChem may return different glucose compounds like derivatives)
        weight = float(first["molecular_weight"])
        assert weight > 0, f"Molecular weight should be positive, got {weight}"

    def test_real_search_returns_smiles(self):
        """Test that real search returns SMILES notation."""
        from local_deep_research.web_search_engines.engines.search_engine_pubchem import (
            PubChemSearchEngine,
        )

        engine = PubChemSearchEngine(max_results=1)
        results = engine._get_previews("ethanol")

        assert len(results) > 0, "Should find ethanol"

        first = results[0]
        assert first.get("smiles"), "Should have SMILES"
        # Ethanol SMILES should contain C and O
        assert "C" in first["smiles"] and "O" in first["smiles"], (
            f"Ethanol SMILES should contain C and O, got: {first['smiles']}"
        )

    def test_real_search_returns_cid(self):
        """Test that real search returns compound ID."""
        from local_deep_research.web_search_engines.engines.search_engine_pubchem import (
            PubChemSearchEngine,
        )

        engine = PubChemSearchEngine(max_results=1)
        results = engine._get_previews("water")

        assert len(results) > 0, "Should find water"

        first = results[0]
        assert first.get("cid"), "Should have CID"
        assert isinstance(first["cid"], int), "CID should be integer"

    def test_real_search_ibuprofen(self):
        """Test real search for ibuprofen."""
        from local_deep_research.web_search_engines.engines.search_engine_pubchem import (
            PubChemSearchEngine,
        )

        engine = PubChemSearchEngine(max_results=1)
        results = engine._get_previews("ibuprofen")

        assert len(results) > 0, "Should find ibuprofen"

        first = results[0]
        # Ibuprofen CID is 3672
        assert first.get("cid"), "Should have CID"
        assert first.get("iupac_name"), "Should have IUPAC name"

    def test_real_search_partial_name(self):
        """Test real search with partial compound name."""
        from local_deep_research.web_search_engines.engines.search_engine_pubchem import (
            PubChemSearchEngine,
        )

        engine = PubChemSearchEngine(max_results=5)
        results = engine._get_previews("acet")

        # Should find compounds starting with "acet" like acetone, acetamide, etc.
        assert len(results) > 0, "Should find compounds starting with 'acet'"

        # At least one should have a title starting with "acet"
        titles = [r["title"].lower() for r in results]
        has_acet = any("acet" in t for t in titles)
        assert has_acet, (
            f"Should find compound starting with 'acet', got: {titles}"
        )
