package graph_test

import (
	"encoding/json"
	"testing"

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

func TestArtifactJSONRoundTrip(t *testing.T) {
	original := graph.Artifact{
		SchemaVersion: graph.SchemaVersionV1,
		RepoID:        "ad/adpilot-indexing-code-parser.com",
		CommitSHA:     "a3f9c12b",
		SnapshotID:    "snap_01",
		Nodes: []graph.Node{
			{ID: "file:main.go", Kind: graph.NodeKindFile, Name: "main.go", Language: "go", Path: "main.go"},
			{ID: "pkg:main", Kind: graph.NodeKindPackage, Name: "main", Language: "go"},
			{ID: "symbol:main.go#main", Kind: graph.NodeKindSymbol, Name: "main", Language: "go", Path: "main.go"},
		},
		Edges: []graph.Edge{
			{ID: "e1", Kind: graph.EdgeKindContains, SourceID: "file:main.go", TargetID: "pkg:main"},
			{ID: "e2", Kind: graph.EdgeKindDefines, SourceID: "file:main.go", TargetID: "symbol:main.go#main"},
			{ID: "e3", Kind: graph.EdgeKindImports, SourceID: "file:main.go", TargetID: "pkg:fmt"},
			{ID: "e4", Kind: graph.EdgeKindCalls, SourceID: "symbol:main.go#main", TargetID: "symbol:fmt.go#Println"},
		},
	}

	data, err := json.Marshal(original)
	if err != nil {
		t.Fatalf("marshal: %v", err)
	}

	var decoded graph.Artifact
	if err := json.Unmarshal(data, &decoded); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}

	if decoded.SchemaVersion != graph.SchemaVersionV1 {
		t.Errorf("schema_version: got %q, want v1", decoded.SchemaVersion)
	}
	if len(decoded.Nodes) != 3 {
		t.Errorf("nodes: got %d, want 3", len(decoded.Nodes))
	}
	if len(decoded.Edges) != 4 {
		t.Errorf("edges: got %d, want 4", len(decoded.Edges))
	}
	if decoded.Edges[2].Kind != graph.EdgeKindImports {
		t.Errorf("edge kind: got %q, want imports", decoded.Edges[2].Kind)
	}
}
