package domain

import "testing"

func TestIsValidTier(t *testing.T) {
	t.Parallel()

	valid := []string{"free", "premium", "pro"}
	for _, tier := range valid {
		if !IsValidTier(tier) {
			t.Errorf("expected %q to be valid", tier)
		}
	}

	invalid := []string{"", "basic", "enterprise", "FREE", "Pro"}
	for _, tier := range invalid {
		if IsValidTier(tier) {
			t.Errorf("expected %q to be invalid", tier)
		}
	}
}

func TestGetTierLimits_Free(t *testing.T) {
	t.Parallel()

	limits := GetTierLimits(TierFree)
	if limits.MaxAddresses != 1 {
		t.Errorf("free MaxAddresses = %d, want 1", limits.MaxAddresses)
	}
	if limits.MaxAlertTypes != 1 {
		t.Errorf("free MaxAlertTypes = %d, want 1", limits.MaxAlertTypes)
	}
	if len(limits.AllowedChannels) != 1 || limits.AllowedChannels[0] != "email" {
		t.Errorf("free AllowedChannels = %v, want [email]", limits.AllowedChannels)
	}
	if limits.IsUnlimitedAddresses() {
		t.Error("free should not have unlimited addresses")
	}
	if limits.IsUnlimitedAlertTypes() {
		t.Error("free should not have unlimited alert types")
	}
}

func TestGetTierLimits_Premium(t *testing.T) {
	t.Parallel()

	limits := GetTierLimits(TierPremium)
	if limits.MaxAddresses != 3 {
		t.Errorf("premium MaxAddresses = %d, want 3", limits.MaxAddresses)
	}
	if limits.MaxAlertTypes != 2 {
		t.Errorf("premium MaxAlertTypes = %d, want 2", limits.MaxAlertTypes)
	}
	if !limits.ChannelAllowed("email") {
		t.Error("premium should allow email")
	}
	if !limits.ChannelAllowed("discord") {
		t.Error("premium should allow discord")
	}
	if !limits.ChannelAllowed("telegram") {
		t.Error("premium should allow telegram")
	}
	if limits.ChannelAllowed("slack") {
		t.Error("premium should NOT allow slack")
	}
}

func TestGetTierLimits_Pro(t *testing.T) {
	t.Parallel()

	limits := GetTierLimits(TierPro)
	if !limits.IsUnlimitedAddresses() {
		t.Error("pro should have unlimited addresses")
	}
	if !limits.IsUnlimitedAlertTypes() {
		t.Error("pro should have unlimited alert types")
	}
	for _, ch := range []string{"email", "discord", "telegram", "slack"} {
		if !limits.ChannelAllowed(ch) {
			t.Errorf("pro should allow %s", ch)
		}
	}
}

func TestGetTierLimits_Unknown(t *testing.T) {
	t.Parallel()

	limits := GetTierLimits(SubscriptionTier("unknown"))
	if limits.MaxAddresses != 1 {
		t.Errorf("unknown tier should default to free limits, got MaxAddresses=%d", limits.MaxAddresses)
	}
}

func TestChannelAllowed_NotInList(t *testing.T) {
	t.Parallel()

	limits := GetTierLimits(TierFree)
	if limits.ChannelAllowed("discord") {
		t.Error("free should not allow discord")
	}
	if limits.ChannelAllowed("nonexistent") {
		t.Error("nonexistent channel should not be allowed")
	}
}
