using Azure;
using Azure.Security.KeyVault.Secrets;
namespace Core.Blueprint.KeyVault
{
    /// 
    /// Provides operations for managing secrets in Azure Key Vault.
    /// 
    public sealed class KeyVaultProvider(SecretClient keyVaultProvider): IKeyVaultProvider
    {
        /// 
        /// Creates a new secret in Azure Key Vault.
        /// 
        /// The request containing the name and value of the secret.
        /// The cancellation token to cancel the operation.
        /// A  containing the details of the created secret.
        public async ValueTask CreateSecretAsync(KeyVaultRequest keyVaultRequest, CancellationToken cancellationToken)
        {
            KeyVaultResponse _response = new();
            KeyVaultSecret azureResponse = await keyVaultProvider.SetSecretAsync(new KeyVaultSecret(keyVaultRequest.Name, keyVaultRequest.Value), cancellationToken);
            _response.Value = azureResponse.Value;
            _response.Name = azureResponse.Name;
            return _response;
        }
        /// 
        /// Deletes a secret from Azure Key Vault if it exists.
        /// 
        /// The name of the secret to delete.
        /// The cancellation token to cancel the operation.
        /// 
        /// A  containing a status message and a boolean indicating whether the secret was successfully deleted.
        /// 
        public async ValueTask> DeleteSecretAsync(string secretName, CancellationToken cancellationToken)
        {
            var existingSecret = await this.GetSecretAsync(secretName, cancellationToken);
            if (existingSecret != null)
            {
                await keyVaultProvider.StartDeleteSecretAsync(secretName, cancellationToken);
                return new("Key Deleted", true);
            }
            return new("Key Not Found", false);
        }
        /// 
        /// Retrieves a secret from Azure Key Vault.
        /// 
        /// The name of the secret to retrieve.
        /// The cancellation token to cancel the operation.
        /// 
        /// A  containing the  with secret details 
        /// and an optional error message if the secret was not found.
        /// 
        public async ValueTask> GetSecretAsync(string secretName, CancellationToken cancellationToken)
        {
            KeyVaultSecret azureResponse = await keyVaultProvider.GetSecretAsync(secretName, cancellationToken: cancellationToken);
            if (azureResponse == null)
            {
                return new(new KeyVaultResponse(), "Key Not Found");
            }
            return new(new KeyVaultResponse { Name = secretName, Value = azureResponse.Value }, string.Empty);
        }
        /// 
        /// Updates an existing secret in Azure Key Vault. If the secret does not exist, an error is returned.
        /// 
        /// The updated secret information.
        /// The cancellation token to cancel the operation.
        /// 
        /// A  containing the updated  and an optional error message if the secret was not found.
        /// 
        public async ValueTask> UpdateSecretAsync(KeyVaultRequest newSecret, CancellationToken cancellationToken)
        {
            KeyVaultResponse _response = new();
            var existingSecret = await this.GetSecretAsync(newSecret.Name, cancellationToken);
            if (existingSecret == null)
            {
                return new(new KeyVaultResponse(), "Key Not Found");
            }
            KeyVaultSecret azureResponse = await keyVaultProvider.SetSecretAsync(new KeyVaultSecret(newSecret.Name, newSecret.Value), cancellationToken);
            _response.Value = azureResponse.Value;
            _response.Name = azureResponse.Name;
            return new(new KeyVaultResponse { Name = newSecret.Name, Value = azureResponse.Value }, string.Empty);
        }
    }
}