package threads_account

import (
	"context"

	pqdomain "haixun-backend/internal/model/publish_queue/domain/usecase"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"
)

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

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

func (l *PatchPublishQueueItemLogic) PatchPublishQueueItem(req *types.PatchPublishQueueHandlerReq) (*types.PublishQueueItemData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	result, err := l.svcCtx.PublishQueue.Update(l.ctx, pqdomain.UpdateRequest{
		TenantID:    tenantID,
		OwnerUID:    uid,
		AccountID:   req.ID,
		QueueID:     req.QID,
		Text:        req.Text,
		TopicTag:    req.TopicTag,
		ScheduledAt: req.ScheduledAt,
	})
	if err != nil {
		return nil, err
	}
	return toPublishQueueItemData(result), nil
}
