"""Run a real PubMed pipeline to test enrichment fields.

Pulls ~500 real articles from NCBI E-utilities via the existing PubMed API service,
stores them in the local PostgreSQL, and reports which enrichment fields get populated.

Usage: PYTHONPATH=. python scripts/test_enrichment_pipeline.py
"""
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

os.environ.setdefault("DATABASE_URL", "postgresql+asyncpg://scilit:scilit_dev@localhost:5432/scilit")
os.environ.setdefault("JWT_SECRET", "dev-secret-change-in-production")
os.environ.setdefault("DEBUG", "true")
os.environ.setdefault("SPECIALTY", "oncology")

import asyncio
from app.core.database import async_session_factory, get_db_session
from app.models.literature import GlobalLiterature
from app.services.pubmed_api import _run_pipeline


async def main():
    print("=" * 60)
    print("Running PubMed pipeline (MAJR mode) — pulling ~500 real articles")
    print("=" * 60)

    session = async_session_factory()
    try:
        db = session()
        await db.__aenter__()

        # Run the pipeline in broad mode (title/abstract search, higher recall)
        # max_per_query=50, up to 10 queries = 500 articles
        result = await _run_pipeline(
            db=db,
            use_majr=False,
            use_broad=True,
            max_per_query=50,
        )
        print(f"\nPipeline result: {result}")

        # Now check what enrichment fields were populated
        count = await db.execute(
            "SELECT count(*) FROM global_literature"
        )
        total = count.scalar()
        print(f"\nTotal literature in DB: {total}")

        # Check each enrichment field
        checks = [
            ("chemical_list", "chemical_list IS NOT NULL AND chemical_list != '[]'::jsonb"),
            ("gene_symbols", "gene_symbols IS NOT NULL AND gene_symbols != '[]'::jsonb"),
            ("num_refs", "num_refs IS NOT NULL"),
            ("publication_status", "publication_status IS NOT NULL"),
            ("article_date", "article_date IS NOT NULL"),
            ("databank_list", "databank_list IS NOT NULL AND databank_list != '[]'::jsonb"),
            ("suppl_mesh_list", "suppl_mesh_list IS NOT NULL AND suppl_mesh_list != '[]'::jsonb"),
        ]

        print("\n── Enrichment field coverage ──")
        for field_name, condition in checks:
            r = await db.execute(
                f"SELECT count(*) FROM global_literature WHERE {condition}"
            )
            filled = r.scalar()
            pct = filled / total * 100 if total else 0
            print(f"  {field_name:20s}: {filled:4d}/{total} ({pct:4.1f}%)")

        # Show 5 sample PMIDs with rich data
        r = await db.execute(
            "SELECT pmid, chemical_list, gene_symbols, num_refs, publication_status, "
            "article_date, databank_list, suppl_mesh_list "
            "FROM global_literature "
            "WHERE chemical_list != '[]'::jsonb "
            "LIMIT 5"
        )
        rows = r.fetchall()
        print(f"\n── Sample articles with chemical data ({len(rows)} shown) ──")
        for row in rows:
            print(f"  PMID {row.pmid}:")
            print(f"    chemicals={len(row.chemical_list) if row.chemical_list else 0}")
            print(f"    genes={row.gene_symbols}")
            print(f"    refs={row.num_refs}")
            print(f"    status={row.publication_status}")
            print(f"    article_date={row.article_date}")
            print(f"    databanks={len(row.databank_list) if row.databank_list else 0}")
            print(f"    supplmesh={len(row.suppl_mesh_list) if row.suppl_mesh_list else 0}")

        await db.commit()
        print("\n✅ Pipeline test complete")

    finally:
        await session.close()


if __name__ == "__main__":
    asyncio.run(main())
