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 ActionCreateChangefeed struct {
	hook appworkflow.CDCNewArchHook
}

func NewCreateChangefeedAction(hook appworkflow.CDCNewArchHook) *action.Wrapper {
	return action.MustNewActionWrapper(
		appworkflow.ActionCreateNewArchFeed,
		&ActionCreateChangefeed{hook: hook},
		action.SetRunRetryFun(action.InvokeRunActionWithInfinitelyRetry),
	)
}

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

func (a *ActionCreateChangefeed) Run(ctx context.Context, logger *zap.Logger, params any) error {
	input := params.(*workflowchangefeed.CommonParams)
	logger.Info("creating newarch changefeed after cluster is ready")
	return a.hook.CreateChangefeed(ctx, input.ClusterID, input.ChangefeedID)
}
