package create

import (
	"context"

	appworkflow "github.com/tidbcloud/serverless-service/internal/workflow"
	"github.com/tidbcloud/serverless-service/internal/workflow/action"
	workflowchangefeed "github.com/tidbcloud/serverless-service/internal/workflow/action/changefeed"
	"go.uber.org/zap"
)

type ActionRegisterClusterJob struct {
	hook appworkflow.CDCNewArchHook
}

func NewRegisterClusterJobAction(hook appworkflow.CDCNewArchHook) *action.Wrapper {
	return action.MustNewActionWrapper(
		appworkflow.ActionRegisterClusterJob,
		&ActionRegisterClusterJob{hook: hook},
		action.SetRunRetryFun(action.InvokeRunActionWithInfinitelyRetry),
	)
}

func (a *ActionRegisterClusterJob) Params() any {
	return &workflowchangefeed.CommonParams{}
}

func (a *ActionRegisterClusterJob) Run(ctx context.Context, logger *zap.Logger, params any) error {
	input := params.(*workflowchangefeed.CommonParams)
	logger.Info("registering cluster job for newarch changefeed protection")
	return a.hook.RegisterCDCClusterJob(ctx, input.ClusterID, input.ChangefeedID)
}
