"""Tests for src/pygui/backend/splitter.py."""
from pygui.backend.splitter import DataSegment, segment_profile


def test_all_below_threshold():
    temps = [10.0, 20.0, 30.0]
    result = segment_profile(temps, threshold=40.0)
    assert result == [
        DataSegment(label="Ramp", start_frame=0, end_frame=2, avg_temp=20.0)
    ]


def test_all_above_threshold():
    temps = [50.0, 60.0, 70.0]
    result = segment_profile(temps, threshold=40.0)
    assert result == [
        DataSegment(label="Soak", start_frame=0, end_frame=2, avg_temp=60.0)
    ]


def test_ramp_soak_cool_full_cycle():
    temps = [20.0, 30.0, 50.0, 60.0, 45.0, 35.0]
    result = segment_profile(temps, threshold=40.0)
    assert result == [
        DataSegment(label="Ramp", start_frame=0, end_frame=1, avg_temp=25.0),
        DataSegment(label="Soak", start_frame=2, end_frame=4, avg_temp=51.67),
        DataSegment(label="Cool", start_frame=5, end_frame=5, avg_temp=35.0),
    ]


def test_multiple_oscillations():
    temps = [20.0, 50.0, 30.0, 60.0, 25.0]
    result = segment_profile(temps, threshold=40.0)
    assert result == [
        DataSegment(label="Ramp", start_frame=0, end_frame=0, avg_temp=20.0),
        DataSegment(label="Soak", start_frame=1, end_frame=1, avg_temp=50.0),
        DataSegment(label="Cool", start_frame=2, end_frame=2, avg_temp=30.0),
        DataSegment(label="Soak", start_frame=3, end_frame=3, avg_temp=60.0),
        DataSegment(label="Cool", start_frame=4, end_frame=4, avg_temp=25.0),
    ]


def test_no_cool_phase():
    temps = [20.0, 30.0, 50.0, 60.0]
    result = segment_profile(temps, threshold=40.0)
    assert result == [
        DataSegment(label="Ramp", start_frame=0, end_frame=1, avg_temp=25.0),
        DataSegment(label="Soak", start_frame=2, end_frame=3, avg_temp=55.0),
    ]


def test_default_threshold():
    temps = [20.0, 50.0, 30.0]
    result = segment_profile(temps)
    assert result == [
        DataSegment(label="Ramp", start_frame=0, end_frame=0, avg_temp=20.0),
        DataSegment(label="Soak", start_frame=1, end_frame=1, avg_temp=50.0),
        DataSegment(label="Cool", start_frame=2, end_frame=2, avg_temp=30.0),
    ]


def test_single_frame_below():
    temps = [25.0]
    result = segment_profile(temps, threshold=40.0)
    assert result == [
        DataSegment(label="Ramp", start_frame=0, end_frame=0, avg_temp=25.0),
    ]


def test_single_frame_above():
    temps = [55.0]
    result = segment_profile(temps, threshold=40.0)
    assert result == [
        DataSegment(label="Soak", start_frame=0, end_frame=0, avg_temp=55.0),
    ]


def test_empty_input():
    temps: list[float] = []
    result = segment_profile(temps)
    assert result == []


def test_precision_rounding():
    temps = [10.0, 20.0, 30.0]
    result = segment_profile(temps, threshold=15.0)
    assert result == [
        DataSegment(label="Ramp", start_frame=0, end_frame=0, avg_temp=10.0),
        DataSegment(label="Soak", start_frame=1, end_frame=2, avg_temp=25.0),
    ]
