#!/usr/bin/env python3
"""omniclick — macOS UI automation: screencapture -> OmniParser -> cliclick.

Turns a screenshot into labelled UI elements (OmniParser v2: YOLOv9-E icon
detector + EasyOCR + Florence-2 captions) and clicks them by name.

    omniclick.py doctor                     # can this machine click at all?
    omniclick.py displays                   # geometry + measured retina scale
    omniclick.py snapshot                   # JSON of every detected element
    omniclick.py click "Search"             # find best match, click it
    omniclick.py click "Search" --dry-run    # resolve only, do not click

The hard part of this integration is the coordinate space, so it is worth
stating explicitly:

  * `screencapture` writes **backing-store pixels**. On a retina display that
    is 2x the logical size; on a HiDPI-scaled mode it can be a fractional
    factor such as 1.7083.
  * `cliclick` consumes **logical points** in the *global* desktop space,
    where display origins are stacked next to each other and can be negative.

This tool never assumes 2.0. It measures the factor per capture as
`image_pixels / CGDisplayBounds_points` and applies the display origin, so a
non-retina screen, a retina screen and a scaled secondary monitor all map
correctly. See `omniclick.py displays`.

Environment:
  OMNIPARSER_HOME   OmniParser checkout. Defaults to <data-dir>/omniparser, which
                    is where `alvin-bot omniclick install` puts it. Point this at
                    an existing checkout to reuse it instead of downloading again.
  ALVIN_DATA_DIR    bot data directory (default ~/.alvin-bot)
  OMNICLICK_DEVICE  torch device: mps | cpu | cuda (default mps)
"""

from __future__ import annotations

import argparse
import contextlib
import ctypes
import difflib
import json
import os
import plistlib
import re
import shutil
import subprocess
import sys
import tempfile
import time
import unicodedata
from dataclasses import dataclass, field
from pathlib import Path

_T_PROCESS = time.time()  # for honest wall-clock accounting
_T_CMD = _T_PROCESS

def _default_omniparser_home() -> Path:
    """Where the OmniParser checkout lives.

    No absolute path is baked in: the default follows the bot's own data
    directory, so the same script works for every install. `OMNIPARSER_HOME`
    overrides it for anyone who already has a checkout elsewhere.
    """
    env = os.environ.get("OMNIPARSER_HOME")
    if env:
        return Path(env).expanduser()
    data_dir = os.environ.get("ALVIN_DATA_DIR")
    base = Path(data_dir).expanduser() if data_dir else Path.home() / ".alvin-bot"
    return base / "omniparser"


OMNIPARSER_HOME = _default_omniparser_home()
SCREENCAPTURE = "/usr/sbin/screencapture"  # not on PATH in daemon/agent shells
CLICLICK_CANDIDATES = ("/opt/homebrew/bin/cliclick", "/usr/local/bin/cliclick", "cliclick")

# Upstream ships the detector as `icon_detect` on main but as `icon_detect_v3` in
# the pull request that carries the v3 weights, and the caption weights are called
# `icon_caption` on the hub while the README renames them to
# `icon_caption_florence`. Accept either spelling rather than making the install
# depend on which of the two a given download produced.
DETECTOR_DIRS = ("icon_detect_v3", "icon_detect")
CAPTION_DIRS = ("icon_caption_florence", "icon_caption")


def weight_path(dir_names: tuple, filename: str) -> Path:
    """First existing <home>/weights/<name>/<filename>, else the canonical one.

    Returning the canonical path when nothing exists keeps error messages and
    `doctor` output pointing at the name the installer actually creates.
    """
    for name in dir_names:
        p = OMNIPARSER_HOME / "weights" / name / filename
        if p.is_file():
            return p
    return OMNIPARSER_HOME / "weights" / dir_names[0] / filename


def detector_weights() -> Path:
    return weight_path(DETECTOR_DIRS, "model.pt")


def caption_weights_dir() -> Path:
    return weight_path(CAPTION_DIRS, "model.safetensors").parent

# A capture with less content than this is a locked screen or a missing Screen
# Recording grant, not a UI. Clicking on it would be clicking blind.
BLANK_CONTENT_RATIO = 0.001

EXIT_OK, EXIT_USAGE, EXIT_NO_MATCH, EXIT_BLOCKED, EXIT_CLICK_FAILED = 0, 2, 3, 4, 5


# --------------------------------------------------------------------------
# interpreter bootstrap
# --------------------------------------------------------------------------

def ensure_omniparser_interpreter(need_torch: bool = False) -> None:
    """Re-exec under the OmniParser venv if this python is not it.

    Lets the script be invoked as plain `omniclick.py` from any shell while the
    heavy deps stay pinned inside the OmniParser venv (torch 2.13 + the
    transformers==4.49 pin Florence-2 needs). Pillow is the probe rather than
    torch because importing torch costs ~6.5s and only the parsing commands
    actually need it — those pass need_torch=True.
    """
    try:
        if need_torch:
            import torch  # noqa: F401
        else:
            from PIL import Image  # noqa: F401
        return
    except ImportError:
        pass
    venv_python = OMNIPARSER_HOME / ".venv" / "bin" / "python"
    if not venv_python.is_file():
        sys.exit(f"omniclick: no Pillow here and no venv at {venv_python}\n"
                 f"           set OMNIPARSER_HOME or install OmniParser first.")
    if os.environ.get("_OMNICLICK_REEXEC"):
        sys.exit(f"omniclick: {venv_python} still cannot import Pillow.")
    os.environ["_OMNICLICK_REEXEC"] = "1"
    os.execv(str(venv_python), [str(venv_python), os.path.abspath(__file__), *sys.argv[1:]])


