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

package threads_account

import (
	"context"

	guarddomain "haixun-backend/internal/model/publish_guard/domain/usecase"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"

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

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

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

func (l *UpsertPublishGuardPolicyLogic) UpsertPublishGuardPolicy(req *types.UpsertPublishGuardPolicyHandlerReq) (resp *types.PublishGuardPolicyData, err error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	item, err := l.svcCtx.PublishGuard.Upsert(l.ctx, guarddomain.UpsertRequest{
		TenantID: tenantID, OwnerUID: uid, AccountID: req.ID, Enabled: req.Enabled,
		MaxDailyPosts: req.MaxDailyPosts, MinIntervalMinutes: req.MinIntervalMinutes,
		ConsecutiveFailurePauseLimit: req.ConsecutiveFailurePauseLimit,
		Paused:                       req.Paused, PausedReason: req.PausedReason,
	})
	if err != nil {
		return nil, err
	}
	return toGuardPolicyData(item), nil
}
