# truewave-platform\backend\apps\platform\accounts\throttles.py
from rest_framework.throttling import SimpleRateThrottle


class CustomSimpleRateThrottle(SimpleRateThrottle):
    """
    Custom throttle to handle cache key generation.
    Uses user ID if authenticated, else IP address.
    """

    def get_cache_key(self, request, view):
        if request.user and request.user.is_authenticated:
            ident = request.user.id
        else:
            ident = self.get_ident(request)

        return self.cache_format % {
            'scope': self.scope,
            'ident': ident
        }


class LoginRateThrottle(CustomSimpleRateThrottle):
    scope = 'login'


class OTPRateThrottle(CustomSimpleRateThrottle):
    scope = 'otp'


class PurchaseRateThrottle(CustomSimpleRateThrottle):
    scope = 'purchase'


class WithdrawalRateThrottle(CustomSimpleRateThrottle):
    scope = 'withdrawal'