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

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

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

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

// Get - Get a Config Template Version Resource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-06-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - configTemplateName - The name of the ConfigTemplate
//   - configTemplateVersionName - The name of the ConfigTemplateVersion
//   - options - ConfigTemplateVersionsClientGetOptions contains the optional parameters for the ConfigTemplateVersionsClient.Get
//     method.
func (client *ConfigTemplateVersionsClient) Get(ctx context.Context, resourceGroupName string, configTemplateName string, configTemplateVersionName string, options *ConfigTemplateVersionsClientGetOptions) (ConfigTemplateVersionsClientGetResponse, error) {
	var err error
	const operationName = "ConfigTemplateVersionsClient.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, configTemplateName, configTemplateVersionName, options)
	if err != nil {
		return ConfigTemplateVersionsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ConfigTemplateVersionsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ConfigTemplateVersionsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *ConfigTemplateVersionsClient) getCreateRequest(ctx context.Context, resourceGroupName string, configTemplateName string, configTemplateVersionName string, _ *ConfigTemplateVersionsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/configTemplates/{configTemplateName}/versions/{configTemplateVersionName}"
	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 configTemplateName == "" {
		return nil, errors.New("parameter configTemplateName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{configTemplateName}", url.PathEscape(configTemplateName))
	if configTemplateVersionName == "" {
		return nil, errors.New("parameter configTemplateVersionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{configTemplateVersionName}", url.PathEscape(configTemplateVersionName))
	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", "2025-06-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// NewListByConfigTemplatePager - List Config Template Version Resources
//
// Generated from API version 2025-06-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - configTemplateName - The name of the ConfigTemplate
//   - options - ConfigTemplateVersionsClientListByConfigTemplateOptions contains the optional parameters for the ConfigTemplateVersionsClient.NewListByConfigTemplatePager
//     method.
func (client *ConfigTemplateVersionsClient) NewListByConfigTemplatePager(resourceGroupName string, configTemplateName string, options *ConfigTemplateVersionsClientListByConfigTemplateOptions) *runtime.Pager[ConfigTemplateVersionsClientListByConfigTemplateResponse] {
	return runtime.NewPager(runtime.PagingHandler[ConfigTemplateVersionsClientListByConfigTemplateResponse]{
		More: func(page ConfigTemplateVersionsClientListByConfigTemplateResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *ConfigTemplateVersionsClientListByConfigTemplateResponse) (ConfigTemplateVersionsClientListByConfigTemplateResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "ConfigTemplateVersionsClient.NewListByConfigTemplatePager")
			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.listByConfigTemplateCreateRequest(ctx, resourceGroupName, configTemplateName, options)
			}, nil)
			if err != nil {
				return ConfigTemplateVersionsClientListByConfigTemplateResponse{}, err
			}
			return client.listByConfigTemplateHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByConfigTemplateCreateRequest creates the ListByConfigTemplate request.
func (client *ConfigTemplateVersionsClient) listByConfigTemplateCreateRequest(ctx context.Context, resourceGroupName string, configTemplateName string, _ *ConfigTemplateVersionsClientListByConfigTemplateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/configTemplates/{configTemplateName}/versions"
	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 configTemplateName == "" {
		return nil, errors.New("parameter configTemplateName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{configTemplateName}", url.PathEscape(configTemplateName))
	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", "2025-06-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByConfigTemplateHandleResponse handles the ListByConfigTemplate response.
func (client *ConfigTemplateVersionsClient) listByConfigTemplateHandleResponse(resp *http.Response) (ConfigTemplateVersionsClientListByConfigTemplateResponse, error) {
	result := ConfigTemplateVersionsClientListByConfigTemplateResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ConfigTemplateVersionListResult); err != nil {
		return ConfigTemplateVersionsClientListByConfigTemplateResponse{}, err
	}
	return result, nil
}
