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

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

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

// Get - Get a DbServer
// 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.
//   - cloudexadatainfrastructurename - CloudExadataInfrastructure name
//   - dbserverocid - DbServer OCID.
//   - options - DbServersClientGetOptions contains the optional parameters for the DbServersClient.Get method.
func (client *DbServersClient) Get(ctx context.Context, resourceGroupName string, cloudexadatainfrastructurename string, dbserverocid string, options *DbServersClientGetOptions) (DbServersClientGetResponse, error) {
	var err error
	const operationName = "DbServersClient.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, cloudexadatainfrastructurename, dbserverocid, options)
	if err != nil {
		return DbServersClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return DbServersClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return DbServersClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *DbServersClient) getCreateRequest(ctx context.Context, resourceGroupName string, cloudexadatainfrastructurename string, dbserverocid string, _ *DbServersClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Oracle.Database/cloudExadataInfrastructures/{cloudexadatainfrastructurename}/dbServers/{dbserverocid}"
	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 cloudexadatainfrastructurename == "" {
		return nil, errors.New("parameter cloudexadatainfrastructurename cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{cloudexadatainfrastructurename}", url.PathEscape(cloudexadatainfrastructurename))
	if dbserverocid == "" {
		return nil, errors.New("parameter dbserverocid cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{dbserverocid}", url.PathEscape(dbserverocid))
	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 *DbServersClient) getHandleResponse(resp *http.Response) (DbServersClientGetResponse, error) {
	result := DbServersClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.DbServer); err != nil {
		return DbServersClientGetResponse{}, err
	}
	return result, nil
}

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

// listByCloudExadataInfrastructureCreateRequest creates the ListByCloudExadataInfrastructure request.
func (client *DbServersClient) listByCloudExadataInfrastructureCreateRequest(ctx context.Context, resourceGroupName string, cloudexadatainfrastructurename string, _ *DbServersClientListByCloudExadataInfrastructureOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Oracle.Database/cloudExadataInfrastructures/{cloudexadatainfrastructurename}/dbServers"
	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 cloudexadatainfrastructurename == "" {
		return nil, errors.New("parameter cloudexadatainfrastructurename cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{cloudexadatainfrastructurename}", url.PathEscape(cloudexadatainfrastructurename))
	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
}

// listByCloudExadataInfrastructureHandleResponse handles the ListByCloudExadataInfrastructure response.
func (client *DbServersClient) listByCloudExadataInfrastructureHandleResponse(resp *http.Response) (DbServersClientListByCloudExadataInfrastructureResponse, error) {
	result := DbServersClientListByCloudExadataInfrastructureResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.DbServerListResult); err != nil {
		return DbServersClientListByCloudExadataInfrastructureResponse{}, err
	}
	return result, nil
}
