package threads_account

import (
	"context"

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

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

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

func (l *CreatePublishQueueItemLogic) CreatePublishQueueItem(req *types.CreatePublishQueueHandlerReq) (*types.PublishQueueItemData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	result, err := l.svcCtx.PublishQueue.Create(l.ctx, pqdomain.CreateRequest{
		TenantID:    tenantID,
		OwnerUID:    uid,
		AccountID:   req.ID,
		Text:        req.Text,
		TopicTag:    req.TopicTag,
		ScheduledAt: req.ScheduledAt,
	})
	if err != nil {
		return nil, err
	}
	return toPublishQueueItemData(result), nil
}
