// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1

package threads_account

import (
	"context"

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

	"github.com/zeromicro/go-zero/core/logx"
)

type StartPublishInventoryRefillJobLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewStartPublishInventoryRefillJobLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StartPublishInventoryRefillJobLogic {
	return &StartPublishInventoryRefillJobLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *StartPublishInventoryRefillJobLogic) StartPublishInventoryRefillJob(req *types.StartPublishInventoryRefillJobHandlerReq) (resp *types.StartPublishInventoryRefillJobData, err error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	if _, err := l.svcCtx.ThreadsAccount.Get(l.ctx, tenantID, uid, req.ID); err != nil {
		return nil, err
	}
	count := req.Count
	if count <= 0 {
		count = 3
	}
	run, err := l.svcCtx.Job.CreateRun(l.ctx, jobdom.CreateRunRequest{
		TemplateType: "refill-publish-inventory",
		Scope:        "threads_account",
		ScopeID:      req.ID,
		TenantID:     tenantID,
		OwnerUID:     uid,
		Payload: map[string]any{
			"tenant_id":  tenantID,
			"owner_uid":  uid,
			"account_id": req.ID,
			"count":      count,
		},
	})
	if err != nil {
		return nil, err
	}
	return &types.StartPublishInventoryRefillJobData{
		JobID: run.ID.Hex(), Status: string(run.Status), Message: "補庫存任務已建立",
	}, nil
}
