// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// Code generated by Microsoft (R) AutoRest Code Generator. DO NOT EDIT.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

package armpostgresqlflexibleservers

import (
	"context"
	"errors"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
	"net/http"
	"net/url"
	"strings"
)

// TuningConfigurationClient contains the methods for the TuningConfiguration group.
// Don't use this type directly, use NewTuningConfigurationClient() instead.
type TuningConfigurationClient struct {
	internal       *arm.Client
	subscriptionID string
}

// NewTuningConfigurationClient creates a new instance of TuningConfigurationClient with the specified values.
//   - subscriptionID - The ID of the target subscription. The value must be an UUID.
//   - credential - used to authorize requests. Usually a credential from azidentity.
//   - options - pass nil to accept the default values.
func NewTuningConfigurationClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*TuningConfigurationClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &TuningConfigurationClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

// BeginDisable - Disables the config tuning.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-01-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - serverName - The name of the server.
//   - tuningOption - The name of the tuning option.
//   - options - TuningConfigurationClientBeginDisableOptions contains the optional parameters for the TuningConfigurationClient.BeginDisable
//     method.
func (client *TuningConfigurationClient) BeginDisable(ctx context.Context, resourceGroupName string, serverName string, tuningOption TuningOptionEnum, options *TuningConfigurationClientBeginDisableOptions) (*runtime.Poller[TuningConfigurationClientDisableResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.disable(ctx, resourceGroupName, serverName, tuningOption, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[TuningConfigurationClientDisableResponse]{
			FinalStateVia: runtime.FinalStateViaLocation,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[TuningConfigurationClientDisableResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Disable - Disables the config tuning.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-01-01-preview
func (client *TuningConfigurationClient) disable(ctx context.Context, resourceGroupName string, serverName string, tuningOption TuningOptionEnum, options *TuningConfigurationClientBeginDisableOptions) (*http.Response, error) {
	var err error
	const operationName = "TuningConfigurationClient.BeginDisable"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.disableCreateRequest(ctx, resourceGroupName, serverName, tuningOption, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// disableCreateRequest creates the Disable request.
func (client *TuningConfigurationClient) disableCreateRequest(ctx context.Context, resourceGroupName string, serverName string, tuningOption TuningOptionEnum, _ *TuningConfigurationClientBeginDisableOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforPostgreSQL/flexibleServers/{serverName}/tuningOptions/{tuningOption}/disable"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if serverName == "" {
		return nil, errors.New("parameter serverName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{serverName}", url.PathEscape(serverName))
	if tuningOption == "" {
		return nil, errors.New("parameter tuningOption cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{tuningOption}", url.PathEscape(string(tuningOption)))
	req, err := runtime.NewRequest(ctx, http.MethodPost, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2025-01-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// BeginEnable - Enables the config tuning.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-01-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - serverName - The name of the server.
//   - tuningOption - The name of the tuning option.
//   - options - TuningConfigurationClientBeginEnableOptions contains the optional parameters for the TuningConfigurationClient.BeginEnable
//     method.
func (client *TuningConfigurationClient) BeginEnable(ctx context.Context, resourceGroupName string, serverName string, tuningOption TuningOptionEnum, options *TuningConfigurationClientBeginEnableOptions) (*runtime.Poller[TuningConfigurationClientEnableResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.enable(ctx, resourceGroupName, serverName, tuningOption, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[TuningConfigurationClientEnableResponse]{
			FinalStateVia: runtime.FinalStateViaLocation,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[TuningConfigurationClientEnableResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Enable - Enables the config tuning.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-01-01-preview
func (client *TuningConfigurationClient) enable(ctx context.Context, resourceGroupName string, serverName string, tuningOption TuningOptionEnum, options *TuningConfigurationClientBeginEnableOptions) (*http.Response, error) {
	var err error
	const operationName = "TuningConfigurationClient.BeginEnable"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.enableCreateRequest(ctx, resourceGroupName, serverName, tuningOption, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// enableCreateRequest creates the Enable request.
func (client *TuningConfigurationClient) enableCreateRequest(ctx context.Context, resourceGroupName string, serverName string, tuningOption TuningOptionEnum, _ *TuningConfigurationClientBeginEnableOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforPostgreSQL/flexibleServers/{serverName}/tuningOptions/{tuningOption}/enable"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if serverName == "" {
		return nil, errors.New("parameter serverName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{serverName}", url.PathEscape(serverName))
	if tuningOption == "" {
		return nil, errors.New("parameter tuningOption cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{tuningOption}", url.PathEscape(string(tuningOption)))
	req, err := runtime.NewRequest(ctx, http.MethodPost, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2025-01-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// NewListSessionDetailsPager - Gets the session details of a config tuning session.
//
// Generated from API version 2025-01-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - serverName - The name of the server.
//   - tuningOption - The name of the tuning option.
//   - sessionID - Guid of the objectId for the session.
//   - options - TuningConfigurationClientListSessionDetailsOptions contains the optional parameters for the TuningConfigurationClient.NewListSessionDetailsPager
//     method.
func (client *TuningConfigurationClient) NewListSessionDetailsPager(resourceGroupName string, serverName string, tuningOption TuningOptionEnum, sessionID string, options *TuningConfigurationClientListSessionDetailsOptions) *runtime.Pager[TuningConfigurationClientListSessionDetailsResponse] {
	return runtime.NewPager(runtime.PagingHandler[TuningConfigurationClientListSessionDetailsResponse]{
		More: func(page TuningConfigurationClientListSessionDetailsResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *TuningConfigurationClientListSessionDetailsResponse) (TuningConfigurationClientListSessionDetailsResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "TuningConfigurationClient.NewListSessionDetailsPager")
			nextLink := ""
			if page != nil {
				nextLink = *page.NextLink
			}
			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
				return client.listSessionDetailsCreateRequest(ctx, resourceGroupName, serverName, tuningOption, sessionID, options)
			}, nil)
			if err != nil {
				return TuningConfigurationClientListSessionDetailsResponse{}, err
			}
			return client.listSessionDetailsHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listSessionDetailsCreateRequest creates the ListSessionDetails request.
func (client *TuningConfigurationClient) listSessionDetailsCreateRequest(ctx context.Context, resourceGroupName string, serverName string, tuningOption TuningOptionEnum, sessionID string, _ *TuningConfigurationClientListSessionDetailsOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforPostgreSQL/flexibleServers/{serverName}/tuningOptions/{tuningOption}/sessionDetails"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if serverName == "" {
		return nil, errors.New("parameter serverName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{serverName}", url.PathEscape(serverName))
	if tuningOption == "" {
		return nil, errors.New("parameter tuningOption cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{tuningOption}", url.PathEscape(string(tuningOption)))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2025-01-01-preview")
	reqQP.Set("sessionId", sessionID)
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listSessionDetailsHandleResponse handles the ListSessionDetails response.
func (client *TuningConfigurationClient) listSessionDetailsHandleResponse(resp *http.Response) (TuningConfigurationClientListSessionDetailsResponse, error) {
	result := TuningConfigurationClientListSessionDetailsResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.SessionDetailsListResult); err != nil {
		return TuningConfigurationClientListSessionDetailsResponse{}, err
	}
	return result, nil
}

// NewListSessionsPager - Gets up the config tuning session status.
//
// Generated from API version 2025-01-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - serverName - The name of the server.
//   - tuningOption - The name of the tuning option.
//   - options - TuningConfigurationClientListSessionsOptions contains the optional parameters for the TuningConfigurationClient.NewListSessionsPager
//     method.
func (client *TuningConfigurationClient) NewListSessionsPager(resourceGroupName string, serverName string, tuningOption TuningOptionEnum, options *TuningConfigurationClientListSessionsOptions) *runtime.Pager[TuningConfigurationClientListSessionsResponse] {
	return runtime.NewPager(runtime.PagingHandler[TuningConfigurationClientListSessionsResponse]{
		More: func(page TuningConfigurationClientListSessionsResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *TuningConfigurationClientListSessionsResponse) (TuningConfigurationClientListSessionsResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "TuningConfigurationClient.NewListSessionsPager")
			nextLink := ""
			if page != nil {
				nextLink = *page.NextLink
			}
			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
				return client.listSessionsCreateRequest(ctx, resourceGroupName, serverName, tuningOption, options)
			}, nil)
			if err != nil {
				return TuningConfigurationClientListSessionsResponse{}, err
			}
			return client.listSessionsHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listSessionsCreateRequest creates the ListSessions request.
func (client *TuningConfigurationClient) listSessionsCreateRequest(ctx context.Context, resourceGroupName string, serverName string, tuningOption TuningOptionEnum, _ *TuningConfigurationClientListSessionsOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforPostgreSQL/flexibleServers/{serverName}/tuningOptions/{tuningOption}/sessions"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if serverName == "" {
		return nil, errors.New("parameter serverName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{serverName}", url.PathEscape(serverName))
	if tuningOption == "" {
		return nil, errors.New("parameter tuningOption cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{tuningOption}", url.PathEscape(string(tuningOption)))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2025-01-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listSessionsHandleResponse handles the ListSessions response.
func (client *TuningConfigurationClient) listSessionsHandleResponse(resp *http.Response) (TuningConfigurationClientListSessionsResponse, error) {
	result := TuningConfigurationClientListSessionsResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.SessionsListResult); err != nil {
		return TuningConfigurationClientListSessionsResponse{}, err
	}
	return result, nil
}

// BeginStartSession - Starts up the config tuning session.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-01-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - serverName - The name of the server.
//   - tuningOption - The name of the tuning option.
//   - configTuningRequest - The parameters for tuning request.
//   - options - TuningConfigurationClientBeginStartSessionOptions contains the optional parameters for the TuningConfigurationClient.BeginStartSession
//     method.
func (client *TuningConfigurationClient) BeginStartSession(ctx context.Context, resourceGroupName string, serverName string, tuningOption TuningOptionEnum, configTuningRequest ConfigTuningRequestParameter, options *TuningConfigurationClientBeginStartSessionOptions) (*runtime.Poller[TuningConfigurationClientStartSessionResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.startSession(ctx, resourceGroupName, serverName, tuningOption, configTuningRequest, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[TuningConfigurationClientStartSessionResponse]{
			FinalStateVia: runtime.FinalStateViaLocation,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[TuningConfigurationClientStartSessionResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// StartSession - Starts up the config tuning session.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-01-01-preview
func (client *TuningConfigurationClient) startSession(ctx context.Context, resourceGroupName string, serverName string, tuningOption TuningOptionEnum, configTuningRequest ConfigTuningRequestParameter, options *TuningConfigurationClientBeginStartSessionOptions) (*http.Response, error) {
	var err error
	const operationName = "TuningConfigurationClient.BeginStartSession"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.startSessionCreateRequest(ctx, resourceGroupName, serverName, tuningOption, configTuningRequest, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// startSessionCreateRequest creates the StartSession request.
func (client *TuningConfigurationClient) startSessionCreateRequest(ctx context.Context, resourceGroupName string, serverName string, tuningOption TuningOptionEnum, configTuningRequest ConfigTuningRequestParameter, _ *TuningConfigurationClientBeginStartSessionOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforPostgreSQL/flexibleServers/{serverName}/tuningOptions/{tuningOption}/startSession"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if serverName == "" {
		return nil, errors.New("parameter serverName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{serverName}", url.PathEscape(serverName))
	if tuningOption == "" {
		return nil, errors.New("parameter tuningOption cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{tuningOption}", url.PathEscape(string(tuningOption)))
	req, err := runtime.NewRequest(ctx, http.MethodPost, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2025-01-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, configTuningRequest); err != nil {
		return nil, err
	}
	return req, nil
}

// BeginStopSession - Stops the config tuning session.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-01-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - serverName - The name of the server.
//   - tuningOption - The name of the tuning option.
//   - options - TuningConfigurationClientBeginStopSessionOptions contains the optional parameters for the TuningConfigurationClient.BeginStopSession
//     method.
func (client *TuningConfigurationClient) BeginStopSession(ctx context.Context, resourceGroupName string, serverName string, tuningOption TuningOptionEnum, options *TuningConfigurationClientBeginStopSessionOptions) (*runtime.Poller[TuningConfigurationClientStopSessionResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.stopSession(ctx, resourceGroupName, serverName, tuningOption, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[TuningConfigurationClientStopSessionResponse]{
			FinalStateVia: runtime.FinalStateViaLocation,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[TuningConfigurationClientStopSessionResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// StopSession - Stops the config tuning session.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-01-01-preview
func (client *TuningConfigurationClient) stopSession(ctx context.Context, resourceGroupName string, serverName string, tuningOption TuningOptionEnum, options *TuningConfigurationClientBeginStopSessionOptions) (*http.Response, error) {
	var err error
	const operationName = "TuningConfigurationClient.BeginStopSession"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.stopSessionCreateRequest(ctx, resourceGroupName, serverName, tuningOption, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// stopSessionCreateRequest creates the StopSession request.
func (client *TuningConfigurationClient) stopSessionCreateRequest(ctx context.Context, resourceGroupName string, serverName string, tuningOption TuningOptionEnum, _ *TuningConfigurationClientBeginStopSessionOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforPostgreSQL/flexibleServers/{serverName}/tuningOptions/{tuningOption}/stopSession"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if serverName == "" {
		return nil, errors.New("parameter serverName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{serverName}", url.PathEscape(serverName))
	if tuningOption == "" {
		return nil, errors.New("parameter tuningOption cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{tuningOption}", url.PathEscape(string(tuningOption)))
	req, err := runtime.NewRequest(ctx, http.MethodPost, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2025-01-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}
