package regionalservice

import (
	"context"
	"strconv"

	"github.com/pingcap/log"
	"github.com/tidbcloud/observability/pkg/common/constants"
	"github.com/tidbcloud/observability/pkg/common/message"
	"github.com/tidbcloud/observability/pkg/resourcemanager"
	"go.uber.org/zap"
)

func (c *ClusterHandler) PauseReconcile(ctx context.Context, req message.PauseReconcileReq) error {
	cluster, err := resourcemanager.GetClusterReaderWriter(ctx).GetCluster(ctx, req.ID)
	if err != nil {
		log.Error("get cluster failed", zap.Error(err))
		return err
	}
	if cluster.Annotations == nil {
		cluster.SetAnnotations(map[string]string{})
	}

	cluster.Annotations[constants.AnnotationKeyPauseReconcile] = strconv.FormatBool(req.PauseCluster)
	if err := resourcemanager.GetClusterReaderWriter(ctx).UpdateCluster(ctx, cluster); err != nil {
		log.Error("update cluster pause-reconcile failed", zap.Error(err))
		return err
	}
	return nil
}
