package placement_topic

import (
	"context"

	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"

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

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

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

func (l *GetPlacementTopicGraphLogic) GetPlacementTopicGraph(req *types.PlacementTopicPath) (*types.KnowledgeGraphData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	scope, err := resolveScope(l.ctx, l.svcCtx, tenantID, uid, req.ID)
	if err != nil {
		return nil, err
	}
	graph, err := l.svcCtx.KnowledgeGraph.GetByTopic(l.ctx, tenantID, uid, req.ID, scope.BrandID)
	if err != nil {
		return nil, err
	}
	data := toKnowledgeGraphData(graph)
	return &data, nil
}
