// 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) AutoRest Code Generator. DO NOT EDIT.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

package armappcontainers

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

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

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

// DeleteLabelHistory - Delete the history of a label.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-02-02-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - containerAppName - Name of the Container App
//   - labelName - Name of the Container App label.
//   - options - ContainerAppsLabelHistoryClientDeleteLabelHistoryOptions contains the optional parameters for the ContainerAppsLabelHistoryClient.DeleteLabelHistory
//     method.
func (client *ContainerAppsLabelHistoryClient) DeleteLabelHistory(ctx context.Context, resourceGroupName string, containerAppName string, labelName string, options *ContainerAppsLabelHistoryClientDeleteLabelHistoryOptions) (ContainerAppsLabelHistoryClientDeleteLabelHistoryResponse, error) {
	var err error
	const operationName = "ContainerAppsLabelHistoryClient.DeleteLabelHistory"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.deleteLabelHistoryCreateRequest(ctx, resourceGroupName, containerAppName, labelName, options)
	if err != nil {
		return ContainerAppsLabelHistoryClientDeleteLabelHistoryResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ContainerAppsLabelHistoryClientDeleteLabelHistoryResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return ContainerAppsLabelHistoryClientDeleteLabelHistoryResponse{}, err
	}
	return ContainerAppsLabelHistoryClientDeleteLabelHistoryResponse{}, nil
}

// deleteLabelHistoryCreateRequest creates the DeleteLabelHistory request.
func (client *ContainerAppsLabelHistoryClient) deleteLabelHistoryCreateRequest(ctx context.Context, resourceGroupName string, containerAppName string, labelName string, _ *ContainerAppsLabelHistoryClientDeleteLabelHistoryOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.App/containerApps/{containerAppName}/labelHistory/{labelName}"
	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 containerAppName == "" {
		return nil, errors.New("parameter containerAppName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{containerAppName}", url.PathEscape(containerAppName))
	if labelName == "" {
		return nil, errors.New("parameter labelName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{labelName}", url.PathEscape(labelName))
	req, err := runtime.NewRequest(ctx, http.MethodDelete, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2025-02-02-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// GetLabelHistory - Get the history of a label.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-02-02-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - containerAppName - Name of the Container App
//   - labelName - Name of the Container App label.
//   - options - ContainerAppsLabelHistoryClientGetLabelHistoryOptions contains the optional parameters for the ContainerAppsLabelHistoryClient.GetLabelHistory
//     method.
func (client *ContainerAppsLabelHistoryClient) GetLabelHistory(ctx context.Context, resourceGroupName string, containerAppName string, labelName string, options *ContainerAppsLabelHistoryClientGetLabelHistoryOptions) (ContainerAppsLabelHistoryClientGetLabelHistoryResponse, error) {
	var err error
	const operationName = "ContainerAppsLabelHistoryClient.GetLabelHistory"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getLabelHistoryCreateRequest(ctx, resourceGroupName, containerAppName, labelName, options)
	if err != nil {
		return ContainerAppsLabelHistoryClientGetLabelHistoryResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ContainerAppsLabelHistoryClientGetLabelHistoryResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ContainerAppsLabelHistoryClientGetLabelHistoryResponse{}, err
	}
	resp, err := client.getLabelHistoryHandleResponse(httpResp)
	return resp, err
}

// getLabelHistoryCreateRequest creates the GetLabelHistory request.
func (client *ContainerAppsLabelHistoryClient) getLabelHistoryCreateRequest(ctx context.Context, resourceGroupName string, containerAppName string, labelName string, _ *ContainerAppsLabelHistoryClientGetLabelHistoryOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.App/containerApps/{containerAppName}/labelHistory/{labelName}"
	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 containerAppName == "" {
		return nil, errors.New("parameter containerAppName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{containerAppName}", url.PathEscape(containerAppName))
	if labelName == "" {
		return nil, errors.New("parameter labelName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{labelName}", url.PathEscape(labelName))
	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-02-02-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getLabelHistoryHandleResponse handles the GetLabelHistory response.
func (client *ContainerAppsLabelHistoryClient) getLabelHistoryHandleResponse(resp *http.Response) (ContainerAppsLabelHistoryClientGetLabelHistoryResponse, error) {
	result := ContainerAppsLabelHistoryClientGetLabelHistoryResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.LabelHistory); err != nil {
		return ContainerAppsLabelHistoryClientGetLabelHistoryResponse{}, err
	}
	return result, nil
}

// NewListLabelHistoryPager - Get the Label History for a given Container App.
//
// Generated from API version 2025-02-02-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - containerAppName - Name of the Container App
//   - options - ContainerAppsLabelHistoryClientListLabelHistoryOptions contains the optional parameters for the ContainerAppsLabelHistoryClient.NewListLabelHistoryPager
//     method.
func (client *ContainerAppsLabelHistoryClient) NewListLabelHistoryPager(resourceGroupName string, containerAppName string, options *ContainerAppsLabelHistoryClientListLabelHistoryOptions) *runtime.Pager[ContainerAppsLabelHistoryClientListLabelHistoryResponse] {
	return runtime.NewPager(runtime.PagingHandler[ContainerAppsLabelHistoryClientListLabelHistoryResponse]{
		More: func(page ContainerAppsLabelHistoryClientListLabelHistoryResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *ContainerAppsLabelHistoryClientListLabelHistoryResponse) (ContainerAppsLabelHistoryClientListLabelHistoryResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "ContainerAppsLabelHistoryClient.NewListLabelHistoryPager")
			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.listLabelHistoryCreateRequest(ctx, resourceGroupName, containerAppName, options)
			}, nil)
			if err != nil {
				return ContainerAppsLabelHistoryClientListLabelHistoryResponse{}, err
			}
			return client.listLabelHistoryHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listLabelHistoryCreateRequest creates the ListLabelHistory request.
func (client *ContainerAppsLabelHistoryClient) listLabelHistoryCreateRequest(ctx context.Context, resourceGroupName string, containerAppName string, options *ContainerAppsLabelHistoryClientListLabelHistoryOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.App/containerApps/{containerAppName}/labelHistory"
	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 containerAppName == "" {
		return nil, errors.New("parameter containerAppName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{containerAppName}", url.PathEscape(containerAppName))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	if options != nil && options.Filter != nil {
		reqQP.Set("$filter", *options.Filter)
	}
	reqQP.Set("api-version", "2025-02-02-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listLabelHistoryHandleResponse handles the ListLabelHistory response.
func (client *ContainerAppsLabelHistoryClient) listLabelHistoryHandleResponse(resp *http.Response) (ContainerAppsLabelHistoryClientListLabelHistoryResponse, error) {
	result := ContainerAppsLabelHistoryClientListLabelHistoryResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.LabelHistoryCollection); err != nil {
		return ContainerAppsLabelHistoryClientListLabelHistoryResponse{}, err
	}
	return result, nil
}
