package instance

import (
	"github.com/tidbcloud/devops/api"
	opent "github.com/tidbcloud/devops/entity/operation"
	"github.com/tidbcloud/devops/pkg/infra/errs"
)

type actorWhenPending struct {
	*baseActor
}

func (a *actorWhenPending) StartAction(order opent.ActionOrder, maunal bool) error {
	if !order.IsFirst() {
		return errs.APIErrorf(api.ErrCodeInvalidArgument, "order must 0 for the first action")
	}

	if err := a.ensureInExecutionWindow(); err != nil {
		return err
	}

	return a.baseActor.startAction(order, maunal, func(action *opent.InstanceAction) error {
		if action.StartedAt != nil {
			return errs.APIErrorf(api.ErrCodeFailedPrecond, "action %s with order %d is already started", action.Name, order)
		}
		return nil
	})
}

func (a *actorWhenPending) Cancel() error {
	return a.cancel()
}
