package aws

import (
	"fmt"
	"testing"
	"time"

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

func ExecuteTidbToMySQL(t *testing.T) {
	test := &CdcTidbToMySQLSceneTest{
		cdc.CommonSceneTest{
			SceneTest: dpCommon.SceneTest{
				T:                t,
				DbaasRequestInfo: common.GetDbassRequestInfo(t),
				ClusterId:        GetClusterId(),
				TestSchemaName: fmt.Sprintf("%s_%s",
					dpCommon.SCHEMA_TIDB_TO_MYSQL, util.FnvHash(common.Envn+string(dpCommon.CloudProviderTypeAWS)+common.GetDbassRequestInfo(t).CommonEnv.Email)),
			},
			StartUTC: time.Now().UTC(),
		},
	}
	test.Run()
}

type CdcTidbToMySQLSceneTest struct {
	cdc.CommonSceneTest
}

func (s *CdcTidbToMySQLSceneTest) Run() {
	allure.Test(s.T,
		allure.Epic(fmt.Sprintf("%s-%s", dpCommon.CloudProviderTypeAWS, dpCommon.TidbToMysql)),
		allure.Feature(string(dpCommon.TidbToMysql)),
		allure.Description(string(dpCommon.TidbToMysql)),
		allure.Action(func() {
			s.GetClusterVersion()
			s.TestDownStreamConnectionStep(s.buildTestConnectToMySQLReq())
			s.ExecuteSQLToMySQLStep(s.InitMySQL)
			s.ExecuteTidbDataWithDefaultClusterIDStep(s.InitTidb)
			s.ExecuteTidbDataWithDefaultClusterIDStep(s.SetGC720h)
			s.DescribeTidbSchemaTableStep()
			s.CreateChangefeedStep(s.buildCreateTidbToMySQLChangefeedReq())
			s.WaitChangefeedStatusReadyStep(models.DataflowConnectorStatusRUNNING)
			s.ExecuteTidbDataWithDefaultClusterIDStep(s.SetGC10m)
			s.ExecuteTidbDataWithDefaultClusterIDStep(s.InitTidb)
			s.ExecuteTidbDataWithDefaultClusterIDStep(s.InsertToFullStageTable)
			s.ExecuteMySQLDataWithRetryStep(s.CheckFullStageTableCount)
			//s.GetMetrics()
			s.StopChangefeedStep()
			s.WaitChangefeedStatusReadyStep(models.DataflowConnectorStatusPAUSED)
			s.EditChangefeedStep(s.buildEditTidbToMySQLChangefeedReq())
			s.WaitChangefeedStatusReadyStep(models.DataflowConnectorStatusPAUSED)
			s.StartChangefeedStep()
			s.WaitChangefeedStatusReadyStep(models.DataflowConnectorStatusRUNNING)
			s.ExecuteTidbDataWithDefaultClusterIDStep(s.CreateIncreStageTable)
			s.ExecuteTidbDataWithDefaultClusterIDStep(s.InsertToIncreStageTable)
			s.ExecuteMySQLDataWithRetryStep(s.CheckTableIncreTestData)
			s.DeleteChangefeedStep()
		}))
}

func (s *CdcTidbToMySQLSceneTest) buildTestConnectToMySQLReq() *models.DataflowConnectorCreateReq {
	return &models.DataflowConnectorCreateReq{
		ConnectorType: models.DataflowConnectorTypeMYSQL.Pointer(),
		MysqlInfo: &models.DataflowMySQLInfo{
			Connection: &models.DataflowConnection{
				Endpoint: s.DbaasRequestInfo.CommonEnv.MysqlUrl,
				Username: s.DbaasRequestInfo.CommonEnv.MysqlUser,
				Password: s.DbaasRequestInfo.CommonEnv.MysqlPassword,
			},
		},
	}
}

func (s *CdcTidbToMySQLSceneTest) buildCreateTidbToMySQLChangefeedReq() *models.DataflowConnectorCreateReq {
	return &models.DataflowConnectorCreateReq{
		ConnectorType: models.DataflowConnectorTypeMYSQL.Pointer(),
		MysqlInfo: &models.DataflowMySQLInfo{
			Connection: &models.DataflowConnection{
				Endpoint: s.DbaasRequestInfo.CommonEnv.MysqlUrl,
				Username: s.DbaasRequestInfo.CommonEnv.MysqlUser,
				Password: s.DbaasRequestInfo.CommonEnv.MysqlPassword,
			},
			TableConfig: &models.DataflowTableConfig{
				FilterRule: []string{
					fmt.Sprintf("%s.test*", s.TestSchemaName),
				},
				Mode: models.DataflowTableModeFORCESYNC.Pointer(),
			},
			StartPosition: &models.DataflowStartPosition{
				Mode: models.DataflowStartModeFROMNOW.Pointer(),
			},
		},
		ChangefeedInstanceType: dpCommon.RCU2,
	}
}

func (s *CdcTidbToMySQLSceneTest) buildEditTidbToMySQLChangefeedReq() *models.DataflowConnectorEditReq {
	return &models.DataflowConnectorEditReq{
		ConnectorID:   s.ChangefeedId,
		ConnectorType: models.DataflowConnectorTypeMYSQL.Pointer(),
		MysqlInfo: &models.DataflowMySQLInfo{
			Connection: &models.DataflowConnection{
				Endpoint: s.DbaasRequestInfo.CommonEnv.MysqlUrl,
				Username: s.DbaasRequestInfo.CommonEnv.MysqlUser,
				Password: s.DbaasRequestInfo.CommonEnv.MysqlPassword,
			},
			TableConfig: &models.DataflowTableConfig{
				FilterRule: []string{
					fmt.Sprintf("%s.test*", s.TestSchemaName),
					fmt.Sprintf("%s.incre_test*", s.TestSchemaName),
				},
				Mode: models.DataflowTableModeFORCESYNC.Pointer(),
			},
			StartPosition: &models.DataflowStartPosition{
				Mode: models.DataflowStartModeFROMNOW.Pointer(),
			},
		},
	}
}
