package profiling

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestRecordTaskExecution(t *testing.T) {
	profiler := NewWorkflowProfiler()
	
	profiler.RecordTaskExecution("wf-1", "task-1", 100, 0.5, 0.3)
	
	profile, exists := profiler.GetProfile("wf-1")
	assert.True(t, exists)
	assert.Equal(t, 1, len(profile.Tasks))
}

func TestGetSlowTasks(t *testing.T) {
	profiler := NewWorkflowProfiler()
	
	profiler.RecordTaskExecution("wf-1", "task-1", 100, 0.5, 0.3)
	profiler.RecordTaskExecution("wf-1", "task-2", 500, 0.8, 0.6)
	profiler.RecordTaskExecution("wf-1", "task-3", 200, 0.4, 0.2)
	
	slow := profiler.GetSlowTasks("wf-1", 2)
	assert.Equal(t, 2, len(slow))
	assert.Equal(t, "task-2", slow[0])
}

func TestGetHighCPUTasks(t *testing.T) {
	profiler := NewWorkflowProfiler()
	
	profiler.RecordTaskExecution("wf-1", "task-1", 100, 0.5, 0.3)
	profiler.RecordTaskExecution("wf-1", "task-2", 200, 0.9, 0.6)
	
	highCPU := profiler.GetHighCPUTasks("wf-1", 0.7)
	assert.Equal(t, 1, len(highCPU))
	assert.Equal(t, "task-2", highCPU[0])
}

func TestGetHighMemTasks(t *testing.T) {
	profiler := NewWorkflowProfiler()
	
	profiler.RecordTaskExecution("wf-1", "task-1", 100, 0.5, 0.3)
	profiler.RecordTaskExecution("wf-1", "task-2", 200, 0.6, 0.9)
	
	highMem := profiler.GetHighMemTasks("wf-1", 0.7)
	assert.Equal(t, 1, len(highMem))
	assert.Equal(t, "task-2", highMem[0])
}

func TestGetOptimizationSuggestions(t *testing.T) {
	profiler := NewWorkflowProfiler()
	
	profiler.RecordTaskExecution("wf-1", "task-1", 100, 0.5, 0.3)
	profiler.RecordTaskExecution("wf-1", "task-2", 200, 0.9, 0.9)
	
	suggestions := profiler.GetOptimizationSuggestions("wf-1")
	assert.Greater(t, len(suggestions), 0)
}

func TestGetProfile(t *testing.T) {
	profiler := NewWorkflowProfiler()
	
	profiler.RecordTaskExecution("wf-1", "task-1", 100, 0.5, 0.3)
	
	profile, exists := profiler.GetProfile("wf-1")
	assert.True(t, exists)
	assert.Equal(t, "wf-1", profile.WorkflowID)
}

func TestGetTaskProfile(t *testing.T) {
	profiler := NewWorkflowProfiler()
	
	profiler.RecordTaskExecution("wf-1", "task-1", 100, 0.5, 0.3)
	
	taskProfile, err := profiler.GetTaskProfile("wf-1", "task-1")
	assert.NoError(t, err)
	assert.Equal(t, 100.0, taskProfile.Duration)
}

func TestTaskNotFound(t *testing.T) {
	profiler := NewWorkflowProfiler()
	
	_, err := profiler.GetTaskProfile("wf-1", "task-999")
	assert.Error(t, err)
}

func TestClear(t *testing.T) {
	profiler := NewWorkflowProfiler()
	
	profiler.RecordTaskExecution("wf-1", "task-1", 100, 0.5, 0.3)
	profiler.Clear()
	
	profile, exists := profiler.GetProfile("wf-1")
	assert.False(t, exists)
	assert.Nil(t, profile)
}

func TestThroughputCalculation(t *testing.T) {
	profiler := NewWorkflowProfiler()
	
	profiler.RecordTaskExecution("wf-1", "task-1", 1000, 0.5, 0.3)
	
	profile, _ := profiler.GetProfile("wf-1")
	taskProfile := profile.Tasks["task-1"]
	
	assert.Equal(t, 1.0, taskProfile.Throughput) // 1000ms = 1 task per second
}

func TestMultipleWorkflows(t *testing.T) {
	profiler := NewWorkflowProfiler()
	
	profiler.RecordTaskExecution("wf-1", "task-1", 100, 0.5, 0.3)
	profiler.RecordTaskExecution("wf-2", "task-1", 200, 0.6, 0.4)
	
	profile1, exists1 := profiler.GetProfile("wf-1")
	profile2, exists2 := profiler.GetProfile("wf-2")
	
	assert.True(t, exists1)
	assert.True(t, exists2)
	assert.Equal(t, 100.0, profile1.TotalDuration)
	assert.Equal(t, 200.0, profile2.TotalDuration)
}
