//go:build !native_libs
// +build !native_libs

package benchmarks

import (
	"testing"

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

// BenchmarkFilesystemQueueRebuildIndex profiles the queue index rebuild hot path.
// Called on every task add/update - walks directory, reads JSON, sorts tasks.
// Tier 1 C++ candidate for:
//   - Memory-mapped JSON parsing
//   - Binary index format (instead of JSON)
//   - Zero-copy sorting
func BenchmarkFilesystemQueueRebuildIndex(b *testing.B) {
	tmpDir := b.TempDir()
	q, err := queue.NewFilesystemQueue(tmpDir)
	if err != nil {
		b.Fatal(err)
	}
	defer q.Close()

	// Seed with tasks
	for i := range 100 {
		task := &queue.Task{
			ID:       "task-" + string(rune('0'+i/10)) + string(rune('0'+i%10)),
			JobName:  "job-" + string(rune('0'+i/10)),
			Priority: int64(100 - i),
		}
		if err := q.AddTask(task); err != nil {
			b.Fatal(err)
		}
	}

	b.ResetTimer()
	b.ReportAllocs()

	// Benchmark just the rebuild (not the full AddTask)
	for i := 0; b.Loop(); i++ {
		// Force rebuild by adding one more task
		task := &queue.Task{
			ID:       "bench-task-" + string(rune('0'+i%10)),
			JobName:  "bench-job",
			Priority: int64(i),
		}
		if err := q.AddTask(task); err != nil {
			b.Fatal(err)
		}
	}
}

// BenchmarkFilesystemQueueClaimNext profiles task claiming (priority selection)
func BenchmarkFilesystemQueueClaimNext(b *testing.B) {
	tmpDir := b.TempDir()
	q, err := queue.NewFilesystemQueue(tmpDir)
	if err != nil {
		b.Fatal(err)
	}
	defer q.Close()

	// Seed with tasks
	for i := range 100 {
		task := &queue.Task{
			ID:       "task-" + string(rune('0'+i/10)) + string(rune('0'+i%10)),
			JobName:  "job-" + string(rune('0'+i/10)),
			Priority: int64(100 - i),
		}
		if err := q.AddTask(task); err != nil {
			b.Fatal(err)
		}
	}

	b.ResetTimer()
	b.ReportAllocs()

	for b.Loop() {
		// This triggers ReadDir + JSON unmarshal + sort
		_, _ = q.PeekNextTask()
	}
}

// BenchmarkFilesystemQueueGetAllTasks profiles full task scan
func BenchmarkFilesystemQueueGetAllTasks(b *testing.B) {
	tmpDir := b.TempDir()
	q, err := queue.NewFilesystemQueue(tmpDir)
	if err != nil {
		b.Fatal(err)
	}
	defer q.Close()

	// Seed with tasks
	for i := range 100 {
		task := &queue.Task{
			ID:       "task-" + string(rune('0'+i/10)) + string(rune('0'+i%10)),
			JobName:  "job-" + string(rune('0'+i/10)),
			Priority: int64(100 - i),
		}
		if err := q.AddTask(task); err != nil {
			b.Fatal(err)
		}
	}

	b.ResetTimer()
	b.ReportAllocs()

	for b.Loop() {
		_, err := q.GetAllTasks()
		if err != nil {
			b.Fatal(err)
		}
	}
}
