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

import (
	"context"
	"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"
	"strings"
)

// Client contains the methods for the service.
// Don't use this type directly, use NewClient() instead.
//
// Generated from API versions 2020-09-01-preview, 2021-06-01-preview, 2024-04-01
type Client struct {
	internal *arm.Client
}

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

// ResourceChangeDetails - Get resource change details.
// If the operation fails it returns an *azcore.ResponseError type.
//   - parameters - The request body
//   - options - ClientResourceChangeDetailsOptions contains the optional parameters for the Client.ResourceChangeDetails method.
func (client *Client) ResourceChangeDetails(ctx context.Context, parameters ResourceChangeDetailsRequestParameters, options *ClientResourceChangeDetailsOptions) (ClientResourceChangeDetailsResponse, error) {
	var err error
	const operationName = "Client.ResourceChangeDetails"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.resourceChangeDetailsCreateRequest(ctx, parameters, options)
	if err != nil {
		return ClientResourceChangeDetailsResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ClientResourceChangeDetailsResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ClientResourceChangeDetailsResponse{}, err
	}
	resp, err := client.resourceChangeDetailsHandleResponse(httpResp)
	return resp, err
}

// resourceChangeDetailsCreateRequest creates the ResourceChangeDetails request.
func (client *Client) resourceChangeDetailsCreateRequest(ctx context.Context, parameters ResourceChangeDetailsRequestParameters, _ *ClientResourceChangeDetailsOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.ResourceGraph/resourceChangeDetails"
	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", version20200901Preview)
	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, parameters); err != nil {
		return nil, err
	}
	return req, nil
}

// resourceChangeDetailsHandleResponse handles the ResourceChangeDetails response.
func (client *Client) resourceChangeDetailsHandleResponse(resp *http.Response) (ClientResourceChangeDetailsResponse, error) {
	result := ClientResourceChangeDetailsResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ResourceChangeDataArray); err != nil {
		return ClientResourceChangeDetailsResponse{}, err
	}
	return result, nil
}

// ResourceChanges - List changes to a resource for a given time interval.
// If the operation fails it returns an *azcore.ResponseError type.
//   - parameters - The request body
//   - options - ClientResourceChangesOptions contains the optional parameters for the Client.ResourceChanges method.
func (client *Client) ResourceChanges(ctx context.Context, parameters ResourceChangesRequestParameters, options *ClientResourceChangesOptions) (ClientResourceChangesResponse, error) {
	var err error
	const operationName = "Client.ResourceChanges"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.resourceChangesCreateRequest(ctx, parameters, options)
	if err != nil {
		return ClientResourceChangesResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ClientResourceChangesResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ClientResourceChangesResponse{}, err
	}
	resp, err := client.resourceChangesHandleResponse(httpResp)
	return resp, err
}

// resourceChangesCreateRequest creates the ResourceChanges request.
func (client *Client) resourceChangesCreateRequest(ctx context.Context, parameters ResourceChangesRequestParameters, _ *ClientResourceChangesOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.ResourceGraph/resourceChanges"
	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", version20200901Preview)
	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, parameters); err != nil {
		return nil, err
	}
	return req, nil
}

// resourceChangesHandleResponse handles the ResourceChanges response.
func (client *Client) resourceChangesHandleResponse(resp *http.Response) (ClientResourceChangesResponse, error) {
	result := ClientResourceChangesResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ResourceChangeList); err != nil {
		return ClientResourceChangesResponse{}, err
	}
	return result, nil
}

// Resources - Queries the resources managed by Azure Resource Manager for scopes specified in the request.
// If the operation fails it returns an *azcore.ResponseError type.
//   - query - The request body
//   - options - ClientResourcesOptions contains the optional parameters for the Client.Resources method.
func (client *Client) Resources(ctx context.Context, query QueryRequest, options *ClientResourcesOptions) (ClientResourcesResponse, error) {
	var err error
	const operationName = "Client.Resources"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.resourcesCreateRequest(ctx, query, options)
	if err != nil {
		return ClientResourcesResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ClientResourcesResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ClientResourcesResponse{}, err
	}
	resp, err := client.resourcesHandleResponse(httpResp)
	return resp, err
}

// resourcesCreateRequest creates the Resources request.
func (client *Client) resourcesCreateRequest(ctx context.Context, query QueryRequest, _ *ClientResourcesOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.ResourceGraph/resources"
	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", version20240401)
	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, query); err != nil {
		return nil, err
	}
	return req, nil
}

// resourcesHandleResponse handles the Resources response.
func (client *Client) resourcesHandleResponse(resp *http.Response) (ClientResourcesResponse, error) {
	result := ClientResourcesResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.QueryResponse); err != nil {
		return ClientResourcesResponse{}, err
	}
	return result, nil
}

// ResourcesHistory - List all snapshots of a resource for a given time interval.
// If the operation fails it returns an *azcore.ResponseError type.
//   - request - The request body
//   - options - ClientResourcesHistoryOptions contains the optional parameters for the Client.ResourcesHistory method.
func (client *Client) ResourcesHistory(ctx context.Context, request ResourcesHistoryRequest, options *ClientResourcesHistoryOptions) (ClientResourcesHistoryResponse, error) {
	var err error
	const operationName = "Client.ResourcesHistory"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.resourcesHistoryCreateRequest(ctx, request, options)
	if err != nil {
		return ClientResourcesHistoryResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ClientResourcesHistoryResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ClientResourcesHistoryResponse{}, err
	}
	resp, err := client.resourcesHistoryHandleResponse(httpResp)
	return resp, err
}

// resourcesHistoryCreateRequest creates the ResourcesHistory request.
func (client *Client) resourcesHistoryCreateRequest(ctx context.Context, request ResourcesHistoryRequest, _ *ClientResourcesHistoryOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.ResourceGraph/resourcesHistory"
	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", version20210601Preview)
	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, request); err != nil {
		return nil, err
	}
	return req, nil
}

// resourcesHistoryHandleResponse handles the ResourcesHistory response.
func (client *Client) resourcesHistoryHandleResponse(resp *http.Response) (ClientResourcesHistoryResponse, error) {
	result := ClientResourcesHistoryResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.Interface); err != nil {
		return ClientResourcesHistoryResponse{}, err
	}
	return result, nil
}
