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

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

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

// BeginBulkDeploySolution - Post request for bulk deploy
// 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.
//   - solutionTemplateName - The name of the SolutionTemplate
//   - solutionTemplateVersionName - The name of the SolutionTemplateVersion
//   - body - The content of the action request
//   - options - SolutionTemplateVersionsClientBeginBulkDeploySolutionOptions contains the optional parameters for the SolutionTemplateVersionsClient.BeginBulkDeploySolution
//     method.
func (client *SolutionTemplateVersionsClient) BeginBulkDeploySolution(ctx context.Context, resourceGroupName string, solutionTemplateName string, solutionTemplateVersionName string, body BulkDeploySolutionParameter, options *SolutionTemplateVersionsClientBeginBulkDeploySolutionOptions) (*runtime.Poller[SolutionTemplateVersionsClientBulkDeploySolutionResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.bulkDeploySolution(ctx, resourceGroupName, solutionTemplateName, solutionTemplateVersionName, body, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[SolutionTemplateVersionsClientBulkDeploySolutionResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[SolutionTemplateVersionsClientBulkDeploySolutionResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// BulkDeploySolution - Post request for bulk deploy
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-06-01
func (client *SolutionTemplateVersionsClient) bulkDeploySolution(ctx context.Context, resourceGroupName string, solutionTemplateName string, solutionTemplateVersionName string, body BulkDeploySolutionParameter, options *SolutionTemplateVersionsClientBeginBulkDeploySolutionOptions) (*http.Response, error) {
	var err error
	const operationName = "SolutionTemplateVersionsClient.BeginBulkDeploySolution"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.bulkDeploySolutionCreateRequest(ctx, resourceGroupName, solutionTemplateName, solutionTemplateVersionName, body, 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.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// bulkDeploySolutionCreateRequest creates the BulkDeploySolution request.
func (client *SolutionTemplateVersionsClient) bulkDeploySolutionCreateRequest(ctx context.Context, resourceGroupName string, solutionTemplateName string, solutionTemplateVersionName string, body BulkDeploySolutionParameter, _ *SolutionTemplateVersionsClientBeginBulkDeploySolutionOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/solutionTemplates/{solutionTemplateName}/versions/{solutionTemplateVersionName}/bulkDeploySolution"
	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 solutionTemplateName == "" {
		return nil, errors.New("parameter solutionTemplateName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{solutionTemplateName}", url.PathEscape(solutionTemplateName))
	if solutionTemplateVersionName == "" {
		return nil, errors.New("parameter solutionTemplateVersionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{solutionTemplateVersionName}", url.PathEscape(solutionTemplateVersionName))
	req, err := runtime.NewRequest(ctx, http.MethodPost, 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["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, body); err != nil {
		return nil, err
	}
	return req, nil
}

// BeginBulkPublishSolution - Post request for bulk publish
// 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.
//   - solutionTemplateName - The name of the SolutionTemplate
//   - solutionTemplateVersionName - The name of the SolutionTemplateVersion
//   - body - The content of the action request
//   - options - SolutionTemplateVersionsClientBeginBulkPublishSolutionOptions contains the optional parameters for the SolutionTemplateVersionsClient.BeginBulkPublishSolution
//     method.
func (client *SolutionTemplateVersionsClient) BeginBulkPublishSolution(ctx context.Context, resourceGroupName string, solutionTemplateName string, solutionTemplateVersionName string, body BulkPublishSolutionParameter, options *SolutionTemplateVersionsClientBeginBulkPublishSolutionOptions) (*runtime.Poller[SolutionTemplateVersionsClientBulkPublishSolutionResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.bulkPublishSolution(ctx, resourceGroupName, solutionTemplateName, solutionTemplateVersionName, body, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[SolutionTemplateVersionsClientBulkPublishSolutionResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[SolutionTemplateVersionsClientBulkPublishSolutionResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// BulkPublishSolution - Post request for bulk publish
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-06-01
func (client *SolutionTemplateVersionsClient) bulkPublishSolution(ctx context.Context, resourceGroupName string, solutionTemplateName string, solutionTemplateVersionName string, body BulkPublishSolutionParameter, options *SolutionTemplateVersionsClientBeginBulkPublishSolutionOptions) (*http.Response, error) {
	var err error
	const operationName = "SolutionTemplateVersionsClient.BeginBulkPublishSolution"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.bulkPublishSolutionCreateRequest(ctx, resourceGroupName, solutionTemplateName, solutionTemplateVersionName, body, 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.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// bulkPublishSolutionCreateRequest creates the BulkPublishSolution request.
func (client *SolutionTemplateVersionsClient) bulkPublishSolutionCreateRequest(ctx context.Context, resourceGroupName string, solutionTemplateName string, solutionTemplateVersionName string, body BulkPublishSolutionParameter, _ *SolutionTemplateVersionsClientBeginBulkPublishSolutionOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/solutionTemplates/{solutionTemplateName}/versions/{solutionTemplateVersionName}/bulkPublishSolution"
	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 solutionTemplateName == "" {
		return nil, errors.New("parameter solutionTemplateName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{solutionTemplateName}", url.PathEscape(solutionTemplateName))
	if solutionTemplateVersionName == "" {
		return nil, errors.New("parameter solutionTemplateVersionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{solutionTemplateVersionName}", url.PathEscape(solutionTemplateVersionName))
	req, err := runtime.NewRequest(ctx, http.MethodPost, 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["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, body); err != nil {
		return nil, err
	}
	return req, nil
}

// Get - Get a Solution 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.
//   - solutionTemplateName - The name of the SolutionTemplate
//   - solutionTemplateVersionName - The name of the SolutionTemplateVersion
//   - options - SolutionTemplateVersionsClientGetOptions contains the optional parameters for the SolutionTemplateVersionsClient.Get
//     method.
func (client *SolutionTemplateVersionsClient) Get(ctx context.Context, resourceGroupName string, solutionTemplateName string, solutionTemplateVersionName string, options *SolutionTemplateVersionsClientGetOptions) (SolutionTemplateVersionsClientGetResponse, error) {
	var err error
	const operationName = "SolutionTemplateVersionsClient.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, solutionTemplateName, solutionTemplateVersionName, options)
	if err != nil {
		return SolutionTemplateVersionsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return SolutionTemplateVersionsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return SolutionTemplateVersionsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *SolutionTemplateVersionsClient) getCreateRequest(ctx context.Context, resourceGroupName string, solutionTemplateName string, solutionTemplateVersionName string, _ *SolutionTemplateVersionsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/solutionTemplates/{solutionTemplateName}/versions/{solutionTemplateVersionName}"
	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 solutionTemplateName == "" {
		return nil, errors.New("parameter solutionTemplateName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{solutionTemplateName}", url.PathEscape(solutionTemplateName))
	if solutionTemplateVersionName == "" {
		return nil, errors.New("parameter solutionTemplateVersionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{solutionTemplateVersionName}", url.PathEscape(solutionTemplateVersionName))
	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 *SolutionTemplateVersionsClient) getHandleResponse(resp *http.Response) (SolutionTemplateVersionsClientGetResponse, error) {
	result := SolutionTemplateVersionsClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.SolutionTemplateVersion); err != nil {
		return SolutionTemplateVersionsClientGetResponse{}, err
	}
	return result, nil
}

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

// listBySolutionTemplateCreateRequest creates the ListBySolutionTemplate request.
func (client *SolutionTemplateVersionsClient) listBySolutionTemplateCreateRequest(ctx context.Context, resourceGroupName string, solutionTemplateName string, _ *SolutionTemplateVersionsClientListBySolutionTemplateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/solutionTemplates/{solutionTemplateName}/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 solutionTemplateName == "" {
		return nil, errors.New("parameter solutionTemplateName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{solutionTemplateName}", url.PathEscape(solutionTemplateName))
	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
}

// listBySolutionTemplateHandleResponse handles the ListBySolutionTemplate response.
func (client *SolutionTemplateVersionsClient) listBySolutionTemplateHandleResponse(resp *http.Response) (SolutionTemplateVersionsClientListBySolutionTemplateResponse, error) {
	result := SolutionTemplateVersionsClientListBySolutionTemplateResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.SolutionTemplateVersionListResult); err != nil {
		return SolutionTemplateVersionsClientListBySolutionTemplateResponse{}, err
	}
	return result, nil
}
