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

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

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

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

// BeginCreate - Create a WorkloadImpact
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-05-01-preview
//   - workloadImpactName - workloadImpact resource
//   - resource - Resource create parameters.
//   - options - WorkloadImpactsClientBeginCreateOptions contains the optional parameters for the WorkloadImpactsClient.BeginCreate
//     method.
func (client *WorkloadImpactsClient) BeginCreate(ctx context.Context, workloadImpactName string, resource WorkloadImpact, options *WorkloadImpactsClientBeginCreateOptions) (*runtime.Poller[WorkloadImpactsClientCreateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.create(ctx, workloadImpactName, resource, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[WorkloadImpactsClientCreateResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[WorkloadImpactsClientCreateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Create - Create a WorkloadImpact
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-05-01-preview
func (client *WorkloadImpactsClient) create(ctx context.Context, workloadImpactName string, resource WorkloadImpact, options *WorkloadImpactsClientBeginCreateOptions) (*http.Response, error) {
	var err error
	const operationName = "WorkloadImpactsClient.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, workloadImpactName, resource, 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 *WorkloadImpactsClient) createCreateRequest(ctx context.Context, workloadImpactName string, resource WorkloadImpact, _ *WorkloadImpactsClientBeginCreateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Impact/workloadImpacts/{workloadImpactName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if workloadImpactName == "" {
		return nil, errors.New("parameter workloadImpactName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{workloadImpactName}", url.PathEscape(workloadImpactName))
	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-05-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, resource); err != nil {
		return nil, err
	}
	return req, nil
}

// Delete - Delete a WorkloadImpact
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-05-01-preview
//   - workloadImpactName - workloadImpact resource
//   - options - WorkloadImpactsClientDeleteOptions contains the optional parameters for the WorkloadImpactsClient.Delete method.
func (client *WorkloadImpactsClient) Delete(ctx context.Context, workloadImpactName string, options *WorkloadImpactsClientDeleteOptions) (WorkloadImpactsClientDeleteResponse, error) {
	var err error
	const operationName = "WorkloadImpactsClient.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, workloadImpactName, options)
	if err != nil {
		return WorkloadImpactsClientDeleteResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return WorkloadImpactsClientDeleteResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return WorkloadImpactsClientDeleteResponse{}, err
	}
	return WorkloadImpactsClientDeleteResponse{}, nil
}

// deleteCreateRequest creates the Delete request.
func (client *WorkloadImpactsClient) deleteCreateRequest(ctx context.Context, workloadImpactName string, _ *WorkloadImpactsClientDeleteOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Impact/workloadImpacts/{workloadImpactName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if workloadImpactName == "" {
		return nil, errors.New("parameter workloadImpactName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{workloadImpactName}", url.PathEscape(workloadImpactName))
	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-05-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	return req, nil
}

// Get - Get a WorkloadImpact
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-05-01-preview
//   - workloadImpactName - workloadImpact resource
//   - options - WorkloadImpactsClientGetOptions contains the optional parameters for the WorkloadImpactsClient.Get method.
func (client *WorkloadImpactsClient) Get(ctx context.Context, workloadImpactName string, options *WorkloadImpactsClientGetOptions) (WorkloadImpactsClientGetResponse, error) {
	var err error
	const operationName = "WorkloadImpactsClient.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, workloadImpactName, options)
	if err != nil {
		return WorkloadImpactsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return WorkloadImpactsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return WorkloadImpactsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *WorkloadImpactsClient) getCreateRequest(ctx context.Context, workloadImpactName string, _ *WorkloadImpactsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Impact/workloadImpacts/{workloadImpactName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if workloadImpactName == "" {
		return nil, errors.New("parameter workloadImpactName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{workloadImpactName}", url.PathEscape(workloadImpactName))
	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-05-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 *WorkloadImpactsClient) getHandleResponse(resp *http.Response) (WorkloadImpactsClientGetResponse, error) {
	result := WorkloadImpactsClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.WorkloadImpact); err != nil {
		return WorkloadImpactsClientGetResponse{}, err
	}
	return result, nil
}

// NewListBySubscriptionPager - List WorkloadImpact resources by subscription ID
//
// Generated from API version 2024-05-01-preview
//   - options - WorkloadImpactsClientListBySubscriptionOptions contains the optional parameters for the WorkloadImpactsClient.NewListBySubscriptionPager
//     method.
func (client *WorkloadImpactsClient) NewListBySubscriptionPager(options *WorkloadImpactsClientListBySubscriptionOptions) *runtime.Pager[WorkloadImpactsClientListBySubscriptionResponse] {
	return runtime.NewPager(runtime.PagingHandler[WorkloadImpactsClientListBySubscriptionResponse]{
		More: func(page WorkloadImpactsClientListBySubscriptionResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *WorkloadImpactsClientListBySubscriptionResponse) (WorkloadImpactsClientListBySubscriptionResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "WorkloadImpactsClient.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 WorkloadImpactsClientListBySubscriptionResponse{}, err
			}
			return client.listBySubscriptionHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listBySubscriptionCreateRequest creates the ListBySubscription request.
func (client *WorkloadImpactsClient) listBySubscriptionCreateRequest(ctx context.Context, _ *WorkloadImpactsClientListBySubscriptionOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Impact/workloadImpacts"
	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-05-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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