path = "/home/mailshift.enest.in/public_html/backend/cloud.py"
with open(path, "r") as f:
    c = f.read()

old = '''def mail_auth_url(provider, state):
    """Authorize URL for connecting a mailbox for IMAP migration (not Drive/Contacts)."""
    from urllib.parse import urlencode
    if provider == "google":
        q = urlencode({"client_id": _cfg("GOOGLE_CLIENT_ID"), "redirect_uri": mail_redirect_uri("google"),
                       "response_type": "code", "scope": GOOGLE_MAIL_SCOPES, "access_type": "offline",
                       "prompt": "consent", "state": state})
        return f"https://accounts.google.com/o/oauth2/v2/auth?{q}"
    tenant = _cfg("MS_TENANT", "common") or "common"
    q = urlencode({"client_id": _cfg("MS_CLIENT_ID"), "redirect_uri": mail_redirect_uri("microsoft"),
                   "response_type": "code", "scope": MS_MAIL_SCOPES, "state": state, "response_mode": "query"})
    return f"https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize?{q}"'''

new = '''def mail_auth_url(provider, state):
    """Authorize URL for connecting a mailbox for IMAP migration (not Drive/Contacts)."""
    from urllib.parse import urlencode
    if provider == "google":
        q = urlencode({"client_id": _cfg("GOOGLE_CLIENT_ID"), "redirect_uri": mail_redirect_uri("google"),
                       "response_type": "code", "scope": GOOGLE_MAIL_SCOPES, "access_type": "offline",
                       "prompt": "select_account consent", "state": state})
        return f"https://accounts.google.com/o/oauth2/v2/auth?{q}"
    tenant = _cfg("MS_TENANT", "common") or "common"
    q = urlencode({"client_id": _cfg("MS_CLIENT_ID"), "redirect_uri": mail_redirect_uri("microsoft"),
                   "response_type": "code", "scope": MS_MAIL_SCOPES, "state": state, "response_mode": "query",
                   "prompt": "select_account"})
    return f"https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize?{q}"'''

if old in c:
    c = c.replace(old, new)
    with open(path, "w") as f:
        f.write(c)
    print("PATCHED - account picker will now show on every connect.")
else:
    print("MARKER NOT FOUND")
