package workflow

import (
	"context"

	"github.com/shiningrush/fastflow/pkg/entity"
	"github.com/shiningrush/fastflow/pkg/entity/run"
	"github.com/shiningrush/fastflow/pkg/mod"
)

type Workflow interface {
	Close()

	// RegisterDag register a dag to workflow
	RegisterDag(dag *entity.Dag, actions []run.Action) error

	// RunDag start a dag instance
	RunDag(dagId string, specVar map[string]string) (*entity.DagInstance, error)
	// RunDagWithTags start a dag instance with tags
	RunDagWithTags(dagId string, specVar map[string]string, tags map[string]string) (*entity.DagInstance, error)
	// RetryDagIns retry a dag instance
	RetryDagIns(dagInsId string, ops ...mod.CommandOptSetter) error
	// CancelDagIns cancel a dag instance
	CancelDagIns(dagInsId string, ops ...mod.CommandOptSetter) error

	// ListDagIns list dag instances
	ListDagIns(input *mod.ListDagInstanceInput) ([]*entity.DagInstance, error)

	// FindDagInstancesByBusinessID looks up DAG instances by the unique
	// `business_id` tag, then narrows to the given dag_id allow-list and
	// status set. It is a selective-first replacement for ListDagIns when
	// the caller has a unique business_id, avoiding fastflow's
	// (key,value) IN + GROUP BY HAVING aggregation which scans every row
	// tagged with the (very common) business_type / business_action values.
	//
	// businessID is required. dagIDs / statuses are optional filters.
	// limit <= 0 means no limit.
	FindDagInstancesByBusinessID(
		ctx context.Context,
		businessID string,
		dagIDs []string,
		statuses []entity.DagInstanceStatus,
		limit int,
	) ([]*entity.DagInstance, error)
}
