# Copyright (c) 2025 CoReason, Inc.
#
# This software is proprietary and dual-licensed.
# Licensed under the Prosperity Public License 3.0 (the "License").
# A copy of the license is available at https://prosperitylicense.com/versions/3.0.0
# For details, see the LICENSE file.
# Commercial use beyond a 30-day trial requires a separate license.
#
# Source Code: https://github.com/CoReason-AI/coreason_vault

from typing import Any, Dict, Optional, Union

import httpx

from coreason_vault.auth_async import VaultAuthenticationAsync
from coreason_vault.cipher_async import TransitCipherAsync
from coreason_vault.config import CoreasonVaultConfig
from coreason_vault.keeper_async import SecretKeeperAsync


class VaultManagerAsync:
    """Async Service Core for Vault operations."""

    def __init__(self, config: Optional[CoreasonVaultConfig] = None, client: Optional[httpx.AsyncClient] = None):
        self.config = config or CoreasonVaultConfig()
        self.auth = VaultAuthenticationAsync(self.config, client=client)
        self.keeper = SecretKeeperAsync(self.auth, self.config)
        self.cipher = TransitCipherAsync(self.auth)

    async def __aenter__(self) -> "VaultManagerAsync":
        # Check authentication on enter?
        # Maybe. Or lazily.
        return self

    async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
        await self.auth.close()

    async def get_secret(self, path: str) -> Dict[str, Any]:
        """Async version of get_secret."""
        return await self.keeper.get_secret(path)

    async def get_dynamic_secret(self, path: str) -> Dict[str, Any]:
        """Async version of get_dynamic_secret."""
        return await self.keeper.get_dynamic_secret(path)

    async def encrypt(self, plaintext: Union[str, bytes], key_name: str, context: Optional[str] = None) -> str:
        """Async encrypt."""
        return await self.cipher.encrypt(plaintext, key_name, context)

    async def decrypt(self, ciphertext: str, key_name: str, context: Optional[str] = None) -> Union[str, bytes]:
        """Async decrypt."""
        return await self.cipher.decrypt(ciphertext, key_name, context)
