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

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

// SolutionConfigurationsClient contains the methods for the SolutionConfigurations group.
// Don't use this type directly, use NewSolutionConfigurationsClient() instead.
type SolutionConfigurationsClient struct {
	internal *arm.Client
}

// NewSolutionConfigurationsClient creates a new instance of SolutionConfigurationsClient with the specified values.
//   - credential - used to authorize requests. Usually a credential from azidentity.
//   - options - pass nil to accept the default values.
func NewSolutionConfigurationsClient(credential azcore.TokenCredential, options *arm.ClientOptions) (*SolutionConfigurationsClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &SolutionConfigurationsClient{
		internal: cl,
	}
	return client, nil
}

// CreateOrUpdate - Create a SolutionConfiguration
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-12-01
//   - resourceURI - The fully qualified Azure Resource manager identifier of the resource.
//   - solutionConfiguration - Represent Solution Configuration Resource.
//   - resource - Resource create parameters.
//   - options - SolutionConfigurationsClientCreateOrUpdateOptions contains the optional parameters for the SolutionConfigurationsClient.CreateOrUpdate
//     method.
func (client *SolutionConfigurationsClient) CreateOrUpdate(ctx context.Context, resourceURI string, solutionConfiguration string, resource SolutionConfiguration, options *SolutionConfigurationsClientCreateOrUpdateOptions) (SolutionConfigurationsClientCreateOrUpdateResponse, error) {
	var err error
	const operationName = "SolutionConfigurationsClient.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, resourceURI, solutionConfiguration, resource, options)
	if err != nil {
		return SolutionConfigurationsClientCreateOrUpdateResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return SolutionConfigurationsClientCreateOrUpdateResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
		err = runtime.NewResponseError(httpResp)
		return SolutionConfigurationsClientCreateOrUpdateResponse{}, err
	}
	resp, err := client.createOrUpdateHandleResponse(httpResp)
	return resp, err
}

// createOrUpdateCreateRequest creates the CreateOrUpdate request.
func (client *SolutionConfigurationsClient) createOrUpdateCreateRequest(ctx context.Context, resourceURI string, solutionConfiguration string, resource SolutionConfiguration, _ *SolutionConfigurationsClientCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/{resourceUri}/providers/Microsoft.HybridConnectivity/solutionConfigurations/{solutionConfiguration}"
	urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI)
	if solutionConfiguration == "" {
		return nil, errors.New("parameter solutionConfiguration cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{solutionConfiguration}", url.PathEscape(solutionConfiguration))
	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-12-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
}

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

// Delete - Delete a SolutionConfiguration
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-12-01
//   - resourceURI - The fully qualified Azure Resource manager identifier of the resource.
//   - solutionConfiguration - Represent Solution Configuration Resource.
//   - options - SolutionConfigurationsClientDeleteOptions contains the optional parameters for the SolutionConfigurationsClient.Delete
//     method.
func (client *SolutionConfigurationsClient) Delete(ctx context.Context, resourceURI string, solutionConfiguration string, options *SolutionConfigurationsClientDeleteOptions) (SolutionConfigurationsClientDeleteResponse, error) {
	var err error
	const operationName = "SolutionConfigurationsClient.Delete"
	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, resourceURI, solutionConfiguration, options)
	if err != nil {
		return SolutionConfigurationsClientDeleteResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return SolutionConfigurationsClientDeleteResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return SolutionConfigurationsClientDeleteResponse{}, err
	}
	return SolutionConfigurationsClientDeleteResponse{}, nil
}

