//go:build go1.18
// +build go1.18

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

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

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

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

// GetByRevisionNumber - Gets the properties of the specified revision of the container group profile in the given subscription
// and resource group. The operation returns the properties of container group profile including
// containers, image registry credentials, restart policy, IP address type, OS type, volumes, current revision number, etc.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-05-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - containerGroupProfileName - The name of the container group profile.
//   - revisionNumber - The revision number of the container group profile.
//   - options - ContainerGroupProfileClientGetByRevisionNumberOptions contains the optional parameters for the ContainerGroupProfileClient.GetByRevisionNumber
//     method.
func (client *ContainerGroupProfileClient) GetByRevisionNumber(ctx context.Context, resourceGroupName string, containerGroupProfileName string, revisionNumber string, options *ContainerGroupProfileClientGetByRevisionNumberOptions) (ContainerGroupProfileClientGetByRevisionNumberResponse, error) {
	var err error
	const operationName = "ContainerGroupProfileClient.GetByRevisionNumber"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getByRevisionNumberCreateRequest(ctx, resourceGroupName, containerGroupProfileName, revisionNumber, options)
	if err != nil {
		return ContainerGroupProfileClientGetByRevisionNumberResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ContainerGroupProfileClientGetByRevisionNumberResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ContainerGroupProfileClientGetByRevisionNumberResponse{}, err
	}
	resp, err := client.getByRevisionNumberHandleResponse(httpResp)
	return resp, err
}

// getByRevisionNumberCreateRequest creates the GetByRevisionNumber request.
func (client *ContainerGroupProfileClient) getByRevisionNumberCreateRequest(ctx context.Context, resourceGroupName string, containerGroupProfileName string, revisionNumber string, options *ContainerGroupProfileClientGetByRevisionNumberOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ContainerInstance/containerGroupProfiles/{containerGroupProfileName}/revisions/{revisionNumber}"
	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 containerGroupProfileName == "" {
		return nil, errors.New("parameter containerGroupProfileName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{containerGroupProfileName}", url.PathEscape(containerGroupProfileName))
	if revisionNumber == "" {
		return nil, errors.New("parameter revisionNumber cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{revisionNumber}", url.PathEscape(revisionNumber))
	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", "2024-05-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getByRevisionNumberHandleResponse handles the GetByRevisionNumber response.
func (client *ContainerGroupProfileClient) getByRevisionNumberHandleResponse(resp *http.Response) (ContainerGroupProfileClientGetByRevisionNumberResponse, error) {
	result := ContainerGroupProfileClientGetByRevisionNumberResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ContainerGroupProfile); err != nil {
		return ContainerGroupProfileClientGetByRevisionNumberResponse{}, err
	}
	return result, nil
}

// NewListAllRevisionsPager - Get a list of all the revisions of the specified container group profile in the given subscription
// and resource group. This operation returns properties of each revision of the specified container
// group profile including containers, image registry credentials, restart policy, IP address type, OS type volumes, revision
// number, etc.
//
// Generated from API version 2024-05-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - containerGroupProfileName - The name of the container group profile.
//   - options - ContainerGroupProfileClientListAllRevisionsOptions contains the optional parameters for the ContainerGroupProfileClient.NewListAllRevisionsPager
//     method.
func (client *ContainerGroupProfileClient) NewListAllRevisionsPager(resourceGroupName string, containerGroupProfileName string, options *ContainerGroupProfileClientListAllRevisionsOptions) *runtime.Pager[ContainerGroupProfileClientListAllRevisionsResponse] {
	return runtime.NewPager(runtime.PagingHandler[ContainerGroupProfileClientListAllRevisionsResponse]{
		More: func(page ContainerGroupProfileClientListAllRevisionsResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *ContainerGroupProfileClientListAllRevisionsResponse) (ContainerGroupProfileClientListAllRevisionsResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "ContainerGroupProfileClient.NewListAllRevisionsPager")
			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.listAllRevisionsCreateRequest(ctx, resourceGroupName, containerGroupProfileName, options)
			}, nil)
			if err != nil {
				return ContainerGroupProfileClientListAllRevisionsResponse{}, err
			}
			return client.listAllRevisionsHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listAllRevisionsCreateRequest creates the ListAllRevisions request.
func (client *ContainerGroupProfileClient) listAllRevisionsCreateRequest(ctx context.Context, resourceGroupName string, containerGroupProfileName string, options *ContainerGroupProfileClientListAllRevisionsOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ContainerInstance/containerGroupProfiles/{containerGroupProfileName}/revisions"
	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 containerGroupProfileName == "" {
		return nil, errors.New("parameter containerGroupProfileName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{containerGroupProfileName}", url.PathEscape(containerGroupProfileName))
	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", "2024-05-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listAllRevisionsHandleResponse handles the ListAllRevisions response.
func (client *ContainerGroupProfileClient) listAllRevisionsHandleResponse(resp *http.Response) (ContainerGroupProfileClientListAllRevisionsResponse, error) {
	result := ContainerGroupProfileClientListAllRevisionsResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ContainerGroupProfileListResult); err != nil {
		return ContainerGroupProfileClientListAllRevisionsResponse{}, err
	}
	return result, nil
}
