// 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) Go Code Generator. DO NOT EDIT.

package armsql

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"
)

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

// NewInstancePoolOperationsClient creates a new instance of InstancePoolOperationsClient 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 - Contains optional client configuration. Pass nil to accept the default values.
func NewInstancePoolOperationsClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*InstancePoolOperationsClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &InstancePoolOperationsClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

// Get - Gets a management operation on a instance pool.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-02-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - instancePoolName - The name of the instance pool to be retrieved.
//   - options - InstancePoolOperationsClientGetOptions contains the optional parameters for the InstancePoolOperationsClient.Get
//     method.
func (client *InstancePoolOperationsClient) Get(ctx context.Context, resourceGroupName string, instancePoolName string, operationID string, options *InstancePoolOperationsClientGetOptions) (InstancePoolOperationsClientGetResponse, error) {
	var err error
	const operationName = "InstancePoolOperationsClient.Get"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getCreateRequest(ctx, resourceGroupName, instancePoolName, operationID, options)
	if err != nil {
		return InstancePoolOperationsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return InstancePoolOperationsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return InstancePoolOperationsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *InstancePoolOperationsClient) getCreateRequest(ctx context.Context, resourceGroupName string, instancePoolName string, operationID string, _ *InstancePoolOperationsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/instancePools/{instancePoolName}/operations/{operationId}"
	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 instancePoolName == "" {
		return nil, errors.New("parameter instancePoolName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{instancePoolName}", url.PathEscape(instancePoolName))
	if operationID == "" {
		return nil, errors.New("parameter operationID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{operationId}", url.PathEscape(operationID))
	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-02-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getHandleResponse handles the Get response.
func (client *InstancePoolOperationsClient) getHandleResponse(resp *http.Response) (InstancePoolOperationsClientGetResponse, error) {
	result := InstancePoolOperationsClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.InstancePoolOperation); err != nil {
		return InstancePoolOperationsClientGetResponse{}, err
	}
	return result, nil
}

// NewListByInstancePoolPager - Gets a list of operations performed on the instance pool.
//
// Generated from API version 2025-02-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - instancePoolName - The name of the instance pool to be retrieved.
//   - options - InstancePoolOperationsClientListByInstancePoolOptions contains the optional parameters for the InstancePoolOperationsClient.NewListByInstancePoolPager
//     method.
func (client *InstancePoolOperationsClient) NewListByInstancePoolPager(resourceGroupName string, instancePoolName string, options *InstancePoolOperationsClientListByInstancePoolOptions) *runtime.Pager[InstancePoolOperationsClientListByInstancePoolResponse] {
	return runtime.NewPager(runtime.PagingHandler[InstancePoolOperationsClientListByInstancePoolResponse]{
		More: func(page InstancePoolOperationsClientListByInstancePoolResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *InstancePoolOperationsClientListByInstancePoolResponse) (InstancePoolOperationsClientListByInstancePoolResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "InstancePoolOperationsClient.NewListByInstancePoolPager")
			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.listByInstancePoolCreateRequest(ctx, resourceGroupName, instancePoolName, options)
			}, nil)
			if err != nil {
				return InstancePoolOperationsClientListByInstancePoolResponse{}, err
			}
			return client.listByInstancePoolHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByInstancePoolCreateRequest creates the ListByInstancePool request.
func (client *InstancePoolOperationsClient) listByInstancePoolCreateRequest(ctx context.Context, resourceGroupName string, instancePoolName string, _ *InstancePoolOperationsClientListByInstancePoolOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/instancePools/{instancePoolName}/operations"
	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 instancePoolName == "" {
		return nil, errors.New("parameter instancePoolName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{instancePoolName}", url.PathEscape(instancePoolName))
	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-02-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByInstancePoolHandleResponse handles the ListByInstancePool response.
func (client *InstancePoolOperationsClient) listByInstancePoolHandleResponse(resp *http.Response) (InstancePoolOperationsClientListByInstancePoolResponse, error) {
	result := InstancePoolOperationsClientListByInstancePoolResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.InstancePoolOperationListResult); err != nil {
		return InstancePoolOperationsClientListByInstancePoolResponse{}, err
	}
	return result, nil
}