# --------------------------------------------------------------------------
# display geometry  (the retina / multi-monitor core)
# --------------------------------------------------------------------------

class _CGPoint(ctypes.Structure):
    _fields_ = [("x", ctypes.c_double), ("y", ctypes.c_double)]


class _CGSize(ctypes.Structure):
    _fields_ = [("width", ctypes.c_double), ("height", ctypes.c_double)]


class _CGRect(ctypes.Structure):
    _fields_ = [("origin", _CGPoint), ("size", _CGSize)]


def _cg():
    cg = ctypes.CDLL("/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics")
    cg.CGDisplayBounds.restype = _CGRect
    cg.CGDisplayBounds.argtypes = [ctypes.c_uint32]
    cg.CGDisplayCopyDisplayMode.restype = ctypes.c_void_p
    cg.CGDisplayCopyDisplayMode.argtypes = [ctypes.c_uint32]
    cg.CGDisplayModeRelease.argtypes = [ctypes.c_void_p]
    for fn in ("CGDisplayModeGetPixelWidth", "CGDisplayModeGetPixelHeight",
               "CGDisplayModeGetWidth", "CGDisplayModeGetHeight"):
        f = getattr(cg, fn)
        f.restype, f.argtypes = ctypes.c_size_t, [ctypes.c_void_p]
    cg.CGMainDisplayID.restype = ctypes.c_uint32
    return cg


@dataclass
class Display:
    index: int          # 1-based, matches `screencapture -D`
    display_id: int
    origin: tuple       # (x, y) logical points, global desktop space
    size_pt: tuple      # (w, h) logical points
    size_px: tuple      # (w, h) backing pixels as reported by the display mode
    is_main: bool

    @property
    def reported_scale(self) -> float:
        return self.size_px[0] / self.size_pt[0] if self.size_pt[0] else 1.0

    def as_dict(self) -> dict:
        return {"index": self.index, "display_id": self.display_id,
                "origin_pt": list(self.origin), "size_pt": list(self.size_pt),
                "size_px": list(self.size_px),
                "reported_scale": round(self.reported_scale, 4), "main": self.is_main}


def list_displays() -> list[Display]:
    cg = _cg()
    max_d = 16
    ids = (ctypes.c_uint32 * max_d)()
    count = ctypes.c_uint32()
    if cg.CGGetActiveDisplayList(max_d, ids, ctypes.byref(count)) != 0:
        raise RuntimeError("CGGetActiveDisplayList failed")
    main = cg.CGMainDisplayID()
    out = []
    for i in range(count.value):
        did = ids[i]
        b = cg.CGDisplayBounds(did)
        mode = cg.CGDisplayCopyDisplayMode(did)
        px = (cg.CGDisplayModeGetPixelWidth(mode), cg.CGDisplayModeGetPixelHeight(mode))
        cg.CGDisplayModeRelease(mode)
        out.append(Display(index=i + 1, display_id=did,
                           origin=(b.origin.x, b.origin.y),
                           size_pt=(b.size.width, b.size.height),
                           size_px=px, is_main=(did == main)))
    return out


def pick_display(displays: list[Display], want: int | None) -> Display:
    if want is None:
        return next((d for d in displays if d.is_main), displays[0])
    for d in displays:
        if d.index == want:
            return d
    raise SystemExit(f"omniclick: no display #{want} (have 1..{len(displays)})")


@dataclass
class Frame:
    """A captured image plus everything needed to map its pixels to points."""
    path: Path
    size_px: tuple
    origin_pt: tuple          # logical origin of the captured area
    size_pt: tuple            # logical size of the captured area
    display: Display | None = None
    notes: list = field(default_factory=list)
    content: dict = field(default_factory=dict)

    @property
    def scale(self) -> tuple:
        return (self.size_px[0] / self.size_pt[0], self.size_px[1] / self.size_pt[1])

    def to_point(self, px: float, py: float) -> tuple:
        """Pixel in this image -> logical point in the global desktop space."""
        sx, sy = self.scale
        return (self.origin_pt[0] + px / sx, self.origin_pt[1] + py / sy)

    def as_dict(self) -> dict:
        sx, sy = self.scale
        return {"image": str(self.path), "size_px": list(self.size_px),
                "origin_pt": list(self.origin_pt), "size_pt": list(self.size_pt),
                "scale": [round(sx, 4), round(sy, 4)],
                "display_index": self.display.index if self.display else None,
                "content": self.content, "notes": self.notes}


# --------------------------------------------------------------------------
# session / permission state
# --------------------------------------------------------------------------

