package job

import (
	"context"

	domusecase "haixun-backend/internal/model/job/domain/usecase"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"
)

type CreateJobLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateJobLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateJobLogic {
	return &CreateJobLogic{ctx: ctx, svcCtx: svcCtx}
}

func (l *CreateJobLogic) CreateJob(req *types.CreateJobReq) (*types.JobData, error) {
	run, err := l.svcCtx.Job.CreateRun(l.ctx, domusecase.CreateRunRequest{
		TemplateType: req.TemplateType,
		Scope:        req.Scope,
		ScopeID:      req.ScopeID,
		Payload:      req.Payload,
	})
	if err != nil {
		return nil, err
	}
	data := toJobData(run)
	return &data, nil
}
