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

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

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

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

// Get - Get InstanceHistory Resource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-06-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - targetName - Name of the target
//   - solutionName - Name of the solution
//   - instanceName - Name of the instance
//   - instanceHistoryName - Name of the instance history
//   - options - InstanceHistoriesClientGetOptions contains the optional parameters for the InstanceHistoriesClient.Get method.
func (client *InstanceHistoriesClient) Get(ctx context.Context, resourceGroupName string, targetName string, solutionName string, instanceName string, instanceHistoryName string, options *InstanceHistoriesClientGetOptions) (InstanceHistoriesClientGetResponse, error) {
	var err error
	const operationName = "InstanceHistoriesClient.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, targetName, solutionName, instanceName, instanceHistoryName, options)
	if err != nil {
		return InstanceHistoriesClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return InstanceHistoriesClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return InstanceHistoriesClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *InstanceHistoriesClient) getCreateRequest(ctx context.Context, resourceGroupName string, targetName string, solutionName string, instanceName string, instanceHistoryName string, _ *InstanceHistoriesClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/targets/{targetName}/solutions/{solutionName}/instances/{instanceName}/histories/{instanceHistoryName}"
	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 targetName == "" {
		return nil, errors.New("parameter targetName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{targetName}", url.PathEscape(targetName))
	if solutionName == "" {
		return nil, errors.New("parameter solutionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{solutionName}", url.PathEscape(solutionName))
	if instanceName == "" {
		return nil, errors.New("parameter instanceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{instanceName}", url.PathEscape(instanceName))
	if instanceHistoryName == "" {
		return nil, errors.New("parameter instanceHistoryName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{instanceHistoryName}", url.PathEscape(instanceHistoryName))
	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-06-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// NewListByInstancePager - List InstanceHistory Resources
//
// Generated from API version 2025-06-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - targetName - Name of the target
//   - solutionName - Name of the solution
//   - instanceName - Name of the instance
//   - options - InstanceHistoriesClientListByInstanceOptions contains the optional parameters for the InstanceHistoriesClient.NewListByInstancePager
//     method.
func (client *InstanceHistoriesClient) NewListByInstancePager(resourceGroupName string, targetName string, solutionName string, instanceName string, options *InstanceHistoriesClientListByInstanceOptions) *runtime.Pager[InstanceHistoriesClientListByInstanceResponse] {
	return runtime.NewPager(runtime.PagingHandler[InstanceHistoriesClientListByInstanceResponse]{
		More: func(page InstanceHistoriesClientListByInstanceResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *InstanceHistoriesClientListByInstanceResponse) (InstanceHistoriesClientListByInstanceResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "InstanceHistoriesClient.NewListByInstancePager")
			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.listByInstanceCreateRequest(ctx, resourceGroupName, targetName, solutionName, instanceName, options)
			}, nil)
			if err != nil {
				return InstanceHistoriesClientListByInstanceResponse{}, err
			}
			return client.listByInstanceHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByInstanceCreateRequest creates the ListByInstance request.
func (client *InstanceHistoriesClient) listByInstanceCreateRequest(ctx context.Context, resourceGroupName string, targetName string, solutionName string, instanceName string, _ *InstanceHistoriesClientListByInstanceOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/targets/{targetName}/solutions/{solutionName}/instances/{instanceName}/histories"
	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 targetName == "" {
		return nil, errors.New("parameter targetName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{targetName}", url.PathEscape(targetName))
	if solutionName == "" {
		return nil, errors.New("parameter solutionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{solutionName}", url.PathEscape(solutionName))
	if instanceName == "" {
		return nil, errors.New("parameter instanceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{instanceName}", url.PathEscape(instanceName))
	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-06-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByInstanceHandleResponse handles the ListByInstance response.
func (client *InstanceHistoriesClient) listByInstanceHandleResponse(resp *http.Response) (InstanceHistoriesClientListByInstanceResponse, error) {
	result := InstanceHistoriesClientListByInstanceResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.InstanceHistoryListResult); err != nil {
		return InstanceHistoriesClientListByInstanceResponse{}, err
	}
	return result, nil
}
