package aws

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"
	bucket_create "github.com/tidbcloud/dataflow-service/app/workflow/action/bucket/create"
	"github.com/tidbcloud/dataflow-service/app/workflow/action/bucket/create/aws"
)

//nolint:funlen,gomnd
func GetCreateBucketDag() (*entity.Dag, []run.Action) {
	actions := []run.Action{
		aws.NewCreateBucketAction(
			container.Global().TransProvider,
			container.Global().BucketRepo,
			container.Global().AWSBucketManagerModel,
		),
		aws.NewPutBucketDefaultTagsAction(
			container.Global().TransProvider,
			container.Global().BucketRepo,
			container.Global().AWSBucketManagerModel,
		),
		aws.NewEnableBucketVersioningAction(
			container.Global().TransProvider,
			container.Global().BucketRepo,
			container.Global().AWSBucketManagerModel,
		),
		aws.NewEnableBucketEncryptionAction(
			container.Global().TransProvider,
			container.Global().BucketRepo,
			container.Global().AWSBucketManagerModel,
		),
		aws.NewCreateBucketGCRuleAction(
			container.Global().TransProvider,
			container.Global().BucketRepo,
			container.Global().AWSBucketManagerModel,
		),
		aws.NewCreateDefaultBucketPolicy(
			container.Global().TransProvider,
			container.Global().BucketRepo,
			container.Global().BucketModelProxy,
		),
		bucket_create.NewCreateBucketFinalAction(
			container.Global().TransProvider,
			container.Global().BucketRepo,
		),
	}
	dag := &entity.Dag{
		BaseInfo: entity.BaseInfo{
			ID: workflow.DagCreateBucketAWS,
		},
		Name:   workflow.DagCreateBucketAWS,
		Status: entity.DagStatusNormal,
		Vars: map[string]entity.DagVar{
			workflow.KeyBucketID:               {},
			workflow.KeyBucketVersioningSwitch: {},
			workflow.KeyBucketEncryptionSwitch: {},
			workflow.KeyBucketGCRuleSwitch:     {},
		},
		Tasks: []entity.Task{
			{
				ID:          workflow.ActionCreateBucket,
				ActionName:  workflow.ActionCreateBucket,
				TimeoutSecs: 60 * 3,
			},
			{
				ID:          workflow.ActionPutBucketDefaultTags,
				ActionName:  workflow.ActionPutBucketDefaultTags,
				DependOn:    []string{workflow.ActionCreateBucket},
				TimeoutSecs: 30,
			},
			{
				ID:          workflow.ActionEnableBucketVersioning,
				ActionName:  workflow.ActionEnableBucketVersioning,
				DependOn:    []string{workflow.ActionCreateBucket},
				TimeoutSecs: 30,
				PreChecks: map[string]*entity.Check{
					workflow.KeyBucketVersioningSwitch: {
						Conditions: []entity.TaskCondition{
							{
								Source: entity.TaskConditionSourceVars,
								Key:    workflow.KeyBucketVersioningSwitch,
								Values: []string{workflow.ValueBucketVersioningSwitchTrue},
								Op:     entity.OperatorNotIn,
							},
						},
						Act: entity.ActiveActionSkip,
					},
				},
			},
			{
				ID:          workflow.ActionEnableBucketEncryption,
				ActionName:  workflow.ActionEnableBucketEncryption,
				DependOn:    []string{workflow.ActionCreateBucket},
				TimeoutSecs: 30,
				PreChecks: map[string]*entity.Check{
					workflow.ActionEnableBucketEncryption: {
						Conditions: []entity.TaskCondition{
							{
								Source: entity.TaskConditionSourceVars,
								Key:    workflow.KeyBucketEncryptionSwitch,
								Values: []string{workflow.ValueBucketEncryptionSwitchTrue},
								Op:     entity.OperatorNotIn,
							},
						},
						Act: entity.ActiveActionSkip,
					},
				},
			},
			{
				ID:          workflow.ActionCreateBucketGCRule,
				ActionName:  workflow.ActionCreateBucketGCRule,
				DependOn:    []string{workflow.ActionCreateBucket},
				TimeoutSecs: 30,
				PreChecks: map[string]*entity.Check{
					workflow.ActionCreateBucketGCRule: {
						Conditions: []entity.TaskCondition{
							{
								Source: entity.TaskConditionSourceVars,
								Key:    workflow.KeyBucketGCRuleSwitch,
								Values: []string{workflow.ValueBucketGCRuleSwitchTrue},
								Op:     entity.OperatorNotIn,
							},
						},
						Act: entity.ActiveActionSkip,
					},
				},
			},
			{
				ID:         workflow.ActionCreateDefaultBucketPolicy,
				ActionName: workflow.ActionCreateDefaultBucketPolicy,
				DependOn: []string{
					workflow.ActionPutBucketDefaultTags,
					workflow.ActionEnableBucketVersioning,
					workflow.ActionEnableBucketEncryption,
					workflow.ActionCreateBucketGCRule,
				},
				TimeoutSecs: 30,
			},
			{
				ID:         workflow.ActionCreateBucketFinalStep,
				ActionName: workflow.ActionCreateBucketFinalStep,
				DependOn: []string{
					workflow.ActionCreateDefaultBucketPolicy,
				},
				TimeoutSecs: 30,
			},
		},
	}
	return dag, actions
}
