"""Softened side-by-side wording vs the committed one, locator only, 3 runs per page."""
import asyncio, json, tempfile
from pathlib import Path
import pypdfium2 as pdfium
from quber.agents import grid_locator as gl
from quber.core.extractors.camelot.acquire import render_pages
S=Path('/tmp/claude-1000/-home-mande-repo-quber/bc2f817a-5c6c-4515-9621-2302b82f7f68/scratchpad')
CUR=gl.build_prompt(gl.DEFAULT_ROWS, gl.DEFAULT_COLS)
OLD="""- Two tables printed SIDE BY SIDE are separate tables: each has its own
  column-header block and its own row-label column, and a vertical band of
  empty grid columns separates them. Report each with its own column span;
  never report one region spanning both.
"""
NEW="""- Two or more tables printed SIDE BY SIDE, each with its own column-header
  block and its own row-label column, are separate tables. Report each with
  its own column span.
"""
assert OLD in CUR
SOFT=CUR.replace(OLD,NEW)
cases=[('KREF p23','998c8ed06c0be22d','data/playground/998c8ed06c0be22d.pdf',23),('KREF p33','998c8ed06c0be22d','data/playground/998c8ed06c0be22d.pdf',33),('ABR p10','ea79061bb6be6ce0',str(S/'hosted-pdfs/ea79061bb6be6ce0.pdf'),10),('FBRT p11','16e4d896059a4eff',str(S/'hosted-pdfs/16e4d896059a4eff.pdf'),11)]
N=3; results=[]
async def main():
    for label,key,path,pg in cases:
        src=pdfium.PdfDocument(path); one=pdfium.PdfDocument.new(); one.import_pages(src,[pg-1]); pdf=S/f'var-{key}-p{pg}.pdf'; one.save(str(pdf))
        with tempfile.TemporaryDirectory() as tmp:
            img=render_pages(pdf, 200, Path(tmp))[0]
            gl.build_prompt=lambda r,c,_p=SOFT: _p
            loc=gl.PydanticAIGridLocator()
            for i in range(N):
                located=await loc.locate(img, pdf, 1)
                rec={'page':label,'run':i+1,'n':len(located),'titles':[t.title[:28] for t in located],'x':[f"{t.region[0]:.2f}-{t.region[2]:.2f}" for t in located]}
                results.append(rec); print(json.dumps(rec), flush=True)
    json.dump(results, open(S/'som_variance3.json','w'), indent=1)
asyncio.run(main())
