package graph_test

import (
	"testing"

	"bit.admedia.com/scm/ad/adpilot-indexing-code-parser.com/internal/contracts/graph"
)

func TestComputeFileDelta(t *testing.T) {
	base := graph.Artifact{
		Nodes: []graph.Node{
			{ID: "file:main.go", Kind: graph.NodeKindFile, Name: "main.go", Path: "main.go"},
			{ID: "symbol:main.go#main", Kind: graph.NodeKindSymbol, Name: "main", Path: "main.go"},
			{ID: "symbol:main.go#helper", Kind: graph.NodeKindSymbol, Name: "helper", Path: "main.go"},
		},
		Edges: []graph.Edge{
			{ID: "edge:1", Kind: graph.EdgeKindDefines, SourceID: "file:main.go", TargetID: "symbol:main.go#helper"},
		},
	}
	target := graph.Artifact{
		Nodes: []graph.Node{
			{ID: "file:main.go", Kind: graph.NodeKindFile, Name: "main.go", Path: "main.go"},
			{ID: "symbol:main.go#main", Kind: graph.NodeKindSymbol, Name: "main", Path: "main.go"},
			{ID: "symbol:main.go#run", Kind: graph.NodeKindSymbol, Name: "run", Path: "main.go"},
		},
		Edges: []graph.Edge{
			{ID: "edge:2", Kind: graph.EdgeKindCalls, SourceID: "symbol:main.go#main", TargetID: "symbol:main.go#run"},
		},
	}

	delta := graph.ComputeFileDelta(base, target, "main.go", "art_base", "art_target")

	if len(delta.AddedNodes) != 1 || delta.AddedNodes[0].Name != "run" {
		t.Fatalf("added nodes: %+v", delta.AddedNodes)
	}
	if len(delta.RemovedNodes) != 1 || delta.RemovedNodes[0].Name != "helper" {
		t.Fatalf("removed nodes: %+v", delta.RemovedNodes)
	}
	if len(delta.AddedEdges) != 1 {
		t.Fatalf("added edges: %+v", delta.AddedEdges)
	}
	if len(delta.RemovedEdges) != 1 {
		t.Fatalf("removed edges: %+v", delta.RemovedEdges)
	}
}

func TestDeltaCounts(t *testing.T) {
	delta := graph.AggregateDelta("ad/example", "snap_1", "base", "target", []string{"main.go"}, []graph.FileDelta{
		{
			AddedNodes:   []graph.Node{{ID: "n1"}},
			RemovedNodes: []graph.Node{{ID: "n2"}},
			AddedEdges:   []graph.Edge{{ID: "e1"}},
		},
	})

	added, removed, edges := delta.Counts()
	if added != 1 || removed != 1 || edges != 1 {
		t.Fatalf("counts: added=%d removed=%d edges=%d", added, removed, edges)
	}
}
