"""runner dev <app> — open your app, hosted.

Your app is rows in hosted Runner (pen.acsetra.com), so "dev" just opens the
canonical hosted surface for a scope in your browser. The engine, DB, workers,
auth, and metering all stay server-side; sign in to your workspace once in the
browser and you're watching the live app as you author.

Agents don't use this. To reach the runtime endpoints (/bundle, /read, /run,
/feed) programmatically, call api.surface_get() — it trades the signed-in
bearer for a per-app session cookie and attaches it directly. No localhost
proxy: an agent is an HTTP client, not a browser.

  runner dev <app>               open http://…/<app-path>   (acme.crm -> /acme/crm)
  runner dev <app> --no-browser  just print the URL

The hosted shell resolves the scope from the path (?scope= still works as a
dev/debug fallback). See design.md "Navigation".
"""
import sys
import webbrowser

from . import config


def dev(app, open_browser=True):
    base = config.api_base()
    scope = app or config.default_scope()
    if not scope:
        print("runner dev: which app? pass a scope, e.g. `runner dev acme.crm`", file=sys.stderr)
        return 2
    # Pretty path: the scope's structural public URL (acme.crm -> /acme/crm). The
    # hosted shell resolves it back to the scope server-side; ?scope= still works.
    url = f"{base}/{scope.replace('.', '/')}"
    print(url)
    if open_browser:
        try:
            webbrowser.open(url)
        except Exception:
            pass
    return 0
