//go:build native_libs
// +build native_libs

package benchmarks

import (
	"os"
	"path/filepath"
	"testing"

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

// BenchmarkDirOverallSHA256Hex_Native profiles the directory hashing with native implementation.
// This benchmark only runs when built with -tags native_libs.
func BenchmarkDirOverallSHA256Hex_Native(b *testing.B) {
	// Create a temp directory structure resembling a dataset
	tmpDir := b.TempDir()

	// Create nested structure with files of varying sizes
	sizes := []int{1024, 10240, 102400, 1024 * 1024} // 1KB to 1MB
	for i, size := range sizes {
		subdir := filepath.Join(tmpDir, "subdir", string(rune('a'+i)))
		if err := os.MkdirAll(subdir, 0750); err != nil {
			b.Fatal(err)
		}
		data := make([]byte, size)
		for j := range data {
			data[j] = byte(i + j%256)
		}
		if err := os.WriteFile(filepath.Join(subdir, "data.bin"), data, 0640); err != nil {
			b.Fatal(err)
		}
	}

	// Add some small metadata files
	metaDir := filepath.Join(tmpDir, "meta")
	if err := os.MkdirAll(metaDir, 0750); err != nil {
		b.Fatal(err)
	}
	for i := range 10 {
		if err := os.WriteFile(
			filepath.Join(metaDir, "file"+string(rune('0'+i))+".json"),
			[]byte(`{"key": "value"}`),
			0640,
		); err != nil {
			b.Fatal(err)
		}
	}

	b.ResetTimer()
	b.ReportAllocs()

	for b.Loop() {
		_, err := worker.DirOverallSHA256Hex(tmpDir)
		if err != nil {
			b.Fatal(err)
		}
	}
}

// BenchmarkDirOverallSHA256HexLarge_Native profiles with larger dataset simulation using native
func BenchmarkDirOverallSHA256HexLarge_Native(b *testing.B) {
	tmpDir := b.TempDir()

	// Create 50 files of 100KB each = ~5MB total
	for i := 0; i < 50; i++ {
		subdir := filepath.Join(tmpDir, "data", string(rune('a'+i%26)))
		if err := os.MkdirAll(subdir, 0750); err != nil {
			b.Fatal(err)
		}
		data := make([]byte, 100*1024)
		for j := range data {
			data[j] = byte(i + j%256)
		}
		if err := os.WriteFile(
			filepath.Join(subdir, "chunk"+string(rune('0'+i/26))+".bin"),
			data,
			0640,
		); err != nil {
			b.Fatal(err)
		}
	}

	b.ReportAllocs()

	for b.Loop() {
		_, err := worker.DirOverallSHA256Hex(tmpDir)
		if err != nil {
			b.Fatal(err)
		}
	}
}
