package workflow

import (
	portainer "github.com/portainer/portainer/api"
	"github.com/portainer/portainer/api/dataservices"
)

const BucketName = "workflows"

type Service struct {
	dataservices.BaseDataService[portainer.Workflow, portainer.WorkflowID]
}

func NewService(connection portainer.Connection) (*Service, error) {
	err := connection.SetServiceName(BucketName)
	if err != nil {
		return nil, err
	}

	return &Service{
		BaseDataService: dataservices.BaseDataService[portainer.Workflow, portainer.WorkflowID]{
			Bucket:     BucketName,
			Connection: connection,
		},
	}, nil
}

func (service *Service) Tx(tx portainer.Transaction) ServiceTx {
	return ServiceTx{
		BaseDataServiceTx: dataservices.BaseDataServiceTx[portainer.Workflow, portainer.WorkflowID]{
			Bucket:     BucketName,
			Connection: service.Connection,
			Tx:         tx,
		},
	}
}

func (service *Service) Create(workflow *portainer.Workflow) error {
	return service.Connection.CreateObject(
		BucketName,
		func(id uint64) (int, any) {
			workflow.ID = portainer.WorkflowID(id)
			return int(workflow.ID), workflow
		},
	)
}
