import sys, time
from camoufox.sync_api import Camoufox
with Camoufox(headless=True, humanize=True) as b:
    page = b.new_page()
    page.goto("https://www.msc.com/en/track-a-shipment", wait_until="domcontentloaded")
    page.wait_for_timeout(3000)
    # dismiss cookies
    try:
        page.get_by_role("button", name="Accept All").click(timeout=2000)
        print("cookies dismissed")
    except Exception as e:
        print(f"no cookie btn: {e}")
    page.wait_for_timeout(1000)
    # screenshot
    page.screenshot(path="/tmp/before-fill.png", full_page=True)
    print("screenshot: before-fill.png")
    # look for input
    inputs = page.evaluate("""() => Array.from(document.querySelectorAll('input')).map(i => ({type: i.type, placeholder: i.placeholder, name: i.name, id: i.id}))""")
    print("INPUTS:", inputs)
    # fill
    try:
        page.fill('input[placeholder*="Container" i]', "MEDU9730548")
        print("fill OK")
    except Exception as e:
        print(f"fill failed: {e}")
        # try text inputs
        page.locator("input[type='text']").first.fill("MEDU9730548")
        print("fallback fill OK")
    page.wait_for_timeout(500)
    page.screenshot(path="/tmp/after-fill.png", full_page=True)
    # try click
    try:
        page.get_by_role("button", name="Track").click(timeout=3000)
        print("clicked Track")
    except Exception as e:
        print(f"Track btn: {e}")
        # try submit
        try:
            page.locator("button[type='submit']").first.click()
            print("clicked submit")
        except Exception as e2:
            print(f"submit also failed: {e2}")
    page.wait_for_timeout(5000)
    page.screenshot(path="/tmp/after-click.png", full_page=True)
    print("done")
