package helper

import (
	"context"
	"time"

	"github.com/onsi/ginkgo/v2"
	"github.com/onsi/gomega"
	"github.com/tidbcloud/aws-shared-provider/tests/e2e/common/data"
	waitutil "github.com/tidbcloud/aws-shared-provider/tests/e2e/common/wait"
	"github.com/tidbcloud/aws-shared-provider/tests/framework"
	infra "github.com/tidbcloud/tidb-management-service/api/serverless_infra"
)

var (
	ClusterTimeout = time.Minute * 5
	UpgradeTimeout = time.Minute * 10
)

func CreateDefaultCluster(
	ctx context.Context,
	f *framework.Framework,
	wait bool,
) *infra.Cluster {
	cluster := data.NewDefaultCluster(f.InfraNamespace, f.AWSRegion, f.KubeClusterNameBase)
	err := f.InfraClusterCli.Create(ctx, cluster)
	gomega.Expect(err).NotTo(gomega.HaveOccurred())

	if wait {
		ginkgo.By("wait for cluster running")
		err = waitutil.WaitForClusterPhase(
			ctx,
			f.InfraClusterCli,
			cluster,
			infra.ClusterPhaseRunning,
			ClusterTimeout,
		)
		gomega.Expect(err).NotTo(gomega.HaveOccurred())
	}

	return cluster
}
