#!/usr/bin/env python3
import json, time
from pathlib import Path
import pyautogui, pytesseract
from PIL import Image, ImageOps, ImageFilter
pyautogui.FAILSAFE=False
ROOT=Path(r'C:\MCSJAgent\proof\MichelBridge'); ROOT.mkdir(parents=True,exist_ok=True)
TESS=r'C:\Program Files\Tesseract-OCR\tesseract.exe'
pytesseract.pytesseract.tesseract_cmd=TESS

def shot(label):
    p=ROOT/f'account-probe-{label}-{time.strftime("%Y%m%d-%H%M%S")}.png'
    im=pyautogui.screenshot(); im.save(p); return str(p)

def ocr(path):
    im=Image.open(path)
    gray=ImageOps.grayscale(im).filter(ImageFilter.SHARPEN)
    return pytesseract.image_to_string(gray, config='--psm 6')[:2500]

before=shot('before')
pyautogui.click(675,493); time.sleep(.8)
opened=shot('opened')
text=ocr(opened)
pyautogui.press('esc'); time.sleep(.4)
after=shot('after-esc')
print(json.dumps({'before':before,'opened':opened,'after':after,'opened_ocr':text}, separators=(',',':')))
