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

import (
	"context"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
	"net/http"
	"strings"
)

// KeyVaultClient - The Azure Key Vault Administration service client performs administrative operations
// including RBAC, BackupRestore, and settings management
// against the Azure Key Vault service.
// Don't use this type directly, use a constructor function instead.
//
// Generated from API version 2026-01-01-preview
type KeyVaultClient struct {
	internal     *azcore.Client
	vaultBaseUrl string
}

// CheckEkmConnection - Checks the connectivity and authentication with the EKM proxy.
//
// The External Key Manager (EKM) Check operation checks the connectivity and authentication with the EKM proxy. This operation
// requires ekm/read permission.
// If the operation fails it returns an *azcore.ResponseError type.
//   - options - CheckEkmConnectionOptions contains the optional parameters for the KeyVaultClient.CheckEkmConnection method.
func (client *KeyVaultClient) CheckEkmConnection(ctx context.Context, options *CheckEkmConnectionOptions) (CheckEkmConnectionResponse, error) {
	var err error
	const operationName = "KeyVaultClient.CheckEkmConnection"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.checkEkmConnectionCreateRequest(ctx, options)
	if err != nil {
		return CheckEkmConnectionResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return CheckEkmConnectionResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return CheckEkmConnectionResponse{}, err
	}
	resp, err := client.checkEkmConnectionHandleResponse(httpResp)
	return resp, err
}

