import math

from pygui.backend.models.threshold_classifier import (
    BAND_HIGH,
    BAND_IN,
    BAND_LOW,
    ThresholdConfig,
    classify,
    classify_all,
    resolve_bounds,
)


def test_classify_about_target_margin():
    assert classify(149.0, target=149.0, margin=1.0) ==BAND_IN      # exactly
    assert classify(149.9, target=149.0, margin=1.0) ==BAND_IN      # within
    assert classify(150.5, target=149.0, margin=1.0) ==BAND_HIGH    # 1.5 above
    assert classify(147.5, target=149.0, margin=1.0) ==BAND_LOW     # 1.5 below


def test_manual_bounds_use_set_point_and_margin():
    cfg = ThresholdConfig(set_point=149.0, margin=1.0, auto=False)
    assert resolve_bounds([200.0, 0.0], cfg) == (149.0, 1.0)

def test_auto_bounds_use_mean_and_sigma():
    cfg = ThresholdConfig(auto=True)
    target, margin = resolve_bounds([148.0, 150.0, 149.0], cfg)
    assert target == 149.0
    assert margin == math.sqrt(2 / 3)

def test_classify_all_manual():
    cfg = ThresholdConfig(set_point=149.0, margin=1.0, auto=False)
    assert classify_all([149.0, 151.0, 147.0], cfg) == [BAND_IN, BAND_HIGH, BAND_LOW]
