package datascience

import (
	"math"
	"testing"
)

func TestMetricsDrift(t *testing.T) {
	t.Run("historico vacio retorna drift 0 baseline 0", func(t *testing.T) {
		drift, baseline := MetricsDrift([]int64{}, 100, 0.5)
		if drift != 0 || baseline != 0 {
			t.Errorf("expected drift=0 baseline=0, got drift=%v baseline=%v", drift, baseline)
		}
	})

	t.Run("baseline cero retorna drift 0 baseline 0", func(t *testing.T) {
		// todos ceros -> percentil = 0 -> baseline = 0
		drift, baseline := MetricsDrift([]int64{0, 0, 0}, 50, 0.5)
		if drift != 0 || baseline != 0 {
			t.Errorf("expected drift=0 baseline=0 when baseline is zero, got drift=%v baseline=%v", drift, baseline)
		}
	})

	t.Run("drift positivo cuando current supera baseline", func(t *testing.T) {
		// historico: [100,100,100,100,100], mediana=100
		// current=147 -> drift=0.47
		drift, baseline := MetricsDrift([]int64{100, 100, 100, 100, 100}, 147, 0.5)
		if baseline != 100 {
			t.Errorf("expected baseline=100, got %v", baseline)
		}
		if math.Abs(drift-0.47) > 1e-9 {
			t.Errorf("expected drift=0.47, got %v", drift)
		}
	})

	t.Run("drift negativo cuando current es menor que baseline", func(t *testing.T) {
		// historico: [200,200,200], mediana=200
		// current=100 -> drift=-0.5
		drift, baseline := MetricsDrift([]int64{200, 200, 200}, 100, 0.5)
		if baseline != 200 {
			t.Errorf("expected baseline=200, got %v", baseline)
		}
		if math.Abs(drift-(-0.5)) > 1e-9 {
			t.Errorf("expected drift=-0.5, got %v", drift)
		}
	})
}
