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

// ContainerAppsRevisionFunctionsClient contains the methods for the ContainerAppsRevisionFunctions group.
// Don't use this type directly, use NewContainerAppsRevisionFunctionsClient() instead.
//
// Generated from API version 2025-10-02-preview
type ContainerAppsRevisionFunctionsClient struct {
	internal       *arm.Client
	subscriptionID string
}

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

// Get - Get a specific function of a Container App Revision.
//
// Get a specific function of a Container App Revision.
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - containerAppName - Name of the Container App.
//   - revisionName - Name of the Container App Revision.
//   - functionName - Name of the Function.
//   - options - ContainerAppsRevisionFunctionsClientGetOptions contains the optional parameters for the ContainerAppsRevisionFunctionsClient.Get
//     method.
func (client *ContainerAppsRevisionFunctionsClient) Get(ctx context.Context, resourceGroupName string, containerAppName string, revisionName string, functionName string, options *ContainerAppsRevisionFunctionsClientGetOptions) (ContainerAppsRevisionFunctionsClientGetResponse, error) {
	var err error
	const operationName = "ContainerAppsRevisionFunctionsClient.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, containerAppName, revisionName, functionName, options)
	if err != nil {
		return ContainerAppsRevisionFunctionsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ContainerAppsRevisionFunctionsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ContainerAppsRevisionFunctionsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *ContainerAppsRevisionFunctionsClient) getCreateRequest(ctx context.Context, resourceGroupName string, containerAppName string, revisionName string, functionName string, _ *ContainerAppsRevisionFunctionsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.App/containerApps/{containerAppName}/revisions/{revisionName}/functions/{functionName}"
	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 revisionName == "" {
		return nil, errors.New("parameter revisionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{revisionName}", url.PathEscape(revisionName))
	if functionName == "" {
		return nil, errors.New("parameter functionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{functionName}", url.PathEscape(functionName))
	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", version20251002Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// NewListPager - List the functions for a given Container App Revision.
//
// List the functions for a given Container App Revision.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - containerAppName - Name of the Container App.
//   - revisionName - Name of the Container App Revision.
//   - options - ContainerAppsRevisionFunctionsClientListOptions contains the optional parameters for the ContainerAppsRevisionFunctionsClient.NewListPager
//     method.
func (client *ContainerAppsRevisionFunctionsClient) NewListPager(resourceGroupName string, containerAppName string, revisionName string, options *ContainerAppsRevisionFunctionsClientListOptions) *runtime.Pager[ContainerAppsRevisionFunctionsClientListResponse] {
	return runtime.NewPager(runtime.PagingHandler[ContainerAppsRevisionFunctionsClientListResponse]{
		More: func(page ContainerAppsRevisionFunctionsClientListResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *ContainerAppsRevisionFunctionsClientListResponse) (ContainerAppsRevisionFunctionsClientListResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "ContainerAppsRevisionFunctionsClient.NewListPager")
			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.listCreateRequest(ctx, resourceGroupName, containerAppName, revisionName, options)
			}, nil)
			if err != nil {
				return ContainerAppsRevisionFunctionsClientListResponse{}, err
			}
			return client.listHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listCreateRequest creates the List request.
func (client *ContainerAppsRevisionFunctionsClient) listCreateRequest(ctx context.Context, resourceGroupName string, containerAppName string, revisionName string, _ *ContainerAppsRevisionFunctionsClientListOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.App/containerApps/{containerAppName}/revisions/{revisionName}/functions"
	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 revisionName == "" {
		return nil, errors.New("parameter revisionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{revisionName}", url.PathEscape(revisionName))
	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", version20251002Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listHandleResponse handles the List response.
func (client *ContainerAppsRevisionFunctionsClient) listHandleResponse(resp *http.Response) (ContainerAppsRevisionFunctionsClientListResponse, error) {
	result := ContainerAppsRevisionFunctionsClientListResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ContainerAppsFunctionCollection); err != nil {
		return ContainerAppsRevisionFunctionsClientListResponse{}, err
	}
	return result, nil
}
