package deploy

import (
	"context"

	"github.com/go-gormigrate/gormigrate/v2"
	"gorm.io/gorm"
)

func init() {
	registerMigrateRecord((*migrator).migrate20251107)
}

// upgrade cluster agent and data cleaner
func (m *migrator) migrate20251107(ctx context.Context) gormigrate.MigrateFunc {
	return func(db *gorm.DB) error {

		return InvokeRunMigrateWithRetry(ctx, func() error {
			return db.Transaction(func(tx *gorm.DB) error {
				// sharedPools, err := m.sharedPoolRepo.List(ctx, nil, false)
				// if err != nil {
				// 	return fmt.Errorf("failed to list shared pools: %w", err)
				// }

				// if len(sharedPools) == 0 {
				// 	tilog.PlainInfo("no shared pools found, skip migration")
				// 	return nil
				// }

				// applyClusterAgent := action.NewApplyClusterAgentAction(
				// 	m.dataplaneManager,
				// 	m.sharedPoolRepo,
				// 	m.templateLib,
				// 	&m.csngConfig.ImageConfig,
				// 	&m.csngConfig.BizConfig,
				// )

				// applyDataCleaner := action.NewApplyDataCleanerAction(
				// 	m.dataplaneManager,
				// 	m.sharedPoolRepo,
				// 	m.templateLib,
				// 	&m.csngConfig.ImageConfig,
				// 	&m.csngConfig.BizConfig,
				// )

				// tilog.Info(ctx, "start to upgrade cluster agent and data cleaner for shared pools")

				// for _, sharedPool := range sharedPools {

				// 	tilog.Info(ctx, "upgrading cluster agent for shared pool", zap.String("sharedPoolId", sharedPool.GetId()))

				// 	err = applyClusterAgent.Invoke(actions.ActionArgs{
				// 		workflow.SharedPoolIdKey: actions.ActionArgString(sharedPool.GetId()),
				// 	})
				// 	if err != nil {
				// 		return fmt.Errorf("failed to apply cluster agent for shared pool %s: %w", sharedPool.Id, err)
				// 	}

				// 	tilog.Info(ctx, "upgrading cluster agent completed", zap.String("sharedPoolId", sharedPool.GetId()))

				// 	tilog.Info(ctx, "upgrading data cleaner for shared pool", zap.String("sharedPoolId", sharedPool.GetId()))

				// 	err = applyDataCleaner.Invoke(actions.ActionArgs{
				// 		workflow.SharedPoolIdKey: actions.ActionArgString(sharedPool.GetId()),
				// 	})
				// 	if err != nil {
				// 		return fmt.Errorf("failed to apply data cleaner for shared pool %s: %w", sharedPool.GetId(), err)
				// 	}
				// 	tilog.Info(ctx, "upgrading data cleaner completed", zap.String("sharedPoolId", sharedPool.GetId()))
				// }
				return nil
			})
		})
	}
}
