"""Wallet service utility for credit balance operations."""

from django_wallet_utils import WalletService, WalletRepository
from django_wallet_utils.transaction_types import register_custom_transaction_type

from apps.users.models import User

# Register custom transaction types
MAKE_INVESTMENT = 10000
WITHDRAWAL = 10001

register_custom_transaction_type("make-investment", MAKE_INVESTMENT)
register_custom_transaction_type("withdrawal", WITHDRAWAL)

# Point types configuration - matches User model field precision
POINT_TYPES = {
    "credit_balance": 8,  # 8 decimal places as per User.credit_balance field
    "bonus": 8,
    "bonus_balance": 8,  # Alias for bonus to match User model field name
}

# Singleton wallet service instance
_wallet_service: WalletService | None = None


def get_wallet_service() -> WalletService:
    """
    Get or create the singleton WalletService instance.
    
    Returns:
        WalletService: Configured wallet service instance
    """
    global _wallet_service
    
    if _wallet_service is None:
        repo = WalletRepository(
            user_model=User,
            point_types=POINT_TYPES,
            point_type_field_map={
                "bonus": "bonus_balance",
                "bonus_balance": "bonus_balance",  # Support both names
            },
        )
        _wallet_service = WalletService(repo)
    
    return _wallet_service
