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, restart_daemon

NAME = "work"

# Restart the daemon since session expired
print("Restarting cloud browser with Personal Profile...")
restart_daemon(NAME)
time.sleep(2)
browser = start_remote_daemon(NAME, profileName="Personal Profile")
print(f"Browser started: {browser}")
time.sleep(3)

# Navigate to Ticketmaster SG
print("Navigating to ticketmaster.sg...")
new_tab("https://www.ticketmaster.sg")
wait_for_load()
sleep(5)
screenshot("/tmp/tm_01_homepage.png")
page = page_info()
print(f"Page info: {page}")

# Get the current URL and title
result = cdp("Runtime.evaluate", expression="document.title", returnByValue=True)
print(f"Title: {result}")
result = cdp("Runtime.evaluate", expression="window.location.href", returnByValue=True)
print(f"URL: {result}")