import json, os, sys, urllib.parse, urllib.request, urllib.error

SCOPES = (
    "https://www.googleapis.com/auth/gmail.readonly "
    "https://www.googleapis.com/auth/gmail.compose "
    "https://www.googleapis.com/auth/gmail.send"
)


def _cfg():
    return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))


def main():
    cfg = _cfg()
    client_id = str(cfg.get("client_id") or "")
    if not client_id:
        raise Exception("client_id is not set")
    url_params = {
        "client_id": client_id,
        "redirect_uri": str(cfg.get("redirect_uri") or "http://localhost"),
        "response_type": "code",
        "scope": SCOPES,
        "access_type": "offline",
        "prompt": "consent",
    }
    if cfg.get("email"):
        url_params["login_hint"] = cfg["email"]
    link = "https://accounts.google.com/o/oauth2/v2/auth?" + urllib.parse.urlencode(url_params)
    print(json.dumps({
        "auth_link": link,
        "next": "Open the link, approve access, copy the 'code' from the redirect, then run gmail-single-user-exchange-code.",
    }))


try:
    main()
except urllib.error.HTTPError as e:
    print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
    sys.exit(1)
except Exception as e:
    print(json.dumps({"error": str(e)}))
    sys.exit(1)
