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

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

// InventoryClient contains the methods for the Inventory group.
// Don't use this type directly, use NewInventoryClient() instead.
type InventoryClient struct {
	internal *arm.Client
}

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

// Get - Get a InventoryResource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-12-01
//   - resourceURI - The fully qualified Azure Resource manager identifier of the resource.
//   - solutionConfiguration - Represent Solution Configuration Resource.
//   - inventoryID - Inventory resource
//   - options - InventoryClientGetOptions contains the optional parameters for the InventoryClient.Get method.
func (client *InventoryClient) Get(ctx context.Context, resourceURI string, solutionConfiguration string, inventoryID string, options *InventoryClientGetOptions) (InventoryClientGetResponse, error) {
	var err error
	const operationName = "InventoryClient.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, resourceURI, solutionConfiguration, inventoryID, options)
	if err != nil {
		return InventoryClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return InventoryClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return InventoryClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *InventoryClient) getCreateRequest(ctx context.Context, resourceURI string, solutionConfiguration string, inventoryID string, _ *InventoryClientGetOptions) (*policy.Request, error) {
	urlPath := "/{resourceUri}/providers/Microsoft.HybridConnectivity/solutionConfigurations/{solutionConfiguration}/inventory/{inventoryId}"
	urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI)
	if solutionConfiguration == "" {
		return nil, errors.New("parameter solutionConfiguration cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{solutionConfiguration}", url.PathEscape(solutionConfiguration))
	if inventoryID == "" {
		return nil, errors.New("parameter inventoryID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{inventoryId}", url.PathEscape(inventoryID))
	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-12-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// NewListBySolutionConfigurationPager - List InventoryResource resources by SolutionConfiguration
//
// Generated from API version 2024-12-01
//   - resourceURI - The fully qualified Azure Resource manager identifier of the resource.
//   - solutionConfiguration - Represent Solution Configuration Resource.
//   - options - InventoryClientListBySolutionConfigurationOptions contains the optional parameters for the InventoryClient.NewListBySolutionConfigurationPager
//     method.
func (client *InventoryClient) NewListBySolutionConfigurationPager(resourceURI string, solutionConfiguration string, options *InventoryClientListBySolutionConfigurationOptions) *runtime.Pager[InventoryClientListBySolutionConfigurationResponse] {
	return runtime.NewPager(runtime.PagingHandler[InventoryClientListBySolutionConfigurationResponse]{
		More: func(page InventoryClientListBySolutionConfigurationResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *InventoryClientListBySolutionConfigurationResponse) (InventoryClientListBySolutionConfigurationResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "InventoryClient.NewListBySolutionConfigurationPager")
			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.listBySolutionConfigurationCreateRequest(ctx, resourceURI, solutionConfiguration, options)
			}, nil)
			if err != nil {
				return InventoryClientListBySolutionConfigurationResponse{}, err
			}
			return client.listBySolutionConfigurationHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listBySolutionConfigurationCreateRequest creates the ListBySolutionConfiguration request.
func (client *InventoryClient) listBySolutionConfigurationCreateRequest(ctx context.Context, resourceURI string, solutionConfiguration string, _ *InventoryClientListBySolutionConfigurationOptions) (*policy.Request, error) {
	urlPath := "/{resourceUri}/providers/Microsoft.HybridConnectivity/solutionConfigurations/{solutionConfiguration}/inventory"
	urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI)
	if solutionConfiguration == "" {
		return nil, errors.New("parameter solutionConfiguration cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{solutionConfiguration}", url.PathEscape(solutionConfiguration))
	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-12-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listBySolutionConfigurationHandleResponse handles the ListBySolutionConfiguration response.
func (client *InventoryClient) listBySolutionConfigurationHandleResponse(resp *http.Response) (InventoryClientListBySolutionConfigurationResponse, error) {
	result := InventoryClientListBySolutionConfigurationResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.InventoryResourceListResult); err != nil {
		return InventoryClientListBySolutionConfigurationResponse{}, err
	}
	return result, nil
}
