"""Tests para trend_slope."""

import random

from trend_slope import trend_slope


def test_increasing_series_slope_positive_up_significant():
    values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    result = trend_slope(values)
    assert result["slope"] is not None
    assert result["slope"] > 0
    assert result["direction"] == "up"
    assert result["significant"] is True
    assert result["n"] == 10


def test_decreasing_series_slope_negative_down_significant():
    values = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
    result = trend_slope(values)
    assert result["slope"] < 0
    assert result["direction"] == "down"
    assert result["significant"] is True


def test_flat_constant_series_not_significant():
    values = [5.0] * 12
    result = trend_slope(values)
    assert result["direction"] == "flat"
    assert result["significant"] is False


def test_random_series_flat_not_significant():
    rng = random.Random(42)
    values = [rng.gauss(0, 1) for _ in range(60)]
    result = trend_slope(values)
    assert result["direction"] == "flat"
    assert result["significant"] is False


def test_custom_x_axis():
    x = [0, 10, 20, 30, 40]
    values = [1, 3, 5, 7, 9]
    result = trend_slope(values, x)
    assert result["slope"] > 0
    assert result["direction"] == "up"
    assert abs(result["r_squared"] - 1.0) < 1e-9


def test_too_few_pairs_returns_none_slope():
    result = trend_slope([1, 2])
    assert result["slope"] is None
    assert result["direction"] == "unknown"
    assert result["significant"] is False
    assert result["n"] == 2


def test_drops_none_and_nan_pairs():
    values = [1, None, 3, float("nan"), 5, 6, 7]
    result = trend_slope(values)
    assert result["n"] == 5
    assert result["slope"] > 0
    assert result["direction"] == "up"


def test_too_few_valid_pairs_after_dropping():
    values = [1, None, None, float("nan"), 5]
    result = trend_slope(values)
    assert result["slope"] is None
    assert result["direction"] == "unknown"
    assert result["n"] == 2
