"""
shop_probe.py — test the ShopScanner OCR on a screenshot (no game needed).
    python shop_probe.py [path-to-screenshot.png]
Defaults to the most recent Desktop Screenshot. Prints parsed items and saves an
annotated crop (shop_probe_out.png) so you can check SHOP_REGION alignment.
"""
import sys
import os
import cv2
import shop_scanner as ss


def main():
    path = sys.argv[1] if len(sys.argv) > 1 else os.path.join(
        os.path.expanduser("~"), "Desktop", "Screenshot_25.png")
    img = cv2.imread(path)
    if img is None:
        print(f"could not read {path}")
        return
    print(f"image {img.shape[1]}x{img.shape[0]}  OCR_OK={ss.OCR_OK}")
    loc = ss.locate_window(img)
    if loc is None:
        print("shop window NOT detected (template match below threshold)")
        return
    _, x0, y0, conf = loc
    print(f"shop window detected at ({x0},{y0}) confidence={conf:.3f}")
    items, seller, shop_name = ss.parse_shop(img)
    print(f"seller={seller!r}  shop_name={shop_name!r}  items={len(items)}")
    for name, price in items:
        print(f"   {name:28} {price:>12,}")
    out = os.path.join(os.path.dirname(os.path.abspath(__file__)), "shop_probe_out.png")
    vis = img.copy()
    cv2.rectangle(vis, (x0, y0), (x0 + ss.WIN_W, y0 + ss.WIN_H), (0, 255, 255), 2)
    cv2.imwrite(out, vis[max(0, y0-10):y0+ss.WIN_H+10, max(0, x0-10):x0+ss.WIN_W+10])
    print("saved", out)


if __name__ == "__main__":
    main()
