# Copyright (c) 2025 Patrick Motsch
# All rights reserved.
"""
Password utility functions for secure password handling.
Uses Argon2 for password hashing.
"""

from typing import Optional
from passlib.context import CryptContext

# Password hashing context using Argon2
_pwdContext = CryptContext(schemes=["argon2"], deprecated="auto")


def hashPassword(password: str) -> str:
    """
    Hash a password using Argon2.
    
    Args:
        password: Plain text password to hash
        
    Returns:
        Hashed password string
    """
    return _pwdContext.hash(password)


def verifyPassword(plainPassword: str, hashedPassword: str) -> bool:
    """
    Verify a plain password against a hashed password.
    
    Args:
        plainPassword: Plain text password to verify
        hashedPassword: Hashed password to compare against
        
    Returns:
        True if password matches, False otherwise
    """
    return _pwdContext.verify(plainPassword, hashedPassword)


def getPasswordHash(password: Optional[str]) -> Optional[str]:
    """
    Hash a password, returning None if password is None.
    
    Args:
        password: Plain text password or None
        
    Returns:
        Hashed password or None if input was None
    """
    if password is None:
        return None
    return _pwdContext.hash(password)
