package dto

import "testing"

func TestToSapronakProjectAggregatedFromReportMovesCutOverAdjustmentOutToQtyUsed(t *testing.T) {
	tests := []struct {
		name        string
		groupFlag   string
		filter      string
		productFlag string
	}{
		{
			name:        "pakan cut-over",
			groupFlag:   "PAKAN",
			filter:      "PAKAN",
			productFlag: "PAKAN CUT-OVER",
		},
		{
			name:        "ovk cut over",
			groupFlag:   "OVK",
			filter:      "OVK",
			productFlag: "OVK CUT OVER",
		},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			report := &SapronakReportDTO{
				Groups: []SapronakGroupDTO{
					{
						Flag: tc.groupFlag,
						Items: []SapronakDetailDTO{
							{
								ProductID:      1,
								ProductName:    "CUTOVER ITEM",
								NoReferensi:    "ADJ-CUT-01",
								JenisTransaksi: "Adjustment Keluar",
								QtyKeluar:      5,
								Harga:          15000,
							},
						},
					},
				},
			}

			result := ToSapronakProjectAggregatedFromReport(
				report,
				tc.filter,
				map[uint][]string{
					1: {tc.productFlag},
				},
			)

			var cat *SapronakCategoryDTO
			if tc.groupFlag == "PAKAN" {
				cat = result.Pakan
			} else {
				cat = result.Ovk
			}
			if cat == nil {
				t.Fatalf("expected category payload for %s", tc.groupFlag)
			}
			if len(cat.Rows) != 1 {
				t.Fatalf("expected 1 row, got %d", len(cat.Rows))
			}

			row := cat.Rows[0]
			if row.QtyOut != 0 {
				t.Fatalf("expected qty_out 0 for cut-over adjustment, got %.2f", row.QtyOut)
			}
			if row.QtyUsed != 5 {
				t.Fatalf("expected qty_used 5 for cut-over adjustment, got %.2f", row.QtyUsed)
			}
			if row.UnitPrice != 15000 {
				t.Fatalf("expected unit_price 15000, got %.2f", row.UnitPrice)
			}
			if row.TotalAmount != 75000 {
				t.Fatalf("expected total_amount 75000, got %.2f", row.TotalAmount)
			}
		})
	}
}

func TestToSapronakProjectAggregatedFromReportKeepsNonCutOverAdjustmentOutInQtyOut(t *testing.T) {
	report := &SapronakReportDTO{
		Groups: []SapronakGroupDTO{
			{
				Flag: "PAKAN",
				Items: []SapronakDetailDTO{
					{
						ProductID:      7,
						ProductName:    "PAKAN REGULER",
						NoReferensi:    "ADJ-REG-01",
						JenisTransaksi: "Adjustment Keluar",
						QtyKeluar:      3,
						Harga:          12000,
					},
				},
			},
		},
	}

	result := ToSapronakProjectAggregatedFromReport(
		report,
		"PAKAN",
		map[uint][]string{
			7: {"PAKAN"},
		},
	)
	if result.Pakan == nil || len(result.Pakan.Rows) != 1 {
		t.Fatalf("expected 1 pakan row, got %+v", result.Pakan)
	}

	row := result.Pakan.Rows[0]
	if row.QtyOut != 3 {
		t.Fatalf("expected qty_out 3 for non cut-over adjustment, got %.2f", row.QtyOut)
	}
	if row.QtyUsed != 0 {
		t.Fatalf("expected qty_used 0 for non cut-over adjustment, got %.2f", row.QtyUsed)
	}
	if row.TotalAmount != 0 {
		t.Fatalf("expected total_amount 0 for non cut-over adjustment, got %.2f", row.TotalAmount)
	}
}
