package entity

import "testing"

func TestServicePlan_IsNextGen(t *testing.T) {
	tests := []struct {
		plan ServicePlan
		want bool
	}{
		{ServicePlanPremium, true},
		{ServicePlanEssentialV2, true},
		{ServicePlanEssential, true},
		{ServicePlanStarter, true},
		{ServicePlanDedicated, false},
		{"premium", true},
		{"ESSENTIAL", true},
		{" Starter ", true},
		{"", false},
		{"BYOC", false},
	}
	for _, tt := range tests {
		if got := tt.plan.IsNextGen(); got != tt.want {
			t.Errorf("ServicePlan(%q).IsNextGen() = %v, want %v", tt.plan, got, tt.want)
		}
	}
}

func TestServicePlan_HasProjectConcept(t *testing.T) {
	tests := []struct {
		plan ServicePlan
		want bool
	}{
		{ServicePlanDedicated, true},
		{ServicePlanEssential, false},
		{ServicePlanStarter, false},
		{ServicePlanPremium, false},
		{ServicePlanEssentialV2, false},
		{"premium", false},
		{"dedicated", true},
		{" essential ", false},
		{"starter", false},
		{"", false},
	}
	for _, tt := range tests {
		if got := tt.plan.HasProjectConcept(); got != tt.want {
			t.Errorf("ServicePlan(%q).HasProjectConcept() = %v, want %v", tt.plan, got, tt.want)
		}
	}
}

func TestServicePlan_IsInstanceLevel(t *testing.T) {
	tests := []struct {
		plan ServicePlan
		want bool
	}{
		{ServicePlanPremium, true},
		{ServicePlanEssentialV2, true},
		{ServicePlanEssential, true},
		{ServicePlanStarter, true},
		{"premium", true},
		{" Premium ", true},
		{ServicePlanDedicated, false},
		{"essential", true},
		{" Starter ", true},
		{"", false},
	}
	for _, tt := range tests {
		if got := tt.plan.IsInstanceLevel(); got != tt.want {
			t.Errorf("ServicePlan(%q).IsInstanceLevel() = %v, want %v", tt.plan, got, tt.want)
		}
	}
}

func TestServicePlan_IsServerless(t *testing.T) {
	tests := []struct {
		plan ServicePlan
		want bool
	}{
		{ServicePlanEssential, true},
		{ServicePlanStarter, true},
		{"ESSENTIAL", true},
		{" Starter ", true},
		{ServicePlanPremium, false},
		{ServicePlanEssentialV2, false},
		{ServicePlanDedicated, false},
		{" premium ", false},
		{"", false},
	}
	for _, tt := range tests {
		if got := tt.plan.IsServerless(); got != tt.want {
			t.Errorf("ServicePlan(%q).IsServerless() = %v, want %v", tt.plan, got, tt.want)
		}
	}
}

func TestServicePlan_SupportsCDC(t *testing.T) {
	tests := []struct {
		plan ServicePlan
		want bool
	}{
		{ServicePlanDedicated, true},
		{ServicePlanPremium, true},
		{ServicePlanEssentialV2, true},
		{ServicePlanStarter, false},
		{ServicePlanEssential, false},
		{"premium", true},
		{" starter ", false},
		{"ESSENTIAL", false},
		{"", false},
	}
	for _, tt := range tests {
		if got := tt.plan.SupportsCDC(); got != tt.want {
			t.Errorf("ServicePlan(%q).SupportsCDC() = %v, want %v", tt.plan, got, tt.want)
		}
	}
}

func TestServicePlan_UsesPremiumMetrics(t *testing.T) {
	tests := []struct {
		plan ServicePlan
		want bool
	}{
		{ServicePlanPremium, true},
		{ServicePlanEssentialV2, true},
		{" essential_v2 ", true},
		{ServicePlanEssential, false},
		{ServicePlanStarter, false},
		{ServicePlanDedicated, false},
	}
	for _, tt := range tests {
		if got := tt.plan.UsesPremiumMetrics(); got != tt.want {
			t.Errorf("ServicePlan(%q).UsesPremiumMetrics() = %v, want %v", tt.plan, got, tt.want)
		}
	}
}

func TestServicePlan_IsSupported(t *testing.T) {
	tests := []struct {
		plan ServicePlan
		want bool
	}{
		{ServicePlanDedicated, true},
		{ServicePlanPremium, true},
		{ServicePlanEssentialV2, true},
		{ServicePlanEssential, true},
		{ServicePlanStarter, true},
		{"dedicated", true},
		{"BYOC", false},
		{"unknown", false},
		{"", false},
	}
	for _, tt := range tests {
		if got := tt.plan.IsSupported(); got != tt.want {
			t.Errorf("ServicePlan(%q).IsSupported() = %v, want %v", tt.plan, got, tt.want)
		}
	}
}

func TestNormalizeServicePlan(t *testing.T) {
	tests := []struct {
		name    string
		in      string
		want    ServicePlan
		wantErr bool
	}{
		{name: "empty defaults dedicated", in: "", want: ServicePlanDedicated},
		{name: "trim premium", in: " Premium ", want: ServicePlanPremium},
		{name: "mixed case essential v2", in: " Essential_V2 ", want: ServicePlanEssentialV2},
		{name: "lower essential v2", in: "essential_v2", want: ServicePlanEssentialV2},
		{name: "lower essential", in: "essential", want: ServicePlanEssential},
		{name: "starter", in: "Starter", want: ServicePlanStarter},
		{name: "unknown rejected", in: "BYOC", wantErr: true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := NormalizeServicePlan(tt.in)
			if (err != nil) != tt.wantErr {
				t.Fatalf("NormalizeServicePlan() error = %v, wantErr %v", err, tt.wantErr)
			}
			if err == nil && got != tt.want {
				t.Fatalf("NormalizeServicePlan() = %q, want %q", got, tt.want)
			}
		})
	}
}
