package backup_export

import (
	"github.com/shiningrush/fastflow/pkg/entity"
	"github.com/shiningrush/fastflow/pkg/entity/run"
	"github.com/tidbcloud/dataflow-service/app/container"
	"github.com/tidbcloud/dataflow-service/app/workflow"
	"github.com/tidbcloud/dataflow-service/app/workflow/action"
	export_aws "github.com/tidbcloud/dataflow-service/app/workflow/action/backup_export/aws"
	export_base "github.com/tidbcloud/dataflow-service/app/workflow/action/backup_export/base"
	export_common "github.com/tidbcloud/dataflow-service/app/workflow/action/backup_export/common"
)

//nolint:gomnd,funlen
func GetCreateAWSBackupExportDag() (*entity.Dag, []run.Action) {
	baseExportAction := export_base.NewBaseExportAction(
		container.Global().TransProvider,
		container.Global().BackupRepo,
		container.Global().BackupExportRepo,
		container.Global().BucketRepo,
	)

	actions := []run.Action{
		action.MustNewActionWrapperV2(
			export_common.NewInitialStepAction(baseExportAction),
		),
		action.MustNewActionWrapperV2(
			export_aws.NewGrantPermissionAction(
				baseExportAction,
				container.Global().AWSBatchOpsClient,
				container.Global().BucketModelProxy,
			),
		),
		action.MustNewActionWrapperV2(
			export_aws.NewCreateBatchOpsJobAction(
				baseExportAction,
				container.Global().AWSBatchOpsClient,
			),
		),
		action.MustNewActionWrapperV2(
			export_aws.NewSyncBatchOpsJobStatusAction(baseExportAction, container.Global().AWSBatchOpsClient),
		),
		action.MustNewActionWrapperV2(
			export_common.NewFinalStepAction(baseExportAction),
		),
		action.MustNewActionWrapperV2(
			export_aws.NewRevokePermissionAction(
				baseExportAction,
				container.Global().AWSBatchOpsClient,
				container.Global().BucketModelProxy,
			),
		),
	}

	dag := &entity.Dag{
		BaseInfo: entity.BaseInfo{
			ID: workflow.DagCreateAWSBackupExport,
		},
		Name: workflow.DagCreateAWSBackupExport,
		Vars: entity.DagVars{
			workflow.KeyBackupExportID: {},
			workflow.KeyBackupID:       {},
		},
		Status: entity.DagStatusNormal,
		Tasks: []entity.Task{
			{
				ID:          workflow.ActionBackupExportInitialStep,
				ActionName:  workflow.ActionBackupExportInitialStep,
				DependOn:    []string{},
				TimeoutSecs: 5 * 60,
			},
			{
				ID:          workflow.ActionGrantPermissionToAWSS3BatchOpsRole,
				ActionName:  workflow.ActionGrantPermissionToAWSS3BatchOpsRole,
				DependOn:    []string{workflow.ActionBackupExportInitialStep},
				TimeoutSecs: 10 * 60,
			},
			{
				ID:          workflow.ActionCreateAWSS3BatchOpsJob,
				ActionName:  workflow.ActionCreateAWSS3BatchOpsJob,
				DependOn:    []string{workflow.ActionGrantPermissionToAWSS3BatchOpsRole},
				TimeoutSecs: 60 * 60,
			},
			{
				ID:          workflow.ActionSyncAWSS3BatchOpsJobStatus,
				ActionName:  workflow.ActionSyncAWSS3BatchOpsJobStatus,
				DependOn:    []string{workflow.ActionCreateAWSS3BatchOpsJob},
				TimeoutSecs: 24 * 60 * 60,
			},
			{
				ID:          workflow.ActionBackupExportFinalStep,
				ActionName:  workflow.ActionBackupExportFinalStep,
				DependOn:    []string{workflow.ActionSyncAWSS3BatchOpsJobStatus},
				TimeoutSecs: 5 * 60,
			},
			{
				ID:          workflow.ActionRevokePermissionFromAWSS3BatchOpsRole,
				ActionName:  workflow.ActionRevokePermissionFromAWSS3BatchOpsRole,
				DependOn:    []string{workflow.ActionBackupExportFinalStep},
				TimeoutSecs: 5 * 60,
			},
		},
	}

	return dag, actions
}
