#!/usr/bin/env python3
"""Populate recordgod.disc_cache from the full Discogs mirror — RecordGod's own copy of just
the releases it stocks, so enrichment has no live cross-DB dependency. Run on ultra (where
discogs_full lives); re-run anytime to refresh for current inventory."""
import os

import psycopg

RG = os.getenv("RECORDGOD_DSN", "postgresql://localhost/recordgod")
DG = os.getenv("DISCOGS_FULL_DSN", "postgresql://localhost/discogs_full")


def main():
    rg = psycopg.connect(RG)
    dg = psycopg.connect(DG)
    ids = [r[0] for r in rg.execute(
        "SELECT DISTINCT release_id FROM inventory WHERE release_id IS NOT NULL").fetchall()]
    rg.execute("""CREATE TABLE IF NOT EXISTS disc_cache (
        release_id bigint PRIMARY KEY, title text, artist text, thumb text, weight int)""")
    rg.execute("TRUNCATE disc_cache")
    n = 0
    for i in range(0, len(ids), 5000):
        rows = dg.execute(
            "SELECT id, title, artists_sort, COALESCE(thumb_local_url, thumb), estimated_weight "
            "FROM release WHERE id = ANY(%s)", (ids[i:i + 5000],)).fetchall()
        with rg.cursor() as c:
            c.executemany(
                "INSERT INTO disc_cache (release_id, title, artist, thumb, weight) "
                "VALUES (%s,%s,%s,%s,%s) ON CONFLICT (release_id) DO NOTHING", rows)
        n += len(rows)
    rg.commit()
    print(f"disc_cache: {n} of {len(ids)} stocked releases cached")


if __name__ == "__main__":
    main()
