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

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

// EduEnrollmentsClient contains the methods for the EduEnrollments group.
// Don't use this type directly, use NewEduEnrollmentsClient() instead.
//
// Generated from API version 2026-03-01-preview
type EduEnrollmentsClient struct {
	internal       *arm.Client
	subscriptionID string
}

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

// BeginCreateOrUpdate - Creates or updates the specified edu enrollment.
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - enrollmentName - The name of the edu enrollment
//   - resource - Resource create parameters.
//   - options - EduEnrollmentsClientBeginCreateOrUpdateOptions contains the optional parameters for the EduEnrollmentsClient.BeginCreateOrUpdate
//     method.
func (client *EduEnrollmentsClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, enrollmentName string, resource EduEnrollment, options *EduEnrollmentsClientBeginCreateOrUpdateOptions) (*runtime.Poller[EduEnrollmentsClientCreateOrUpdateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.createOrUpdate(ctx, resourceGroupName, enrollmentName, resource, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[EduEnrollmentsClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[EduEnrollmentsClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// CreateOrUpdate - Creates or updates the specified edu enrollment.
// If the operation fails it returns an *azcore.ResponseError type.
func (client *EduEnrollmentsClient) createOrUpdate(ctx context.Context, resourceGroupName string, enrollmentName string, resource EduEnrollment, options *EduEnrollmentsClientBeginCreateOrUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "EduEnrollmentsClient.BeginCreateOrUpdate"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, enrollmentName, resource, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// createOrUpdateCreateRequest creates the CreateOrUpdate request.
func (client *EduEnrollmentsClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, enrollmentName string, resource EduEnrollment, _ *EduEnrollmentsClientBeginCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ProgramEnrollment/eduEnrollments/{enrollmentName}"
	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 enrollmentName == "" {
		return nil, errors.New("parameter enrollmentName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{enrollmentName}", url.PathEscape(enrollmentName))
	req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20260301Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, resource); err != nil {
		return nil, err
	}
	return req, nil
}

// BeginDelete - Deletes the specified edu enrollment.
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - enrollmentName - The name of the edu enrollment
//   - options - EduEnrollmentsClientBeginDeleteOptions contains the optional parameters for the EduEnrollmentsClient.BeginDelete
//     method.
func (client *EduEnrollmentsClient) BeginDelete(ctx context.Context, resourceGroupName string, enrollmentName string, options *EduEnrollmentsClientBeginDeleteOptions) (*runtime.Poller[EduEnrollmentsClientDeleteResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.deleteOperation(ctx, resourceGroupName, enrollmentName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[EduEnrollmentsClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[EduEnrollmentsClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Delete - Deletes the specified edu enrollment.
// If the operation fails it returns an *azcore.ResponseError type.
func (client *EduEnrollmentsClient) deleteOperation(ctx context.Context, resourceGroupName string, enrollmentName string, options *EduEnrollmentsClientBeginDeleteOptions) (*http.Response, error) {
	var err error
	const operationName = "EduEnrollmentsClient.BeginDelete"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.deleteCreateRequest(ctx, resourceGroupName, enrollmentName, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusAccepted, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// deleteCreateRequest creates the Delete request.
func (client *EduEnrollmentsClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, enrollmentName string, _ *EduEnrollmentsClientBeginDeleteOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ProgramEnrollment/eduEnrollments/{enrollmentName}"
	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 enrollmentName == "" {
		return nil, errors.New("parameter enrollmentName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{enrollmentName}", url.PathEscape(enrollmentName))
	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", version20260301Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	return req, nil
}

// Get - Gets the specified edu enrollment.
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - enrollmentName - The name of the edu enrollment
//   - options - EduEnrollmentsClientGetOptions contains the optional parameters for the EduEnrollmentsClient.Get method.
func (client *EduEnrollmentsClient) Get(ctx context.Context, resourceGroupName string, enrollmentName string, options *EduEnrollmentsClientGetOptions) (EduEnrollmentsClientGetResponse, error) {
	var err error
	const operationName = "EduEnrollmentsClient.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, enrollmentName, options)
	if err != nil {
		return EduEnrollmentsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return EduEnrollmentsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return EduEnrollmentsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *EduEnrollmentsClient) getCreateRequest(ctx context.Context, resourceGroupName string, enrollmentName string, _ *EduEnrollmentsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ProgramEnrollment/eduEnrollments/{enrollmentName}"
	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 enrollmentName == "" {
		return nil, errors.New("parameter enrollmentName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{enrollmentName}", url.PathEscape(enrollmentName))
	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", version20260301Preview)
	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 *EduEnrollmentsClient) getHandleResponse(resp *http.Response) (EduEnrollmentsClientGetResponse, error) {
	result := EduEnrollmentsClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.EduEnrollment); err != nil {
		return EduEnrollmentsClientGetResponse{}, err
	}
	return result, nil
}

// NewListByResourceGroupPager - Lists the edu enrollments in a resource group.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - options - EduEnrollmentsClientListByResourceGroupOptions contains the optional parameters for the EduEnrollmentsClient.NewListByResourceGroupPager
//     method.
func (client *EduEnrollmentsClient) NewListByResourceGroupPager(resourceGroupName string, options *EduEnrollmentsClientListByResourceGroupOptions) *runtime.Pager[EduEnrollmentsClientListByResourceGroupResponse] {
	return runtime.NewPager(runtime.PagingHandler[EduEnrollmentsClientListByResourceGroupResponse]{
		More: func(page EduEnrollmentsClientListByResourceGroupResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *EduEnrollmentsClientListByResourceGroupResponse) (EduEnrollmentsClientListByResourceGroupResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "EduEnrollmentsClient.NewListByResourceGroupPager")
			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.listByResourceGroupCreateRequest(ctx, resourceGroupName, options)
			}, nil)
			if err != nil {
				return EduEnrollmentsClientListByResourceGroupResponse{}, err
			}
			return client.listByResourceGroupHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByResourceGroupCreateRequest creates the ListByResourceGroup request.
func (client *EduEnrollmentsClient) listByResourceGroupCreateRequest(ctx context.Context, resourceGroupName string, _ *EduEnrollmentsClientListByResourceGroupOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ProgramEnrollment/eduEnrollments"
	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))
	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", version20260301Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByResourceGroupHandleResponse handles the ListByResourceGroup response.
func (client *EduEnrollmentsClient) listByResourceGroupHandleResponse(resp *http.Response) (EduEnrollmentsClientListByResourceGroupResponse, error) {
	result := EduEnrollmentsClientListByResourceGroupResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.EduEnrollmentListResult); err != nil {
		return EduEnrollmentsClientListByResourceGroupResponse{}, err
	}
	return result, nil
}

// NewListBySubscriptionPager - Lists the edu enrollments in a subscription.
//   - options - EduEnrollmentsClientListBySubscriptionOptions contains the optional parameters for the EduEnrollmentsClient.NewListBySubscriptionPager
//     method.
func (client *EduEnrollmentsClient) NewListBySubscriptionPager(options *EduEnrollmentsClientListBySubscriptionOptions) *runtime.Pager[EduEnrollmentsClientListBySubscriptionResponse] {
	return runtime.NewPager(runtime.PagingHandler[EduEnrollmentsClientListBySubscriptionResponse]{
		More: func(page EduEnrollmentsClientListBySubscriptionResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *EduEnrollmentsClientListBySubscriptionResponse) (EduEnrollmentsClientListBySubscriptionResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "EduEnrollmentsClient.NewListBySubscriptionPager")
			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.listBySubscriptionCreateRequest(ctx, options)
			}, nil)
			if err != nil {
				return EduEnrollmentsClientListBySubscriptionResponse{}, err
			}
			return client.listBySubscriptionHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listBySubscriptionCreateRequest creates the ListBySubscription request.
func (client *EduEnrollmentsClient) listBySubscriptionCreateRequest(ctx context.Context, _ *EduEnrollmentsClientListBySubscriptionOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.ProgramEnrollment/eduEnrollments"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	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", version20260301Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listBySubscriptionHandleResponse handles the ListBySubscription response.
func (client *EduEnrollmentsClient) listBySubscriptionHandleResponse(resp *http.Response) (EduEnrollmentsClientListBySubscriptionResponse, error) {
	result := EduEnrollmentsClientListBySubscriptionResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.EduEnrollmentListResult); err != nil {
		return EduEnrollmentsClientListBySubscriptionResponse{}, err
	}
	return result, nil
}

// Update - Updates the specified edu enrollment.
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - enrollmentName - The name of the edu enrollment
//   - properties - The resource properties to be updated.
//   - options - EduEnrollmentsClientUpdateOptions contains the optional parameters for the EduEnrollmentsClient.Update method.
func (client *EduEnrollmentsClient) Update(ctx context.Context, resourceGroupName string, enrollmentName string, properties EduEnrollmentPatch, options *EduEnrollmentsClientUpdateOptions) (EduEnrollmentsClientUpdateResponse, error) {
	var err error
	const operationName = "EduEnrollmentsClient.Update"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.updateCreateRequest(ctx, resourceGroupName, enrollmentName, properties, options)
	if err != nil {
		return EduEnrollmentsClientUpdateResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return EduEnrollmentsClientUpdateResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return EduEnrollmentsClientUpdateResponse{}, err
	}
	resp, err := client.updateHandleResponse(httpResp)
	return resp, err
}

// updateCreateRequest creates the Update request.
func (client *EduEnrollmentsClient) updateCreateRequest(ctx context.Context, resourceGroupName string, enrollmentName string, properties EduEnrollmentPatch, _ *EduEnrollmentsClientUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ProgramEnrollment/eduEnrollments/{enrollmentName}"
	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 enrollmentName == "" {
		return nil, errors.New("parameter enrollmentName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{enrollmentName}", url.PathEscape(enrollmentName))
	req, err := runtime.NewRequest(ctx, http.MethodPatch, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20260301Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, properties); err != nil {
		return nil, err
	}
	return req, nil
}

// updateHandleResponse handles the Update response.
func (client *EduEnrollmentsClient) updateHandleResponse(resp *http.Response) (EduEnrollmentsClientUpdateResponse, error) {
	result := EduEnrollmentsClientUpdateResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.EduEnrollment); err != nil {
		return EduEnrollmentsClientUpdateResponse{}, err
	}
	return result, nil
}
