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

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

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

// Create - Create Insight resource, This is Admin only operation
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-05-01-preview
//   - workloadImpactName - workloadImpact resource
//   - insightName - Name of the insight
//   - resource - Resource create parameters.
//   - options - InsightsClientCreateOptions contains the optional parameters for the InsightsClient.Create method.
func (client *InsightsClient) Create(ctx context.Context, workloadImpactName string, insightName string, resource Insight, options *InsightsClientCreateOptions) (InsightsClientCreateResponse, error) {
	var err error
	const operationName = "InsightsClient.Create"
	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, insightName, resource, options)
	if err != nil {
		return InsightsClientCreateResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return InsightsClientCreateResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
		err = runtime.NewResponseError(httpResp)
		return InsightsClientCreateResponse{}, err
	}
	resp, err := client.createHandleResponse(httpResp)
	return resp, err
}

// createCreateRequest creates the Create request.
func (client *InsightsClient) createCreateRequest(ctx context.Context, workloadImpactName string, insightName string, resource Insight, _ *InsightsClientCreateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Impact/workloadImpacts/{workloadImpactName}/insights/{insightName}"
	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))
	if insightName == "" {
		return nil, errors.New("parameter insightName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{insightName}", url.PathEscape(insightName))
	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
}

// createHandleResponse handles the Create response.
func (client *InsightsClient) createHandleResponse(resp *http.Response) (InsightsClientCreateResponse, error) {
	result := InsightsClientCreateResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.Insight); err != nil {
		return InsightsClientCreateResponse{}, err
	}
	return result, nil
}

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

// deleteCreateRequest creates the Delete request.
func (client *InsightsClient) deleteCreateRequest(ctx context.Context, workloadImpactName string, insightName string, _ *InsightsClientDeleteOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Impact/workloadImpacts/{workloadImpactName}/insights/{insightName}"
	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))
	if insightName == "" {
		return nil, errors.New("parameter insightName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{insightName}", url.PathEscape(insightName))
	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 Insight resources by workloadImpactName and insightName
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-05-01-preview
//   - workloadImpactName - workloadImpact resource
//   - insightName - Name of the insight
//   - options - InsightsClientGetOptions contains the optional parameters for the InsightsClient.Get method.
func (client *InsightsClient) Get(ctx context.Context, workloadImpactName string, insightName string, options *InsightsClientGetOptions) (InsightsClientGetResponse, error) {
	var err error
	const operationName = "InsightsClient.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, insightName, options)
	if err != nil {
		return InsightsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return InsightsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return InsightsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

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

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

// listBySubscriptionCreateRequest creates the ListBySubscription request.
func (client *InsightsClient) listBySubscriptionCreateRequest(ctx context.Context, workloadImpactName string, _ *InsightsClientListBySubscriptionOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Impact/workloadImpacts/{workloadImpactName}/insights"
	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
}

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