from sqlalchemy import text

from .db import SessionLocal

# Enrichment from RecordGod's OWN cached catalog (disc_cache) — self-contained, no live
# dependency on the full Discogs dump or dealgod's DB. Populate via build_disc_cache.py.
# DealGod is reached only via its API (for market value), never its database.


async def enrich(ids):
    """release_id(s) → {id: {title, artist, thumb, weight}}. Best-effort."""
    ids = [i for i in set(ids) if i is not None]
    if not ids:
        return {}
    try:
        async with SessionLocal() as s:
            rows = await s.execute(text(
                "SELECT release_id AS id, title, artist, thumb, weight "
                "FROM disc_cache WHERE release_id = ANY(:ids)"), {"ids": ids})
            return {r["id"]: dict(r) for r in rows.mappings()}
    except Exception:
        return {}
