import sqlite3,glob,os,json,sys
# slim main rh day files older than CUT to pools+kv (venuepick first_seen needs pools). Only if rb copy has >= rows.
CUT="2026-09-25"
a=json.load(open('/tmp/cnt_main.json')); b=json.load(open('/tmp/cnt_rb.json'))
log=open('/opt/hist/rh_slim.log','a')
for d in sorted(a):
    if d>=CUT: continue
    f="/opt/hist/rh/%s.db"%d
    if 'ERR' in a[d]:
        # corrupt on main: rb copy is good (rb has pools)
        a[d]={}
    if d not in b or any(b[d].get(t,0)<n for t,n in a[d].items()): print("skip",d,file=log); continue
    if a[d]=={} : cnt=None
    else: cnt=a[d]
    tmp="/opt/hist/rh/%s.slim.tmp"%d
    if os.path.exists(tmp): os.remove(tmp)
    try:
        src=sqlite3.connect("file:/opt/hist/rh_rbcopy/%s.db?mode=ro"%d,uri=True) if cnt is None else sqlite3.connect("file:%s?mode=ro"%f,uri=True)
        dst=sqlite3.connect(tmp)
        for t in ("pools","kv"):
            sql=src.execute("select sql from sqlite_master where name=?",(t,)).fetchone()[0]
            dst.execute(sql)
            rows=src.execute("select * from %s"%t).fetchall()
            if rows: dst.executemany("insert into %s values(%s)"%(t,",".join("?"*len(rows[0]))),rows)
        dst.commit(); dst.close(); src.close()
    except Exception as e:
        print("fail",d,repr(e),file=log); log.flush()
        if os.path.exists(tmp): os.remove(tmp)
        continue
    before=os.path.getsize(f)
    os.replace(tmp,f)
    for s in ("-wal","-shm"):
        if os.path.exists(f+s): os.remove(f+s)
    print("slim",d,"pools",(cnt or {}).get("pools"),"orig_bytes",before,"orig_rows",json.dumps(a[d]),file=log); log.flush()
