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

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

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

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

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

// getCreateRequest creates the Get request.
func (client *HealthValidationsClient) getCreateRequest(ctx context.Context, resourceGroupName string, watcherName string, healthValidationName string, _ *HealthValidationsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DatabaseWatcher/watchers/{watcherName}/healthValidations/{healthValidationName}"
	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 watcherName == "" {
		return nil, errors.New("parameter watcherName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{watcherName}", url.PathEscape(watcherName))
	if healthValidationName == "" {
		return nil, errors.New("parameter healthValidationName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{healthValidationName}", url.PathEscape(healthValidationName))
	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-01-02")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

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

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

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

// BeginStartValidation - Starts health validation for a watcher.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-01-02
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - watcherName - The database watcher name.
//   - healthValidationName - The health validation resource name.
//   - options - HealthValidationsClientBeginStartValidationOptions contains the optional parameters for the HealthValidationsClient.BeginStartValidation
//     method.
func (client *HealthValidationsClient) BeginStartValidation(ctx context.Context, resourceGroupName string, watcherName string, healthValidationName string, options *HealthValidationsClientBeginStartValidationOptions) (*runtime.Poller[HealthValidationsClientStartValidationResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.startValidation(ctx, resourceGroupName, watcherName, healthValidationName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[HealthValidationsClientStartValidationResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[HealthValidationsClientStartValidationResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// StartValidation - Starts health validation for a watcher.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-01-02
func (client *HealthValidationsClient) startValidation(ctx context.Context, resourceGroupName string, watcherName string, healthValidationName string, options *HealthValidationsClientBeginStartValidationOptions) (*http.Response, error) {
	var err error
	const operationName = "HealthValidationsClient.BeginStartValidation"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.startValidationCreateRequest(ctx, resourceGroupName, watcherName, healthValidationName, 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
}

// startValidationCreateRequest creates the StartValidation request.
func (client *HealthValidationsClient) startValidationCreateRequest(ctx context.Context, resourceGroupName string, watcherName string, healthValidationName string, _ *HealthValidationsClientBeginStartValidationOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DatabaseWatcher/watchers/{watcherName}/healthValidations/{healthValidationName}/startValidation"
	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 watcherName == "" {
		return nil, errors.New("parameter watcherName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{watcherName}", url.PathEscape(watcherName))
	if healthValidationName == "" {
		return nil, errors.New("parameter healthValidationName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{healthValidationName}", url.PathEscape(healthValidationName))
	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-01-02")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}
