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

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

// CustomRecommendationsClient contains the methods for the CustomRecommendations group.
// Don't use this type directly, use NewCustomRecommendationsClient() instead.
//
// Generated from API version 2024-08-01
type CustomRecommendationsClient struct {
	internal *arm.Client
}

// NewCustomRecommendationsClient creates a new instance of CustomRecommendationsClient with the specified values.
//   - credential - used to authorize requests. Usually a credential from azidentity.
//   - options - Contains optional client configuration. Pass nil to accept the default values.
func NewCustomRecommendationsClient(credential azcore.TokenCredential, options *arm.ClientOptions) (*CustomRecommendationsClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &CustomRecommendationsClient{
		internal: cl,
	}
	return client, nil
}

// CreateOrUpdate - Creates or updates a custom recommendation over a given scope
// If the operation fails it returns an *azcore.ResponseError type.
//   - scope - The fully qualified Azure Resource manager identifier of the resource.
//   - customRecommendationName - Name of the Custom Recommendation.
//   - customRecommendationBody - Custom Recommendation body
//   - options - CustomRecommendationsClientCreateOrUpdateOptions contains the optional parameters for the CustomRecommendationsClient.CreateOrUpdate
//     method.
func (client *CustomRecommendationsClient) CreateOrUpdate(ctx context.Context, scope string, customRecommendationName string, customRecommendationBody CustomRecommendation, options *CustomRecommendationsClientCreateOrUpdateOptions) (CustomRecommendationsClientCreateOrUpdateResponse, error) {
	var err error
	const operationName = "CustomRecommendationsClient.CreateOrUpdate"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.createOrUpdateCreateRequest(ctx, scope, customRecommendationName, customRecommendationBody, options)
	if err != nil {
		return CustomRecommendationsClientCreateOrUpdateResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return CustomRecommendationsClientCreateOrUpdateResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
		err = runtime.NewResponseError(httpResp)
		return CustomRecommendationsClientCreateOrUpdateResponse{}, err
	}
	resp, err := client.createOrUpdateHandleResponse(httpResp)
	return resp, err
}

// createOrUpdateCreateRequest creates the CreateOrUpdate request.
func (client *CustomRecommendationsClient) createOrUpdateCreateRequest(ctx context.Context, scope string, customRecommendationName string, customRecommendationBody CustomRecommendation, _ *CustomRecommendationsClientCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/{scope}/providers/Microsoft.Security/customRecommendations/{customRecommendationName}"
	if scope == "" {
		return nil, errors.New("parameter scope cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{scope}", scope)
	if customRecommendationName == "" {
		return nil, errors.New("parameter customRecommendationName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{customRecommendationName}", url.PathEscape(customRecommendationName))
	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", version20240801)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, customRecommendationBody); err != nil {
		return nil, err
	}
	return req, nil
}

// createOrUpdateHandleResponse handles the CreateOrUpdate response.
func (client *CustomRecommendationsClient) createOrUpdateHandleResponse(resp *http.Response) (CustomRecommendationsClientCreateOrUpdateResponse, error) {
	result := CustomRecommendationsClientCreateOrUpdateResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.CustomRecommendation); err != nil {
		return CustomRecommendationsClientCreateOrUpdateResponse{}, err
	}
	return result, nil
}

// Delete - Delete a custom recommendation over a given scope
// If the operation fails it returns an *azcore.ResponseError type.
//   - scope - The fully qualified Azure Resource manager identifier of the resource.
//   - customRecommendationName - Name of the Custom Recommendation.
//   - options - CustomRecommendationsClientDeleteOptions contains the optional parameters for the CustomRecommendationsClient.Delete
//     method.
func (client *CustomRecommendationsClient) Delete(ctx context.Context, scope string, customRecommendationName string, options *CustomRecommendationsClientDeleteOptions) (CustomRecommendationsClientDeleteResponse, error) {
	var err error
	const operationName = "CustomRecommendationsClient.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, scope, customRecommendationName, options)
	if err != nil {
		return CustomRecommendationsClientDeleteResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return CustomRecommendationsClientDeleteResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return CustomRecommendationsClientDeleteResponse{}, err
	}
	return CustomRecommendationsClientDeleteResponse{}, nil
}

