"""For every page whose raw grids differ between main and branch, compare the multiset of rows that
carry at least one numeric cell. Prose, heading and furniture rows are ignored; a lost or gained
numeric row is reported."""
import json, glob, re, collections, sys
OUT='/home/mande/repo/quber/data/que370-review/sample'
NUM=re.compile(r'\d')
def txt(c): return (c.get('text') if isinstance(c,dict) else str(c)) or ''
def numeric_rows(t):
    rows=[]
    for r in t['cell_grid']:
        cells=[txt(c).strip() for c in r]
        if any(NUM.search(x) and re.search(r'[\d][\d,.\-%$()]*$', x) for x in cells if len(x)<=20 and x):
            rows.append(tuple(x for x in cells if x))
    return rows
if len(sys.argv)==3:  # explicit pair: numeric_rows_diff.py main.tables.json branch.tables.json
    jobs=[({'folder':'PAIR','filing':''},[sys.argv[1]],[sys.argv[2]])]
else:
    jobs=[]
    for o in json.load(open(f'{OUT}/sample10.json')):
        d=f"{OUT}/{o['folder']}-{o['key']}"
        jobs.append((o,glob.glob(f'{d}/main/*.tables.json'),glob.glob(f'{d}/branch/*.tables.json')))
for o,fm,fb in jobs:
    if not (fm and fb): continue
    m=json.load(open(fm[0])); b=json.load(open(fb[0]))
    pages=sorted({t['page'] for t in m}|{t['page'] for t in b}); clean=0; report=[]
    for pg in pages:
        M=[t for t in m if t['page']==pg]; B=[t for t in b if t['page']==pg]
        rm=collections.Counter(r for t in M for r in numeric_rows(t)); rb=collections.Counter(r for t in B for r in numeric_rows(t))
        if rm==rb: clean+=1; continue
        lost=list((rm-rb).elements()); gained=list((rb-rm).elements())
        report.append((pg,len(M),len(B),lost,gained))
    print(f"{o['folder']:<5} {o['filing']:<5} pages with tables {len(pages)} | numeric rows identical on {clean} | differing pages {len(report)}")
    for pg,nm,nb,lost,gained in report:
        print(f"   p{pg} tables {nm}->{nb} | lost {len(lost)} gained {len(gained)}")
        for r in lost[:4]: print('      -', ' | '.join(x[:18] for x in r)[:110])
        for r in gained[:4]: print('      +', ' | '.join(x[:18] for x in r)[:110])
