package recording

import (
	"testing"

	entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
	validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/validations"
)

func TestMapDepletionsKeepsSourceWarehouseRoutes(t *testing.T) {
	sourceA := uint(11)
	sourceB := uint(12)

	got := MapDepletions(99, []validation.Depletion{
		{ProductWarehouseId: 21, SourceProductWarehouseId: &sourceA, Qty: 3},
		{ProductWarehouseId: 21, SourceProductWarehouseId: &sourceA, Qty: 2},
		{ProductWarehouseId: 21, SourceProductWarehouseId: &sourceB, Qty: 4},
	})

	if len(got) != 2 {
		t.Fatalf("expected 2 depletion routes, got %d", len(got))
	}

	routeQty := DepletionTotalsByRoute(got, func(item entity.RecordingDepletion) (uint, *uint, float64) {
		return item.ProductWarehouseId, item.SourceProductWarehouseId, item.Qty
	})

	if routeQty[DepletionRoute{ProductWarehouseId: 21, SourceProductWarehouseId: sourceA}] != 5 {
		t.Fatalf("expected source A qty 5, got %.2f", routeQty[DepletionRoute{ProductWarehouseId: 21, SourceProductWarehouseId: sourceA}])
	}
	if routeQty[DepletionRoute{ProductWarehouseId: 21, SourceProductWarehouseId: sourceB}] != 4 {
		t.Fatalf("expected source B qty 4, got %.2f", routeQty[DepletionRoute{ProductWarehouseId: 21, SourceProductWarehouseId: sourceB}])
	}
}

func TestMapEggsSetsProjectFlockKandangID(t *testing.T) {
	got := MapEggs(77, 44, 9, []validation.Egg{
		{ProductWarehouseId: 88, Qty: 10},
	})

	if len(got) != 1 {
		t.Fatalf("expected 1 egg row, got %d", len(got))
	}
	if got[0].ProjectFlockKandangId == nil || *got[0].ProjectFlockKandangId != 44 {
		t.Fatalf("expected project flock kandang id 44, got %+v", got[0].ProjectFlockKandangId)
	}
}
