from pygui.backend.models.frame import Frame
from pygui.backend.models.session_model import SessionModel
from pygui.backend.models.threshold_classifier import BAND_HIGH, BAND_IN, BAND_LOW, ThresholdConfig


def test_manual_banding_and_stats():
    model = SessionModel(ThresholdConfig(set_point=149.0, margin=1.0, auto=False))
    update = model.process(Frame(seq=7, time=1.0, values=[151.0, 149.0, 147.0]))
    assert update.seq == 7
    assert update.bands == [BAND_HIGH, BAND_IN, BAND_LOW]
    assert update.stats.avg == 149.0
    assert (update.target, update.margin) == (149.0, 1.0)
    assert update.state in ("idle", "ramp", "set")

def test_auto_banding_uses_mean_and_sigma():
    model = SessionModel(ThresholdConfig(auto=True))
    # Tight cluster around 149 -> small σ -> the 200.0 outlier reads HIGH
    update = model.process(Frame(seq=0, time=0.0, values=[149.0, 149.0, 149.0, 200.0]))
    assert update.bands[3] == BAND_HIGH
    assert update.target == update.stats.avg

def test_set_thresholds_reband_on_next_frame():
    m = SessionModel(ThresholdConfig(set_point=149.0, margin=1.0, auto=False))
    m.set_thresholds(ThresholdConfig(set_point=149.0, margin=5.0, auto=False))
    upd = m.process(Frame(seq=0, time=0.0, values=[151.0]))
    assert upd.bands == [BAND_IN]