def session_state() -> dict:
    """Console session facts from ioreg (no pyobjc dependency)."""
    state = {"screen_locked": None, "on_console": None, "secure_input_pid": None,
             "locked_since": None}
    try:
        raw = subprocess.run(["/usr/sbin/ioreg", "-n", "Root", "-d1", "-a"],
                             capture_output=True, timeout=15).stdout
        root = plistlib.loads(raw)
        # `ioreg -a` returns a dict for a single matched node and a list when
        # several match. Handle both — assuming one shape silently reports the
        # screen as unlocked, which would let a click fire at a blank screen.
        nodes = root if isinstance(root, list) else [root]
        for node in nodes:
            if not isinstance(node, dict):
                continue
            if "IOConsoleLocked" in node:
                state["screen_locked"] = bool(node["IOConsoleLocked"])
            for e in node.get("IOConsoleUsers", []) or []:
                if "CGSSessionScreenIsLocked" in e:
                    state["screen_locked"] = bool(e["CGSSessionScreenIsLocked"])
                if "CGSSessionScreenLockedTime" in e:
                    state["locked_since"] = e["CGSSessionScreenLockedTime"]
                if "kCGSSessionOnConsoleKey" in e:
                    state["on_console"] = bool(e["kCGSSessionOnConsoleKey"])
                if e.get("kCGSSessionSecureInputPID"):
                    state["secure_input_pid"] = e["kCGSSessionSecureInputPID"]
    except Exception as exc:  # ioreg shape varies across releases; never fatal
        state["error"] = str(exc)
    if state["screen_locked"] is None:
        state["screen_locked"] = False  # absent key means unlocked
    return state


def cliclick_path() -> str | None:
    for c in CLICLICK_CANDIDATES:
        p = shutil.which(c) if not c.startswith("/") else (c if Path(c).is_file() else None)
        if p:
            return p
    return None


def cliclick_trusted(path: str) -> tuple:
    """(trusted, cursor_position). cliclick warns on stderr when TCC denies it."""
    try:
        r = subprocess.run([path, "p:"], capture_output=True, text=True, timeout=15)
    except Exception as exc:
        return False, f"cliclick failed: {exc}"
    warned = "Accessibility privileges not enabled" in (r.stderr or "")
    return (not warned), (r.stdout or "").strip()


# --------------------------------------------------------------------------
# capture
# --------------------------------------------------------------------------

class CaptureError(RuntimeError):
    pass


