package e2e

import (
	"fmt"
	"time"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
)

func RestoreInSameRegion(networkName, clusterName, backupName, instanceType string, interval time.Duration) {
	DoRestore(networkName, clusterName, backupName, instanceType, interval)
}

func RestoreInAnotherRegion(clusterName, backupName, anotherRegion, provider, instanceType string, interval time.Duration) {
	By("create another region network")
	project := fmt.Sprintf("%d", time.Now().UnixNano())
	AnotherRegionNetwork := newCommonNetwork(anotherRegion, project, provider)
	Expect(infraCli.Create(ctx, AnotherRegionNetwork)).Should(Succeed())
	waitNetworkReady(ctx, AnotherRegionNetwork, 45*time.Minute, interval)

	By("do another region restore")
	DoRestore(AnotherRegionNetwork.Name, clusterName, backupName, instanceType, interval)

	By("delete another region network")
	Expect(infraCli.Delete(ctx, AnotherRegionNetwork)).Should(Succeed())
	waitNetworkDeleted(ctx, AnotherRegionNetwork, 15*time.Minute, interval)
}

func DoRestore(networkName, clusterName, backupName, instanceType string, interval time.Duration) {
	By("create br cluster")
	brCluster := newCommonCluster(networkName, instanceType)
	Expect(infraCli.Create(ctx, brCluster)).Should(Succeed())
	waitClusterReady(ctx, brCluster, 25*time.Minute, interval)

	Restore := newCommonRestore(clusterName, brCluster.Name, backupName)
	Expect(infraCli.Create(ctx, Restore)).Should(Succeed())
	waitRestoreReady(ctx, Restore, 15*time.Minute, interval)

	By("delete restore")
	Expect(infraCli.Delete(ctx, Restore)).Should(Succeed())
	waitRestoreDeleted(ctx, Restore, 10*time.Minute, interval)

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