import asyncio, json, tempfile
from pathlib import Path
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)
B="""- 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.
"""
MIN="""- Tables printed SIDE BY SIDE in the same rows are separate tables. Report
  each with its own column span.
"""
assert B in CUR; PROMPT=CUR.replace(B,MIN)
cases=[('KREF p23','var-998c8ed06c0be22d-p23.pdf'),('KREF p33','var-998c8ed06c0be22d-p33.pdf'),('ABR p10','var-ea79061bb6be6ce0-p10.pdf'),('FBRT p11','var-16e4d896059a4eff-p11.pdf'),('ABR10Q p48','var-9df78c83fcebc851-p48.pdf')]
results=[]
async def main():
    gl.build_prompt=lambda r,c: PROMPT
    loc=gl.PydanticAIGridLocator()
    for label,fn in cases:
        pdf=S/fn
        with tempfile.TemporaryDirectory() as tmp:
            img=render_pages(pdf, 200, Path(tmp))[0]
            for i in range(3):
                located=await loc.locate(img, pdf, 1)
                rec={'page':label,'run':i+1,'n':len(located),'x':[f"{t.region[0]:.2f}-{t.region[2]:.2f}" for t in located],'titles':[t.title[:20] for t in located]}
                results.append(rec); print(json.dumps(rec), flush=True)
    json.dump(results, open(S/'som_variance6.json','w'), indent=1)
asyncio.run(main())
