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

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

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

// BeginCreateOrUpdate - Get Site Reference 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.
//   - siteReferenceName - The name of the SiteReference
//   - resource - Resource create parameters.
//   - options - SiteReferencesClientBeginCreateOrUpdateOptions contains the optional parameters for the SiteReferencesClient.BeginCreateOrUpdate
//     method.
func (client *SiteReferencesClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, contextName string, siteReferenceName string, resource SiteReference, options *SiteReferencesClientBeginCreateOrUpdateOptions) (*runtime.Poller[SiteReferencesClientCreateOrUpdateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.createOrUpdate(ctx, resourceGroupName, contextName, siteReferenceName, resource, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[SiteReferencesClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[SiteReferencesClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// CreateOrUpdate - Get Site Reference Resource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-06-01
func (client *SiteReferencesClient) createOrUpdate(ctx context.Context, resourceGroupName string, contextName string, siteReferenceName string, resource SiteReference, options *SiteReferencesClientBeginCreateOrUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "SiteReferencesClient.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, siteReferenceName, 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 *SiteReferencesClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, contextName string, siteReferenceName string, resource SiteReference, _ *SiteReferencesClientBeginCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/contexts/{contextName}/siteReferences/{siteReferenceName}"
	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 siteReferenceName == "" {
		return nil, errors.New("parameter siteReferenceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{siteReferenceName}", url.PathEscape(siteReferenceName))
	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 - Get Site Reference 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.
//   - siteReferenceName - The name of the SiteReference
//   - options - SiteReferencesClientBeginDeleteOptions contains the optional parameters for the SiteReferencesClient.BeginDelete
//     method.
func (client *SiteReferencesClient) BeginDelete(ctx context.Context, resourceGroupName string, contextName string, siteReferenceName string, options *SiteReferencesClientBeginDeleteOptions) (*runtime.Poller[SiteReferencesClientDeleteResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.deleteOperation(ctx, resourceGroupName, contextName, siteReferenceName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[SiteReferencesClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[SiteReferencesClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

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

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

// NewListByContextPager - List Site Reference 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.
//   - options - SiteReferencesClientListByContextOptions contains the optional parameters for the SiteReferencesClient.NewListByContextPager
//     method.
func (client *SiteReferencesClient) NewListByContextPager(resourceGroupName string, contextName string, options *SiteReferencesClientListByContextOptions) *runtime.Pager[SiteReferencesClientListByContextResponse] {
	return runtime.NewPager(runtime.PagingHandler[SiteReferencesClientListByContextResponse]{
		More: func(page SiteReferencesClientListByContextResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *SiteReferencesClientListByContextResponse) (SiteReferencesClientListByContextResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "SiteReferencesClient.NewListByContextPager")
			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.listByContextCreateRequest(ctx, resourceGroupName, contextName, options)
			}, nil)
			if err != nil {
				return SiteReferencesClientListByContextResponse{}, err
			}
			return client.listByContextHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByContextCreateRequest creates the ListByContext request.
func (client *SiteReferencesClient) listByContextCreateRequest(ctx context.Context, resourceGroupName string, contextName string, _ *SiteReferencesClientListByContextOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/contexts/{contextName}/siteReferences"
	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))
	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
}

// listByContextHandleResponse handles the ListByContext response.
func (client *SiteReferencesClient) listByContextHandleResponse(resp *http.Response) (SiteReferencesClientListByContextResponse, error) {
	result := SiteReferencesClientListByContextResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.SiteReferenceListResult); err != nil {
		return SiteReferencesClientListByContextResponse{}, err
	}
	return result, nil
}

// BeginUpdate - Get Site Reference 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.
//   - siteReferenceName - The name of the SiteReference
//   - properties - The resource properties to be updated.
//   - options - SiteReferencesClientBeginUpdateOptions contains the optional parameters for the SiteReferencesClient.BeginUpdate
//     method.
func (client *SiteReferencesClient) BeginUpdate(ctx context.Context, resourceGroupName string, contextName string, siteReferenceName string, properties SiteReference, options *SiteReferencesClientBeginUpdateOptions) (*runtime.Poller[SiteReferencesClientUpdateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.update(ctx, resourceGroupName, contextName, siteReferenceName, properties, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[SiteReferencesClientUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[SiteReferencesClientUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Update - Get Site Reference Resource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-06-01
func (client *SiteReferencesClient) update(ctx context.Context, resourceGroupName string, contextName string, siteReferenceName string, properties SiteReference, options *SiteReferencesClientBeginUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "SiteReferencesClient.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, siteReferenceName, 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 *SiteReferencesClient) updateCreateRequest(ctx context.Context, resourceGroupName string, contextName string, siteReferenceName string, properties SiteReference, _ *SiteReferencesClientBeginUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/contexts/{contextName}/siteReferences/{siteReferenceName}"
	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 siteReferenceName == "" {
		return nil, errors.New("parameter siteReferenceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{siteReferenceName}", url.PathEscape(siteReferenceName))
	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
}
