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

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

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

// BeginCreateOrUpdate - Create or update Execution 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.
//   - contextName - The name of the Context.
//   - workflowName - Name of the workflow
//   - versionName - The name of the workflowVersion.
//   - executionName - The name of the Execution.
//   - resource - Resource create parameters.
//   - options - ExecutionsClientBeginCreateOrUpdateOptions contains the optional parameters for the ExecutionsClient.BeginCreateOrUpdate
//     method.
func (client *ExecutionsClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, contextName string, workflowName string, versionName string, executionName string, resource Execution, options *ExecutionsClientBeginCreateOrUpdateOptions) (*runtime.Poller[ExecutionsClientCreateOrUpdateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.createOrUpdate(ctx, resourceGroupName, contextName, workflowName, versionName, executionName, resource, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[ExecutionsClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[ExecutionsClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// CreateOrUpdate - Create or update Execution Resource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-06-01
func (client *ExecutionsClient) createOrUpdate(ctx context.Context, resourceGroupName string, contextName string, workflowName string, versionName string, executionName string, resource Execution, options *ExecutionsClientBeginCreateOrUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "ExecutionsClient.BeginCreateOrUpdate"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, contextName, workflowName, versionName, executionName, resource, 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.StatusOK, http.StatusCreated) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// createOrUpdateCreateRequest creates the CreateOrUpdate request.
func (client *ExecutionsClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, contextName string, workflowName string, versionName string, executionName string, resource Execution, _ *ExecutionsClientBeginCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/contexts/{contextName}/workflows/{workflowName}/versions/{versionName}/executions/{executionName}"
	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 contextName == "" {
		return nil, errors.New("parameter contextName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{contextName}", url.PathEscape(contextName))
	if workflowName == "" {
		return nil, errors.New("parameter workflowName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{workflowName}", url.PathEscape(workflowName))
	if versionName == "" {
		return nil, errors.New("parameter versionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{versionName}", url.PathEscape(versionName))
	if executionName == "" {
		return nil, errors.New("parameter executionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{executionName}", url.PathEscape(executionName))
	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", "2025-06-01")
	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
}

// BeginDelete - Delete Execution 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.
//   - contextName - The name of the Context.
//   - workflowName - Name of the workflow
//   - versionName - The name of the workflowVersion.
//   - executionName - The name of the Execution.
//   - options - ExecutionsClientBeginDeleteOptions contains the optional parameters for the ExecutionsClient.BeginDelete method.
func (client *ExecutionsClient) BeginDelete(ctx context.Context, resourceGroupName string, contextName string, workflowName string, versionName string, executionName string, options *ExecutionsClientBeginDeleteOptions) (*runtime.Poller[ExecutionsClientDeleteResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.deleteOperation(ctx, resourceGroupName, contextName, workflowName, versionName, executionName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[ExecutionsClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[ExecutionsClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Delete - Delete Execution Resource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-06-01
func (client *ExecutionsClient) deleteOperation(ctx context.Context, resourceGroupName string, contextName string, workflowName string, versionName string, executionName string, options *ExecutionsClientBeginDeleteOptions) (*http.Response, error) {
	var err error
	const operationName = "ExecutionsClient.BeginDelete"
	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, resourceGroupName, contextName, workflowName, versionName, executionName, 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, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// deleteCreateRequest creates the Delete request.
func (client *ExecutionsClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, contextName string, workflowName string, versionName string, executionName string, _ *ExecutionsClientBeginDeleteOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/contexts/{contextName}/workflows/{workflowName}/versions/{versionName}/executions/{executionName}"
	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 contextName == "" {
		return nil, errors.New("parameter contextName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{contextName}", url.PathEscape(contextName))
	if workflowName == "" {
		return nil, errors.New("parameter workflowName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{workflowName}", url.PathEscape(workflowName))
	if versionName == "" {
		return nil, errors.New("parameter versionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{versionName}", url.PathEscape(versionName))
	if executionName == "" {
		return nil, errors.New("parameter executionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{executionName}", url.PathEscape(executionName))
	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", "2025-06-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	return req, nil
}

// Get - Get Execution 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.
//   - contextName - The name of the Context.
//   - workflowName - Name of the workflow
//   - versionName - The name of the workflowVersion.
//   - executionName - The name of the Execution.
//   - options - ExecutionsClientGetOptions contains the optional parameters for the ExecutionsClient.Get method.
func (client *ExecutionsClient) Get(ctx context.Context, resourceGroupName string, contextName string, workflowName string, versionName string, executionName string, options *ExecutionsClientGetOptions) (ExecutionsClientGetResponse, error) {
	var err error
	const operationName = "ExecutionsClient.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, contextName, workflowName, versionName, executionName, options)
	if err != nil {
		return ExecutionsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ExecutionsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ExecutionsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *ExecutionsClient) getCreateRequest(ctx context.Context, resourceGroupName string, contextName string, workflowName string, versionName string, executionName string, _ *ExecutionsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/contexts/{contextName}/workflows/{workflowName}/versions/{versionName}/executions/{executionName}"
	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 contextName == "" {
		return nil, errors.New("parameter contextName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{contextName}", url.PathEscape(contextName))
	if workflowName == "" {
		return nil, errors.New("parameter workflowName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{workflowName}", url.PathEscape(workflowName))
	if versionName == "" {
		return nil, errors.New("parameter versionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{versionName}", url.PathEscape(versionName))
	if executionName == "" {
		return nil, errors.New("parameter executionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{executionName}", url.PathEscape(executionName))
	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 *ExecutionsClient) getHandleResponse(resp *http.Response) (ExecutionsClientGetResponse, error) {
	result := ExecutionsClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.Execution); err != nil {
		return ExecutionsClientGetResponse{}, err
	}
	return result, nil
}

// NewListByWorkflowVersionPager - List Execution Resources
//
// Generated from API version 2025-06-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - contextName - The name of the Context.
//   - workflowName - Name of the workflow
//   - versionName - The name of the workflowVersion.
//   - options - ExecutionsClientListByWorkflowVersionOptions contains the optional parameters for the ExecutionsClient.NewListByWorkflowVersionPager
//     method.
func (client *ExecutionsClient) NewListByWorkflowVersionPager(resourceGroupName string, contextName string, workflowName string, versionName string, options *ExecutionsClientListByWorkflowVersionOptions) *runtime.Pager[ExecutionsClientListByWorkflowVersionResponse] {
	return runtime.NewPager(runtime.PagingHandler[ExecutionsClientListByWorkflowVersionResponse]{
		More: func(page ExecutionsClientListByWorkflowVersionResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *ExecutionsClientListByWorkflowVersionResponse) (ExecutionsClientListByWorkflowVersionResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "ExecutionsClient.NewListByWorkflowVersionPager")
			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.listByWorkflowVersionCreateRequest(ctx, resourceGroupName, contextName, workflowName, versionName, options)
			}, nil)
			if err != nil {
				return ExecutionsClientListByWorkflowVersionResponse{}, err
			}
			return client.listByWorkflowVersionHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByWorkflowVersionCreateRequest creates the ListByWorkflowVersion request.
func (client *ExecutionsClient) listByWorkflowVersionCreateRequest(ctx context.Context, resourceGroupName string, contextName string, workflowName string, versionName string, _ *ExecutionsClientListByWorkflowVersionOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/contexts/{contextName}/workflows/{workflowName}/versions/{versionName}/executions"
	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 contextName == "" {
		return nil, errors.New("parameter contextName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{contextName}", url.PathEscape(contextName))
	if workflowName == "" {
		return nil, errors.New("parameter workflowName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{workflowName}", url.PathEscape(workflowName))
	if versionName == "" {
		return nil, errors.New("parameter versionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{versionName}", url.PathEscape(versionName))
	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
}

// listByWorkflowVersionHandleResponse handles the ListByWorkflowVersion response.
func (client *ExecutionsClient) listByWorkflowVersionHandleResponse(resp *http.Response) (ExecutionsClientListByWorkflowVersionResponse, error) {
	result := ExecutionsClientListByWorkflowVersionResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ExecutionListResult); err != nil {
		return ExecutionsClientListByWorkflowVersionResponse{}, err
	}
	return result, nil
}

// BeginUpdate - update an Execution 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.
//   - contextName - The name of the Context.
//   - workflowName - Name of the workflow
//   - versionName - The name of the workflowVersion.
//   - executionName - The name of the Execution.
//   - properties - The resource properties to be updated.
//   - options - ExecutionsClientBeginUpdateOptions contains the optional parameters for the ExecutionsClient.BeginUpdate method.
func (client *ExecutionsClient) BeginUpdate(ctx context.Context, resourceGroupName string, contextName string, workflowName string, versionName string, executionName string, properties Execution, options *ExecutionsClientBeginUpdateOptions) (*runtime.Poller[ExecutionsClientUpdateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.update(ctx, resourceGroupName, contextName, workflowName, versionName, executionName, properties, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[ExecutionsClientUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[ExecutionsClientUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Update - update an Execution Resource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-06-01
func (client *ExecutionsClient) update(ctx context.Context, resourceGroupName string, contextName string, workflowName string, versionName string, executionName string, properties Execution, options *ExecutionsClientBeginUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "ExecutionsClient.BeginUpdate"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.updateCreateRequest(ctx, resourceGroupName, contextName, workflowName, versionName, executionName, properties, 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.StatusOK, http.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// updateCreateRequest creates the Update request.
func (client *ExecutionsClient) updateCreateRequest(ctx context.Context, resourceGroupName string, contextName string, workflowName string, versionName string, executionName string, properties Execution, _ *ExecutionsClientBeginUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/contexts/{contextName}/workflows/{workflowName}/versions/{versionName}/executions/{executionName}"
	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 contextName == "" {
		return nil, errors.New("parameter contextName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{contextName}", url.PathEscape(contextName))
	if workflowName == "" {
		return nil, errors.New("parameter workflowName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{workflowName}", url.PathEscape(workflowName))
	if versionName == "" {
		return nil, errors.New("parameter versionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{versionName}", url.PathEscape(versionName))
	if executionName == "" {
		return nil, errors.New("parameter executionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{executionName}", url.PathEscape(executionName))
	req, err := runtime.NewRequest(ctx, http.MethodPatch, 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"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, properties); err != nil {
		return nil, err
	}
	return req, nil
}
