package repository

import (
	"testing"

	commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
	entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
)

func TestScaleDeliveryProductsByAttribution(t *testing.T) {
	projectFlockKandangID := uint(101)

	deliveryProducts := []entity.MarketingDeliveryProduct{
		{
			Id:          55,
			UsageQty:    100,
			TotalWeight: 180,
			TotalPrice:  3600,
		},
	}
	attributionRows := []commonRepo.MarketingDeliveryAttributionRow{
		{MarketingDeliveryProductID: 55, ProjectFlockKandangID: 101, AllocatedQty: 60},
		{MarketingDeliveryProductID: 55, ProjectFlockKandangID: 102, AllocatedQty: 40},
	}

	got := scaleDeliveryProductsByAttribution(deliveryProducts, attributionRows, projectFlockKandangID)
	if len(got) != 1 {
		t.Fatalf("expected 1 scaled delivery, got %d", len(got))
	}
	if got[0].UsageQty != 60 {
		t.Fatalf("expected usage qty 60, got %.2f", got[0].UsageQty)
	}
	if got[0].TotalWeight != 108 {
		t.Fatalf("expected total weight 108, got %.2f", got[0].TotalWeight)
	}
	if got[0].TotalPrice != 2160 {
		t.Fatalf("expected total price 2160, got %.2f", got[0].TotalPrice)
	}
	if got[0].AttributedProjectFlockKandangId == nil || *got[0].AttributedProjectFlockKandangId != projectFlockKandangID {
		t.Fatalf("expected attributed kandang id %d, got %+v", projectFlockKandangID, got[0].AttributedProjectFlockKandangId)
	}
}
