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

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

// FeaturesClient contains the methods for the Features group.
// Don't use this type directly, use NewFeaturesClient() instead.
//
// Generated from API version 2024-04-01-preview
type FeaturesClient struct {
	internal       *arm.Client
	subscriptionID string
}

// NewFeaturesClient creates a new instance of FeaturesClient 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 - Contains optional client configuration. Pass nil to accept the default values.
func NewFeaturesClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*FeaturesClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &FeaturesClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

// AccountGet - Gets a list of features and their status for the account.
// Status of enabled features will be true. Status of disabled features will be false.
// Features that don't exist will be excluded from the results.
//
// Gets details from a list of feature names.
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - accountName - The name of the account.
//   - featureRequest - Request body with feature names.
//   - options - FeaturesClientAccountGetOptions contains the optional parameters for the FeaturesClient.AccountGet method.
func (client *FeaturesClient) AccountGet(ctx context.Context, resourceGroupName string, accountName string, featureRequest BatchFeatureRequest, options *FeaturesClientAccountGetOptions) (FeaturesClientAccountGetResponse, error) {
	var err error
	const operationName = "FeaturesClient.AccountGet"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.accountGetCreateRequest(ctx, resourceGroupName, accountName, featureRequest, options)
	if err != nil {
		return FeaturesClientAccountGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return FeaturesClientAccountGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return FeaturesClientAccountGetResponse{}, err
	}
	resp, err := client.accountGetHandleResponse(httpResp)
	return resp, err
}

// accountGetCreateRequest creates the AccountGet request.
func (client *FeaturesClient) accountGetCreateRequest(ctx context.Context, resourceGroupName string, accountName string, featureRequest BatchFeatureRequest, _ *FeaturesClientAccountGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Purview/accounts/{accountName}/listFeatures"
	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 accountName == "" {
		return nil, errors.New("parameter accountName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{accountName}", url.PathEscape(accountName))
	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", version20240401Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, featureRequest); err != nil {
		return nil, err
	}
	return req, nil
}

// accountGetHandleResponse handles the AccountGet response.
func (client *FeaturesClient) accountGetHandleResponse(resp *http.Response) (FeaturesClientAccountGetResponse, error) {
	result := FeaturesClientAccountGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.BatchFeatureStatus); err != nil {
		return FeaturesClientAccountGetResponse{}, err
	}
	return result, nil
}

// SubscriptionGet - Gets a list of features and their status for the location and subscription.
// Status of enabled features will be true. Status of disabled features will be false.
// Features that don't exist will be excluded from the results.
//
// Gets details from a list of feature names.
// If the operation fails it returns an *azcore.ResponseError type.
//   - locations - Location of feature.
//   - featureRequest - The request body
//   - options - FeaturesClientSubscriptionGetOptions contains the optional parameters for the FeaturesClient.SubscriptionGet
//     method.
func (client *FeaturesClient) SubscriptionGet(ctx context.Context, locations string, featureRequest BatchFeatureRequest, options *FeaturesClientSubscriptionGetOptions) (FeaturesClientSubscriptionGetResponse, error) {
	var err error
	const operationName = "FeaturesClient.SubscriptionGet"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.subscriptionGetCreateRequest(ctx, locations, featureRequest, options)
	if err != nil {
		return FeaturesClientSubscriptionGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return FeaturesClientSubscriptionGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return FeaturesClientSubscriptionGetResponse{}, err
	}
	resp, err := client.subscriptionGetHandleResponse(httpResp)
	return resp, err
}

// subscriptionGetCreateRequest creates the SubscriptionGet request.
func (client *FeaturesClient) subscriptionGetCreateRequest(ctx context.Context, locations string, featureRequest BatchFeatureRequest, _ *FeaturesClientSubscriptionGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Purview/locations/{locations}/listFeatures"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if locations == "" {
		return nil, errors.New("parameter locations cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{locations}", url.PathEscape(locations))
	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", version20240401Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, featureRequest); err != nil {
		return nil, err
	}
	return req, nil
}

// subscriptionGetHandleResponse handles the SubscriptionGet response.
func (client *FeaturesClient) subscriptionGetHandleResponse(resp *http.Response) (FeaturesClientSubscriptionGetResponse, error) {
	result := FeaturesClientSubscriptionGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.BatchFeatureStatus); err != nil {
		return FeaturesClientSubscriptionGetResponse{}, err
	}
	return result, nil
}