// checkEkmConnectionCreateRequest creates the CheckEkmConnection request.
func (client *KeyVaultClient) checkEkmConnectionCreateRequest(ctx context.Context, _ *CheckEkmConnectionOptions) (*policy.Request, error) {
	urlPath := "/ekm/check"
	req, err := runtime.NewRequest(ctx, http.MethodPost, runtime.JoinPaths(client.vaultBaseUrl, urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20260101Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// checkEkmConnectionHandleResponse handles the CheckEkmConnection response.
func (client *KeyVaultClient) checkEkmConnectionHandleResponse(resp *http.Response) (CheckEkmConnectionResponse, error) {
	result := CheckEkmConnectionResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ProxyInfo); err != nil {
		return CheckEkmConnectionResponse{}, err
	}
	return result, nil
}

// CreateEkmConnection - Creates the EKM connection.
//
// The External Key Manager (EKM) sets up the EKM connection. If the EKM connection already exists, this operation fails.
// This operation requires ekm/write permission.
// If the operation fails it returns an *azcore.ResponseError type.
//   - ekmConnection - The ekmConnection to create.
//   - options - CreateEkmConnectionOptions contains the optional parameters for the KeyVaultClient.CreateEkmConnection method.
func (client *KeyVaultClient) CreateEkmConnection(ctx context.Context, ekmConnection Connection, options *CreateEkmConnectionOptions) (CreateEkmConnectionResponse, error) {
	var err error
	const operationName = "KeyVaultClient.CreateEkmConnection"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.createEkmConnectionCreateRequest(ctx, ekmConnection, options)
	if err != nil {
		return CreateEkmConnectionResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return CreateEkmConnectionResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return CreateEkmConnectionResponse{}, err
	}
	resp, err := client.createEkmConnectionHandleResponse(httpResp)
	return resp, err
}

// createEkmConnectionCreateRequest creates the CreateEkmConnection request.
func (client *KeyVaultClient) createEkmConnectionCreateRequest(ctx context.Context, ekmConnection Connection, _ *CreateEkmConnectionOptions) (*policy.Request, error) {
	urlPath := "/ekm/create"
	req, err := runtime.NewRequest(ctx, http.MethodPost, runtime.JoinPaths(client.vaultBaseUrl, urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20260101Preview)
	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, ekmConnection); err != nil {
		return nil, err
	}
	return req, nil
}

// createEkmConnectionHandleResponse handles the CreateEkmConnection response.
func (client *KeyVaultClient) createEkmConnectionHandleResponse(resp *http.Response) (CreateEkmConnectionResponse, error) {
	result := CreateEkmConnectionResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.Connection); err != nil {
		return CreateEkmConnectionResponse{}, err
	}
	return result, nil
}

// DeleteEkmConnection - Deletes the EKM connection.
//
// The External Key Manager (EKM) deletes the existing EKM connection. If the EKM connection does not already exists, this
// operation fails. This operation requires ekm/delete permission.
// If the operation fails it returns an *azcore.ResponseError type.
//   - options - DeleteEkmConnectionOptions contains the optional parameters for the KeyVaultClient.DeleteEkmConnection method.
func (client *KeyVaultClient) DeleteEkmConnection(ctx context.Context, options *DeleteEkmConnectionOptions) (DeleteEkmConnectionResponse, error) {
	var err error
	const operationName = "KeyVaultClient.DeleteEkmConnection"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.deleteEkmConnectionCreateRequest(ctx, options)
	if err != nil {
		return DeleteEkmConnectionResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return DeleteEkmConnectionResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return DeleteEkmConnectionResponse{}, err
	}
	resp, err := client.deleteEkmConnectionHandleResponse(httpResp)
	return resp, err
}

// deleteEkmConnectionCreateRequest creates the DeleteEkmConnection request.
func (client *KeyVaultClient) deleteEkmConnectionCreateRequest(ctx context.Context, _ *DeleteEkmConnectionOptions) (*policy.Request, error) {
	urlPath := "/ekm"
	req, err := runtime.NewRequest(ctx, http.MethodDelete, runtime.JoinPaths(client.vaultBaseUrl, urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20260101Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// deleteEkmConnectionHandleResponse handles the DeleteEkmConnection response.
func (client *KeyVaultClient) deleteEkmConnectionHandleResponse(resp *http.Response) (DeleteEkmConnectionResponse, error) {
	result := DeleteEkmConnectionResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.Connection); err != nil {
		return DeleteEkmConnectionResponse{}, err
	}
	return result, nil
}

// GetEkmCertificate - Gets the EKM proxy client certificate.
//
// The External Key Manager (EKM) Certificate Get operation returns Proxy client certificate. This operation requires ekm/read
// permission.
// If the operation fails it returns an *azcore.ResponseError type.
//   - options - GetEkmCertificateOptions contains the optional parameters for the KeyVaultClient.GetEkmCertificate method.
func (client *KeyVaultClient) GetEkmCertificate(ctx context.Context, options *GetEkmCertificateOptions) (GetEkmCertificateResponse, error) {
	var err error
	const operationName = "KeyVaultClient.GetEkmCertificate"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getEkmCertificateCreateRequest(ctx, options)
	if err != nil {
		return GetEkmCertificateResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return GetEkmCertificateResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return GetEkmCertificateResponse{}, err
	}
	resp, err := client.getEkmCertificateHandleResponse(httpResp)
	return resp, err
}

// getEkmCertificateCreateRequest creates the GetEkmCertificate request.
func (client *KeyVaultClient) getEkmCertificateCreateRequest(ctx context.Context, _ *GetEkmCertificateOptions) (*policy.Request, error) {
	urlPath := "/ekm/certificate"
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.vaultBaseUrl, urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20260101Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getEkmCertificateHandleResponse handles the GetEkmCertificate response.
func (client *KeyVaultClient) getEkmCertificateHandleResponse(resp *http.Response) (GetEkmCertificateResponse, error) {
	result := GetEkmCertificateResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ProxyClientCertificateInfo); err != nil {
		return GetEkmCertificateResponse{}, err
	}
	return result, nil
}

// GetEkmConnection - Gets the EKM connection.
//
// The External Key Manager (EKM) Get operation returns EKM connection. This operation requires ekm/read permission.
// If the operation fails it returns an *azcore.ResponseError type.
//   - options - GetEkmConnectionOptions contains the optional parameters for the KeyVaultClient.GetEkmConnection method.
func (client *KeyVaultClient) GetEkmConnection(ctx context.Context, options *GetEkmConnectionOptions) (GetEkmConnectionResponse, error) {
	var err error
	const operationName = "KeyVaultClient.GetEkmConnection"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getEkmConnectionCreateRequest(ctx, options)
	if err != nil {
		return GetEkmConnectionResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return GetEkmConnectionResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return GetEkmConnectionResponse{}, err
	}
	resp, err := client.getEkmConnectionHandleResponse(httpResp)
	return resp, err
}

// getEkmConnectionCreateRequest creates the GetEkmConnection request.
func (client *KeyVaultClient) getEkmConnectionCreateRequest(ctx context.Context, _ *GetEkmConnectionOptions) (*policy.Request, error) {
	urlPath := "/ekm"
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.vaultBaseUrl, urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20260101Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getEkmConnectionHandleResponse handles the GetEkmConnection response.
func (client *KeyVaultClient) getEkmConnectionHandleResponse(resp *http.Response) (GetEkmConnectionResponse, error) {
	result := GetEkmConnectionResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.Connection); err != nil {
		return GetEkmConnectionResponse{}, err
	}
	return result, nil
}

// UpdateEkmConnection - Updates the EKM connection.
//
// The External Key Manager (EKM) updates the existing EKM connection. If the EKM connection does not exist, this operation
// fails. This operation requires ekm/write permission.
// If the operation fails it returns an *azcore.ResponseError type.
//   - ekmConnection - The ekmConnection to update.
//   - options - UpdateEkmConnectionOptions contains the optional parameters for the KeyVaultClient.UpdateEkmConnection method.
func (client *KeyVaultClient) UpdateEkmConnection(ctx context.Context, ekmConnection Connection, options *UpdateEkmConnectionOptions) (UpdateEkmConnectionResponse, error) {
	var err error
	const operationName = "KeyVaultClient.UpdateEkmConnection"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.updateEkmConnectionCreateRequest(ctx, ekmConnection, options)
	if err != nil {
		return UpdateEkmConnectionResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return UpdateEkmConnectionResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return UpdateEkmConnectionResponse{}, err
	}
	resp, err := client.updateEkmConnectionHandleResponse(httpResp)
	return resp, err
}

// updateEkmConnectionCreateRequest creates the UpdateEkmConnection request.
func (client *KeyVaultClient) updateEkmConnectionCreateRequest(ctx context.Context, ekmConnection Connection, _ *UpdateEkmConnectionOptions) (*policy.Request, error) {
	urlPath := "/ekm"
	req, err := runtime.NewRequest(ctx, http.MethodPatch, runtime.JoinPaths(client.vaultBaseUrl, urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20260101Preview)
	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, ekmConnection); err != nil {
		return nil, err
	}
	return req, nil
}

// updateEkmConnectionHandleResponse handles the UpdateEkmConnection response.
func (client *KeyVaultClient) updateEkmConnectionHandleResponse(resp *http.Response) (UpdateEkmConnectionResponse, error) {
	result := UpdateEkmConnectionResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.Connection); err != nil {
		return UpdateEkmConnectionResponse{}, err
	}
	return result, nil
}
