// 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) AutoRest Code Generator. DO NOT EDIT.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

package armproviderhub

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

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

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

// CreateOrUpdate - Creates or updates a new region frontload release.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-09-01
//   - providerNamespace - The name of the resource provider hosted within ProviderHub.
//   - releaseName - The name of the release.
//   - options - NewRegionFrontloadReleaseClientCreateOrUpdateOptions contains the optional parameters for the NewRegionFrontloadReleaseClient.CreateOrUpdate
//     method.
func (client *NewRegionFrontloadReleaseClient) CreateOrUpdate(ctx context.Context, providerNamespace string, releaseName string, properties FrontloadPayload, options *NewRegionFrontloadReleaseClientCreateOrUpdateOptions) (NewRegionFrontloadReleaseClientCreateOrUpdateResponse, error) {
	var err error
	const operationName = "NewRegionFrontloadReleaseClient.CreateOrUpdate"
	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, providerNamespace, releaseName, properties, options)
	if err != nil {
		return NewRegionFrontloadReleaseClientCreateOrUpdateResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return NewRegionFrontloadReleaseClientCreateOrUpdateResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
		err = runtime.NewResponseError(httpResp)
		return NewRegionFrontloadReleaseClientCreateOrUpdateResponse{}, err
	}
	resp, err := client.createOrUpdateHandleResponse(httpResp)
	return resp, err
}

// createOrUpdateCreateRequest creates the CreateOrUpdate request.
func (client *NewRegionFrontloadReleaseClient) createOrUpdateCreateRequest(ctx context.Context, providerNamespace string, releaseName string, properties FrontloadPayload, _ *NewRegionFrontloadReleaseClientCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.ProviderHub/providerRegistrations/{providerNamespace}/newRegionFrontloadRelease/{releaseName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if providerNamespace == "" {
		return nil, errors.New("parameter providerNamespace cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{providerNamespace}", url.PathEscape(providerNamespace))
	if releaseName == "" {
		return nil, errors.New("parameter releaseName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{releaseName}", url.PathEscape(releaseName))
	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", "2024-09-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, properties); err != nil {
		return nil, err
	}
	return req, nil
}

// createOrUpdateHandleResponse handles the CreateOrUpdate response.
func (client *NewRegionFrontloadReleaseClient) createOrUpdateHandleResponse(resp *http.Response) (NewRegionFrontloadReleaseClientCreateOrUpdateResponse, error) {
	result := NewRegionFrontloadReleaseClientCreateOrUpdateResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.DefaultRollout); err != nil {
		return NewRegionFrontloadReleaseClientCreateOrUpdateResponse{}, err
	}
	return result, nil
}

// GenerateManifest - Generates the new region frontload manifest.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-09-01
//   - providerNamespace - The name of the resource provider hosted within ProviderHub.
//   - options - NewRegionFrontloadReleaseClientGenerateManifestOptions contains the optional parameters for the NewRegionFrontloadReleaseClient.GenerateManifest
//     method.
func (client *NewRegionFrontloadReleaseClient) GenerateManifest(ctx context.Context, providerNamespace string, properties FrontloadPayload, options *NewRegionFrontloadReleaseClientGenerateManifestOptions) (NewRegionFrontloadReleaseClientGenerateManifestResponse, error) {
	var err error
	const operationName = "NewRegionFrontloadReleaseClient.GenerateManifest"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.generateManifestCreateRequest(ctx, providerNamespace, properties, options)
	if err != nil {
		return NewRegionFrontloadReleaseClientGenerateManifestResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return NewRegionFrontloadReleaseClientGenerateManifestResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return NewRegionFrontloadReleaseClientGenerateManifestResponse{}, err
	}
	resp, err := client.generateManifestHandleResponse(httpResp)
	return resp, err
}

// generateManifestCreateRequest creates the GenerateManifest request.
func (client *NewRegionFrontloadReleaseClient) generateManifestCreateRequest(ctx context.Context, providerNamespace string, properties FrontloadPayload, _ *NewRegionFrontloadReleaseClientGenerateManifestOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.ProviderHub/providerRegistrations/{providerNamespace}/generateNewRegionFrontloadManifest"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if providerNamespace == "" {
		return nil, errors.New("parameter providerNamespace cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{providerNamespace}", url.PathEscape(providerNamespace))
	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", "2024-09-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, properties); err != nil {
		return nil, err
	}
	return req, nil
}

