package placement_topic

import (
	"context"

	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 UpdatePlacementTopicLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

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

func (l *UpdatePlacementTopicLogic) UpdatePlacementTopic(req *types.UpdatePlacementTopicHandlerReq) (*types.PlacementTopicData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	item, err := l.svcCtx.PlacementTopic.Update(l.ctx, topicdomain.UpdateRequest{
		TenantID: tenantID,
		OwnerUID: uid,
		TopicID:  req.ID,
		Patch:    toTopicPatch(&req.UpdatePlacementTopicReq),
	})
	if err != nil {
		return nil, err
	}
	data := toPlacementTopicData(*item)
	return &data, nil
}
