package teamsalarypolicy

import "testing"

func TestPolicyModelFromRequestBuildsBDLevels(t *testing.T) {
	item, err := policyModelFromRequest("lalu", 7, policyRequest{
		PolicyType:            "bd",
		Name:                  "BD 默认政策",
		RegionID:              101,
		Status:                "active",
		SettlementTriggerMode: "manual",
		Levels: []levelRequest{
			{Level: 2, ThresholdUSD: "200", RatePercent: "8", SalaryUSD: "16"},
			{Level: 1, ThresholdUSD: "100", RatePercent: "8", SalaryUSD: "8"},
		},
	})
	if err != nil {
		t.Fatalf("policy should be valid: %v", err)
	}
	if item.PolicyType != policyTypeBD || item.CreatedByAdminID != 7 || item.SettlementTriggerMode != settlementTriggerManual {
		t.Fatalf("policy fields mismatch: %+v", item)
	}
	if len(item.Levels) != 2 || item.Levels[0].LevelNo != 1 || item.Levels[1].ThresholdUSD != "200.00" {
		t.Fatalf("levels should be sorted and normalized: %+v", item.Levels)
	}
	if item.Levels[0].RatePercent != "8.0000" || item.Levels[1].SalaryUSD != "16.00" {
		t.Fatalf("level decimals mismatch: %+v", item.Levels)
	}
}

func TestPolicyModelFromRequestRejectsNonIncreasingThreshold(t *testing.T) {
	_, err := policyModelFromRequest("lalu", 1, policyRequest{
		PolicyType: "admin",
		Name:       "Invalid",
		RegionID:   101,
		Levels: []levelRequest{
			{Level: 1, ThresholdUSD: "500", RatePercent: "20", SalaryUSD: "100"},
			{Level: 2, ThresholdUSD: "200", RatePercent: "20", SalaryUSD: "200"},
		},
	})
	if err == nil {
		t.Fatal("expected non-increasing threshold to fail")
	}
}

func TestPolicyModelFromRequestRejectsInvalidPolicyType(t *testing.T) {
	_, err := policyModelFromRequest("lalu", 1, policyRequest{
		PolicyType: "host",
		Name:       "Invalid",
		RegionID:   101,
		Levels: []levelRequest{
			{Level: 1, ThresholdUSD: "100", RatePercent: "8", SalaryUSD: "8"},
		},
	})
	if err == nil {
		t.Fatal("expected invalid policy type to fail")
	}
}