// deleteCreateRequest creates the Delete request.
func (client *SolutionConfigurationsClient) deleteCreateRequest(ctx context.Context, resourceURI string, solutionConfiguration string, _ *SolutionConfigurationsClientDeleteOptions) (*policy.Request, error) {
	urlPath := "/{resourceUri}/providers/Microsoft.HybridConnectivity/solutionConfigurations/{solutionConfiguration}"
	urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI)
	if solutionConfiguration == "" {
		return nil, errors.New("parameter solutionConfiguration cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{solutionConfiguration}", url.PathEscape(solutionConfiguration))
	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", "2024-12-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// Get - Get a SolutionConfiguration
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-12-01
//   - resourceURI - The fully qualified Azure Resource manager identifier of the resource.
//   - solutionConfiguration - Represent Solution Configuration Resource.
//   - options - SolutionConfigurationsClientGetOptions contains the optional parameters for the SolutionConfigurationsClient.Get
//     method.
func (client *SolutionConfigurationsClient) Get(ctx context.Context, resourceURI string, solutionConfiguration string, options *SolutionConfigurationsClientGetOptions) (SolutionConfigurationsClientGetResponse, error) {
	var err error
	const operationName = "SolutionConfigurationsClient.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, resourceURI, solutionConfiguration, options)
	if err != nil {
		return SolutionConfigurationsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return SolutionConfigurationsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return SolutionConfigurationsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *SolutionConfigurationsClient) getCreateRequest(ctx context.Context, resourceURI string, solutionConfiguration string, _ *SolutionConfigurationsClientGetOptions) (*policy.Request, error) {
	urlPath := "/{resourceUri}/providers/Microsoft.HybridConnectivity/solutionConfigurations/{solutionConfiguration}"
	urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI)
	if solutionConfiguration == "" {
		return nil, errors.New("parameter solutionConfiguration cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{solutionConfiguration}", url.PathEscape(solutionConfiguration))
	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-12-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// NewListPager - List SolutionConfiguration resources by parent
//
// Generated from API version 2024-12-01
//   - resourceURI - The fully qualified Azure Resource manager identifier of the resource.
//   - options - SolutionConfigurationsClientListOptions contains the optional parameters for the SolutionConfigurationsClient.NewListPager
//     method.
func (client *SolutionConfigurationsClient) NewListPager(resourceURI string, options *SolutionConfigurationsClientListOptions) *runtime.Pager[SolutionConfigurationsClientListResponse] {
	return runtime.NewPager(runtime.PagingHandler[SolutionConfigurationsClientListResponse]{
		More: func(page SolutionConfigurationsClientListResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *SolutionConfigurationsClientListResponse) (SolutionConfigurationsClientListResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "SolutionConfigurationsClient.NewListPager")
			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.listCreateRequest(ctx, resourceURI, options)
			}, nil)
			if err != nil {
				return SolutionConfigurationsClientListResponse{}, err
			}
			return client.listHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listCreateRequest creates the List request.
func (client *SolutionConfigurationsClient) listCreateRequest(ctx context.Context, resourceURI string, _ *SolutionConfigurationsClientListOptions) (*policy.Request, error) {
	urlPath := "/{resourceUri}/providers/Microsoft.HybridConnectivity/solutionConfigurations"
	urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI)
	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-12-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listHandleResponse handles the List response.
func (client *SolutionConfigurationsClient) listHandleResponse(resp *http.Response) (SolutionConfigurationsClientListResponse, error) {
	result := SolutionConfigurationsClientListResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.SolutionConfigurationListResult); err != nil {
		return SolutionConfigurationsClientListResponse{}, err
	}
	return result, nil
}

// BeginSyncNow - Trigger immediate sync with source cloud
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-12-01
//   - resourceURI - The fully qualified Azure Resource manager identifier of the resource.
//   - solutionConfiguration - Represent Solution Configuration Resource.
//   - options - SolutionConfigurationsClientBeginSyncNowOptions contains the optional parameters for the SolutionConfigurationsClient.BeginSyncNow
//     method.
func (client *SolutionConfigurationsClient) BeginSyncNow(ctx context.Context, resourceURI string, solutionConfiguration string, options *SolutionConfigurationsClientBeginSyncNowOptions) (*runtime.Poller[SolutionConfigurationsClientSyncNowResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.syncNow(ctx, resourceURI, solutionConfiguration, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[SolutionConfigurationsClientSyncNowResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[SolutionConfigurationsClientSyncNowResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// SyncNow - Trigger immediate sync with source cloud
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-12-01
func (client *SolutionConfigurationsClient) syncNow(ctx context.Context, resourceURI string, solutionConfiguration string, options *SolutionConfigurationsClientBeginSyncNowOptions) (*http.Response, error) {
	var err error
	const operationName = "SolutionConfigurationsClient.BeginSyncNow"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.syncNowCreateRequest(ctx, resourceURI, solutionConfiguration, 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
}

// syncNowCreateRequest creates the SyncNow request.
func (client *SolutionConfigurationsClient) syncNowCreateRequest(ctx context.Context, resourceURI string, solutionConfiguration string, _ *SolutionConfigurationsClientBeginSyncNowOptions) (*policy.Request, error) {
	urlPath := "/{resourceUri}/providers/Microsoft.HybridConnectivity/solutionConfigurations/{solutionConfiguration}/syncNow"
	urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI)
	if solutionConfiguration == "" {
		return nil, errors.New("parameter solutionConfiguration cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{solutionConfiguration}", url.PathEscape(solutionConfiguration))
	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-12-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// Update - Update a SolutionConfiguration
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-12-01
//   - resourceURI - The fully qualified Azure Resource manager identifier of the resource.
//   - solutionConfiguration - Represent Solution Configuration Resource.
//   - properties - The resource properties to be updated.
//   - options - SolutionConfigurationsClientUpdateOptions contains the optional parameters for the SolutionConfigurationsClient.Update
//     method.
func (client *SolutionConfigurationsClient) Update(ctx context.Context, resourceURI string, solutionConfiguration string, properties SolutionConfiguration, options *SolutionConfigurationsClientUpdateOptions) (SolutionConfigurationsClientUpdateResponse, error) {
	var err error
	const operationName = "SolutionConfigurationsClient.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, resourceURI, solutionConfiguration, properties, options)
	if err != nil {
		return SolutionConfigurationsClientUpdateResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return SolutionConfigurationsClientUpdateResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return SolutionConfigurationsClientUpdateResponse{}, err
	}
	resp, err := client.updateHandleResponse(httpResp)
	return resp, err
}

// updateCreateRequest creates the Update request.
func (client *SolutionConfigurationsClient) updateCreateRequest(ctx context.Context, resourceURI string, solutionConfiguration string, properties SolutionConfiguration, _ *SolutionConfigurationsClientUpdateOptions) (*policy.Request, error) {
	urlPath := "/{resourceUri}/providers/Microsoft.HybridConnectivity/solutionConfigurations/{solutionConfiguration}"
	urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI)
	if solutionConfiguration == "" {
		return nil, errors.New("parameter solutionConfiguration cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{solutionConfiguration}", url.PathEscape(solutionConfiguration))
	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", "2024-12-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 *SolutionConfigurationsClient) updateHandleResponse(resp *http.Response) (SolutionConfigurationsClientUpdateResponse, error) {
	result := SolutionConfigurationsClientUpdateResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.SolutionConfiguration); err != nil {
		return SolutionConfigurationsClientUpdateResponse{}, err
	}
	return result, nil
}
