import sys, time, json
sys.path.insert(0, "/tmp/amdwork")
import cdp

# Create a FRESH tab to probe identity routes; do NOT disturb the parked confirm-email tab
before = set((p.get("id") or p.get("url")) for p in cdp.list_pages())
cdp.new_target("https://static-100.advancedmd.com/apps/identity/#/")
time.sleep(3)
target = None
for p in cdp.list_pages():
    if "apps/identity" in (p.get("url") or "") and (p.get("id") or p.get("url")) not in before:
        target = p; break
if not target:
    # fallback: any identity tab that is NOT the confirm-email one
    for p in cdp.list_pages():
        u = p.get("url") or ""
        if "apps/identity" in u and "confirm-email" not in u:
            target = p; break
pg = cdp.attach(target)
pg.enable()
time.sleep(1)
print("CUR_URL:", pg.eval("location.href"))

routes = ["#/", "#/security", "#/account", "#/profile", "#/mfa", "#/two-factor",
          "#/authenticator", "#/manage-account", "#/settings", "#/multifactor"]
base = "https://static-100.advancedmd.com/apps/identity/"
for r in routes:
    pg.navigate(base + r)
    time.sleep(3)
    url = pg.eval("location.href")
    body = (pg.eval("document.body.innerText") or "")[:300].replace("\n"," | ")
    has_auth = pg.eval("/authenticator|totp|google authenticator|qr code|scan|two-factor|2-step|multi-factor/i.test(document.body.innerText)")
    print("ROUTE", r, "->", url[-40:], "| AUTH_KEYWORDS:", has_auth, "| BODY:", body[:160])
print("IDENTITY_PROBE_DONE")
pg.close()
