package e2e

import (
	"fmt"
	"strings"
	"time"

	_ "github.com/go-sql-driver/mysql"
	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
	"k8s.io/apimachinery/pkg/types"

	cloudv1 "github.com/tidbcloud/infra-api/api/v1"
	"github.com/tidbcloud/infra-api/pkg/consts"
)

var errWait = fmt.Errorf("wait for condition met")

var _ = Describe("AWS cluster test", func() {
	interval := 10 * time.Second
	instanceType := "t3.medium"

	It("Should create AWS cluster successfully", func() {
		By("create cluster")
		cluster := newCommonCluster(e2eEnv.AwsNetwork.Name, instanceType)
		Expect(infraCli.Create(ctx, cluster)).Should(Succeed())

		By("wait cluster ready")
		waitClusterReady(ctx, cluster, 25*time.Minute, interval)
		Expect(cluster.Status.SqlEndpoints.PublicEndpoint).NotTo(BeNil())

		if !isLocalTest {
			By("wait TiDB ready")
			waitTiDBReady(ctx, cluster, 15*time.Minute, interval)

			By("delete cluster")
			Expect(infraCli.Delete(ctx, cluster)).Should(Succeed())
			waitClusterDeleted(ctx, cluster, 5*time.Minute, interval)
		}
	})

	It("Should enable tiflash successfully after cluster running", func() {
		By("create cluster")
		provider := new(cloudv1.Provider)
		err := infraCli.Get(ctx, types.NamespacedName{Name: consts.ProviderTypeAWS}, provider)
		Expect(err).Should(Succeed())
		// Pick anyone to use as StorageClassName
		var tiflashSCName string
		for _, sc := range provider.Spec.StorageClasses {
			if strings.HasPrefix(sc.Name, "tiflash") {
				tiflashSCName = sc.Name
				break
			}
		}

		cluster := newCommonCluster(e2eEnv.AwsNetwork.Name, instanceType)
		cluster.Spec.Cluster.TiFlash.StorageClassName = tiflashSCName
		tiflash := cluster.Spec.Cluster.TiFlash.DeepCopy()
		Expect(tiflash.Replicas).ShouldNot(Equal(0))
		cluster.Spec.Cluster.TiFlash.Replicas = 0
		Expect(infraCli.Create(ctx, cluster)).Should(Succeed())

		By("wait cluster ready")
		waitClusterReady(ctx, cluster, 25*time.Minute, interval)
		Expect(cluster.Status.SqlEndpoints.PublicEndpoint).NotTo(BeNil())

		By("start enable tiflash")
		cluster.Spec.Cluster.TiFlash.Replicas = tiflash.Replicas
		err = infraCli.Update(ctx, cluster)
		Expect(err).Should(Succeed())

		waitClusterSynced(ctx, cluster, 25*time.Minute, interval)
	})

})

var _ = Describe("GCP cluster test", func() {
	interval := 10 * time.Second
	tiflashSCName := "tiflash-pd-ssd"

	It("Should enable tiflash successfully after cluster running", func() {
		instanceType := e2eEnv.Config.GCP.InstanceType

		By("create cluster")
		cluster := newCommonCluster(e2eEnv.GcpNetwork.Name, instanceType)
		tiflash := cluster.Spec.Cluster.TiFlash.DeepCopy()
		cluster.Spec.Cluster.TiFlash.StorageClassName = tiflashSCName
		Expect(tiflash.Replicas).ShouldNot(Equal(0))
		cluster.Spec.Cluster.TiFlash.Replicas = 0
		Expect(infraCli.Create(ctx, cluster)).Should(Succeed())

		By("wait cluster ready")
		waitClusterReady(ctx, cluster, 25*time.Minute, interval)
		Expect(cluster.Status.SqlEndpoints.PublicEndpoint).NotTo(BeNil())

		By("start enable tiflash")
		cluster.Spec.Cluster.TiFlash.Replicas = tiflash.Replicas
		err := infraCli.Update(ctx, cluster)
		Expect(err).Should(Succeed())

		waitClusterSynced(ctx, cluster, 25*time.Minute, interval)
	})

})
