import sys, time
from pathlib import Path
sys.path.append(str(Path("05_Production_Pipeline").resolve()))
from invisible_playwright import InvisiblePlaywright
from muse_invpw_driver import sync_worker_profile, DEFAULT_PROFILE

def monitor_network():
    sync_worker_profile()
    with InvisiblePlaywright(profile_dir=DEFAULT_PROFILE, headless=True) as ctx:
        page = ctx.pages[0] if ctx.pages else ctx.new_page()
        
        page.on("request", lambda r: print(f"[REQ] {r.method} {r.url[:100]}"))
        page.on("requestfailed", lambda r: print(f"[REQ FAIL] {r.method} {r.url[:100]} - {r.failure}"))
        page.on("response", lambda r: print(f"[RESP {r.status}] {r.url[:100]}"))
        page.on("console", lambda m: print(f"[CONSOLE {m.type}] {m.text[:120]}"))
        page.on("pageerror", lambda e: print(f"[PAGE ERROR] {e}"))
        page.on("websocket", lambda ws: print(f"[WS OPEN] {ws.url}"))
        
        print("Navigating to https://muse.ai...")
        page.goto("https://muse.ai", wait_until="domcontentloaded", timeout=30000)
        time.sleep(5)
        
        print("\n--- Current active thread status ---")
        body = page.locator("body").inner_text()
        print("Body text snippet:\n", body[:400])

if __name__ == '__main__':
    monitor_network()
