package aws

import (
	"fmt"
	"testing"

	"github.com/dailymotion/allure-go"
	"github.com/tidbcloud/qa-PlatformAPIAuto/common"
	"github.com/tidbcloud/qa-PlatformAPIAuto/util"
	"github.com/tidbcloud/qa-PlatformAPIAuto/webScene/br"
	dpCommon "github.com/tidbcloud/qa-PlatformAPIAuto/webScene/common"
)

func ExecuteBRSmokeUseCase(t *testing.T) {
	uniqueKey := fmt.Sprintf("%s_%s",
		dpCommon.SCHEMA_BR_SMOKE_USE_CASE, util.FnvHash(common.Envn+string(dpCommon.CloudProviderTypeAWS)+common.GetDbassRequestInfo(t).CommonEnv.Email))
	test := &BRSmokeUseCaseSceneTest{
		br.CommonSceneTest{
			SceneTest: dpCommon.SceneTest{
				T:                t,
				DbaasRequestInfo: common.GetDbassRequestInfo(t),
				ClusterId:        GetClusterId(),
				TestSchemaName:   uniqueKey,
			},
		},
	}
	test.Run()
}

type BRSmokeUseCaseSceneTest struct {
	br.CommonSceneTest
}

func (s *BRSmokeUseCaseSceneTest) Run() {
	allure.Test(s.T,
		allure.Epic(fmt.Sprintf("%s-%s", dpCommon.CloudProviderTypeAWS, dpCommon.BRSmokeUseCase)),
		allure.Feature(string(dpCommon.BRSmokeUseCase)),
		allure.Description(string(dpCommon.BRSmokeUseCase)),
		allure.Action(func() {
			s.ExecuteTidbDataWithDefaultClusterIDStep(s.InitTidb)
			s.ExecuteTidbDataWithDefaultClusterIDStep(s.InsertToFullStageTable)
			// open pitr
			s.OpenPiTR()
			s.WaitOpenPiTRReady()
			// open dual
			s.QueryDualRegionOption()
			s.OpenDualRegion()
			// restart pitr in dual region
			s.RestartPiTR()
			// if pitr is enabled, dual region will not be ready until pitr is restarted
			s.WaitOpenDualRegionReady()
			s.ExecuteTidbDataWithDefaultClusterIDStep(s.CreateIncreStageTable)
			s.ExecuteTidbDataWithDefaultClusterIDStep(s.InsertToIncreStageTable)
			// snapshot restore
			s.ExecuteSnapshotRestore()
			s.WaitSnapshotRestoreReady()
			s.AddClusterTraffic(s.SnapshotRestoreClusterID)
			s.ExecuteTiDBDataWithRetryStep(s.SnapshotRestoreClusterID, s.CheckFullStageTableCount)
			s.DeleteCluster(s.SnapshotRestoreClusterID)
			// pit restore
			s.ExecutePiTRestore()
			s.WaitPitRestoreReady()
			s.AddClusterTraffic(s.PitRestoreClusterID)
			s.ExecuteTiDBDataWithRetryStep(s.PitRestoreClusterID, s.CheckFullStageTableCount)
			s.ExecuteTiDBDataWithRetryStep(s.PitRestoreClusterID, s.CheckTableIncreTestData)
			s.DeleteCluster(s.PitRestoreClusterID)
			//close dual
			s.CloseDualRegion()
			s.WaitCloseDualRegionReady()
			// close pitr
			s.ClosePiTR()
			s.WaitClosePiTRReady()
			//delete backups
			s.CleanupBackups()
		}))
}
