import sys,json,os
from cdp import CDP
product,clip,number=sys.argv[1:4]
c=CDP(os.environ.get('VS_TARGET') or ('evidencecopilot' if product=='ec' else ('e166ecfa' if clip=='midway' else '332fd292')))
c.call('Page.bringToFront')
if product=='ec':
    result=c.js('JSON.stringify([...document.querySelectorAll("[data-testid=chat-panel] [data-status=complete]")].map(x=>x.innerText))')
    entries=json.loads(result)
    question,answer=entries[-2:]
else:
    result=c.js('JSON.stringify([...document.querySelectorAll("div.rounded-lg.px-3.py-2")].map(x=>x.innerText).filter(x=>x.startsWith("You\\n")||x.startsWith("Emma\\n")).map(x=>x.replace(/^(You|Emma)\\n\\n/,"")))')
    entries=json.loads(result)
    questions=["What is the make of each vehicle in this video?","What is the model of each vehicle in this video?","What is the license plate of each vehicle in this video?","List every appearance of each vehicle with timestamps.","What direction of travel is each vehicle going?","List all visible on-screen text in this video.","Who said what in this video, with timestamps?"]
    question=questions[int(number)-1]
    index=max(i for i,e in enumerate(entries) if e==question)
    answer=entries[index+1]
path=f'<local path>{product}_{clip}_q{number}.png'
c.shot(path)
with open('<local path>','a') as f:
    f.write(f'\n## {"Evidence Copilot" if product=="ec" else "gamma Echosaw"} — {clip} — Q{number}\n\n')
    f.write(f'**Question:** {question}\n\n**Answer (verbatim rendered text):**\n\n{answer}\n\n**Screenshot:** `{path}`\n\n')
print(question); print(answer); print(path)
