// Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.9, generator: @autorest/go@4.0.0-preview.74)
// Changes may cause incorrect behavior and will be lost if the code is regenerated.
// Code generated by @autorest/go. DO NOT EDIT.

package armredhatopenshifthcp

import (
	"context"
	"errors"
	"net/http"
	"net/url"
	"strings"

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

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

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

// BeginCreateOrUpdate - Create a ExternalAuth
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2026-09-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - hcpOpenShiftClusterName - The name of the HcpOpenShiftCluster
//   - externalAuthName - The name of the ExternalAuth
//   - resource - Resource create parameters.
//   - options - ExternalAuthsClientBeginCreateOrUpdateOptions contains the optional parameters for the ExternalAuthsClient.BeginCreateOrUpdate
//     method.
func (client *ExternalAuthsClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, hcpOpenShiftClusterName string, externalAuthName string, resource ExternalAuth, options *ExternalAuthsClientBeginCreateOrUpdateOptions) (*runtime.Poller[ExternalAuthsClientCreateOrUpdateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.createOrUpdate(ctx, resourceGroupName, hcpOpenShiftClusterName, externalAuthName, resource, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[ExternalAuthsClientCreateOrUpdateResponse]{
			FinalStateVia: runtime.FinalStateViaAzureAsyncOp,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[ExternalAuthsClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// CreateOrUpdate - Create a ExternalAuth
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2026-09-01-preview
func (client *ExternalAuthsClient) createOrUpdate(ctx context.Context, resourceGroupName string, hcpOpenShiftClusterName string, externalAuthName string, resource ExternalAuth, options *ExternalAuthsClientBeginCreateOrUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "ExternalAuthsClient.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, hcpOpenShiftClusterName, externalAuthName, 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 *ExternalAuthsClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, hcpOpenShiftClusterName string, externalAuthName string, resource ExternalAuth, _ *ExternalAuthsClientBeginCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RedHatOpenShift/hcpOpenShiftClusters/{hcpOpenShiftClusterName}/externalAuths/{externalAuthName}"
	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 hcpOpenShiftClusterName == "" {
		return nil, errors.New("parameter hcpOpenShiftClusterName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{hcpOpenShiftClusterName}", url.PathEscape(hcpOpenShiftClusterName))
	if externalAuthName == "" {
		return nil, errors.New("parameter externalAuthName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{externalAuthName}", url.PathEscape(externalAuthName))
	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", "2026-09-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, resource); err != nil {
		return nil, err
	}
	return req, nil
}

// BeginDelete - Delete a ExternalAuth
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2026-09-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - hcpOpenShiftClusterName - The name of the HcpOpenShiftCluster
//   - externalAuthName - The name of the ExternalAuth
//   - options - ExternalAuthsClientBeginDeleteOptions contains the optional parameters for the ExternalAuthsClient.BeginDelete
//     method.
func (client *ExternalAuthsClient) BeginDelete(ctx context.Context, resourceGroupName string, hcpOpenShiftClusterName string, externalAuthName string, options *ExternalAuthsClientBeginDeleteOptions) (*runtime.Poller[ExternalAuthsClientDeleteResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.deleteOperation(ctx, resourceGroupName, hcpOpenShiftClusterName, externalAuthName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[ExternalAuthsClientDeleteResponse]{
			FinalStateVia: runtime.FinalStateViaLocation,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[ExternalAuthsClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Delete - Delete a ExternalAuth
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2026-09-01-preview
func (client *ExternalAuthsClient) deleteOperation(ctx context.Context, resourceGroupName string, hcpOpenShiftClusterName string, externalAuthName string, options *ExternalAuthsClientBeginDeleteOptions) (*http.Response, error) {
	var err error
	const operationName = "ExternalAuthsClient.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, hcpOpenShiftClusterName, externalAuthName, 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 *ExternalAuthsClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, hcpOpenShiftClusterName string, externalAuthName string, _ *ExternalAuthsClientBeginDeleteOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RedHatOpenShift/hcpOpenShiftClusters/{hcpOpenShiftClusterName}/externalAuths/{externalAuthName}"
	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 hcpOpenShiftClusterName == "" {
		return nil, errors.New("parameter hcpOpenShiftClusterName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{hcpOpenShiftClusterName}", url.PathEscape(hcpOpenShiftClusterName))
	if externalAuthName == "" {
		return nil, errors.New("parameter externalAuthName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{externalAuthName}", url.PathEscape(externalAuthName))
	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", "2026-09-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// Get - Get a ExternalAuth
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2026-09-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - hcpOpenShiftClusterName - The name of the HcpOpenShiftCluster
//   - externalAuthName - The name of the ExternalAuth
//   - options - ExternalAuthsClientGetOptions contains the optional parameters for the ExternalAuthsClient.Get method.
func (client *ExternalAuthsClient) Get(ctx context.Context, resourceGroupName string, hcpOpenShiftClusterName string, externalAuthName string, options *ExternalAuthsClientGetOptions) (ExternalAuthsClientGetResponse, error) {
	var err error
	const operationName = "ExternalAuthsClient.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, hcpOpenShiftClusterName, externalAuthName, options)
	if err != nil {
		return ExternalAuthsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ExternalAuthsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ExternalAuthsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *ExternalAuthsClient) getCreateRequest(ctx context.Context, resourceGroupName string, hcpOpenShiftClusterName string, externalAuthName string, _ *ExternalAuthsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RedHatOpenShift/hcpOpenShiftClusters/{hcpOpenShiftClusterName}/externalAuths/{externalAuthName}"
	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 hcpOpenShiftClusterName == "" {
		return nil, errors.New("parameter hcpOpenShiftClusterName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{hcpOpenShiftClusterName}", url.PathEscape(hcpOpenShiftClusterName))
	if externalAuthName == "" {
		return nil, errors.New("parameter externalAuthName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{externalAuthName}", url.PathEscape(externalAuthName))
	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", "2026-09-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// NewListByParentPager - List ExternalAuth resources by HcpOpenShiftCluster
//
// Generated from API version 2026-09-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - hcpOpenShiftClusterName - The name of the HcpOpenShiftCluster
//   - options - ExternalAuthsClientListByParentOptions contains the optional parameters for the ExternalAuthsClient.NewListByParentPager
//     method.
func (client *ExternalAuthsClient) NewListByParentPager(resourceGroupName string, hcpOpenShiftClusterName string, options *ExternalAuthsClientListByParentOptions) *runtime.Pager[ExternalAuthsClientListByParentResponse] {
	return runtime.NewPager(runtime.PagingHandler[ExternalAuthsClientListByParentResponse]{
		More: func(page ExternalAuthsClientListByParentResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *ExternalAuthsClientListByParentResponse) (ExternalAuthsClientListByParentResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "ExternalAuthsClient.NewListByParentPager")
			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.listByParentCreateRequest(ctx, resourceGroupName, hcpOpenShiftClusterName, options)
			}, nil)
			if err != nil {
				return ExternalAuthsClientListByParentResponse{}, err
			}
			return client.listByParentHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByParentCreateRequest creates the ListByParent request.
func (client *ExternalAuthsClient) listByParentCreateRequest(ctx context.Context, resourceGroupName string, hcpOpenShiftClusterName string, _ *ExternalAuthsClientListByParentOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RedHatOpenShift/hcpOpenShiftClusters/{hcpOpenShiftClusterName}/externalAuths"
	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 hcpOpenShiftClusterName == "" {
		return nil, errors.New("parameter hcpOpenShiftClusterName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{hcpOpenShiftClusterName}", url.PathEscape(hcpOpenShiftClusterName))
	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", "2026-09-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByParentHandleResponse handles the ListByParent response.
func (client *ExternalAuthsClient) listByParentHandleResponse(resp *http.Response) (ExternalAuthsClientListByParentResponse, error) {
	result := ExternalAuthsClientListByParentResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ExternalAuthListResult); err != nil {
		return ExternalAuthsClientListByParentResponse{}, err
	}
	return result, nil
}

// BeginUpdate - Update a ExternalAuth
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2026-09-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - hcpOpenShiftClusterName - The name of the HcpOpenShiftCluster
//   - externalAuthName - The name of the ExternalAuth
//   - properties - The resource properties to be updated.
//   - options - ExternalAuthsClientBeginUpdateOptions contains the optional parameters for the ExternalAuthsClient.BeginUpdate
//     method.
func (client *ExternalAuthsClient) BeginUpdate(ctx context.Context, resourceGroupName string, hcpOpenShiftClusterName string, externalAuthName string, properties ExternalAuthUpdate, options *ExternalAuthsClientBeginUpdateOptions) (*runtime.Poller[ExternalAuthsClientUpdateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.update(ctx, resourceGroupName, hcpOpenShiftClusterName, externalAuthName, properties, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[ExternalAuthsClientUpdateResponse]{
			FinalStateVia: runtime.FinalStateViaLocation,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[ExternalAuthsClientUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Update - Update a ExternalAuth
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2026-09-01-preview
func (client *ExternalAuthsClient) update(ctx context.Context, resourceGroupName string, hcpOpenShiftClusterName string, externalAuthName string, properties ExternalAuthUpdate, options *ExternalAuthsClientBeginUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "ExternalAuthsClient.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, hcpOpenShiftClusterName, externalAuthName, 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 *ExternalAuthsClient) updateCreateRequest(ctx context.Context, resourceGroupName string, hcpOpenShiftClusterName string, externalAuthName string, properties ExternalAuthUpdate, _ *ExternalAuthsClientBeginUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RedHatOpenShift/hcpOpenShiftClusters/{hcpOpenShiftClusterName}/externalAuths/{externalAuthName}"
	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 hcpOpenShiftClusterName == "" {
		return nil, errors.New("parameter hcpOpenShiftClusterName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{hcpOpenShiftClusterName}", url.PathEscape(hcpOpenShiftClusterName))
	if externalAuthName == "" {
		return nil, errors.New("parameter externalAuthName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{externalAuthName}", url.PathEscape(externalAuthName))
	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", "2026-09-01-preview")
	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
}