// generateManifestHandleResponse handles the GenerateManifest response.
func (client *NewRegionFrontloadReleaseClient) generateManifestHandleResponse(resp *http.Response) (NewRegionFrontloadReleaseClientGenerateManifestResponse, error) {
	result := NewRegionFrontloadReleaseClientGenerateManifestResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ResourceProviderManifest); err != nil {
		return NewRegionFrontloadReleaseClientGenerateManifestResponse{}, err
	}
	return result, nil
}

// Get - Gets a new region frontload release.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-09-01
//   - providerNamespace - The name of the resource provider hosted within ProviderHub.
//   - releaseName - The name of the release.
//   - options - NewRegionFrontloadReleaseClientGetOptions contains the optional parameters for the NewRegionFrontloadReleaseClient.Get
//     method.
func (client *NewRegionFrontloadReleaseClient) Get(ctx context.Context, providerNamespace string, releaseName string, options *NewRegionFrontloadReleaseClientGetOptions) (NewRegionFrontloadReleaseClientGetResponse, error) {
	var err error
	const operationName = "NewRegionFrontloadReleaseClient.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, providerNamespace, releaseName, options)
	if err != nil {
		return NewRegionFrontloadReleaseClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return NewRegionFrontloadReleaseClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return NewRegionFrontloadReleaseClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *NewRegionFrontloadReleaseClient) getCreateRequest(ctx context.Context, providerNamespace string, releaseName string, _ *NewRegionFrontloadReleaseClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.ProviderHub/providerRegistrations/{providerNamespace}/newRegionFrontloadRelease/{releaseName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if providerNamespace == "" {
		return nil, errors.New("parameter providerNamespace cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{providerNamespace}", url.PathEscape(providerNamespace))
	if releaseName == "" {
		return nil, errors.New("parameter releaseName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{releaseName}", url.PathEscape(releaseName))
	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-09-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// Stop - Stops a new region frontload release.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-09-01
//   - providerNamespace - The name of the resource provider hosted within ProviderHub.
//   - releaseName - The name of the release.
//   - options - NewRegionFrontloadReleaseClientStopOptions contains the optional parameters for the NewRegionFrontloadReleaseClient.Stop
//     method.
func (client *NewRegionFrontloadReleaseClient) Stop(ctx context.Context, providerNamespace string, releaseName string, options *NewRegionFrontloadReleaseClientStopOptions) (NewRegionFrontloadReleaseClientStopResponse, error) {
	var err error
	const operationName = "NewRegionFrontloadReleaseClient.Stop"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.stopCreateRequest(ctx, providerNamespace, releaseName, options)
	if err != nil {
		return NewRegionFrontloadReleaseClientStopResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return NewRegionFrontloadReleaseClientStopResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return NewRegionFrontloadReleaseClientStopResponse{}, err
	}
	return NewRegionFrontloadReleaseClientStopResponse{}, nil
}

// stopCreateRequest creates the Stop request.
func (client *NewRegionFrontloadReleaseClient) stopCreateRequest(ctx context.Context, providerNamespace string, releaseName string, _ *NewRegionFrontloadReleaseClientStopOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.ProviderHub/providerRegistrations/{providerNamespace}/newRegionFrontloadRelease/{releaseName}/stop"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if providerNamespace == "" {
		return nil, errors.New("parameter providerNamespace cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{providerNamespace}", url.PathEscape(providerNamespace))
	if releaseName == "" {
		return nil, errors.New("parameter releaseName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{releaseName}", url.PathEscape(releaseName))
	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", "2024-09-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}
