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

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

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

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

// Get - Get a AvsSummary
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-01-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - projectName - Assessment Project Name
//   - businessCaseName - Business case ARM name
//   - avsSummaryName - Gets the Name of the Avs Summary.
//   - options - BusinessCaseAvsSummaryOperationsClientGetOptions contains the optional parameters for the BusinessCaseAvsSummaryOperationsClient.Get
//     method.
func (client *BusinessCaseAvsSummaryOperationsClient) Get(ctx context.Context, resourceGroupName string, projectName string, businessCaseName string, avsSummaryName string, options *BusinessCaseAvsSummaryOperationsClientGetOptions) (BusinessCaseAvsSummaryOperationsClientGetResponse, error) {
	var err error
	const operationName = "BusinessCaseAvsSummaryOperationsClient.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, projectName, businessCaseName, avsSummaryName, options)
	if err != nil {
		return BusinessCaseAvsSummaryOperationsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return BusinessCaseAvsSummaryOperationsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return BusinessCaseAvsSummaryOperationsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *BusinessCaseAvsSummaryOperationsClient) getCreateRequest(ctx context.Context, resourceGroupName string, projectName string, businessCaseName string, avsSummaryName string, _ *BusinessCaseAvsSummaryOperationsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Migrate/assessmentProjects/{projectName}/businessCases/{businessCaseName}/avsSummaries/{avsSummaryName}"
	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 projectName == "" {
		return nil, errors.New("parameter projectName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{projectName}", url.PathEscape(projectName))
	if businessCaseName == "" {
		return nil, errors.New("parameter businessCaseName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{businessCaseName}", url.PathEscape(businessCaseName))
	if avsSummaryName == "" {
		return nil, errors.New("parameter avsSummaryName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{avsSummaryName}", url.PathEscape(avsSummaryName))
	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-01-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 *BusinessCaseAvsSummaryOperationsClient) getHandleResponse(resp *http.Response) (BusinessCaseAvsSummaryOperationsClientGetResponse, error) {
	result := BusinessCaseAvsSummaryOperationsClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.AvsSummary); err != nil {
		return BusinessCaseAvsSummaryOperationsClientGetResponse{}, err
	}
	return result, nil
}

// NewListByBusinessCasePager - List AvsSummary resources by BusinessCase
//
// Generated from API version 2024-01-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - projectName - Assessment Project Name
//   - businessCaseName - Business case ARM name
//   - options - BusinessCaseAvsSummaryOperationsClientListByBusinessCaseOptions contains the optional parameters for the BusinessCaseAvsSummaryOperationsClient.NewListByBusinessCasePager
//     method.
func (client *BusinessCaseAvsSummaryOperationsClient) NewListByBusinessCasePager(resourceGroupName string, projectName string, businessCaseName string, options *BusinessCaseAvsSummaryOperationsClientListByBusinessCaseOptions) *runtime.Pager[BusinessCaseAvsSummaryOperationsClientListByBusinessCaseResponse] {
	return runtime.NewPager(runtime.PagingHandler[BusinessCaseAvsSummaryOperationsClientListByBusinessCaseResponse]{
		More: func(page BusinessCaseAvsSummaryOperationsClientListByBusinessCaseResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *BusinessCaseAvsSummaryOperationsClientListByBusinessCaseResponse) (BusinessCaseAvsSummaryOperationsClientListByBusinessCaseResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "BusinessCaseAvsSummaryOperationsClient.NewListByBusinessCasePager")
			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.listByBusinessCaseCreateRequest(ctx, resourceGroupName, projectName, businessCaseName, options)
			}, nil)
			if err != nil {
				return BusinessCaseAvsSummaryOperationsClientListByBusinessCaseResponse{}, err
			}
			return client.listByBusinessCaseHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByBusinessCaseCreateRequest creates the ListByBusinessCase request.
func (client *BusinessCaseAvsSummaryOperationsClient) listByBusinessCaseCreateRequest(ctx context.Context, resourceGroupName string, projectName string, businessCaseName string, _ *BusinessCaseAvsSummaryOperationsClientListByBusinessCaseOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Migrate/assessmentProjects/{projectName}/businessCases/{businessCaseName}/avsSummaries"
	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 projectName == "" {
		return nil, errors.New("parameter projectName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{projectName}", url.PathEscape(projectName))
	if businessCaseName == "" {
		return nil, errors.New("parameter businessCaseName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{businessCaseName}", url.PathEscape(businessCaseName))
	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-01-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByBusinessCaseHandleResponse handles the ListByBusinessCase response.
func (client *BusinessCaseAvsSummaryOperationsClient) listByBusinessCaseHandleResponse(resp *http.Response) (BusinessCaseAvsSummaryOperationsClientListByBusinessCaseResponse, error) {
	result := BusinessCaseAvsSummaryOperationsClientListByBusinessCaseResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.AvsSummaryListResult); err != nil {
		return BusinessCaseAvsSummaryOperationsClientListByBusinessCaseResponse{}, err
	}
	return result, nil
}
