package crrf

import (
	"fmt"

	"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"
	crrf_common "github.com/tidbcloud/dataflow-service/app/workflow/action/crrf/common"
	crrf_enable "github.com/tidbcloud/dataflow-service/app/workflow/action/crrf/enable"
)

// GetEnableCRRFDag returns the DAG definition and actions for enabling CRRF.
//
// DAG sequence:
//  1. enable_dual_region                   — triggers S3 CRR dual-region setup and waits until active; caches bucket IDs
//  2. enable_pitr                          — triggers PiTR log backup and waits until running (up to 48h)
//  3. modify_replication_rtc (enable=true)  — enables S3 RTC synchronously (cloud API + transactional DB update)
//  4. enable_crrf_daemon                   — calls EnableCRRF on cluster-server and waits until RUNNING
//  5. toggle_periodic_backup (enable=true) — enables CRRF periodic backup via mgmt service
//  6. mark_enabled                         — transitions CRRFSetting enabling → enabled
func GetEnableCRRFDag() (*entity.Dag, []run.Action) {
	c := container.Global()

	actions := []run.Action{
		crrf_enable.NewEnableDualRegionAction(c.BackupSettingRepo, c.BackupSettingModel, c.CRRFSettingRepo),
		crrf_enable.NewEnablePitrAction(c.BackupSettingRepo, c.BackupSettingModel, c.LogBackupRepo, c.CRRFSettingRepo),
		crrf_common.NewCRRFModifyReplicationRTCAction(c.BackupSettingModel, c.CRRFSettingRepo),
		crrf_enable.NewEnableCRRFDaemonAction(c.BackupSettingRepo, c.LogBackupRepo, c.CRRFSettingRepo, c.BucketRepo, c.CRRFFacade),
		crrf_common.NewTogglePeriodicClusterSettingsBackupAction(c.DedicatedGlobalGrpcClient),
		crrf_enable.NewMarkEnabledAction(c.CRRFSettingRepo),
	}

	dag := &entity.Dag{
		BaseInfo: entity.BaseInfo{
			ID: workflow.DagEnableCRRF,
		},
		Name: workflow.DagEnableCRRF,
		Vars: entity.DagVars{
			workflow.KeyCRRFSettingID:            {},
			workflow.KeyBackupSettingId:          {},
			workflow.ClusterID:                   {},
			workflow.KeySecondaryRegion:          {},
			workflow.KeySkipEnablePiTRDualRegion: {},
		},
		Status: entity.DagStatusNormal,
		Tasks: []entity.Task{
			{
				ID:         workflow.ActionCRRFEnableDualRegion,
				ActionName: workflow.ActionCRRFEnableDualRegion,
				Params: map[string]interface{}{
					workflow.ClusterID:                   fmt.Sprintf("{{%v}}", workflow.ClusterID),
					workflow.KeySecondaryRegion:          fmt.Sprintf("{{%v}}", workflow.KeySecondaryRegion),
					workflow.KeySkipEnablePiTRDualRegion: fmt.Sprintf("{{%v}}", workflow.KeySkipEnablePiTRDualRegion),
				},
				TimeoutSecs: 40 * 60,
			},
			{
				ID:         workflow.ActionCRRFEnablePitr,
				ActionName: workflow.ActionCRRFEnablePitr,
				Params: map[string]interface{}{
					workflow.ClusterID:                   fmt.Sprintf("{{%v}}", workflow.ClusterID),
					workflow.KeySkipEnablePiTRDualRegion: fmt.Sprintf("{{%v}}", workflow.KeySkipEnablePiTRDualRegion),
				},
				DependOn:    []string{workflow.ActionCRRFEnableDualRegion},
				TimeoutSecs: 48 * 60 * 60, // 48h — PiTR enable may wait for next scheduled snapshot backup
			},
			{
				ID:          workflow.ActionCRRFModifyReplicationRTC + "_enable",
				ActionName:  workflow.ActionCRRFModifyReplicationRTC,
				Params:      map[string]interface{}{workflow.ClusterID: fmt.Sprintf("{{%v}}", workflow.ClusterID), "enable": true},
				DependOn:    []string{workflow.ActionCRRFEnablePitr},
				TimeoutSecs: 5 * 60,
			},
			{
				ID:          workflow.ActionCRRFEnableDaemon,
				ActionName:  workflow.ActionCRRFEnableDaemon,
				Params:      map[string]interface{}{workflow.ClusterID: fmt.Sprintf("{{%v}}", workflow.ClusterID)},
				DependOn:    []string{workflow.ActionCRRFModifyReplicationRTC + "_enable"},
				TimeoutSecs: 15 * 60,
			},
			{
				ID:          workflow.ActionCRRFTogglePeriodicBackup + "_enable",
				ActionName:  workflow.ActionCRRFTogglePeriodicBackup,
				Params:      map[string]interface{}{workflow.ClusterID: fmt.Sprintf("{{%v}}", workflow.ClusterID), "enable": true},
				DependOn:    []string{workflow.ActionCRRFEnableDaemon},
				TimeoutSecs: 5 * 60,
			},
			{
				ID:          workflow.ActionCRRFMarkEnabled,
				ActionName:  workflow.ActionCRRFMarkEnabled,
				Params:      map[string]interface{}{workflow.KeyCRRFSettingID: fmt.Sprintf("{{%v}}", workflow.KeyCRRFSettingID), workflow.ClusterID: fmt.Sprintf("{{%v}}", workflow.ClusterID)},
				DependOn:    []string{workflow.ActionCRRFTogglePeriodicBackup + "_enable"},
				TimeoutSecs: 5 * 60,
			},
		},
	}
	return dag, actions
}
