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)
MIN="""- Tables printed SIDE BY SIDE in the same rows are separate tables. Report
  each with its own column span.
"""
assert MIN in CUR
W={'W1':CUR.replace(MIN,"""- Two tables printed SIDE BY SIDE, one to the left of the other in the same
  rows, are separate tables. Report each with its own column span.
"""),
   'W3':CUR.replace(MIN,"""- Two or more tables printed SIDE BY SIDE, each with its own column headers,
  are separate tables. Report each with its own column span.
""")}
cases=[('ABR99-2 p8','var-ea79061bb6be6ce0-p8.pdf'),('ABR p10','var-ea79061bb6be6ce0-p10.pdf'),('FBRT p11','var-16e4d896059a4eff-p11.pdf'),('ABR10Q p48','var-9df78c83fcebc851-p48.pdf'),('KREF p23','var-998c8ed06c0be22d-p23.pdf'),('KREF p33','var-998c8ed06c0be22d-p33.pdf')]
results=[]
async def main():
    for name,prompt in W.items():
        gl.build_prompt=lambda r,c,_p=prompt: _p
        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={'w':name,'page':label,'run':i+1,'n':len(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_variance7.json','w'), indent=1)
asyncio.run(main())
