"""
login.py — open a visible (headed) Chromium on Flyff and keep it open so you can
log in by hand. The session is saved to ./chrome-profile (persistent profile),
so future runs (m0_probe.py, the bot) start already logged in.

Run:  python login.py
Then: log in, get fully in-game, and just close the browser window when done.
"""
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"


def main():
    with sync_playwright() as p:
        ctx = p.chromium.launch_persistent_context(
            user_data_dir=str(USER_DATA),
            headless=False,            # visible window you can use normally
            no_viewport=True,          # behave like a normal resizable window
            args=["--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"(navigation note: {e}) — you can browse to the game manually.")

        print("=" * 60)
        print("Flyff is open in the visible window.")
        print("1) Log in and get FULLY in-game.")
        print("2) Then just CLOSE the browser window.")
        print("Your login is saved to ./chrome-profile and reused next time.")
        print("=" * 60)

        # Stay alive until the user closes the browser/tab.
        try:
            page.wait_for_event("close", timeout=0)
        except Exception:
            pass
    print("Done — login session saved to ./chrome-profile")


if __name__ == "__main__":
    main()
