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

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

// SQLVulnerabilityAssessmentScansClient contains the methods for the SQLVulnerabilityAssessmentScans group.
// Don't use this type directly, use NewSQLVulnerabilityAssessmentScansClient() instead.
//
// Generated from API version 2026-04-01-preview
type SQLVulnerabilityAssessmentScansClient struct {
	internal *arm.Client
}

// NewSQLVulnerabilityAssessmentScansClient creates a new instance of SQLVulnerabilityAssessmentScansClient with the specified values.
//   - credential - used to authorize requests. Usually a credential from azidentity.
//   - options - Contains optional client configuration. Pass nil to accept the default values.
func NewSQLVulnerabilityAssessmentScansClient(credential azcore.TokenCredential, options *arm.ClientOptions) (*SQLVulnerabilityAssessmentScansClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &SQLVulnerabilityAssessmentScansClient{
		internal: cl,
	}
	return client, nil
}

// Get - Gets the scan details of a single scan record.
//
// Gets the scan details of a single scan record.
// If the operation fails it returns an *azcore.ResponseError type.
//   - options - SQLVulnerabilityAssessmentScansClientGetOptions contains the optional parameters for the SQLVulnerabilityAssessmentScansClient.Get
//     method.
func (client *SQLVulnerabilityAssessmentScansClient) Get(ctx context.Context, scanID string, resourceID string, options *SQLVulnerabilityAssessmentScansClientGetOptions) (SQLVulnerabilityAssessmentScansClientGetResponse, error) {
	var err error
	const operationName = "SQLVulnerabilityAssessmentScansClient.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, scanID, resourceID, options)
	if err != nil {
		return SQLVulnerabilityAssessmentScansClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return SQLVulnerabilityAssessmentScansClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return SQLVulnerabilityAssessmentScansClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *SQLVulnerabilityAssessmentScansClient) getCreateRequest(ctx context.Context, scanID string, resourceID string, options *SQLVulnerabilityAssessmentScansClientGetOptions) (*policy.Request, error) {
	urlPath := "/{resourceId}/providers/Microsoft.Security/sqlVulnerabilityAssessments/default/scans/{scanId}"
	if scanID == "" {
		return nil, errors.New("parameter scanID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{scanId}", url.PathEscape(scanID))
	if resourceID == "" {
		return nil, errors.New("parameter resourceID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceId}", resourceID)
	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", version20260401Preview)
	if options != nil && options.DatabaseName != nil {
		reqQP.Set("databaseName", *options.DatabaseName)
	}
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// GetScanOperationResult - Gets the result of a scan operation initiated by the InitiateScan action.
//
// Gets the result of a scan operation initiated by the InitiateScan action.
// If the operation fails it returns an *azcore.ResponseError type.
//   - options - SQLVulnerabilityAssessmentScansClientGetScanOperationResultOptions contains the optional parameters for the SQLVulnerabilityAssessmentScansClient.GetScanOperationResult
//     method.
func (client *SQLVulnerabilityAssessmentScansClient) GetScanOperationResult(ctx context.Context, resourceID string, operationID string, options *SQLVulnerabilityAssessmentScansClientGetScanOperationResultOptions) (SQLVulnerabilityAssessmentScansClientGetScanOperationResultResponse, error) {
	var err error
	const operationName = "SQLVulnerabilityAssessmentScansClient.GetScanOperationResult"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getScanOperationResultCreateRequest(ctx, resourceID, operationID, options)
	if err != nil {
		return SQLVulnerabilityAssessmentScansClientGetScanOperationResultResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return SQLVulnerabilityAssessmentScansClientGetScanOperationResultResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return SQLVulnerabilityAssessmentScansClientGetScanOperationResultResponse{}, err
	}
	resp, err := client.getScanOperationResultHandleResponse(httpResp)
	return resp, err
}

// getScanOperationResultCreateRequest creates the GetScanOperationResult request.
func (client *SQLVulnerabilityAssessmentScansClient) getScanOperationResultCreateRequest(ctx context.Context, resourceID string, operationID string, options *SQLVulnerabilityAssessmentScansClientGetScanOperationResultOptions) (*policy.Request, error) {
	urlPath := "/{resourceId}/providers/Microsoft.Security/sqlVulnerabilityAssessments/default/scans/scanOperationResults/{operationId}"
	if resourceID == "" {
		return nil, errors.New("parameter resourceID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceId}", resourceID)
	if operationID == "" {
		return nil, errors.New("parameter operationID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{operationId}", url.PathEscape(operationID))
	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", version20260401Preview)
	if options != nil && options.DatabaseName != nil {
		reqQP.Set("databaseName", *options.DatabaseName)
	}
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getScanOperationResultHandleResponse handles the GetScanOperationResult response.
func (client *SQLVulnerabilityAssessmentScansClient) getScanOperationResultHandleResponse(resp *http.Response) (SQLVulnerabilityAssessmentScansClientGetScanOperationResultResponse, error) {
	result := SQLVulnerabilityAssessmentScansClientGetScanOperationResultResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.SQLVulnerabilityAssessmentScanOperationResult); err != nil {
		return SQLVulnerabilityAssessmentScansClientGetScanOperationResultResponse{}, err
	}
	return result, nil
}

// BeginInitiateScan - Initiates a vulnerability assessment scan.
//
// Initiates a vulnerability assessment scan.
// If the operation fails it returns an *azcore.ResponseError type.
//   - options - SQLVulnerabilityAssessmentScansClientBeginInitiateScanOptions contains the optional parameters for the SQLVulnerabilityAssessmentScansClient.BeginInitiateScan
//     method.
func (client *SQLVulnerabilityAssessmentScansClient) BeginInitiateScan(ctx context.Context, resourceID string, options *SQLVulnerabilityAssessmentScansClientBeginInitiateScanOptions) (*runtime.Poller[SQLVulnerabilityAssessmentScansClientInitiateScanResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.initiateScan(ctx, resourceID, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[SQLVulnerabilityAssessmentScansClientInitiateScanResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[SQLVulnerabilityAssessmentScansClientInitiateScanResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// InitiateScan - Initiates a vulnerability assessment scan.
//
// Initiates a vulnerability assessment scan.
// If the operation fails it returns an *azcore.ResponseError type.
func (client *SQLVulnerabilityAssessmentScansClient) initiateScan(ctx context.Context, resourceID string, options *SQLVulnerabilityAssessmentScansClientBeginInitiateScanOptions) (*http.Response, error) {
	var err error
	const operationName = "SQLVulnerabilityAssessmentScansClient.BeginInitiateScan"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.initiateScanCreateRequest(ctx, resourceID, 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
}

// initiateScanCreateRequest creates the InitiateScan request.
func (client *SQLVulnerabilityAssessmentScansClient) initiateScanCreateRequest(ctx context.Context, resourceID string, options *SQLVulnerabilityAssessmentScansClientBeginInitiateScanOptions) (*policy.Request, error) {
	urlPath := "/{resourceId}/providers/Microsoft.Security/sqlVulnerabilityAssessments/default/scans/initiateScan"
	if resourceID == "" {
		return nil, errors.New("parameter resourceID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceId}", resourceID)
	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", version20260401Preview)
	if options != nil && options.DatabaseName != nil {
		reqQP.Set("databaseName", *options.DatabaseName)
	}
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// NewListPager - Gets a list of scan records.
//
// Gets a list of scan records.
//   - resourceID - The fully qualified Azure Resource manager identifier of the resource.
//   - options - SQLVulnerabilityAssessmentScansClientListOptions contains the optional parameters for the SQLVulnerabilityAssessmentScansClient.NewListPager
//     method.
func (client *SQLVulnerabilityAssessmentScansClient) NewListPager(resourceID string, options *SQLVulnerabilityAssessmentScansClientListOptions) *runtime.Pager[SQLVulnerabilityAssessmentScansClientListResponse] {
	return runtime.NewPager(runtime.PagingHandler[SQLVulnerabilityAssessmentScansClientListResponse]{
		More: func(page SQLVulnerabilityAssessmentScansClientListResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *SQLVulnerabilityAssessmentScansClientListResponse) (SQLVulnerabilityAssessmentScansClientListResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "SQLVulnerabilityAssessmentScansClient.NewListPager")
			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.listCreateRequest(ctx, resourceID, options)
			}, nil)
			if err != nil {
				return SQLVulnerabilityAssessmentScansClientListResponse{}, err
			}
			return client.listHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listCreateRequest creates the List request.
func (client *SQLVulnerabilityAssessmentScansClient) listCreateRequest(ctx context.Context, resourceID string, options *SQLVulnerabilityAssessmentScansClientListOptions) (*policy.Request, error) {
	urlPath := "/{resourceId}/providers/Microsoft.Security/sqlVulnerabilityAssessments/default/scans"
	if resourceID == "" {
		return nil, errors.New("parameter resourceID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceId}", resourceID)
	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", version20260401Preview)
	if options != nil && options.DatabaseName != nil {
		reqQP.Set("databaseName", *options.DatabaseName)
	}
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listHandleResponse handles the List response.
func (client *SQLVulnerabilityAssessmentScansClient) listHandleResponse(resp *http.Response) (SQLVulnerabilityAssessmentScansClientListResponse, error) {
	result := SQLVulnerabilityAssessmentScansClientListResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ScansV2); err != nil {
		return SQLVulnerabilityAssessmentScansClientListResponse{}, err
	}
	return result, nil
}
