"""Token encryption utilities using Fernet symmetric encryption."""

from cryptography.fernet import Fernet
from src.config import settings


def encrypt_token(token: str) -> str:
    """
    Encrypt a token string using Fernet symmetric encryption.

    Args:
        token: The plaintext token to encrypt

    Returns:
        Base64-encoded encrypted token string
    """
    if not settings.encryption_key:
        raise ValueError("ENCRYPTION_KEY not configured in environment")

    cipher = Fernet(settings.encryption_key.encode())
    return cipher.encrypt(token.encode()).decode()


def decrypt_token(encrypted: str) -> str:
    """
    Decrypt a Fernet-encrypted token string.

    Args:
        encrypted: The base64-encoded encrypted token

    Returns:
        The decrypted plaintext token
    """
    if not settings.encryption_key:
        raise ValueError("ENCRYPTION_KEY not configured in environment")

    cipher = Fernet(settings.encryption_key.encode())
    return cipher.decrypt(encrypted.encode()).decode()
