#!/usr/bin/env python3
"""Regenera /tmp/synbatch_noisefull/ (worklist OCR_NOISE full) desde
out_syn/*.candidates.jsonl. Determinista. Usar si /tmp se borró antes de
reanudar el workflow wf_487a47a4-947.
  python3 build_noise_worklist.py
"""
import json, os, math
from collections import defaultdict
HERE=os.path.dirname(os.path.abspath(__file__))
CFG=json.load(open(os.path.join(HERE,'books.json'))); SCAN_ROOT=CFG['_scan_root']
stem2pdf={}
for book,rels in CFG['books'].items():
    for rel in rels:
        stem2pdf[os.path.splitext(os.path.basename(rel))[0]]=os.path.join(SCAN_ROOT,rel)
sampled=set()
if os.path.exists('/tmp/syn_noise_sample.json'):
    sampled={v['page_ref'] for v in json.load(open('/tmp/syn_noise_sample.json')) if v.get('page_ref')}
pages=defaultdict(lambda:{'candidates':[]})
for bk,fn in [('SB','srimad-bhagavatam'),('CC','sri-caitanya-caritamrta')]:
    for c in (json.loads(l) for l in open(os.path.join(HERE,'out_syn',fn+'.candidates.jsonl'))):
        if c['verdict']!='OCR_NOISE': continue
        stem,pno=c['page'].split('#p'); pdf=stem2pdf.get(stem)
        if not (pdf and os.path.exists(pdf)): continue
        if c['page'] in sampled: continue
        p=pages[c['page']]; p.update(book=bk,stem=stem,pdf=pdf,page_no=int(pno),page_ref=c['page'])
        p['candidates'].append({'verdict':c['verdict'],'op':c['op'],'repo':c['repo'],
                                'scan':c['scan'],'context':c['context'],'file':c['file']})
pages=sorted(pages.values(),key=lambda p:(p['book'],p['stem'],p['page_no']))
D='/tmp/synbatch_noisefull'; os.makedirs(D,exist_ok=True)
for f in os.listdir(D): os.remove(os.path.join(D,f))
BATCH=12; nb=math.ceil(len(pages)/BATCH)
for i in range(nb):
    json.dump(pages[i*BATCH:(i+1)*BATCH],open(f'{D}/batch_{i:04d}.json','w'),ensure_ascii=False)
meta={'count':nb,'dir':D,'batch':BATCH,'pages':len(pages),'cands':sum(len(p['candidates']) for p in pages)}
json.dump(meta,open(f'{D}/meta.json','w'))
print("regenerado:", json.dumps(meta))
