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

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

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

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

// BeginAction - VM actions on DbNode of ExadbVmCluster by the provided filter
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-09-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - exadbVMClusterName - The name of the ExadbVmCluster
//   - exascaleDbNodeName - The name of the ExascaleDbNode
//   - body - The content of the action request
//   - options - ExascaleDbNodesClientBeginActionOptions contains the optional parameters for the ExascaleDbNodesClient.BeginAction
//     method.
func (client *ExascaleDbNodesClient) BeginAction(ctx context.Context, resourceGroupName string, exadbVMClusterName string, exascaleDbNodeName string, body DbNodeAction, options *ExascaleDbNodesClientBeginActionOptions) (*runtime.Poller[ExascaleDbNodesClientActionResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.action(ctx, resourceGroupName, exadbVMClusterName, exascaleDbNodeName, body, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[ExascaleDbNodesClientActionResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[ExascaleDbNodesClientActionResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Action - VM actions on DbNode of ExadbVmCluster by the provided filter
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-09-01
func (client *ExascaleDbNodesClient) action(ctx context.Context, resourceGroupName string, exadbVMClusterName string, exascaleDbNodeName string, body DbNodeAction, options *ExascaleDbNodesClientBeginActionOptions) (*http.Response, error) {
	var err error
	const operationName = "ExascaleDbNodesClient.BeginAction"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.actionCreateRequest(ctx, resourceGroupName, exadbVMClusterName, exascaleDbNodeName, body, 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
}

// actionCreateRequest creates the Action request.
func (client *ExascaleDbNodesClient) actionCreateRequest(ctx context.Context, resourceGroupName string, exadbVMClusterName string, exascaleDbNodeName string, body DbNodeAction, _ *ExascaleDbNodesClientBeginActionOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Oracle.Database/exadbVmClusters/{exadbVmClusterName}/dbNodes/{exascaleDbNodeName}/action"
	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 exadbVMClusterName == "" {
		return nil, errors.New("parameter exadbVMClusterName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{exadbVmClusterName}", url.PathEscape(exadbVMClusterName))
	if exascaleDbNodeName == "" {
		return nil, errors.New("parameter exascaleDbNodeName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{exascaleDbNodeName}", url.PathEscape(exascaleDbNodeName))
	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", "2025-09-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, body); err != nil {
		return nil, err
	}
	return req, nil
}

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

// getCreateRequest creates the Get request.
func (client *ExascaleDbNodesClient) getCreateRequest(ctx context.Context, resourceGroupName string, exadbVMClusterName string, exascaleDbNodeName string, _ *ExascaleDbNodesClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Oracle.Database/exadbVmClusters/{exadbVmClusterName}/dbNodes/{exascaleDbNodeName}"
	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 exadbVMClusterName == "" {
		return nil, errors.New("parameter exadbVMClusterName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{exadbVmClusterName}", url.PathEscape(exadbVMClusterName))
	if exascaleDbNodeName == "" {
		return nil, errors.New("parameter exascaleDbNodeName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{exascaleDbNodeName}", url.PathEscape(exascaleDbNodeName))
	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-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 *ExascaleDbNodesClient) getHandleResponse(resp *http.Response) (ExascaleDbNodesClientGetResponse, error) {
	result := ExascaleDbNodesClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ExascaleDbNode); err != nil {
		return ExascaleDbNodesClientGetResponse{}, err
	}
	return result, nil
}

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

// listByParentCreateRequest creates the ListByParent request.
func (client *ExascaleDbNodesClient) listByParentCreateRequest(ctx context.Context, resourceGroupName string, exadbVMClusterName string, _ *ExascaleDbNodesClientListByParentOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Oracle.Database/exadbVmClusters/{exadbVmClusterName}/dbNodes"
	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 exadbVMClusterName == "" {
		return nil, errors.New("parameter exadbVMClusterName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{exadbVmClusterName}", url.PathEscape(exadbVMClusterName))
	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-09-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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