    raise GmailOauthError(
        "No Google OAuth client configuration available. Import a client_secret.json in "
        "Gmail Accounts → Import Gmail Configuration, or set GOOGLE_OAUTH_CLIENT_JSON_PATH."
    )


def parse_client_json(content: bytes) -> dict:
    """Validate an uploaded client_secret JSON. Returns normalized dict; raises on invalid."""
    try:
        data = json.loads(content.decode("utf-8"))
    except Exception as e:
        raise GmailOauthError(f"File is not valid JSON: {e}")
    inst = data.get("installed") or data.get("web")
    if not inst or not inst.get("client_id") or not inst.get("client_secret"):
        raise GmailOauthError("JSON must contain an 'installed' or 'web' object with client_id and client_secret")
    return {
        "project_id": inst.get("project", "") or inst.get("project_id", ""),
        "client_id": inst["client_id"],
        "client_secret": inst["client_secret"],
        "redirect_uris": inst.get("redirect_uris", []),
        "auth_uri": inst.get("auth_uri", ""),
        "token_uri": inst.get("token_uri", ""),
    }


# ---------------------------------------------------------------------------
