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

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

// AttributeNamespacesClient contains the methods for the AttributeNamespaces group.
// Don't use this type directly, use NewAttributeNamespacesClient() instead.
//
// Generated from API version 2025-12-01-preview
type AttributeNamespacesClient struct {
	internal *arm.Client
}

// NewAttributeNamespacesClient creates a new instance of AttributeNamespacesClient with the specified values.
//   - credential - used to authorize requests. Usually a credential from azidentity.
//   - options - Contains optional client configuration. Pass nil to accept the default values.
func NewAttributeNamespacesClient(credential azcore.TokenCredential, options *arm.ClientOptions) (*AttributeNamespacesClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &AttributeNamespacesClient{
		internal: cl,
	}
	return client, nil
}

// Create - Creates a new attribute namespace.
// If the operation fails it returns an *azcore.ResponseError type.
//   - attributeNamespace - The name of the attribute namespace to get.
//   - parameters - Parameters for creating the attribute namespace.
//   - options - AttributeNamespacesClientCreateOptions contains the optional parameters for the AttributeNamespacesClient.Create
//     method.
func (client *AttributeNamespacesClient) Create(ctx context.Context, attributeNamespace string, parameters AttributeNamespaceCreateRequest, options *AttributeNamespacesClientCreateOptions) (AttributeNamespacesClientCreateResponse, error) {
	var err error
	const operationName = "AttributeNamespacesClient.Create"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.createCreateRequest(ctx, attributeNamespace, parameters, options)
	if err != nil {
		return AttributeNamespacesClientCreateResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return AttributeNamespacesClientCreateResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return AttributeNamespacesClientCreateResponse{}, err
	}
	resp, err := client.createHandleResponse(httpResp)
	return resp, err
}

// createCreateRequest creates the Create request.
func (client *AttributeNamespacesClient) createCreateRequest(ctx context.Context, attributeNamespace string, parameters AttributeNamespaceCreateRequest, _ *AttributeNamespacesClientCreateOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Authorization/attributeNamespaces/{attributeNamespace}/action"
	if attributeNamespace == "" {
		return nil, errors.New("parameter attributeNamespace cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{attributeNamespace}", url.PathEscape(attributeNamespace))
	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", version20251201Preview)
	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, parameters); err != nil {
		return nil, err
	}
	return req, nil
}

// createHandleResponse handles the Create response.
func (client *AttributeNamespacesClient) createHandleResponse(resp *http.Response) (AttributeNamespacesClientCreateResponse, error) {
	result := AttributeNamespacesClientCreateResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.AttributeNamespace); err != nil {
		return AttributeNamespacesClientCreateResponse{}, err
	}
	return result, nil
}

// Delete - Deletes the specified attribute namespace.
// If the operation fails it returns an *azcore.ResponseError type.
//   - attributeNamespace - The name of the attribute namespace to get.
//   - options - AttributeNamespacesClientDeleteOptions contains the optional parameters for the AttributeNamespacesClient.Delete
//     method.
func (client *AttributeNamespacesClient) Delete(ctx context.Context, attributeNamespace string, options *AttributeNamespacesClientDeleteOptions) (AttributeNamespacesClientDeleteResponse, error) {
	var err error
	const operationName = "AttributeNamespacesClient.Delete"
	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, attributeNamespace, options)
	if err != nil {
		return AttributeNamespacesClientDeleteResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return AttributeNamespacesClientDeleteResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return AttributeNamespacesClientDeleteResponse{}, err
	}
	return AttributeNamespacesClientDeleteResponse{}, nil
}

// deleteCreateRequest creates the Delete request.
func (client *AttributeNamespacesClient) deleteCreateRequest(ctx context.Context, attributeNamespace string, _ *AttributeNamespacesClientDeleteOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Authorization/attributeNamespaces/{attributeNamespace}"
	if attributeNamespace == "" {
		return nil, errors.New("parameter attributeNamespace cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{attributeNamespace}", url.PathEscape(attributeNamespace))
	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", version20251201Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	return req, nil
}

// Get - Gets the specified attribute namespace.
// If the operation fails it returns an *azcore.ResponseError type.
//   - attributeNamespace - The name of the attribute namespace to get.
//   - options - AttributeNamespacesClientGetOptions contains the optional parameters for the AttributeNamespacesClient.Get method.
func (client *AttributeNamespacesClient) Get(ctx context.Context, attributeNamespace string, options *AttributeNamespacesClientGetOptions) (AttributeNamespacesClientGetResponse, error) {
	var err error
	const operationName = "AttributeNamespacesClient.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, attributeNamespace, options)
	if err != nil {
		return AttributeNamespacesClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return AttributeNamespacesClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return AttributeNamespacesClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *AttributeNamespacesClient) getCreateRequest(ctx context.Context, attributeNamespace string, _ *AttributeNamespacesClientGetOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Authorization/attributeNamespaces/{attributeNamespace}"
	if attributeNamespace == "" {
		return nil, errors.New("parameter attributeNamespace cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{attributeNamespace}", url.PathEscape(attributeNamespace))
	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", version20251201Preview)
	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 *AttributeNamespacesClient) getHandleResponse(resp *http.Response) (AttributeNamespacesClientGetResponse, error) {
	result := AttributeNamespacesClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.AttributeNamespace); err != nil {
		return AttributeNamespacesClientGetResponse{}, err
	}
	return result, nil
}
