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_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"
	export_gcp "github.com/tidbcloud/dataflow-service/app/workflow/action/backup_export/gcp"
)

//nolint:gomnd,funlen
func GetCreateGCPBackupExportDag() (*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_gcp.NewGrantPermissionAction(baseExportAction, container.Global().GCPStorageTransferClient),
		),
		action.MustNewActionWrapperV2(
			export_gcp.NewCreateTransferJobAction(baseExportAction, container.Global().GCPStorageTransferClient),
		),
		action.MustNewActionWrapperV2(
			export_gcp.NewSyncTransferJobStatusAction(baseExportAction, container.Global().GCPStorageTransferClient),
		),
		action.MustNewActionWrapperV2(
			export_common.NewFinalStepAction(baseExportAction),
		),
	}

	dag := &entity.Dag{
		BaseInfo: entity.BaseInfo{
			ID: workflow.DagCreateGCPBackupExport,
		},
		Name: workflow.DagCreateGCPBackupExport,
		Vars: entity.DagVars{
			workflow.KeyBackupExportID: {},
			workflow.KeyBackupID:       {},
		},
		Status: entity.DagStatusNormal,
		Tasks: []entity.Task{
			{
				ID:          workflow.ActionBackupExportInitialStep,
				ActionName:  workflow.ActionBackupExportInitialStep,
				DependOn:    []string{},
				TimeoutSecs: 5 * 60, // 5 minutes
			},
			{
				ID:          workflow.ActionGrantPermissionToGCPStorageTransferService,
				ActionName:  workflow.ActionGrantPermissionToGCPStorageTransferService,
				DependOn:    []string{workflow.ActionBackupExportInitialStep},
				TimeoutSecs: 5 * 60, // 5 minutes
			},
			{
				ID:          workflow.ActionCreateGCPStorageTransferJob,
				ActionName:  workflow.ActionCreateGCPStorageTransferJob,
				DependOn:    []string{workflow.ActionGrantPermissionToGCPStorageTransferService},
				TimeoutSecs: 5 * 60, // 5 minutes
			},
			{
				ID:          workflow.ActionSyncGCPStorageTransferJobStatus,
				ActionName:  workflow.ActionSyncGCPStorageTransferJobStatus,
				DependOn:    []string{workflow.ActionCreateGCPStorageTransferJob},
				TimeoutSecs: 24 * 60 * 60, // 24 hours
			},
			{
				ID:          workflow.ActionBackupExportFinalStep,
				ActionName:  workflow.ActionBackupExportFinalStep,
				DependOn:    []string{workflow.ActionSyncGCPStorageTransferJobStatus},
				TimeoutSecs: 5 * 60, // 5 minutes
			},
		},
	}

	return dag, actions
}
