import os
import sys
sys.path.insert(0, ".")
from PIL import Image
from src.common.vision_ocr import safe_recognize_pil_sync

print("--- Scanning captures for TRAVERSE and ENDGAME ---")
for root, dirs, files in os.walk("captures"):
    for f in files:
        if f.endswith(".png") and any(k in f for k in ["STAGE3", "ATLAS", "traverse", "live", "clear"]):
            p = os.path.join(root, f)
            try:
                img = Image.open(p)
                w, h = img.size
                res = safe_recognize_pil_sync(img)
                for line in res.get("lines", []):
                    t = line.get("text", "").upper()
                    if "TRAVERSE" in t:
                        for word in line.get("words", []):
                            if "TRAV" in word.get("text", "").upper():
                                rect = word["bounding_rect"]
                                cx = rect["x"] + rect["width"] / 2.0
                                cy = rect["y"] + rect["height"] / 2.0
                                print(f"[TRAVERSE] {p} ({w}x{h}): cx={cx:.1f}, cy={cy:.1f}, w={rect['width']}, h={rect['height']}")
                    if "ENDGAME" in t:
                        for word in line.get("words", []):
                            if "END" in word.get("text", "").upper() or "GAME" in word.get("text", "").upper():
                                rect = word["bounding_rect"]
                                cx = rect["x"] + rect["width"] / 2.0
                                cy = rect["y"] + rect["height"] / 2.0
                                print(f"[ENDGAME] {p} ({w}x{h}): cx={cx:.1f}, cy={cy:.1f}, w={rect['width']}, h={rect['height']}, text={word.get('text')}")
            except Exception:
                pass
