import logging

logger = logging.getLogger(__name__)

class ThresholdManager:
    def __init__(self, thresholds, hysteresis_percentages):
        """
        Initializes the ThresholdManager.

        Args:
            thresholds (list): A list of threshold values (numeric).
            hysteresis_percentages (list): A list of hysteresis percentages corresponding to each threshold.
                                        Must be one element shorter than thresholds.
        """
        if len(thresholds) != len(hysteresis_percentages) + 1:
            raise ValueError("Number of thresholds must be one more than the number of hysteresis percentages.")

        self.thresholds = thresholds
        self.hysteresis_percentages = hysteresis_percentages
        self.current_threshold_index = 0

    def get_current_threshold(self):
        """Returns the current threshold value."""
        return self.thresholds[self.current_threshold_index]

    def check_threshold(self, metric_value):
        """
        Checks if the metric value exceeds the current threshold and transitions to a higher or lower threshold if necessary.

        Args:
            metric_value (float): The value of the metric being checked.
        """
        current_threshold = self.thresholds[self.current_threshold_index]

        # Check for transition to higher threshold
        if self.current_threshold_index < len(self.thresholds) - 1:
            upper_threshold = self.thresholds[self.current_threshold_index + 1]
            hysteresis = upper_threshold * (self.hysteresis_percentages[self.current_threshold_index] / 100.0)
            if metric_value > upper_threshold + hysteresis:
                logger.info(f"Threshold transition: Metric value {metric_value} exceeded upper threshold {upper_threshold} + hysteresis {hysteresis}. Moving to threshold {self.thresholds[self.current_threshold_index + 1]}")
                self.current_threshold_index += 1
                return

        # Check for transition to lower threshold
        if self.current_threshold_index > 0:
            lower_threshold = self.thresholds[self.current_threshold_index - 1]
            hysteresis = lower_threshold * (self.hysteresis_percentages[self.current_threshold_index - 1] / 100.0)
            if metric_value < lower_threshold - hysteresis:
                logger.info(f"Threshold transition: Metric value {metric_value} fell below lower threshold {lower_threshold} - hysteresis {hysteresis}. Moving to threshold {self.thresholds[self.current_threshold_index - 1]}")
                self.current_threshold_index -= 1
                return