package deploy

import (
	"context"

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

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

// create sa and create config center meta
func (m *migrator) migrate0901001(ctx context.Context) gormigrate.MigrateFunc {
	return func(db *gorm.DB) error {
		return nil
		// return InvokeRunMigrateWithRetry(ctx, func() error {
		// 	return nil
		// 	// 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
		// 	// 	}

		// 	// 	for _, sharedPool := range sharedPools {
		// 	// 		remoteCli, err := m.dataplaneManager.GetClient(sharedPool.GetDataplaneName())
		// 	// 		if err != nil {
		// 	// 			return fmt.Errorf("failed to get remote client for dataplane name %s: %w", sharedPool.GetDataplaneName(), err)
		// 	// 		}

		// 	// 		// tidb-cluster sa for the shared pool
		// 	// 		{
		// 	// 			pc := &corev1.ServiceAccount{
		// 	// 				ObjectMeta: metav1.ObjectMeta{
		// 	// 					Name:      "tidb-cluster",
		// 	// 					Namespace: entity.GetDataPlaneNamespace(sharedPool.GetId()),
		// 	// 				},
		// 	// 			}

		// 	// 			if err := k8s.CreateIfNotExist(ctx, remoteCli, pc); err != nil {
		// 	// 				return fmt.Errorf("failed to create sharedpool service account: %w", err)
		// 	// 			}
		// 	// 		}

		// 	// 		// for placeholder pods
		// 	// 		{

		// 	// 			sa := &corev1.ServiceAccount{
		// 	// 				ObjectMeta: metav1.ObjectMeta{
		// 	// 					Name:      "placeholder",
		// 	// 					Namespace: "default",
		// 	// 				},
		// 	// 			}

		// 	// 			err = k8s.CreateIfNotExist(ctx, remoteCli, sa)
		// 	// 			if err != nil {
		// 	// 				return fmt.Errorf("failed to create placeholder service account: %w", err)
		// 	// 			}
		// 	// 		}

		// 	// 		// create shared pool config meta
		// 	// 		err = m.clusterConfig.CreateClusterMeta(ctx, sharedPool)
		// 	// 		if err != nil {
		// 	// 			return fmt.Errorf("failed to create cluster config meta: %w", err)
		// 	// 		}

		// 	// 		tidbClusters, err := m.tidbClusterRepo.List(ctx, &repo.TiDBClusterListFilter{
		// 	// 			Deleted:       false,
		// 	// 			SharedPoolIds: []string{sharedPool.GetId()},
		// 	// 		})
		// 	// 		if err != nil {
		// 	// 			return fmt.Errorf("failed to list tidb groups: %w", err)
		// 	// 		}

		// 	// 		for _, tidbCluster := range tidbClusters {
		// 	// 			if tidbCluster.GetVersion() == "" {
		// 	// 				tidbCluster.Version = sharedPool.GetVersion()
		// 	// 			}

		// 	// 			// create tidb cluster config meta
		// 	// 			err = m.clusterConfig.CreateTiDBClusterMeta(ctx, tidbCluster)
		// 	// 			if err != nil {
		// 	// 				return fmt.Errorf("failed to create tidb cluster config meta: %w", err)
		// 	// 			}

		// 	// 			tidbGroups, err := m.tidbGroupRepo.List(ctx, []string{sharedPool.GetId()}, nil, []string{tidbCluster.GetId()}, nil, false)
		// 	// 			if err != nil {
		// 	// 				return fmt.Errorf("failed to list tidb groups: %w", err)
		// 	// 			}

		// 	// 			if len(tidbGroups) == 0 {
		// 	// 				tilog.PlainInfo("no tidb groups found for tidb cluster, skip", zap.String("tidbClusterId", tidbCluster.GetId()))
		// 	// 				continue
		// 	// 			}
		// 	// 			for _, tidbGroup := range tidbGroups {
		// 	// 				// create tidb group config
		// 	// 				err = m.clusterConfig.CreateTiDBGroupConfig(ctx, tidbGroup)
		// 	// 				if err != nil {
		// 	// 					return fmt.Errorf("failed to create tidb group config: %w", err)
		// 	// 				}
		// 	// 			}
		// 	// 		}
		// 	// 	}

		// 	// 	// update version
		// 	// 	var (
		// 	// 		sharedPoolTemplVersion  int64
		// 	// 		tidbClusterTemplVersion int64
		// 	// 		tidbGroupTemplVersion   int64
		// 	// 	)

		// 	// 	err = db.Raw("SELECT COALESCE(MAX(template_version), 0) FROM shared_pools where deleted_at IS NULL").Scan(&sharedPoolTemplVersion).Error
		// 	// 	if err != nil {
		// 	// 		return fmt.Errorf("failed to get max template version from shared_pools: %w", err)
		// 	// 	}

		// 	// 	err = db.Raw("SELECT COALESCE(MAX(template_version), 0) FROM tidb_clusters where deleted_at IS NULL").Scan(&tidbClusterTemplVersion).Error
		// 	// 	if err != nil {
		// 	// 		return fmt.Errorf("failed to get max template version from tidb_clusters: %w", err)
		// 	// 	}

		// 	// 	err = db.Raw("SELECT COALESCE(MAX(template_version), 0) FROM tidb_groups where deleted_at IS NULL").Scan(&tidbGroupTemplVersion).Error
		// 	// 	if err != nil {
		// 	// 		return fmt.Errorf("failed to get max template version from tidb_groups: %w", err)
		// 	// 	}

		// 	// 	tilog.PlainInfo(
		// 	// 		"updated template versions",
		// 	// 		zap.Int64("old sharedPoolTemplateVersion", sharedPoolTemplVersion),
		// 	// 		zap.Int64("old tidbClusterTemplateVersion", tidbClusterTemplVersion),
		// 	// 		zap.Int64("old tidbGroupTemplateVersion", tidbGroupTemplVersion),
		// 	// 	)

		// 	// 	if sharedPoolTemplVersion != 0 {
		// 	// 		err = db.Exec("UPDATE shared_pools SET template_version = ? where deleted_at IS NULL", sharedPoolTemplVersion+1).Error
		// 	// 		if err != nil {
		// 	// 			return fmt.Errorf("failed to update template version in shared_pools: %w", err)
		// 	// 		}
		// 	// 	}

		// 	// 	if tidbClusterTemplVersion != 0 {
		// 	// 		err = db.Exec("UPDATE tidb_clusters SET template_version = ? where deleted_at IS NULL", tidbClusterTemplVersion+1).Error
		// 	// 		if err != nil {
		// 	// 			return fmt.Errorf("failed to update template version in tidb_clusters: %w", err)
		// 	// 		}
		// 	// 	}

		// 	// 	if tidbGroupTemplVersion != 0 {
		// 	// 		err = db.Exec("UPDATE tidb_groups SET template_version = ? where deleted_at IS NULL", tidbGroupTemplVersion+1).Error
		// 	// 		if err != nil {
		// 	// 			return fmt.Errorf("failed to update template version in tidb_groups: %w", err)
		// 	// 		}
		// 	// 	}
		// 	// 	return nil
		// 	// })
		// })
	}
}
