package luckygift

import (
	"testing"
)

func TestDefaultRuleConfigPassesValidation(t *testing.T) {
	config := DefaultRuleConfig("hyapp", "pool_v2")
	config.Enabled = true

	if err := validateRuleConfig(config); err != nil {
		t.Fatalf("default v2 lucky gift config should be publishable: %v", err)
	}
}

func TestValidateRuleConfigRejectsPercentSubmittedAsPPM(t *testing.T) {
	config := DefaultRuleConfig("hyapp", "pool_bad_rtp")
	config.TargetRTPPPM = 95

	if err := validateRuleConfig(config); err == nil {
		t.Fatalf("expected raw percent value in ppm field to be rejected")
	}
}

func TestValidateRuleConfigRejectsStageWeightMismatch(t *testing.T) {
	config := DefaultRuleConfig("hyapp", "pool_bad_weight")
	config.Stages[0].Tiers[0].BaseWeightPPM = 299_999

	if err := validateRuleConfig(config); err == nil {
		t.Fatalf("expected stage probability sum mismatch to be rejected")
	}
}

func TestValidateRuleConfigRejectsMissingRiskCaps(t *testing.T) {
	config := DefaultRuleConfig("hyapp", "pool_bad_cap")
	config.RoomHourlyPayoutCap = 0

	if err := validateRuleConfig(config); err == nil {
		t.Fatalf("expected missing v2 risk cap to be rejected")
	}
}

func TestValidateRuleConfigRejectsInvalidEquivalentDrawThresholds(t *testing.T) {
	config := DefaultRuleConfig("hyapp", "pool_bad_stage_threshold")
	config.NormalMaxEquivalentDraws = config.NoviceMaxEquivalentDraws - 1

	if err := validateRuleConfig(config); err == nil {
		t.Fatalf("expected invalid equivalent draw thresholds to be rejected")
	}
}

func TestValidateRuleConfigRequiresExplicitZeroTier(t *testing.T) {
	config := DefaultRuleConfig("hyapp", "pool_no_zero")
	config.Stages[1].Tiers = config.Stages[1].Tiers[1:]

	if err := validateRuleConfig(config); err == nil {
		t.Fatalf("expected stage without 0x tier to be rejected")
	}
}

func TestValidateRuleConfigRejectsDuplicatedStageTierID(t *testing.T) {
	config := DefaultRuleConfig("hyapp", "pool_duplicate_tier")
	advanced := &config.Stages[2]
	duplicate := advanced.Tiers[len(advanced.Tiers)-1]
	duplicate.MultiplierPPM = 10_000_000
	duplicate.BaseWeightPPM = 0
	advanced.Tiers = append(advanced.Tiers, duplicate)

	if err := validateRuleConfig(config); err == nil {
		t.Fatalf("expected duplicate tier id inside one stage to be rejected")
	}
}