// deleteCreateRequest creates the Delete request.
func (client *CustomRecommendationsClient) deleteCreateRequest(ctx context.Context, scope string, customRecommendationName string, _ *CustomRecommendationsClientDeleteOptions) (*policy.Request, error) {
	urlPath := "/{scope}/providers/Microsoft.Security/customRecommendations/{customRecommendationName}"
	if scope == "" {
		return nil, errors.New("parameter scope cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{scope}", scope)
	if customRecommendationName == "" {
		return nil, errors.New("parameter customRecommendationName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{customRecommendationName}", url.PathEscape(customRecommendationName))
	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", version20240801)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	return req, nil
}

// Get - Get a specific custom recommendation for the requested scope by customRecommendationName
// If the operation fails it returns an *azcore.ResponseError type.
//   - scope - The fully qualified Azure Resource manager identifier of the resource.
//   - customRecommendationName - Name of the Custom Recommendation.
//   - options - CustomRecommendationsClientGetOptions contains the optional parameters for the CustomRecommendationsClient.Get
//     method.
func (client *CustomRecommendationsClient) Get(ctx context.Context, scope string, customRecommendationName string, options *CustomRecommendationsClientGetOptions) (CustomRecommendationsClientGetResponse, error) {
	var err error
	const operationName = "CustomRecommendationsClient.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, scope, customRecommendationName, options)
	if err != nil {
		return CustomRecommendationsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return CustomRecommendationsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return CustomRecommendationsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *CustomRecommendationsClient) getCreateRequest(ctx context.Context, scope string, customRecommendationName string, _ *CustomRecommendationsClientGetOptions) (*policy.Request, error) {
	urlPath := "/{scope}/providers/Microsoft.Security/customRecommendations/{customRecommendationName}"
	if scope == "" {
		return nil, errors.New("parameter scope cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{scope}", scope)
	if customRecommendationName == "" {
		return nil, errors.New("parameter customRecommendationName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{customRecommendationName}", url.PathEscape(customRecommendationName))
	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", version20240801)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// NewListPager - Get a list of all relevant custom recommendations over a scope
//   - scope - The fully qualified Azure Resource manager identifier of the resource.
//   - options - CustomRecommendationsClientListOptions contains the optional parameters for the CustomRecommendationsClient.NewListPager
//     method.
func (client *CustomRecommendationsClient) NewListPager(scope string, options *CustomRecommendationsClientListOptions) *runtime.Pager[CustomRecommendationsClientListResponse] {
	return runtime.NewPager(runtime.PagingHandler[CustomRecommendationsClientListResponse]{
		More: func(page CustomRecommendationsClientListResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *CustomRecommendationsClientListResponse) (CustomRecommendationsClientListResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "CustomRecommendationsClient.NewListPager")
			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.listCreateRequest(ctx, scope, options)
			}, nil)
			if err != nil {
				return CustomRecommendationsClientListResponse{}, err
			}
			return client.listHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listCreateRequest creates the List request.
func (client *CustomRecommendationsClient) listCreateRequest(ctx context.Context, scope string, _ *CustomRecommendationsClientListOptions) (*policy.Request, error) {
	urlPath := "/{scope}/providers/Microsoft.Security/customRecommendations"
	if scope == "" {
		return nil, errors.New("parameter scope cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{scope}", scope)
	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", version20240801)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listHandleResponse handles the List response.
func (client *CustomRecommendationsClient) listHandleResponse(resp *http.Response) (CustomRecommendationsClientListResponse, error) {
	result := CustomRecommendationsClientListResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.CustomRecommendationsList); err != nil {
		return CustomRecommendationsClientListResponse{}, err
	}
	return result, nil
}
