package benchmarks

import (
	"os"
	"testing"

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

// TestGoNativeLeakStress runs 1000 iterations through Go->C++ integration
func TestGoNativeLeakStress(t *testing.T) {
	tmpDir := t.TempDir()

	// Create multiple test files
	for i := range 10 {
		content := make([]byte, 1024*1024) // 1MB each
		for j := range content {
			content[j] = byte(i * j)
		}
		if err := os.WriteFile(tmpDir+"/test_"+string(rune('a'+i))+".dat", content, 0644); err != nil {
			t.Fatal(err)
		}
	}

	// Run 1000 hash operations through Go wrapper
	for i := range 1000 {
		hash, err := worker.DirOverallSHA256Hex(tmpDir)
		if err != nil {
			t.Fatalf("Hash %d failed: %v", i, err)
		}
		if len(hash) != 64 {
			t.Fatalf("Hash %d: expected 64 chars, got %d", i, len(hash))
		}

		if i%100 == 0 {
			t.Logf("Completed %d iterations", i)
		}
	}

	t.Logf("Completed 1000 iterations through Go->C++ integration")
}

// TestGoNativeArtifactScanLeak tests artifact scanner through Go
func TestGoNativeArtifactScanLeak(t *testing.T) {
	if !worker.HasSIMDSHA256() {
		t.Skip("Native libraries not available (build with -tags native_libs)")
	}

	tmpDir := t.TempDir()

	// Create test files
	for i := range 50 {
		if err := os.WriteFile(tmpDir+"/file_"+string(rune('a'+i%26))+".txt", []byte("data"), 0644); err != nil {
			t.Fatal(err)
		}
	}

	// Run 100 scans
	for i := range 100 {
		_, err := worker.ScanArtifactsNative(tmpDir)
		if err != nil {
			t.Logf("Scan %d: %v (may be expected if native disabled)", i, err)
		}

		if i%25 == 0 {
			t.Logf("Completed %d scans", i)
		}
	}
}