def content_stats(path: Path) -> dict:
    """How much of this image is actually something?

    Luma extrema alone are not enough: a locked screen with a single bright
    artefact in one corner spreads 10..114 and would pass as real UI. So the
    image is box-downscaled first (which averages specks away) and then the
    share of pixels deviating from the median luma is measured.
    """
    from PIL import Image
    with Image.open(path) as im:
        g = im.convert("L")
        lo, hi = g.getextrema()
        w = 400
        thumb = g.resize((w, max(1, round(g.height * w / g.width))), Image.BOX)
    px = thumb.tobytes()  # mode "L" -> one byte per pixel
    px_sorted = sorted(px)
    median = px_sorted[len(px_sorted) // 2]
    deviating = sum(1 for p in px if abs(p - median) > 10)
    return {"luma_min": lo, "luma_max": hi, "luma_spread": hi - lo,
            "median_luma": median,
            "content_ratio": round(deviating / len(px), 5)}


def _run_screencapture(args: list, dest: Path) -> None:
    if not Path(SCREENCAPTURE).is_file():
        raise CaptureError(f"{SCREENCAPTURE} missing")
    r = subprocess.run([SCREENCAPTURE, "-x", *args, str(dest)],
                       capture_output=True, text=True, timeout=60)
    # screencapture exits 0 even when it writes nothing at all.
    if not dest.is_file() or dest.stat().st_size == 0:
        raise CaptureError(f"screencapture wrote no file ({(r.stderr or '').strip() or 'no stderr'})")


def capture(display: Display | None = None, region: tuple | None = None,
            window_id: int | None = None, dest: Path | None = None,
            allow_blank: bool = False) -> Frame:
    from PIL import Image
    dest = dest or Path(tempfile.mkdtemp(prefix="omniclick-")) / "capture.png"
    dest.parent.mkdir(parents=True, exist_ok=True)

    if window_id is not None:
        _run_screencapture(["-l", str(window_id)], dest)
        origin_pt, size_pt_hint = (0.0, 0.0), None
    elif region is not None:
        x, y, w, h = region
        _run_screencapture(["-R", f"{x},{y},{w},{h}"], dest)
        origin_pt, size_pt_hint = (float(x), float(y)), (float(w), float(h))
    else:
        assert display is not None
        _run_screencapture(["-D", str(display.index)], dest)
        origin_pt, size_pt_hint = display.origin, display.size_pt

    with Image.open(dest) as im:
        size_px = im.size

    if size_pt_hint is None:
        # Window capture: the backing scale of the owning display is the best
        # available estimate, and the origin is unknown, so clicking a window
        # capture is refused later rather than guessed.
        s = display.reported_scale if display else 1.0
        size_pt_hint = (size_px[0] / s, size_px[1] / s)

    frame = Frame(path=dest, size_px=size_px, origin_pt=origin_pt,
                  size_pt=size_pt_hint, display=display)

    sx, sy = frame.scale
    if abs(sx - sy) > 0.01:
        frame.notes.append(f"non-uniform scale {sx:.4f}x{sy:.4f} — capture may be cropped")

    stats = content_stats(dest)
    frame.content = stats
    if stats["content_ratio"] < BLANK_CONTENT_RATIO:
        msg = (f"capture is blank (content ratio {stats['content_ratio']} < "
               f"{BLANK_CONTENT_RATIO}, luma {stats['luma_min']}..{stats['luma_max']}). "
               "The screen is locked, asleep, or this process lacks the Screen "
               "Recording grant. Nothing was detected, so nothing will be clicked.")
        if not allow_blank:
            raise CaptureError(msg)
        frame.notes.append(msg)
    return frame


# --------------------------------------------------------------------------
# OmniParser
# --------------------------------------------------------------------------

_MODELS: dict = {}


@contextlib.contextmanager
def quiet_stdout():
    """Keep stdout pure JSON.

    EasyOCR, ultralytics and transformers all print progress to stdout. Anything
    consuming `omniclick snapshot` with json.loads would choke on it, so their
    chatter is rerouted to stderr where it stays visible for debugging.
    """
    with contextlib.redirect_stdout(sys.stderr):
        yield


def _ensure_omniparser_on_path() -> None:
    """`util.utils` only resolves once the checkout is on sys.path."""
    p = str(OMNIPARSER_HOME)
    if p not in sys.path:
        sys.path.insert(0, p)
    os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")


def load_models(device: str, captions: bool):
    key = (device, captions)
    if key in _MODELS:
        return _MODELS[key]
    _ensure_omniparser_on_path()
    from util.utils import get_caption_model_processor, get_yolo_model
    t0 = time.time()
    with quiet_stdout():
        som = get_yolo_model(model_path=str(detector_weights()), device=device)
        cap = None
        if captions:
            cap = get_caption_model_processor(
                model_name="florence2",
                model_name_or_path=str(caption_weights_dir()),
                device=device)
    _MODELS[key] = (som, cap, time.time() - t0)
    return _MODELS[key]


def parse_frame(frame: Frame, device: str, captions: bool,
                box_threshold: float = 0.05, iou_threshold: float = 0.7,
                batch_size: int = 64, annotate: Path | None = None) -> dict:
    """Run OmniParser over a frame and return elements in pixel + point space."""
    import base64
    from PIL import Image
    _ensure_omniparser_on_path()
    # This import pulls in easyocr + torchvision and costs ~15s cold. It used to
    # sit outside every timer, which made the reported total look impossible.
    t_imp = time.time()
    with quiet_stdout():
        from util.utils import check_ocr_box, get_som_labeled_img
    t_import = time.time() - t_imp

    som, cap, t_load = load_models(device, captions)
    image = Image.open(frame.path).convert("RGB")

    t0 = time.time()
    with quiet_stdout():
        (ocr_text, ocr_bbox), _ = check_ocr_box(
            image, display_img=False, output_bb_format="xyxy",
            easyocr_args={"text_threshold": 0.8}, use_paddleocr=False)
    t_ocr = time.time() - t0

    ratio = max(image.size) / 3200
    draw_cfg = {"text_scale": 0.8 * ratio, "text_thickness": max(int(2 * ratio), 1),
                "text_padding": max(int(3 * ratio), 1), "thickness": max(int(3 * ratio), 1)}

    t0 = time.time()
    with quiet_stdout():
        labeled_b64, _coords, parsed = get_som_labeled_img(
            image, som, BOX_TRESHOLD=box_threshold, output_coord_in_ratio=True,
            ocr_bbox=ocr_bbox, draw_bbox_config=draw_cfg, caption_model_processor=cap,
            ocr_text=ocr_text, use_local_semantics=captions, iou_threshold=iou_threshold,
            scale_img=False, batch_size=batch_size)
    t_parse = time.time() - t0

    if annotate:
        annotate.parent.mkdir(parents=True, exist_ok=True)
        annotate.write_bytes(base64.b64decode(labeled_b64))

    w, h = image.size
    elements = []
    for i, e in enumerate(parsed):
        nx1, ny1, nx2, ny2 = e["bbox"]                      # normalised 0..1
        x1, y1, x2, y2 = nx1 * w, ny1 * h, nx2 * w, ny2 * h  # image pixels
        cx, cy = (x1 + x2) / 2, (y1 + y2) / 2
        px, py = frame.to_point(cx, cy)                      # logical, global
        elements.append({
            "id": i,
            "label": (e.get("content") or "").strip(),
            "type": e.get("type"),
            "interactive": bool(e.get("interactivity")),
            "source": e.get("source"),
            "bbox": [round(v, 1) for v in (x1, y1, x2, y2)],
            "center": [round(cx, 1), round(cy, 1)],
            "click_point": [round(px, 1), round(py, 1)],
            "bbox_norm": [round(v, 6) for v in (nx1, ny1, nx2, ny2)],
            "area_px": round((x2 - x1) * (y2 - y1), 1),
        })

    return {
        "frame": frame.as_dict(),
        "device": device,
        "captions_enabled": captions,
        "timings_sec": {"import_omniparser": round(t_import, 2),
                        "model_load": round(t_load, 2), "ocr": round(t_ocr, 2),
                        "detect_and_caption": round(t_parse, 2),
                        "interpreter_startup": round(_T_CMD - _T_PROCESS, 2)},
        "counts": {"total": len(elements),
                   "text": sum(1 for e in elements if e["type"] == "text"),
                   "icon": sum(1 for e in elements if e["type"] == "icon"),
                   "interactive": sum(1 for e in elements if e["interactive"])},
        "annotated_image": str(annotate) if annotate else None,
        "elements": elements,
    }


# --------------------------------------------------------------------------
# fuzzy label matching
# --------------------------------------------------------------------------

def normalise(s: str) -> str:
    s = unicodedata.normalize("NFKD", (s or "").casefold())
    s = "".join(c for c in s if not unicodedata.combining(c))
    s = re.sub(r"[^\w\s]", " ", s)
    return re.sub(r"\s+", " ", s).strip()


def _whole_token(needle: str, hay: str) -> bool:
    return re.search(rf"(?:^|\s){re.escape(needle)}(?:$|\s)", hay) is not None


def score_label(query: str, label: str) -> float:
    """How well does `query` name the element labelled `label`? 0..1.

    Containment is deliberately restricted to whole tokens of at least three
    characters. A raw-substring rule looks harmless until a two-letter label
    such as an "EN" language switch scores 0.8 against "Bananenbrot backen"
    (which contains "en"), at which point the tool confidently clicks the wrong
    control. Short labels stay reachable — but only by naming them exactly.
    """
    q, l = normalise(query), normalise(label)
    if not q or not l:
        return 0.0
    if q == l:
        return 1.0

    best = difflib.SequenceMatcher(None, q, l).ratio()

    # "Login" naming the element "Login button", and the reverse. The bonus
    # scales with how much of the other side is covered, so a short needle in a
    # long haystack stays modest.
    if len(q) >= 3 and _whole_token(q, l):
        best = max(best, 0.70 + 0.25 * (len(q) / len(l)))
    if len(l) >= 3 and _whole_token(l, q):
        best = max(best, 0.70 + 0.25 * (len(l) / len(q)))

    q_tok, l_tok = set(q.split()), set(l.split())
    shared = q_tok & l_tok
    if shared:
        # Weighted by coverage of *both* sides: one shared word inside a
        # twelve-word paragraph must not outrank a real two-word button.
        best = max(best, 0.55 * (len(shared) / len(q_tok))
                   + 0.35 * (len(shared) / len(l_tok)))

    return round(min(best, 0.999), 4)


def rank(query: str, elements: list, interactive_only: bool = False,
         prefer_interactive: bool = True, top: int = 5) -> list:
    cands = []
    for e in elements:
        if not e["label"]:
            continue
        if interactive_only and not e["interactive"]:
            continue
        s = score_label(query, e["label"])
        if s <= 0:
            continue
        adj = min(s + 0.03, 0.999) if (prefer_interactive and e["interactive"]) else s
        cands.append({**e, "score": s, "ranked_score": round(adj, 4)})
    # Ties break toward the smaller box: the tighter target is the real control.
    cands.sort(key=lambda c: (-c["ranked_score"], c["area_px"]))
    return cands[:top]


# --------------------------------------------------------------------------
# clicking
# --------------------------------------------------------------------------

def frontmost_app() -> str | None:
    try:
        r = subprocess.run(["/usr/bin/lsappinfo", "front"], capture_output=True,
                           text=True, timeout=10)
        asn = (r.stdout or "").strip()
        if not asn:
            return None
        r2 = subprocess.run(["/usr/bin/lsappinfo", "info", "-only", "name", asn],
                            capture_output=True, text=True, timeout=10)
        m = re.search(r'"LSDisplayName"="([^"]*)"', r2.stdout or "")
        return m.group(1) if m else (r2.stdout or "").strip() or None
    except Exception:
        return None


def preflight(require_click: bool) -> dict:
    """Everything that must hold before a click may be issued."""
    st = session_state()
    cli = cliclick_path()
    trusted, pos = (False, None)
    if cli:
        trusted, pos = cliclick_trusted(cli)
    blockers = []
    if st.get("screen_locked"):
        blockers.append("session is locked (CGSSessionScreenIsLocked=true) — "
                        "screen content is withheld and synthetic events are ignored")
    if require_click:
        if not cli:
            blockers.append("cliclick not installed (brew install cliclick)")
        elif not trusted:
            blockers.append("cliclick lacks Accessibility — grant it in System Settings > "
                            "Privacy & Security > Accessibility for the app that runs it "
                            f"(this process: {sys.executable})")
        if st.get("secure_input_pid"):
            blockers.append(f"secure input is active (pid {st['secure_input_pid']}) — "
                            "synthetic events are suppressed")
    return {"session": st, "cliclick": cli, "cliclick_trusted": trusted,
            "cursor": pos, "blockers": blockers}


def do_click(cli: str, x: float, y: float, button: str = "left",
             double: bool = False, restore: bool = False) -> dict:
    before = subprocess.run([cli, "p:"], capture_output=True, text=True, timeout=15).stdout.strip()
    verb = {"left": "dc" if double else "c", "right": "rc", "middle": "c"}[button]
    xi, yi = int(round(x)), int(round(y))
    r = subprocess.run([cli, "-w", "20", f"{verb}:{xi},{yi}"],
                       capture_output=True, text=True, timeout=30)
    after = subprocess.run([cli, "p:"], capture_output=True, text=True, timeout=15).stdout.strip()
    moved = after != before
    landed = None
    try:
        ax, ay = (int(v) for v in after.split(","))
        landed = (abs(ax - xi) <= 1 and abs(ay - yi) <= 1)
    except Exception:
        pass
    if restore and before:
        subprocess.run([cli, f"m:{before}"], capture_output=True, timeout=15)
    return {"command": f"{verb}:{xi},{yi}", "exit": r.returncode,
            "stderr": (r.stderr or "").strip() or None,
            "cursor_before": before, "cursor_after": after,
            "cursor_moved": moved, "cursor_landed_on_target": landed}


def verify_click(frame_before: Frame, display: Display, point: tuple,
                 radius: int = 60) -> dict:
    """Re-capture and measure how much changed around the click point."""
    from PIL import Image, ImageChops
    try:
        after = capture(display=display, allow_blank=True)
    except CaptureError as exc:
        return {"ok": False, "error": str(exc)}
    a = Image.open(frame_before.path).convert("RGB")
    b = Image.open(after.path).convert("RGB")
    if a.size != b.size:
        return {"ok": True, "changed_ratio_full": 1.0, "note": "capture size changed"}
    diff = ImageChops.difference(a, b).convert("L")
    full = sum(1 for p in diff.getdata() if p > 12) / (a.size[0] * a.size[1])
    sx, sy = frame_before.scale
    cx = int((point[0] - frame_before.origin_pt[0]) * sx)
    cy = int((point[1] - frame_before.origin_pt[1]) * sy)
    r = int(radius * sx)
    box = (max(0, cx - r), max(0, cy - r), min(a.size[0], cx + r), min(a.size[1], cy + r))
    crop = diff.crop(box)
    n = max(crop.size[0] * crop.size[1], 1)
    local = sum(1 for p in crop.getdata() if p > 12) / n
    return {"ok": True, "after_image": str(after.path),
            "changed_ratio_full": round(full, 5),
            "changed_ratio_around_click": round(local, 5),
            "interpretation": ("UI reacted" if local > 0.02 or full > 0.01
                               else "no visible change — click may have missed")}


# --------------------------------------------------------------------------
# commands
# --------------------------------------------------------------------------

def acquire_frame(args, allow_blank: bool = False) -> Frame:
    """Either load an existing image or capture the screen."""
    from PIL import Image
    if args.from_image:
        p = Path(args.from_image).expanduser()
        if not p.is_file():
            raise SystemExit(f"omniclick: no such image: {p}")
        with Image.open(p) as im:
            size_px = im.size
        scale = args.image_scale
        origin = tuple(float(v) for v in args.image_origin.split(",")) if args.image_origin else (0.0, 0.0)
        return Frame(path=p, size_px=size_px, origin_pt=origin,
                     size_pt=(size_px[0] / scale, size_px[1] / scale), display=None,
                     notes=[f"from-image, scale assumed {scale}"])
    displays = list_displays()
    disp = pick_display(displays, args.display)
    region = None
    if args.region:
        region = tuple(float(v) for v in args.region.split(","))
        if len(region) != 4:
            raise SystemExit("omniclick: --region needs x,y,w,h in logical points")
    return capture(display=disp, region=region, window_id=args.window,
                   allow_blank=allow_blank)


def cmd_displays(args) -> int:
    displays = list_displays()
    out = {"displays": [d.as_dict() for d in displays], "session": session_state()}
    if args.measure:
        measured = []
        for d in displays:
            try:
                f = capture(display=d, allow_blank=True)
                sx, sy = f.scale
                measured.append({"index": d.index, "capture_px": list(f.size_px),
                                 "measured_scale": [round(sx, 4), round(sy, 4)],
                                 "reported_scale": round(d.reported_scale, 4),
                                 "agrees": abs(sx - d.reported_scale) < 0.01,
                                 "notes": f.notes})
            except CaptureError as exc:
                measured.append({"index": d.index, "error": str(exc)})
        out["measured"] = measured
    print(json.dumps(out, indent=2))
    return EXIT_OK


def cmd_doctor(args) -> int:
    pf = preflight(require_click=True)
    displays = list_displays()
    checks = []

    def add(name, ok, detail):
        checks.append({"check": name, "ok": bool(ok), "detail": detail})

    add("omniparser_repo", OMNIPARSER_HOME.is_dir(), str(OMNIPARSER_HOME))
    det = detector_weights()
    cap = caption_weights_dir() / "model.safetensors"
    add("detector_weights", det.is_file(), f"{det} ({det.stat().st_size if det.is_file() else 0} B)")
    add("caption_weights", cap.is_file(), f"{cap} ({cap.stat().st_size if cap.is_file() else 0} B)")
    add("screencapture_binary", Path(SCREENCAPTURE).is_file(), SCREENCAPTURE)
    add("cliclick_binary", bool(pf["cliclick"]), pf["cliclick"] or "not found")
    add("cliclick_accessibility", pf["cliclick_trusted"],
        "granted" if pf["cliclick_trusted"] else "DENIED — cliclick cannot post events")
    add("session_unlocked", not pf["session"].get("screen_locked"),
        f"screen_locked={pf['session'].get('screen_locked')} "
        f"secure_input_pid={pf['session'].get('secure_input_pid')}")
    add("displays_found", bool(displays), f"{len(displays)} active")

    capture_ok, capture_detail = False, ""
    try:
        f = capture(display=pick_display(displays, None))
        capture_ok = True
        sx, sy = f.scale
        capture_detail = (f"{f.size_px[0]}x{f.size_px[1]}px, measured scale {sx:.4f}, "
                          f"content ratio {f.content.get('content_ratio')}")
    except CaptureError as exc:
        capture_detail = str(exc)
    add("screen_capture_has_content", capture_ok, capture_detail)

    try:
        import torch
        add("torch_device", True,
            f"torch {torch.__version__}, mps={torch.backends.mps.is_available()}, "
            f"cuda={torch.cuda.is_available()}")
    except Exception as exc:
        add("torch_device", False, str(exc))

    result = {"ok": all(c["ok"] for c in checks), "checks": checks,
              "blockers": pf["blockers"], "cursor": pf["cursor"]}
    if args.json:
        print(json.dumps(result, indent=2))
    else:
        for c in checks:
            print(f"{'✅' if c['ok'] else '❌'} {c['check']:<28} {c['detail']}")
        if pf["blockers"]:
            print("\nBlockers for clicking:")
            for b in pf["blockers"]:
                print(f"  • {b}")
        print(f"\noverall: {'ready' if result['ok'] else 'NOT ready'}")
    return EXIT_OK if result["ok"] else EXIT_BLOCKED


def cmd_snapshot(args) -> int:
    t0 = time.time()
    frame = acquire_frame(args, allow_blank=args.allow_blank)
    t_cap = time.time() - t0
    result = parse_frame(frame, device=args.device, captions=not args.no_captions,
                         box_threshold=args.box_threshold, iou_threshold=args.iou_threshold,
                         batch_size=args.batch_size,
                         annotate=Path(args.annotate).expanduser() if args.annotate else None)
    result["timings_sec"]["capture"] = round(t_cap, 2)
    result["timings_sec"]["total"] = round(time.time() - t0, 2)
    if args.interactive_only:
        result["elements"] = [e for e in result["elements"] if e["interactive"]]
    payload = json.dumps(result, indent=2, ensure_ascii=False)
    if args.out:
        Path(args.out).expanduser().write_text(payload)
        print(f"wrote {args.out} ({result['counts']['total']} elements, "
              f"{result['timings_sec']['total']}s)")
    else:
        print(payload)
    return EXIT_OK


def cmd_click(args) -> int:
    t0 = time.time()

    if args.from_snapshot:
        snap = json.loads(Path(args.from_snapshot).expanduser().read_text())
        elements = snap["elements"]
        fd = snap["frame"]
        frame = Frame(path=Path(fd["image"]), size_px=tuple(fd["size_px"]),
                      origin_pt=tuple(fd["origin_pt"]), size_pt=tuple(fd["size_pt"]))
        timings = dict(snap.get("timings_sec", {}), reused_snapshot=True)
    else:
        frame = acquire_frame(args, allow_blank=False)
        t_cap = time.time() - t0
        snap = parse_frame(frame, device=args.device, captions=not args.no_captions,
                           box_threshold=args.box_threshold, iou_threshold=args.iou_threshold,
                           batch_size=args.batch_size,
                           annotate=Path(args.annotate).expanduser() if args.annotate else None)
        elements = snap["elements"]
        timings = dict(snap["timings_sec"], capture=round(t_cap, 2))

    cands = rank(args.label, elements, interactive_only=args.interactive_only, top=args.top)
    result = {"query": args.label, "frame": frame.as_dict(),
              "candidates": [{k: c[k] for k in
                              ("id", "label", "type", "interactive", "score", "ranked_score",
                               "bbox", "center", "click_point")} for c in cands],
              "timings_sec": timings}

    if not cands:
        result["status"] = "no_match"
        result["error"] = f"nothing resembling {args.label!r} among {len(elements)} elements"
        print(json.dumps(result, indent=2, ensure_ascii=False))
        return EXIT_NO_MATCH

    target = cands[min(args.index, len(cands) - 1)]
    result["target"] = {k: target[k] for k in
                        ("id", "label", "type", "interactive", "score", "ranked_score",
                         "bbox", "center", "click_point")}

    if target["score"] < args.min_score:
        result["status"] = "below_threshold"
        result["error"] = (f"best match {target['label']!r} scored {target['score']} "
                           f"< --min-score {args.min_score}")
        print(json.dumps(result, indent=2, ensure_ascii=False))
        return EXIT_NO_MATCH

    if frame.display is None and not args.from_snapshot and (args.window or args.from_image):
        result["status"] = "unmapped_frame"
        result["error"] = ("this frame has no known position on the desktop "
                           "(window/from-image capture), so a click cannot be aimed. "
                           "Use --region or a display capture, or pass --image-origin.")
        print(json.dumps(result, indent=2, ensure_ascii=False))
        return EXIT_BLOCKED

    x, y = target["click_point"]
    result["click_point"] = [x, y]

    if args.dry_run:
        result["status"] = "dry_run"
        result["timings_sec"]["total"] = round(time.time() - t0, 2)
        print(json.dumps(result, indent=2, ensure_ascii=False))
        return EXIT_OK

    pf = preflight(require_click=True)
    result["preflight"] = {"blockers": pf["blockers"], "cliclick": pf["cliclick"]}
    if pf["blockers"] and not args.force:
        result["status"] = "blocked"
        result["error"] = "; ".join(pf["blockers"])
        result["timings_sec"]["total"] = round(time.time() - t0, 2)
        print(json.dumps(result, indent=2, ensure_ascii=False))
        return EXIT_BLOCKED

    result["click"] = do_click(pf["cliclick"], x, y, button=args.button,
                               double=args.double, restore=args.restore_cursor)
    if args.verify and frame.display is not None:
        time.sleep(args.verify_delay)
        result["verification"] = verify_click(frame, frame.display, (x, y))

    ok = result["click"]["exit"] == 0 and result["click"].get("cursor_landed_on_target") is not False
    result["status"] = "clicked" if ok else "click_failed"
    result["timings_sec"]["total"] = round(time.time() - t0, 2)
    print(json.dumps(result, indent=2, ensure_ascii=False))
    return EXIT_OK if ok else EXIT_CLICK_FAILED


def cmd_match(args) -> int:
    """Offline: rank labels from a saved snapshot. No capture, no click."""
    snap = json.loads(Path(args.from_snapshot).expanduser().read_text())
    cands = rank(args.label, snap["elements"], interactive_only=args.interactive_only,
                 top=args.top)
    print(json.dumps({"query": args.label, "candidates": cands}, indent=2, ensure_ascii=False))
    return EXIT_OK if cands else EXIT_NO_MATCH


# --------------------------------------------------------------------------

def build_parser() -> argparse.ArgumentParser:
    ap = argparse.ArgumentParser(prog="omniclick", description=__doc__,
                                 formatter_class=argparse.RawDescriptionHelpFormatter)
    sub = ap.add_subparsers(dest="cmd", required=True)

    def add_capture_opts(p):
        p.add_argument("--display", type=int, help="1-based display index (default: main)")
        p.add_argument("--region", help="x,y,w,h in logical points instead of a full display")
        p.add_argument("--window", type=int, help="capture a window id (see `screencapture -l`)")
        p.add_argument("--from-image", help="parse an existing image instead of capturing")
        p.add_argument("--image-scale", type=float, default=1.0,
                       help="pixels per point for --from-image (default 1.0)")
        p.add_argument("--image-origin", help="x,y logical origin for --from-image")

    def add_model_opts(p):
        p.add_argument("--device", default=os.environ.get("OMNICLICK_DEVICE", "mps"),
                       choices=["mps", "cpu", "cuda"])
        p.add_argument("--no-captions", action="store_true",
                       help="skip Florence-2 (≈8x faster; OCR labels are kept)")
        p.add_argument("--box-threshold", type=float, default=0.05)
        p.add_argument("--iou-threshold", type=float, default=0.7)
        p.add_argument("--batch-size", type=int, default=64)
        p.add_argument("--annotate", help="write the numbered-box overlay here")
        p.add_argument("--interactive-only", action="store_true")

    d = sub.add_parser("displays", help="display geometry and measured retina scale")
    d.add_argument("--measure", action="store_true", help="also capture to measure the real scale")
    d.set_defaults(func=cmd_displays)

    doc = sub.add_parser("doctor", help="check whether capture+click can work here")
    doc.add_argument("--json", action="store_true")
    doc.set_defaults(func=cmd_doctor)

    s = sub.add_parser("snapshot", help="detect UI elements, print JSON")
    add_capture_opts(s)
    add_model_opts(s)
    s.add_argument("--out", help="write JSON here instead of stdout")
    s.add_argument("--allow-blank", action="store_true",
                   help="do not refuse an empty capture (diagnostics only)")
    s.set_defaults(func=cmd_snapshot)

    c = sub.add_parser("click", help="find an element by label and click it")
    c.add_argument("label")
    add_capture_opts(c)
    add_model_opts(c)
    c.add_argument("--from-snapshot", help="reuse a snapshot JSON instead of re-capturing")
    c.add_argument("--min-score", type=float, default=0.55)
    c.add_argument("--index", type=int, default=0, help="pick the Nth candidate (default best)")
    c.add_argument("--top", type=int, default=5)
    c.add_argument("--button", default="left", choices=["left", "right", "middle"])
    c.add_argument("--double", action="store_true")
    c.add_argument("--dry-run", action="store_true", help="resolve and report, never click")
    c.add_argument("--force", action="store_true", help="click despite preflight blockers")
    c.add_argument("--restore-cursor", action="store_true")
    c.add_argument("--verify", action="store_true", help="re-capture and measure what changed")
    c.add_argument("--verify-delay", type=float, default=0.8)
    c.set_defaults(func=cmd_click)

    m = sub.add_parser("match", help="rank labels in a saved snapshot (offline)")
    m.add_argument("label")
    m.add_argument("--from-snapshot", required=True)
    m.add_argument("--top", type=int, default=5)
    m.add_argument("--interactive-only", action="store_true")
    m.set_defaults(func=cmd_match)
    return ap


def main() -> int:
    global _T_CMD
    args = build_parser().parse_args()
    # Every command needs PIL at least (capture, blank detection); the parsing
    # commands additionally need torch. `doctor` is included because it reports
    # on torch, and reporting "no torch" from an interpreter that was never
    # going to do the parsing is a false alarm.
    needs_torch = args.cmd in ("snapshot", "doctor") or (
        args.cmd == "click" and not getattr(args, "from_snapshot", None))
    ensure_omniparser_interpreter(need_torch=needs_torch)
    _T_CMD = time.time()
    try:
        return args.func(args)
    except CaptureError as exc:
        print(json.dumps({"status": "blocked", "error": str(exc)}, indent=2))
        return EXIT_BLOCKED


if __name__ == "__main__":
    raise SystemExit(main())
