package placement_topic

import (
	"context"
	"strings"

	topicdomain "haixun-backend/internal/model/placement_topic/domain/usecase"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"

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

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

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

func (l *CreatePlacementTopicLogic) CreatePlacementTopic(req *types.CreatePlacementTopicHandlerReq) (*types.PlacementTopicData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	item, err := l.svcCtx.PlacementTopic.Create(l.ctx, topicdomain.CreateRequest{
		TenantID:  tenantID,
		OwnerUID:  uid,
		BrandID:   strings.TrimSpace(req.BrandID),
		TopicName: strings.TrimSpace(req.TopicName),
		SeedQuery: strings.TrimSpace(req.SeedQuery),
		Brief:     strings.TrimSpace(req.Brief),
		ProductID: strings.TrimSpace(req.ProductID),
	})
	if err != nil {
		return nil, err
	}
	data := toPlacementTopicData(*item)
	return &data, nil
}
