package planreplayer

import (
	"testing"
	"time"

	globalcommonv1 "github.com/tidbcloud/tidb-management-service/api/spec/global/common/v1"
	globalngv1 "github.com/tidbcloud/tidb-management-service/api/spec/global/nextgen/v1"
	"github.com/tidbcloud/tidb-management-service/nextgen-global-service/domain/entity"
)

func TestValidateServicePlan(t *testing.T) {
	tests := []struct {
		name string
		plan globalcommonv1.ServicePlan
		ok   bool
	}{
		{name: "premium", plan: globalcommonv1.ServicePlan_Premium, ok: true},
		{name: "essential v2", plan: globalcommonv1.ServicePlan_Essential_V2, ok: true},
		{name: "premium reserved", plan: globalcommonv1.ServicePlan_Premium_Reserved, ok: true},
		{name: "starter", plan: globalcommonv1.ServicePlan_Starter, ok: false},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			cluster := &entity.Cluster{Cluster: &globalngv1.Cluster{ServicePlan: tc.plan}}
			err := validateServicePlan(cluster)
			if tc.ok && err != nil {
				t.Fatalf("expected no error, got %v", err)
			}
			if !tc.ok && err == nil {
				t.Fatalf("expected error, got nil")
			}
		})
	}
}

func TestBuildStatus(t *testing.T) {
	now := time.Now().UTC()
	future := now.Add(10 * time.Minute)
	past := now.Add(-10 * time.Minute)

	status := buildStatus(1, nil)
	if status.GetEnabled() {
		t.Fatalf("expected disabled when expire is nil")
	}

	status = buildStatus(2, &future)
	if !status.GetEnabled() {
		t.Fatalf("expected enabled when expire is in future")
	}
	if status.GetExpireTime() == nil {
		t.Fatalf("expected expire_time when enabled")
	}

	status = buildStatus(3, &past)
	if status.GetEnabled() {
		t.Fatalf("expected disabled when expire is in past")
	}
}
