# -*- coding: utf-8 -*-
"""Hub native-confirm gate (take-two F-16, the hub sibling of drill F-11) — native confirm()/
prompt() dialogs are OS-modals that freeze browser automation (CDP) and are banned from the hub
UI; consequential buttons use the inline felhomConfirm helper (templates/inline_confirm.html).
The danger-zone typed-confirm flow does not use native dialogs either.

Run from the repo root:  python scripts/hub_confirm_gate.py
Exit 1 if any native confirm(/prompt( call remains in hub templates.
"""
import io, os, re, sys

ROOT = os.path.join("hub", "internal", "web", "templates")

# A bare confirm(/prompt( CALL with an argument: not preceded by an identifier character, so felhomConfirm(
# never matches. window.confirm( still matches ('.' is not an identifier char).
NATIVE = re.compile(r"(?<![A-Za-z0-9_$])(?:confirm|prompt)\(\s*[^)\s]")


def main():
    total = 0
    for fn in sorted(os.listdir(ROOT)):
        if not fn.endswith(".html"):
            continue
        path = os.path.join(ROOT, fn)
        for lineno, line in enumerate(io.open(path, encoding="utf-8"), 1):
            if NATIVE.search(line):
                total += 1
                print("%s:%d  %s" % (fn, lineno, line.strip()[:120].encode('ascii', 'backslashreplace').decode()))
    if total:
        print("HUB CONFIRM GATE FAILED: %d native confirm()/prompt() call(s) remain" % total)
        sys.exit(1)
    print("hub confirm gate OK — no native confirm()/prompt() in hub templates")


if __name__ == "__main__":
    main()
