import os, sys, json, time

os.environ["BU_NAME"] = "work"
sys.path.insert(0, "/opt/data/browser-harness")
from helpers import goto, wait_for_load, sleep, screenshot, cdp, click, scroll, list_tabs, page_info, new_tab, switch_tab
from admin import daemon_alive, start_remote_daemon

NAME = "work"
assert daemon_alive(NAME), "Browser not alive!"

# Wait for the auth page to fully render
sleep(3)

# Get current page content
result = cdp("Runtime.evaluate", expression="document.body.innerText.substring(0, 3000)", returnByValue=True)
print(f"Auth page text: {result}")

# Check for input fields
result2 = cdp("Runtime.evaluate", expression="""
    let inputs = document.querySelectorAll('input');
    let info = [];
    for (let inp of inputs) {
        info.push({
            type: inp.type,
            name: inp.name,
            id: inp.id,
            placeholder: inp.placeholder,
            visible: inp.offsetParent !== null
        });
    }
    JSON.stringify(info);
""", returnByValue=True)
print(f"Input fields: {result2}")

screenshot("/tmp/tm_05_auth_page.png")