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

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

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

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

// Get - Get the resource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-06-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - name - Name of the resource
//   - imageName - The name of the Image
//   - artifactName - The name of the Artifact
//   - options - ArtifactsClientGetOptions contains the optional parameters for the ArtifactsClient.Get method.
func (client *ArtifactsClient) Get(ctx context.Context, resourceGroupName string, name string, imageName string, artifactName string, options *ArtifactsClientGetOptions) (ArtifactsClientGetResponse, error) {
	var err error
	const operationName = "ArtifactsClient.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, name, imageName, artifactName, options)
	if err != nil {
		return ArtifactsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ArtifactsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ArtifactsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *ArtifactsClient) getCreateRequest(ctx context.Context, resourceGroupName string, name string, imageName string, artifactName string, _ *ArtifactsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/disconnectedOperations/{name}/images/{imageName}/artifacts/{artifactName}"
	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 name == "" {
		return nil, errors.New("parameter name cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{name}", url.PathEscape(name))
	if imageName == "" {
		return nil, errors.New("parameter imageName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{imageName}", url.PathEscape(imageName))
	if artifactName == "" {
		return nil, errors.New("parameter artifactName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{artifactName}", url.PathEscape(artifactName))
	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-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// NewListByParentPager - List by parent
//
// Generated from API version 2025-06-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - name - Name of the resource
//   - imageName - The name of the Image
//   - options - ArtifactsClientListByParentOptions contains the optional parameters for the ArtifactsClient.NewListByParentPager
//     method.
func (client *ArtifactsClient) NewListByParentPager(resourceGroupName string, name string, imageName string, options *ArtifactsClientListByParentOptions) *runtime.Pager[ArtifactsClientListByParentResponse] {
	return runtime.NewPager(runtime.PagingHandler[ArtifactsClientListByParentResponse]{
		More: func(page ArtifactsClientListByParentResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *ArtifactsClientListByParentResponse) (ArtifactsClientListByParentResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "ArtifactsClient.NewListByParentPager")
			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.listByParentCreateRequest(ctx, resourceGroupName, name, imageName, options)
			}, nil)
			if err != nil {
				return ArtifactsClientListByParentResponse{}, err
			}
			return client.listByParentHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByParentCreateRequest creates the ListByParent request.
func (client *ArtifactsClient) listByParentCreateRequest(ctx context.Context, resourceGroupName string, name string, imageName string, _ *ArtifactsClientListByParentOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/disconnectedOperations/{name}/images/{imageName}/artifacts"
	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 name == "" {
		return nil, errors.New("parameter name cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{name}", url.PathEscape(name))
	if imageName == "" {
		return nil, errors.New("parameter imageName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{imageName}", url.PathEscape(imageName))
	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-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByParentHandleResponse handles the ListByParent response.
func (client *ArtifactsClient) listByParentHandleResponse(resp *http.Response) (ArtifactsClientListByParentResponse, error) {
	result := ArtifactsClientListByParentResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ArtifactListResult); err != nil {
		return ArtifactsClientListByParentResponse{}, err
	}
	return result, nil
}

// ListDownloadURI - Get artifact download link.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-06-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - name - Name of the resource
//   - imageName - The name of the Image
//   - artifactName - The name of the Artifact
//   - options - ArtifactsClientListDownloadURIOptions contains the optional parameters for the ArtifactsClient.ListDownloadURI
//     method.
func (client *ArtifactsClient) ListDownloadURI(ctx context.Context, resourceGroupName string, name string, imageName string, artifactName string, options *ArtifactsClientListDownloadURIOptions) (ArtifactsClientListDownloadURIResponse, error) {
	var err error
	const operationName = "ArtifactsClient.ListDownloadURI"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.listDownloadURICreateRequest(ctx, resourceGroupName, name, imageName, artifactName, options)
	if err != nil {
		return ArtifactsClientListDownloadURIResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ArtifactsClientListDownloadURIResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ArtifactsClientListDownloadURIResponse{}, err
	}
	resp, err := client.listDownloadURIHandleResponse(httpResp)
	return resp, err
}

// listDownloadURICreateRequest creates the ListDownloadURI request.
func (client *ArtifactsClient) listDownloadURICreateRequest(ctx context.Context, resourceGroupName string, name string, imageName string, artifactName string, _ *ArtifactsClientListDownloadURIOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Edge/disconnectedOperations/{name}/images/{imageName}/artifacts/{artifactName}/listDownloadUri"
	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 name == "" {
		return nil, errors.New("parameter name cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{name}", url.PathEscape(name))
	if imageName == "" {
		return nil, errors.New("parameter imageName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{imageName}", url.PathEscape(imageName))
	if artifactName == "" {
		return nil, errors.New("parameter artifactName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{artifactName}", url.PathEscape(artifactName))
	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", "2025-06-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listDownloadURIHandleResponse handles the ListDownloadURI response.
func (client *ArtifactsClient) listDownloadURIHandleResponse(resp *http.Response) (ArtifactsClientListDownloadURIResponse, error) {
	result := ArtifactsClientListDownloadURIResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ArtifactDownloadResult); err != nil {
		return ArtifactsClientListDownloadURIResponse{}, err
	}
	return result, nil
}
