// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1

package brand

import (
	"context"

	kgusecase "haixun-backend/internal/model/knowledge_graph/domain/usecase"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"

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

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

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

func (l *GetKnowledgeGraphLogic) GetKnowledgeGraph(req *types.BrandPath) (resp *types.KnowledgeGraphData, err error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	if _, err := l.svcCtx.Brand.Get(l.ctx, tenantID, uid, req.ID); err != nil {
		return nil, err
	}
	graph, err := l.svcCtx.KnowledgeGraph.Get(l.ctx, tenantID, uid, req.ID)
	if err != nil {
		return nil, err
	}
	data := toKnowledgeGraphData(graph)
	return &data, nil
}

func toKnowledgeGraphData(graph *kgusecase.GraphSummary) types.KnowledgeGraphData {
	if graph == nil {
		return types.KnowledgeGraphData{}
	}
	nodes := make([]types.KnowledgeGraphNodeData, 0, len(graph.Nodes))
	for _, node := range graph.Nodes {
		evidence := make([]types.KnowledgeGraphEvidenceData, 0, len(node.Evidence))
		for _, item := range node.Evidence {
			evidence = append(evidence, types.KnowledgeGraphEvidenceData{
				URL:     item.URL,
				Snippet: item.Snippet,
				Query:   item.Query,
			})
		}
		nodes = append(nodes, types.KnowledgeGraphNodeData{
			ID:              node.ID,
			Label:           node.Label,
			NodeKind:        node.NodeKind,
			Type:            node.Type,
			Layer:           node.Layer,
			Relation:        node.Relation,
			PlacementValue:  node.PlacementValue,
			ProductFitScore: node.ProductFitScore,
			SelectedForScan: node.SelectedForScan,
			RelevanceTags:   append([]string{}, node.DerivedTags.Relevance...),
			RecencyTags:     append([]string{}, node.DerivedTags.Recency...),
			Evidence:        evidence,
		})
	}
	edges := make([]types.KnowledgeGraphEdgeData, 0, len(graph.Edges))
	for _, edge := range graph.Edges {
		edges = append(edges, types.KnowledgeGraphEdgeData{
			From:     edge.From,
			To:       edge.To,
			Relation: edge.Relation,
		})
	}
	sources := make([]types.BraveSourceData, 0, len(graph.BraveSources))
	for _, src := range graph.BraveSources {
		sources = append(sources, types.BraveSourceData{
			Query:   src.Query,
			Title:   src.Title,
			URL:     src.URL,
			Snippet: src.Snippet,
		})
	}
	return types.KnowledgeGraphData{
		ID:             graph.ID,
		BrandID:        graph.BrandID,
		Seed:           graph.Seed,
		Nodes:          nodes,
		Edges:          edges,
		BraveSources:   sources,
		ExpandStrategy: graph.ExpandStrategy,
		PainTagCount:   graph.PainTagCount,
		GeneratedAt:    graph.GeneratedAt,
		CreateAt:       graph.CreateAt,
		UpdateAt:       graph.UpdateAt,
	}
}
