"""
host.py — Keep ONE headed Chromium open on Flyff with a CDP debug port, so that
probe / calibration / bot scripts can attach to the SAME live window via
connect_over_cdp (no per-script relaunch, no profile-lock dance, no reposition).

Run this in the background and leave it open for the whole dev session:
    python host.py
Other scripts then do: chromium.connect_over_cdp("http://localhost:9222")
"""
from pathlib import Path
from playwright.sync_api import sync_playwright

USER_DATA = Path(__file__).parent / "chrome-profile"
FLYFF_URL = "https://universe.flyff.com/play"
CDP_PORT = 9222


def main():
    with sync_playwright() as p:
        ctx = p.chromium.launch_persistent_context(
            user_data_dir=str(USER_DATA),
            headless=False,
            no_viewport=True,
            args=[f"--remote-debugging-port={CDP_PORT}", "--start-maximized"],
        )
        page = ctx.pages[0] if ctx.pages else ctx.new_page()
        try:
            page.goto(FLYFF_URL, wait_until="domcontentloaded")
        except Exception as e:
            print(f"(nav note: {e}) — navigate manually if needed.")

        print("=" * 60)
        print(f"HOST READY — CDP on http://localhost:{CDP_PORT}. Leave this open.")
        print("Get in-game; other scripts attach via connect_over_cdp.")
        print("Close the browser window to stop.")
        print("=" * 60)
        try:
            page.wait_for_event("close", timeout=0)
        except Exception:
            pass
    print("Host stopped.")


if __name__ == "__main__":
    main()
