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

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

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

// BeginCreateOrUpdate - Create or update a Schema 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.
//   - schemaName - The name of the Schema
//   - schemaVersionName - The name of the SchemaVersion
//   - resource - Resource create parameters.
//   - options - SchemaVersionsClientBeginCreateOrUpdateOptions contains the optional parameters for the SchemaVersionsClient.BeginCreateOrUpdate
//     method.
func (client *SchemaVersionsClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, schemaName string, schemaVersionName string, resource SchemaVersion, options *SchemaVersionsClientBeginCreateOrUpdateOptions) (*runtime.Poller[SchemaVersionsClientCreateOrUpdateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.createOrUpdate(ctx, resourceGroupName, schemaName, schemaVersionName, resource, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[SchemaVersionsClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[SchemaVersionsClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// CreateOrUpdate - Create or update a Schema Version Resource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-06-01
func (client *SchemaVersionsClient) createOrUpdate(ctx context.Context, resourceGroupName string, schemaName string, schemaVersionName string, resource SchemaVersion, options *SchemaVersionsClientBeginCreateOrUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "SchemaVersionsClient.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, schemaName, schemaVersionName, 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 *SchemaVersionsClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, schemaName string, schemaVersionName string, resource SchemaVersion, _ *SchemaVersionsClientBeginCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/schemas/{schemaName}/versions/{schemaVersionName}"
	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 schemaName == "" {
		return nil, errors.New("parameter schemaName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{schemaName}", url.PathEscape(schemaName))
	if schemaVersionName == "" {
		return nil, errors.New("parameter schemaVersionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{schemaVersionName}", url.PathEscape(schemaVersionName))
	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 a Schema 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.
//   - schemaName - The name of the Schema
//   - schemaVersionName - The name of the SchemaVersion
//   - options - SchemaVersionsClientBeginDeleteOptions contains the optional parameters for the SchemaVersionsClient.BeginDelete
//     method.
func (client *SchemaVersionsClient) BeginDelete(ctx context.Context, resourceGroupName string, schemaName string, schemaVersionName string, options *SchemaVersionsClientBeginDeleteOptions) (*runtime.Poller[SchemaVersionsClientDeleteResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.deleteOperation(ctx, resourceGroupName, schemaName, schemaVersionName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[SchemaVersionsClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[SchemaVersionsClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Delete - Delete a Schema Version Resource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-06-01
func (client *SchemaVersionsClient) deleteOperation(ctx context.Context, resourceGroupName string, schemaName string, schemaVersionName string, options *SchemaVersionsClientBeginDeleteOptions) (*http.Response, error) {
	var err error
	const operationName = "SchemaVersionsClient.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, schemaName, schemaVersionName, 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 *SchemaVersionsClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, schemaName string, schemaVersionName string, _ *SchemaVersionsClientBeginDeleteOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/schemas/{schemaName}/versions/{schemaVersionName}"
	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 schemaName == "" {
		return nil, errors.New("parameter schemaName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{schemaName}", url.PathEscape(schemaName))
	if schemaVersionName == "" {
		return nil, errors.New("parameter schemaVersionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{schemaVersionName}", url.PathEscape(schemaVersionName))
	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 a Schema 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.
//   - schemaName - The name of the Schema
//   - schemaVersionName - The name of the SchemaVersion
//   - options - SchemaVersionsClientGetOptions contains the optional parameters for the SchemaVersionsClient.Get method.
func (client *SchemaVersionsClient) Get(ctx context.Context, resourceGroupName string, schemaName string, schemaVersionName string, options *SchemaVersionsClientGetOptions) (SchemaVersionsClientGetResponse, error) {
	var err error
	const operationName = "SchemaVersionsClient.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, schemaName, schemaVersionName, options)
	if err != nil {
		return SchemaVersionsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return SchemaVersionsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return SchemaVersionsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

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

// NewListBySchemaPager - List by specified resource group
//
// Generated from API version 2025-06-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - schemaName - The name of the Schema
//   - options - SchemaVersionsClientListBySchemaOptions contains the optional parameters for the SchemaVersionsClient.NewListBySchemaPager
//     method.
func (client *SchemaVersionsClient) NewListBySchemaPager(resourceGroupName string, schemaName string, options *SchemaVersionsClientListBySchemaOptions) *runtime.Pager[SchemaVersionsClientListBySchemaResponse] {
	return runtime.NewPager(runtime.PagingHandler[SchemaVersionsClientListBySchemaResponse]{
		More: func(page SchemaVersionsClientListBySchemaResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *SchemaVersionsClientListBySchemaResponse) (SchemaVersionsClientListBySchemaResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "SchemaVersionsClient.NewListBySchemaPager")
			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.listBySchemaCreateRequest(ctx, resourceGroupName, schemaName, options)
			}, nil)
			if err != nil {
				return SchemaVersionsClientListBySchemaResponse{}, err
			}
			return client.listBySchemaHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

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

// listBySchemaHandleResponse handles the ListBySchema response.
func (client *SchemaVersionsClient) listBySchemaHandleResponse(resp *http.Response) (SchemaVersionsClientListBySchemaResponse, error) {
	result := SchemaVersionsClientListBySchemaResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.SchemaVersionListResult); err != nil {
		return SchemaVersionsClientListBySchemaResponse{}, err
	}
	return result, nil
}

// Update - update a Schema 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.
//   - schemaName - The name of the Schema
//   - schemaVersionName - The name of the SchemaVersion
//   - properties - The resource properties to be updated.
//   - options - SchemaVersionsClientUpdateOptions contains the optional parameters for the SchemaVersionsClient.Update method.
func (client *SchemaVersionsClient) Update(ctx context.Context, resourceGroupName string, schemaName string, schemaVersionName string, properties SchemaVersion, options *SchemaVersionsClientUpdateOptions) (SchemaVersionsClientUpdateResponse, error) {
	var err error
	const operationName = "SchemaVersionsClient.Update"
	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, schemaName, schemaVersionName, properties, options)
	if err != nil {
		return SchemaVersionsClientUpdateResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return SchemaVersionsClientUpdateResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return SchemaVersionsClientUpdateResponse{}, err
	}
	resp, err := client.updateHandleResponse(httpResp)
	return resp, err
}

// updateCreateRequest creates the Update request.
func (client *SchemaVersionsClient) updateCreateRequest(ctx context.Context, resourceGroupName string, schemaName string, schemaVersionName string, properties SchemaVersion, _ *SchemaVersionsClientUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/schemas/{schemaName}/versions/{schemaVersionName}"
	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 schemaName == "" {
		return nil, errors.New("parameter schemaName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{schemaName}", url.PathEscape(schemaName))
	if schemaVersionName == "" {
		return nil, errors.New("parameter schemaVersionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{schemaVersionName}", url.PathEscape(schemaVersionName))
	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
}

// updateHandleResponse handles the Update response.
func (client *SchemaVersionsClient) updateHandleResponse(resp *http.Response) (SchemaVersionsClientUpdateResponse, error) {
	result := SchemaVersionsClientUpdateResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.SchemaVersion); err != nil {
		return SchemaVersionsClientUpdateResponse{}, err
	}
	return result, nil
}
