package hostagencypolicy

import "testing"

func TestPolicyModelFromRequestNormalizesAndSortsLevels(t *testing.T) {
	item, err := policyModelFromRequest("lalu", 7, policyRequest{
		Name:                     "Lalu 默认政策",
		RegionID:                 101,
		Status:                   "active",
		SettlementMode:           "half-month",
		SettlementTriggerMode:    "manual",
		GiftCoinToDiamondRatio:   "9",
		ResidualDiamondToUSDRate: "0.000001",
		Levels: []levelRequest{
			{Level: 2, RequiredDiamonds: 700000, HostSalaryUSD: "3", HostCoinReward: 198000, AgencySalaryUSD: "0.8"},
			{Level: 1, RequiredDiamonds: 350000, HostSalaryUSD: "1.5", HostCoinReward: 90000, AgencySalaryUSD: "0.5"},
		},
	})
	if err != nil {
		t.Fatalf("policy should be valid: %v", err)
	}
	if item.AppCode != "lalu" || item.CreatedByAdminID != 7 || item.SettlementMode != settlementModeHalfMonth || item.SettlementTriggerMode != settlementTriggerManual {
		t.Fatalf("policy fields mismatch: %+v", item)
	}
	// 工资政策不能单独改送礼入主播周期钻石的比例；该比例只由礼物钻石配置页控制。
	if item.GiftCoinToDiamondRatio != "1.000000" || item.ResidualDiamondToUSDRate != "0.000001000000" {
		t.Fatalf("ratio fields mismatch: %+v", item)
	}
	if len(item.Levels) != 2 || item.Levels[0].LevelNo != 1 || item.Levels[1].LevelNo != 2 {
		t.Fatalf("levels should be sorted: %+v", item.Levels)
	}
	if item.Levels[0].HostSalaryUSD != "1.50" || item.Levels[1].AgencySalaryUSD != "0.80" {
		t.Fatalf("salary fields should be fixed decimals: %+v", item.Levels)
	}
}

func TestPolicyModelFromRequestRejectsNonIncreasingLevels(t *testing.T) {
	_, err := policyModelFromRequest("lalu", 1, policyRequest{
		Name:           "Invalid",
		RegionID:       101,
		SettlementMode: settlementModeDaily,
		Levels: []levelRequest{
			{Level: 1, RequiredDiamonds: 700000, HostSalaryUSD: "3", HostCoinReward: 198000, AgencySalaryUSD: "0.8"},
			{Level: 2, RequiredDiamonds: 350000, HostSalaryUSD: "1.5", HostCoinReward: 90000, AgencySalaryUSD: "0.5"},
		},
	})
	if err == nil {
		t.Fatal("expected non-increasing levels to fail")
	}
}

func TestPolicyModelFromRequestDefaultsSettlementTriggerMode(t *testing.T) {
	item, err := policyModelFromRequest("lalu", 1, policyRequest{
		Name:           "Default trigger",
		RegionID:       101,
		SettlementMode: settlementModeDaily,
		Levels: []levelRequest{
			{Level: 1, RequiredDiamonds: 350000, HostSalaryUSD: "1.5", HostCoinReward: 90000, AgencySalaryUSD: "0.5"},
		},
	})
	if err != nil {
		t.Fatalf("policy should be valid: %v", err)
	}
	if item.SettlementTriggerMode != settlementTriggerAutomatic {
		t.Fatalf("default trigger mismatch: %+v", item)
	}
}

func TestPolicyModelFromRequestRejectsInvalidSettlementMode(t *testing.T) {
	_, err := policyModelFromRequest("lalu", 1, policyRequest{
		Name:           "Invalid",
		RegionID:       101,
		SettlementMode: "monthly",
		Levels: []levelRequest{
			{Level: 1, RequiredDiamonds: 350000, HostSalaryUSD: "1.5", HostCoinReward: 90000, AgencySalaryUSD: "0.5"},
		},
	})
	if err == nil {
		t.Fatal("expected invalid settlement mode to fail")
	}
}

func TestPolicyModelFromRequestRejectsInvalidSettlementTriggerMode(t *testing.T) {
	_, err := policyModelFromRequest("lalu", 1, policyRequest{
		Name:                  "Invalid",
		RegionID:              101,
		SettlementMode:        settlementModeDaily,
		SettlementTriggerMode: "cron",
		Levels: []levelRequest{
			{Level: 1, RequiredDiamonds: 350000, HostSalaryUSD: "1.5", HostCoinReward: 90000, AgencySalaryUSD: "0.5"},
		},
	})
	if err == nil {
		t.Fatal("expected invalid settlement trigger mode to fail")
	}
}
