package action

import (
	"fmt"

	"github.com/tidbcloud/cluster-service-ng/domain/workflow"
	"github.com/tidbcloud/cluster-service-ng/pkg/runtime/actions"
	"github.com/tidbcloud/cluster-service-ng/pkg/utils"
	"github.com/tidbcloud/tiinf/monitor/tilog"
	"go.uber.org/zap"
)

const (
	DeleteO11yTiDBGroupAgentActionName = "delete-o11y-tidbgroup-agent"
)

type DeleteO11yTiDBGroupAgentAction struct {
	disableO11y bool
}

func NewDeleteO11yTiDBGroupAgentAction(
	disableO11y bool,
) *DeleteO11yTiDBGroupAgentAction {
	return &DeleteO11yTiDBGroupAgentAction{
		disableO11y: disableO11y,
	}
}

func (a *DeleteO11yTiDBGroupAgentAction) Name() string {
	return DeleteO11yTiDBGroupAgentActionName
}

// we do not check o11y agent status since it is not a core step
func (a *DeleteO11yTiDBGroupAgentAction) Invoke(args actions.ActionArgs) error {
	ctx := utils.GenerateContext(args)
	if a.disableO11y {
		tilog.Info(ctx, "o11y is disabled, skip delete o11y tidbgroup agent")
		return nil
	}
	tilog.Info(ctx, "delete o11y tidbgroup agent action start")
	tidbGroupId, err := args.GetString(workflow.TiDBGroupIdKey)
	if err != nil {
		return fmt.Errorf("failed to get tidbgroup id: %w", err)
	}
	tilog.Info(ctx, "skip delete o11y tidbgroup agent; cleanup is deferred to tidb cluster deletion",
		zap.String("tidbGroupId", tidbGroupId),
	)
	return nil
}
