package telemetry

import (
	"testing"

	"github.com/jfraeys/fetch_ml/internal/telemetry"
)

func TestDiffIO(t *testing.T) {
	t.Parallel() // Enable parallel execution

	// Test normal case where after > before
	before := telemetry.IOStats{
		ReadBytes:  1000,
		WriteBytes: 500,
	}

	after := telemetry.IOStats{
		ReadBytes:  1500,
		WriteBytes: 800,
	}

	delta := telemetry.DiffIO(before, after)

	if delta.ReadBytes != 500 {
		t.Errorf("Expected read delta 500, got %d", delta.ReadBytes)
	}

	if delta.WriteBytes != 300 {
		t.Errorf("Expected write delta 300, got %d", delta.WriteBytes)
	}
}

func TestDiffIOZero(t *testing.T) {
	t.Parallel() // Enable parallel execution

	// Test case where no change
	stats := telemetry.IOStats{
		ReadBytes:  1000,
		WriteBytes: 500,
	}

	delta := telemetry.DiffIO(stats, stats)

	if delta.ReadBytes != 0 {
		t.Errorf("Expected read delta 0, got %d", delta.ReadBytes)
	}

	if delta.WriteBytes != 0 {
		t.Errorf("Expected write delta 0, got %d", delta.WriteBytes)
	}
}

func TestDiffIONegative(t *testing.T) {
	t.Parallel() // Enable parallel execution

	// Test case where after < before (should result in 0)
	before := telemetry.IOStats{
		ReadBytes:  1500,
		WriteBytes: 800,
	}

	after := telemetry.IOStats{
		ReadBytes:  1000,
		WriteBytes: 500,
	}

	delta := telemetry.DiffIO(before, after)

	// Should be 0 when after < before
	if delta.ReadBytes != 0 {
		t.Errorf("Expected read delta 0, got %d", delta.ReadBytes)
	}

	if delta.WriteBytes != 0 {
		t.Errorf("Expected write delta 0, got %d", delta.WriteBytes)
	}
}

func TestDiffIOEmpty(t *testing.T) {
	t.Parallel() // Enable parallel execution

	// Test case with empty stats
	before := telemetry.IOStats{}
	after := telemetry.IOStats{
		ReadBytes:  1000,
		WriteBytes: 500,
	}

	delta := telemetry.DiffIO(before, after)

	if delta.ReadBytes != 1000 {
		t.Errorf("Expected read delta 1000, got %d", delta.ReadBytes)
	}

	if delta.WriteBytes != 500 {
		t.Errorf("Expected write delta 500, got %d", delta.WriteBytes)
	}
}

func TestDiffIOReverse(t *testing.T) {
	t.Parallel() // Enable parallel execution

	// Test case with empty stats
	before := telemetry.IOStats{
		ReadBytes:  1000,
		WriteBytes: 500,
	}
	after := telemetry.IOStats{}

	delta := telemetry.DiffIO(before, after)

	// Should be 0 when after < before
	if delta.ReadBytes != 0 {
		t.Errorf("Expected read delta 0, got %d", delta.ReadBytes)
	}

	if delta.WriteBytes != 0 {
		t.Errorf("Expected write delta 0, got %d", delta.WriteBytes)
	}
}
