// 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 armproviderhub

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

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

// NewProviderMonitorSettingsClient creates a new instance of ProviderMonitorSettingsClient 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 NewProviderMonitorSettingsClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*ProviderMonitorSettingsClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &ProviderMonitorSettingsClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

// BeginCreate - Creates the provider monitor setting.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-09-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - providerMonitorSettingName - The name of the provider monitor setting.
//   - properties - The provider monitor setting properties supplied to the create operation.
//   - options - ProviderMonitorSettingsClientBeginCreateOptions contains the optional parameters for the ProviderMonitorSettingsClient.BeginCreate
//     method.
func (client *ProviderMonitorSettingsClient) BeginCreate(ctx context.Context, resourceGroupName string, providerMonitorSettingName string, properties ProviderMonitorSetting, options *ProviderMonitorSettingsClientBeginCreateOptions) (*runtime.Poller[ProviderMonitorSettingsClientCreateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.create(ctx, resourceGroupName, providerMonitorSettingName, properties, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[ProviderMonitorSettingsClientCreateResponse]{
			FinalStateVia: runtime.FinalStateViaAzureAsyncOp,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[ProviderMonitorSettingsClientCreateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Create - Creates the provider monitor setting.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-09-01
func (client *ProviderMonitorSettingsClient) create(ctx context.Context, resourceGroupName string, providerMonitorSettingName string, properties ProviderMonitorSetting, options *ProviderMonitorSettingsClientBeginCreateOptions) (*http.Response, error) {
	var err error
	const operationName = "ProviderMonitorSettingsClient.BeginCreate"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.createCreateRequest(ctx, resourceGroupName, providerMonitorSettingName, properties, 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.StatusOK, http.StatusCreated) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// createCreateRequest creates the Create request.
func (client *ProviderMonitorSettingsClient) createCreateRequest(ctx context.Context, resourceGroupName string, providerMonitorSettingName string, properties ProviderMonitorSetting, _ *ProviderMonitorSettingsClientBeginCreateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ProviderHub/providerMonitorSettings/{providerMonitorSettingName}"
	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 providerMonitorSettingName == "" {
		return nil, errors.New("parameter providerMonitorSettingName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{providerMonitorSettingName}", url.PathEscape(providerMonitorSettingName))
	req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2024-09-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, properties); err != nil {
		return nil, err
	}
	return req, nil
}

// Delete - Deletes a provider monitor setting.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-09-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - providerMonitorSettingName - The name of the provider monitor setting.
//   - options - ProviderMonitorSettingsClientDeleteOptions contains the optional parameters for the ProviderMonitorSettingsClient.Delete
//     method.
func (client *ProviderMonitorSettingsClient) Delete(ctx context.Context, resourceGroupName string, providerMonitorSettingName string, options *ProviderMonitorSettingsClientDeleteOptions) (ProviderMonitorSettingsClientDeleteResponse, error) {
	var err error
	const operationName = "ProviderMonitorSettingsClient.Delete"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.deleteCreateRequest(ctx, resourceGroupName, providerMonitorSettingName, options)
	if err != nil {
		return ProviderMonitorSettingsClientDeleteResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ProviderMonitorSettingsClientDeleteResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return ProviderMonitorSettingsClientDeleteResponse{}, err
	}
	return ProviderMonitorSettingsClientDeleteResponse{}, nil
}

// deleteCreateRequest creates the Delete request.
func (client *ProviderMonitorSettingsClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, providerMonitorSettingName string, _ *ProviderMonitorSettingsClientDeleteOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ProviderHub/providerMonitorSettings/{providerMonitorSettingName}"
	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 providerMonitorSettingName == "" {
		return nil, errors.New("parameter providerMonitorSettingName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{providerMonitorSettingName}", url.PathEscape(providerMonitorSettingName))
	req, err := runtime.NewRequest(ctx, http.MethodDelete, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2024-09-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// Get - Gets the provider monitor setting details.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-09-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - providerMonitorSettingName - The name of the provider monitor setting.
//   - options - ProviderMonitorSettingsClientGetOptions contains the optional parameters for the ProviderMonitorSettingsClient.Get
//     method.
func (client *ProviderMonitorSettingsClient) Get(ctx context.Context, resourceGroupName string, providerMonitorSettingName string, options *ProviderMonitorSettingsClientGetOptions) (ProviderMonitorSettingsClientGetResponse, error) {
	var err error
	const operationName = "ProviderMonitorSettingsClient.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, providerMonitorSettingName, options)
	if err != nil {
		return ProviderMonitorSettingsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ProviderMonitorSettingsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ProviderMonitorSettingsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *ProviderMonitorSettingsClient) getCreateRequest(ctx context.Context, resourceGroupName string, providerMonitorSettingName string, _ *ProviderMonitorSettingsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ProviderHub/providerMonitorSettings/{providerMonitorSettingName}"
	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 providerMonitorSettingName == "" {
		return nil, errors.New("parameter providerMonitorSettingName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{providerMonitorSettingName}", url.PathEscape(providerMonitorSettingName))
	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", "2024-09-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// NewListByResourceGroupPager - Gets the list of the provider monitor settings in the resource group.
//
// Generated from API version 2024-09-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - options - ProviderMonitorSettingsClientListByResourceGroupOptions contains the optional parameters for the ProviderMonitorSettingsClient.NewListByResourceGroupPager
//     method.
func (client *ProviderMonitorSettingsClient) NewListByResourceGroupPager(resourceGroupName string, options *ProviderMonitorSettingsClientListByResourceGroupOptions) *runtime.Pager[ProviderMonitorSettingsClientListByResourceGroupResponse] {
	return runtime.NewPager(runtime.PagingHandler[ProviderMonitorSettingsClientListByResourceGroupResponse]{
		More: func(page ProviderMonitorSettingsClientListByResourceGroupResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *ProviderMonitorSettingsClientListByResourceGroupResponse) (ProviderMonitorSettingsClientListByResourceGroupResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "ProviderMonitorSettingsClient.NewListByResourceGroupPager")
			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.listByResourceGroupCreateRequest(ctx, resourceGroupName, options)
			}, nil)
			if err != nil {
				return ProviderMonitorSettingsClientListByResourceGroupResponse{}, err
			}
			return client.listByResourceGroupHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByResourceGroupCreateRequest creates the ListByResourceGroup request.
func (client *ProviderMonitorSettingsClient) listByResourceGroupCreateRequest(ctx context.Context, resourceGroupName string, _ *ProviderMonitorSettingsClientListByResourceGroupOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ProviderHub/providerMonitorSettings"
	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))
	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", "2024-09-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByResourceGroupHandleResponse handles the ListByResourceGroup response.
func (client *ProviderMonitorSettingsClient) listByResourceGroupHandleResponse(resp *http.Response) (ProviderMonitorSettingsClientListByResourceGroupResponse, error) {
	result := ProviderMonitorSettingsClientListByResourceGroupResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ProviderMonitorSettingArrayResponseWithContinuation); err != nil {
		return ProviderMonitorSettingsClientListByResourceGroupResponse{}, err
	}
	return result, nil
}

// NewListBySubscriptionPager - Gets the list of the provider monitor settings in the subscription.
//
// Generated from API version 2024-09-01
//   - options - ProviderMonitorSettingsClientListBySubscriptionOptions contains the optional parameters for the ProviderMonitorSettingsClient.NewListBySubscriptionPager
//     method.
func (client *ProviderMonitorSettingsClient) NewListBySubscriptionPager(options *ProviderMonitorSettingsClientListBySubscriptionOptions) *runtime.Pager[ProviderMonitorSettingsClientListBySubscriptionResponse] {
	return runtime.NewPager(runtime.PagingHandler[ProviderMonitorSettingsClientListBySubscriptionResponse]{
		More: func(page ProviderMonitorSettingsClientListBySubscriptionResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *ProviderMonitorSettingsClientListBySubscriptionResponse) (ProviderMonitorSettingsClientListBySubscriptionResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "ProviderMonitorSettingsClient.NewListBySubscriptionPager")
			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.listBySubscriptionCreateRequest(ctx, options)
			}, nil)
			if err != nil {
				return ProviderMonitorSettingsClientListBySubscriptionResponse{}, err
			}
			return client.listBySubscriptionHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listBySubscriptionCreateRequest creates the ListBySubscription request.
func (client *ProviderMonitorSettingsClient) listBySubscriptionCreateRequest(ctx context.Context, _ *ProviderMonitorSettingsClientListBySubscriptionOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.ProviderHub/providerMonitorSettings"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	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", "2024-09-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listBySubscriptionHandleResponse handles the ListBySubscription response.
func (client *ProviderMonitorSettingsClient) listBySubscriptionHandleResponse(resp *http.Response) (ProviderMonitorSettingsClientListBySubscriptionResponse, error) {
	result := ProviderMonitorSettingsClientListBySubscriptionResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ProviderMonitorSettingArrayResponseWithContinuation); err != nil {
		return ProviderMonitorSettingsClientListBySubscriptionResponse{}, err
	}
	return result, nil
}

// Update - Updates the provider monitor setting properties as specified in the request body. Update fails if the specified
// provider monitor setting does not already exist.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-09-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - providerMonitorSettingName - The name of the provider monitor setting.
//   - options - ProviderMonitorSettingsClientUpdateOptions contains the optional parameters for the ProviderMonitorSettingsClient.Update
//     method.
func (client *ProviderMonitorSettingsClient) Update(ctx context.Context, resourceGroupName string, providerMonitorSettingName string, options *ProviderMonitorSettingsClientUpdateOptions) (ProviderMonitorSettingsClientUpdateResponse, error) {
	var err error
	const operationName = "ProviderMonitorSettingsClient.Update"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.updateCreateRequest(ctx, resourceGroupName, providerMonitorSettingName, options)
	if err != nil {
		return ProviderMonitorSettingsClientUpdateResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ProviderMonitorSettingsClientUpdateResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ProviderMonitorSettingsClientUpdateResponse{}, err
	}
	resp, err := client.updateHandleResponse(httpResp)
	return resp, err
}

// updateCreateRequest creates the Update request.
func (client *ProviderMonitorSettingsClient) updateCreateRequest(ctx context.Context, resourceGroupName string, providerMonitorSettingName string, _ *ProviderMonitorSettingsClientUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ProviderHub/providerMonitorSettings/{providerMonitorSettingName}"
	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 providerMonitorSettingName == "" {
		return nil, errors.New("parameter providerMonitorSettingName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{providerMonitorSettingName}", url.PathEscape(providerMonitorSettingName))
	req, err := runtime.NewRequest(ctx, http.MethodPatch, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2024-09-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// updateHandleResponse handles the Update response.
func (client *ProviderMonitorSettingsClient) updateHandleResponse(resp *http.Response) (ProviderMonitorSettingsClientUpdateResponse, error) {
	result := ProviderMonitorSettingsClientUpdateResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ProviderMonitorSetting); err != nil {
		return ProviderMonitorSettingsClientUpdateResponse{}, err
	}
	return result, nil
}
