// Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.9, generator: @autorest/go@4.0.0-preview.74)
// Changes may cause incorrect behavior and will be lost if the code is regenerated.
// Code generated by @autorest/go. DO NOT EDIT.

package armredhatopenshifthcp

import (
	"context"
	"errors"
	"net/http"
	"net/url"
	"strings"

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

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

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

// Get - Get a HcpOpenShiftVersion
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2026-06-30-preview
//   - location - The name of the Azure region.
//   - hcpOpenShiftVersionName - The name of the HcpOpenShiftVersion
//   - options - HcpOpenShiftVersionsClientGetOptions contains the optional parameters for the HcpOpenShiftVersionsClient.Get
//     method.
func (client *HcpOpenShiftVersionsClient) Get(ctx context.Context, location string, hcpOpenShiftVersionName string, options *HcpOpenShiftVersionsClientGetOptions) (HcpOpenShiftVersionsClientGetResponse, error) {
	var err error
	const operationName = "HcpOpenShiftVersionsClient.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, location, hcpOpenShiftVersionName, options)
	if err != nil {
		return HcpOpenShiftVersionsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return HcpOpenShiftVersionsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return HcpOpenShiftVersionsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *HcpOpenShiftVersionsClient) getCreateRequest(ctx context.Context, location string, hcpOpenShiftVersionName string, _ *HcpOpenShiftVersionsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.RedHatOpenShift/locations/{location}/hcpOpenShiftVersions/{hcpOpenShiftVersionName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if location == "" {
		return nil, errors.New("parameter location cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{location}", url.PathEscape(location))
	if hcpOpenShiftVersionName == "" {
		return nil, errors.New("parameter hcpOpenShiftVersionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{hcpOpenShiftVersionName}", url.PathEscape(hcpOpenShiftVersionName))
	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", "2026-06-30-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// NewListPager - List HcpOpenShiftVersion resources by SubscriptionLocationResource
//
// Generated from API version 2026-06-30-preview
//   - location - The name of the Azure region.
//   - options - HcpOpenShiftVersionsClientListOptions contains the optional parameters for the HcpOpenShiftVersionsClient.NewListPager
//     method.
func (client *HcpOpenShiftVersionsClient) NewListPager(location string, options *HcpOpenShiftVersionsClientListOptions) *runtime.Pager[HcpOpenShiftVersionsClientListResponse] {
	return runtime.NewPager(runtime.PagingHandler[HcpOpenShiftVersionsClientListResponse]{
		More: func(page HcpOpenShiftVersionsClientListResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *HcpOpenShiftVersionsClientListResponse) (HcpOpenShiftVersionsClientListResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "HcpOpenShiftVersionsClient.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, location, options)
			}, nil)
			if err != nil {
				return HcpOpenShiftVersionsClientListResponse{}, err
			}
			return client.listHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listCreateRequest creates the List request.
func (client *HcpOpenShiftVersionsClient) listCreateRequest(ctx context.Context, location string, _ *HcpOpenShiftVersionsClientListOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.RedHatOpenShift/locations/{location}/hcpOpenShiftVersions"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if location == "" {
		return nil, errors.New("parameter location cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{location}", url.PathEscape(location))
	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", "2026-06-30-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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