package benchmarks

import (
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"testing"
	"time"
)

// BenchmarkResolveWorkspacePath profiles path canonicalization hot path
// Tier 2 C++ candidate: SIMD string operations for path validation
func BenchmarkResolveWorkspacePath(b *testing.B) {
	testPaths := []string{
		"my-workspace",
		"./relative/path",
		"/absolute/path/to/workspace",
		"  trimmed-path  ",
		"deep/nested/workspace/name",
	}

	b.ReportAllocs()

	for i := 0; b.Loop(); i++ {
		path := testPaths[i%len(testPaths)]
		// Simulate the resolveWorkspacePath logic inline
		ws := strings.TrimSpace(path)
		clean := filepath.Clean(ws)
		if !filepath.IsAbs(clean) {
			clean = filepath.Join("/data/active/workspaces", clean)
		}
		_ = clean
	}
}

// BenchmarkStringTrimSpace profiles strings.TrimSpace usage
// Heavy usage in service_manager.go (55+ calls)
func BenchmarkStringTrimSpace(b *testing.B) {
	testInputs := []string{
		"  package-name  ",
		"\t\n trimmed \r\n",
		"normal",
		"   ",
		"",
		"pkg1==1.0.0",
	}

	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; b.Loop(); i++ {
		_ = strings.TrimSpace(testInputs[i%len(testInputs)])
	}
}

// BenchmarkStringSplit profiles strings.Split for package parsing
// Used in parsePipList, parseCondaList, splitPackageList
func BenchmarkStringSplit(b *testing.B) {
	testInputs := []string{
		"numpy,pandas,scikit-learn,tensorflow",
		"package==1.0.0",
		"single",
		"",
		"a,b,c,d,e,f,g,h,i,j",
	}

	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; b.Loop(); i++ {
		_ = strings.Split(testInputs[i%len(testInputs)], ",")
	}
}

// BenchmarkStringHasPrefix profiles strings.HasPrefix
// Used for path traversal detection and comment filtering
func BenchmarkStringHasPrefix(b *testing.B) {
	testInputs := []string{
		"../traversal",
		"normal/path",
		"# comment",
		"package==1.0",
		"..",
	}
	prefixes := []string{"..", "#", "package"}

	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; b.Loop(); i++ {
		_ = strings.HasPrefix(testInputs[i%len(testInputs)], prefixes[i%len(prefixes)])
	}
}

// BenchmarkStringEqualFold profiles case-insensitive comparison
// Used for blocked package matching
func BenchmarkStringEqualFold(b *testing.B) {
	testPairs := []struct {
		a, b string
	}{
		{"numpy", "NUMPY"},
		{"requests", "requests"},
		{"TensorFlow", "tensorflow"},
		{"scikit-learn", "SCIKIT-LEARN"},
		{"off", "OFF"},
	}

	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; b.Loop(); i++ {
		pair := testPairs[i%len(testPairs)]
		_ = strings.EqualFold(pair.a, pair.b)
	}
}

// BenchmarkFilepathClean profiles path canonicalization
// Used in resolveWorkspacePath and other path operations
func BenchmarkFilepathClean(b *testing.B) {
	testPaths := []string{
		"./relative/../path",
		"/absolute//double//slashes",
		"../../../etc/passwd",
		"normal/path",
		".",
		"..",
	}

	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; b.Loop(); i++ {
		_ = filepath.Clean(testPaths[i%len(testPaths)])
	}
}

// BenchmarkFilepathJoin profiles path joining
// Used extensively for building workspace, trash, state paths
func BenchmarkFilepathJoin(b *testing.B) {
	components := [][]string{
		{"/data", "active", "workspaces"},
		{"/tmp", "trash", "jupyter"},
		{"state", "fetch_ml_jupyter_workspaces.json"},
		{"workspace", "subdir", "file.txt"},
	}

	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; b.Loop(); i++ {
		parts := components[i%len(components)]
		_ = filepath.Join(parts...)
	}
}

// BenchmarkStrconvAtoi profiles string to int conversion
// Used for port parsing, resource limits
func BenchmarkStrconvAtoi(b *testing.B) {
	testInputs := []string{
		"8888",
		"0",
		"65535",
		"-1",
		"999999",
	}

	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; b.Loop(); i++ {
		_, _ = strconv.Atoi(testInputs[i%len(testInputs)])
	}
}

// BenchmarkTimeFormat profiles timestamp formatting
// Used for trash destination naming
func BenchmarkTimeFormat(b *testing.B) {
	now := time.Now().UTC()
	format := "20060102_150405"

	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; b.Loop(); i++ {
		_ = now.Format(format)
	}
}

// BenchmarkSprintfConcat profiles string concatenation
// Used for building destination names
func BenchmarkSprintfConcat(b *testing.B) {
	names := []string{"workspace1", "my-project", "test-ws"}
	timestamps := []string{"20240115_143022", "20240212_183045", "20240301_120000"}

	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; b.Loop(); i++ {
		_ = names[i%len(names)] + "_" + timestamps[i%len(timestamps)]
	}
}

// BenchmarkPackageListParsing profiles full package list parsing pipeline
// Combines Split, TrimSpace, and filtering - mirrors splitPackageList
func BenchmarkPackageListParsing(b *testing.B) {
	testInputs := []string{
		"numpy, pandas, scikit-learn, tensorflow",
		"  requests  ,  urllib3  ,  certifi  ",
		"single-package",
		"",
		"a, b, c, d, e, f, g",
	}

	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; b.Loop(); i++ {
		input := testInputs[i%len(testInputs)]
		parts := strings.Split(input, ",")
		out := make([]string, 0, len(parts))
		for _, p := range parts {
			p = strings.TrimSpace(p)
			if p != "" {
				out = append(out, p)
			}
		}
	}
}

// BenchmarkEnvLookup profiles environment variable lookups
// Used for FETCHML_JUPYTER_* configuration
func BenchmarkEnvLookup(b *testing.B) {
	// Pre-set test env vars
	os.Setenv("TEST_JUPYTER_STATE_DIR", "/tmp/jupyter-state")
	os.Setenv("TEST_JUPYTER_WORKSPACE_BASE", "/tmp/workspaces")

	keys := []string{
		"TEST_JUPYTER_STATE_DIR",
		"TEST_JUPYTER_WORKSPACE_BASE",
		"NONEXISTENT_VAR",
	}

	b.ReportAllocs()

	for i := 0; b.Loop(); i++ {
		_, _ = os.LookupEnv(keys[i%len(keys)])
	}
}

// BenchmarkCombinedJupyterHotPath profiles typical service manager operation
// Combines multiple string/path operations as they occur in real usage
func BenchmarkCombinedJupyterHotPath(b *testing.B) {
	testWorkspaces := []string{
		"  my-project  ",
		"./relative-ws",
		"/absolute/path/to/workspace",
		"deep/nested/name",
	}

	b.ReportAllocs()

	for i := 0; b.Loop(); i++ {
		// Simulate resolveWorkspacePath + path building
		ws := strings.TrimSpace(testWorkspaces[i%len(testWorkspaces)])
		clean := filepath.Clean(ws)
		if !filepath.IsAbs(clean) {
			clean = filepath.Join("/data/active/workspaces", clean)
		}
		// Simulate trash path building
		ts := time.Now().UTC().Format("20060102_150405")
		destName := ws + "_" + ts
		_ = filepath.Join("/tmp/trash/jupyter", destName)
	}
}
