package repository

import (
	"context"
	"testing"

	"route256/cart/internal/domain/entity"
	"route256/cart/internal/domain/model"
)

var (
	ctx      = context.Background()
	testItem = &model.Item{
		Product: &model.Product{
			Sku:   1,
			Name:  "bench",
			Price: 100,
		},
		Count: 1,
	}
)

func BenchmarkInMemoryRepository_AddItem(b *testing.B) {
	repo := NewInMemoryRepository(1024)

	b.ResetTimer()
	for i := 1; i < b.N; i++ {
		uid := entity.UID(i % 1000000)
		_ = repo.AddItem(ctx, uid, testItem)
	}
}

func BenchmarkInMemoryRepository_AddItemParallel(b *testing.B) {
	repo := NewInMemoryRepository(1024)

	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		var i int
		for pb.Next() {
			uid := entity.UID(i % 1000000)
			_ = repo.AddItem(ctx, uid, testItem)
			i++
		}
	})
}

func BenchmarkInMemoryRepository_DeleteItem(b *testing.B) {
	repo := NewInMemoryRepository(1024)

	for i := 1; i < 1_000; i++ {
		_ = repo.AddItem(ctx, entity.UID(i), testItem)
	}

	b.ResetTimer()
	for i := 1; i < b.N; i++ {
		uid := entity.UID(i % 1000000)
		_ = repo.DeleteItem(ctx, uid, entity.Sku(testItem.Product.Sku))
	}
}
