#!/usr/bin/env python3
"""Open the Curogram invite email in OWA and extract any curogram.com links.
Non-secret: prints only URLs + visible button text, no PHI."""
import sys, json, time
sys.path.insert(0, "/home/claude/repos/openclaw/.claude/skills/advancedmd/scripts")
import amd_browser as A

t = A.find_target(url_sub="outlook.office.com")
pg = A.attach(t); pg.enable()

# Click the message row whose aria-label/text contains "Curogram".
clicked = pg.eval(
  "(()=>{const rows=[...document.querySelectorAll('[role=option]')];"
  "const m=rows.find(r=>/curogram/i.test(r.getAttribute('aria-label')||r.innerText||''));"
  "if(!m) return 'NOROW'; m.click(); return 'CLICKED';})()"
)
print("ROW:", clicked)
time.sleep(3)

# The reading pane renders the email. Pull all hrefs (incl. inside iframes if same-origin won't work;
# OWA usually inlines). Also grab anchors with curogram text.
links = pg.eval(
  "JSON.stringify([...document.querySelectorAll('a[href]')]"
  ".map(a=>({href:a.href,txt:(a.innerText||a.textContent||'').trim().slice(0,60)}))"
  ".filter(a=>/curogram/i.test(a.href)||/curogram|join|activat|accept|get started|sign up|set up/i.test(a.txt)))"
)
print("LINKS:", links)

# Subject + a slice of body text for confirmation (no PHI in an invite).
subj = pg.eval(
  "(()=>{const h=document.querySelector('[role=main] [role=heading],[aria-label*=Subject]');"
  "return h?h.innerText.slice(0,120):'';})()"
)
print("SUBJ:", subj)
body = pg.eval("(()=>{const m=document.querySelector('[role=main]');return m?m.innerText.slice(0,700):'';})()")
print("BODY:\n", body)
pg.close()
