from utils.timer import Timer

class ScalingLogic:
    def __init__(self, downscale_threshold, downscale_cooldown):
        self.downscale_threshold = downscale_threshold
        self.downscale_cooldown = downscale_cooldown
        self.downscale_timer = Timer()
        self.below_threshold = False

    def should_downscale(self, current_utilization):
        if current_utilization < self.downscale_threshold:
            if not self.below_threshold:
                self.below_threshold = True
                self.downscale_timer.start()
            else:
                elapsed_time = self.downscale_timer.get_elapsed_time()
                if elapsed_time >= self.downscale_cooldown:
                    self.downscale_timer.reset()
                    self.below_threshold = False
                    return True  # Ready to downscale
                else:
                    return False # Not enough time has passed
        else:
            if self.below_threshold:
                self.downscale_timer.reset()
                self.below_threshold = False
            return False # Above threshold, no downscale
