kglite 0.10.26

Pure-Rust knowledge graph engine — Cypher pipeline, snapshot/working CoW transactions, columnar/mmap/disk storage backends, optional dataset loaders (SEC EDGAR, Sodir, Wikidata). PyO3 wrappers live in the sibling kglite-py crate (the Python wheel); embeddable directly from any Rust binary without PyO3 in the dep tree.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
// src/graph/export.rs
//! Export graph data to various visualization formats

use crate::datatypes::values::Value;
use crate::graph::schema::{CurrentSelection, DirGraph};
use crate::graph::storage::GraphRead;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::path::Path;

/// Export the graph (or selection) to GraphML format.
///
/// GraphML is an XML-based format supported by many graph visualization tools
/// including Gephi, yEd, and Cytoscape.
pub fn to_graphml(
    graph: &DirGraph,
    selection: Option<&CurrentSelection>,
) -> Result<String, String> {
    let mut xml = String::with_capacity(64 * 1024); // Pre-allocate 64KB

    // XML header
    xml.push_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    xml.push_str("<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\"\n");
    xml.push_str("         xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
    xml.push_str("         xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns\n");
    xml.push_str("         http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">\n");

    // Define attribute keys for nodes
    xml.push_str(
        "  <key id=\"node_type\" for=\"node\" attr.name=\"type\" attr.type=\"string\"/>\n",
    );
    xml.push_str(
        "  <key id=\"node_title\" for=\"node\" attr.name=\"title\" attr.type=\"string\"/>\n",
    );
    xml.push_str("  <key id=\"node_id\" for=\"node\" attr.name=\"id\" attr.type=\"string\"/>\n");
    xml.push_str("  <key id=\"node_properties\" for=\"node\" attr.name=\"properties\" attr.type=\"string\"/>\n");

    // Define attribute keys for edges
    xml.push_str("  <key id=\"edge_type\" for=\"edge\" attr.name=\"connection_type\" attr.type=\"string\"/>\n");
    xml.push_str("  <key id=\"edge_properties\" for=\"edge\" attr.name=\"properties\" attr.type=\"string\"/>\n");

    xml.push_str("  <graph id=\"G\" edgedefault=\"directed\">\n");

    // Determine which nodes to export
    let node_indices: Vec<_> = if let Some(sel) = selection {
        let level_idx = sel.get_level_count().saturating_sub(1);
        if let Some(level) = sel.get_level(level_idx) {
            level.get_all_nodes()
        } else {
            {
                let g = &graph.graph;
                g.node_indices().collect()
            }
        }
    } else {
        {
            let g = &graph.graph;
            g.node_indices().collect()
        }
    };

    let node_set: std::collections::HashSet<_> = node_indices.iter().copied().collect();

    // Export nodes
    for &idx in &node_indices {
        if let Some(node) = graph.graph.node_weight(idx) {
            xml.push_str(&format!("    <node id=\"n{}\">\n", idx.index()));
            xml.push_str(&format!(
                "      <data key=\"node_type\">{}</data>\n",
                escape_xml(node.node_type_str(&graph.interner))
            ));
            xml.push_str(&format!(
                "      <data key=\"node_title\">{}</data>\n",
                escape_xml(&crate::datatypes::values::raw_string(&node.title()))
            ));
            xml.push_str(&format!(
                "      <data key=\"node_id\">{}</data>\n",
                escape_xml(&crate::datatypes::values::raw_string(&node.id()))
            ));

            // Serialize properties as JSON
            if node.property_count() > 0 {
                let props_json = properties_to_json(node.property_iter(&graph.interner));
                xml.push_str(&format!(
                    "      <data key=\"node_properties\">{}</data>\n",
                    escape_xml(&props_json)
                ));
            }

            xml.push_str("    </node>\n");
        }
    }

    // Export edges (only between selected nodes)
    let mut edge_id = 0;
    for &source_idx in &node_indices {
        for edge in {
            let g = &graph.graph;
            g.edges(source_idx)
        } {
            let target_idx = edge.target();

            // Only include edge if target is in selection
            if node_set.contains(&target_idx) {
                xml.push_str(&format!(
                    "    <edge id=\"e{}\" source=\"n{}\" target=\"n{}\">\n",
                    edge_id,
                    source_idx.index(),
                    target_idx.index()
                ));
                xml.push_str(&format!(
                    "      <data key=\"edge_type\">{}</data>\n",
                    escape_xml(edge.weight().connection_type_str(&graph.interner))
                ));

                if edge.weight().property_count() > 0 {
                    let props_json =
                        properties_to_json(edge.weight().property_iter(&graph.interner));
                    xml.push_str(&format!(
                        "      <data key=\"edge_properties\">{}</data>\n",
                        escape_xml(&props_json)
                    ));
                }

                xml.push_str("    </edge>\n");
                edge_id += 1;
            }
        }
    }

    xml.push_str("  </graph>\n");
    xml.push_str("</graphml>\n");

    Ok(xml)
}

/// Export the graph (or selection) to D3.js compatible JSON format.
///
/// This format is designed for use with D3.js force-directed graph visualizations.
/// The output is a JSON object with "nodes" and "links" arrays.
pub fn to_d3_json(
    graph: &DirGraph,
    selection: Option<&CurrentSelection>,
) -> Result<String, String> {
    // Determine which nodes to export
    let node_indices: Vec<_> = if let Some(sel) = selection {
        let level_idx = sel.get_level_count().saturating_sub(1);
        if let Some(level) = sel.get_level(level_idx) {
            level.get_all_nodes()
        } else {
            {
                let g = &graph.graph;
                g.node_indices().collect()
            }
        }
    } else {
        {
            let g = &graph.graph;
            g.node_indices().collect()
        }
    };

    let node_set: std::collections::HashSet<_> = node_indices.iter().copied().collect();

    // Build index mapping (old index -> array position)
    let mut index_map: HashMap<usize, usize> = HashMap::with_capacity(node_indices.len());
    for (pos, &idx) in node_indices.iter().enumerate() {
        index_map.insert(idx.index(), pos);
    }

    // Build nodes array
    let mut nodes_json = Vec::with_capacity(node_indices.len());
    for &idx in &node_indices {
        if let Some(node) = graph.graph.node_weight(idx) {
            let mut obj = String::from("{");
            obj.push_str(&format!("\"id\":{},", json_value(&node.id())));
            obj.push_str(&format!(
                "\"type\":{},",
                json_string(node.node_type_str(&graph.interner))
            ));
            obj.push_str(&format!("\"title\":{}", json_value(&node.title())));

            // Add select properties (not all to keep output clean)
            for (key, value) in node.property_iter(&graph.interner) {
                if key != "id" && key != "title" && key != "type" {
                    obj.push_str(&format!(",{}:{}", json_string(key), json_value(value)));
                }
            }

            obj.push('}');
            nodes_json.push(obj);
        }
    }

    // Build links array
    let mut links_json = Vec::new();
    for &source_idx in &node_indices {
        for edge in {
            let g = &graph.graph;
            g.edges(source_idx)
        } {
            let target_idx = edge.target();

            if node_set.contains(&target_idx) {
                if let (Some(&source_pos), Some(&target_pos)) = (
                    index_map.get(&source_idx.index()),
                    index_map.get(&target_idx.index()),
                ) {
                    let mut link = String::from("{");
                    link.push_str(&format!("\"source\":{},", source_pos));
                    link.push_str(&format!("\"target\":{},", target_pos));
                    link.push_str(&format!(
                        "\"type\":{}",
                        json_string(edge.weight().connection_type_str(&graph.interner))
                    ));

                    // Add edge properties
                    for (key, value) in edge.weight().property_iter(&graph.interner) {
                        link.push_str(&format!(",{}:{}", json_string(key), json_value(value)));
                    }

                    link.push('}');
                    links_json.push(link);
                }
            }
        }
    }

    // Build final JSON
    let mut result = String::with_capacity(32 * 1024);
    result.push_str("{\n  \"nodes\": [\n    ");
    result.push_str(&nodes_json.join(",\n    "));
    result.push_str("\n  ],\n  \"links\": [\n    ");
    result.push_str(&links_json.join(",\n    "));
    result.push_str("\n  ]\n}");

    Ok(result)
}

/// Export to GEXF format (Gephi native format).
///
/// GEXF is the native format for Gephi and supports dynamic graphs,
/// hierarchies, and rich attribute types.
pub fn to_gexf(graph: &DirGraph, selection: Option<&CurrentSelection>) -> Result<String, String> {
    let mut xml = String::with_capacity(64 * 1024);

    // XML header
    xml.push_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    xml.push_str("<gexf xmlns=\"http://www.gexf.net/1.2draft\"\n");
    xml.push_str("      xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
    xml.push_str("      xsi:schemaLocation=\"http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd\"\n");
    xml.push_str("      version=\"1.2\">\n");
    xml.push_str("  <meta>\n");
    xml.push_str("    <creator>kglite</creator>\n");
    xml.push_str("    <description>Exported from KnowledgeGraph</description>\n");
    xml.push_str("  </meta>\n");
    xml.push_str("  <graph mode=\"static\" defaultedgetype=\"directed\">\n");

    // Define node attributes
    xml.push_str("    <attributes class=\"node\">\n");
    xml.push_str("      <attribute id=\"0\" title=\"type\" type=\"string\"/>\n");
    xml.push_str("      <attribute id=\"1\" title=\"title\" type=\"string\"/>\n");
    xml.push_str("    </attributes>\n");

    // Define edge attributes
    xml.push_str("    <attributes class=\"edge\">\n");
    xml.push_str("      <attribute id=\"0\" title=\"connection_type\" type=\"string\"/>\n");
    xml.push_str("    </attributes>\n");

    // Determine which nodes to export
    let node_indices: Vec<_> = if let Some(sel) = selection {
        let level_idx = sel.get_level_count().saturating_sub(1);
        if let Some(level) = sel.get_level(level_idx) {
            level.get_all_nodes()
        } else {
            {
                let g = &graph.graph;
                g.node_indices().collect()
            }
        }
    } else {
        {
            let g = &graph.graph;
            g.node_indices().collect()
        }
    };

    let node_set: std::collections::HashSet<_> = node_indices.iter().copied().collect();

    // Export nodes
    xml.push_str("    <nodes>\n");
    for &idx in &node_indices {
        if let Some(node) = graph.graph.node_weight(idx) {
            let title_str = crate::datatypes::values::raw_string(&node.title());
            xml.push_str(&format!(
                "      <node id=\"{}\" label=\"{}\">\n",
                idx.index(),
                escape_xml(&title_str)
            ));
            xml.push_str("        <attvalues>\n");
            xml.push_str(&format!(
                "          <attvalue for=\"0\" value=\"{}\"/>\n",
                escape_xml(node.node_type_str(&graph.interner))
            ));
            xml.push_str(&format!(
                "          <attvalue for=\"1\" value=\"{}\"/>\n",
                escape_xml(&title_str)
            ));
            xml.push_str("        </attvalues>\n");
            xml.push_str("      </node>\n");
        }
    }
    xml.push_str("    </nodes>\n");

    // Export edges
    xml.push_str("    <edges>\n");
    let mut edge_id = 0;
    for &source_idx in &node_indices {
        for edge in {
            let g = &graph.graph;
            g.edges(source_idx)
        } {
            let target_idx = edge.target();

            if node_set.contains(&target_idx) {
                xml.push_str(&format!(
                    "      <edge id=\"{}\" source=\"{}\" target=\"{}\">\n",
                    edge_id,
                    source_idx.index(),
                    target_idx.index()
                ));
                xml.push_str("        <attvalues>\n");
                xml.push_str(&format!(
                    "          <attvalue for=\"0\" value=\"{}\"/>\n",
                    escape_xml(edge.weight().connection_type_str(&graph.interner))
                ));
                xml.push_str("        </attvalues>\n");
                xml.push_str("      </edge>\n");
                edge_id += 1;
            }
        }
    }
    xml.push_str("    </edges>\n");

    xml.push_str("  </graph>\n");
    xml.push_str("</gexf>\n");

    Ok(xml)
}

/// Export to CSV format (nodes and edges as separate content).
///
/// Returns a tuple of (nodes_csv, edges_csv).
pub fn to_csv(
    graph: &DirGraph,
    selection: Option<&CurrentSelection>,
) -> Result<(String, String), String> {
    // Determine which nodes to export
    let node_indices: Vec<_> = if let Some(sel) = selection {
        let level_idx = sel.get_level_count().saturating_sub(1);
        if let Some(level) = sel.get_level(level_idx) {
            level.get_all_nodes()
        } else {
            {
                let g = &graph.graph;
                g.node_indices().collect()
            }
        }
    } else {
        {
            let g = &graph.graph;
            g.node_indices().collect()
        }
    };

    let node_set: std::collections::HashSet<_> = node_indices.iter().copied().collect();

    // Build nodes CSV
    let mut nodes_csv = String::from("id,type,title\n");
    for &idx in &node_indices {
        if let Some(node) = graph.graph.node_weight(idx) {
            nodes_csv.push_str(&format!(
                "{},{},{}\n",
                idx.index(),
                escape_csv(node.node_type_str(&graph.interner)),
                escape_csv(&crate::datatypes::values::raw_string(&node.title()))
            ));
        }
    }

    // Build edges CSV
    let mut edges_csv = String::from("source,target,type\n");
    for &source_idx in &node_indices {
        for edge in {
            let g = &graph.graph;
            g.edges(source_idx)
        } {
            let target_idx = edge.target();

            if node_set.contains(&target_idx) {
                edges_csv.push_str(&format!(
                    "{},{},{}\n",
                    source_idx.index(),
                    target_idx.index(),
                    escape_csv(edge.weight().connection_type_str(&graph.interner))
                ));
            }
        }
    }

    Ok((nodes_csv, edges_csv))
}

/// Metadata for a single connection type in the blueprint.
/// (source_type, target_type, property_columns, property_type_map)
type ConnMeta = (String, String, Vec<String>, BTreeMap<String, String>);

/// Summary of a CSV directory export.
pub struct ExportSummary {
    /// Output directory path.
    pub output_dir: String,
    /// Node counts per type: type_name → row count.
    pub nodes: BTreeMap<String, usize>,
    /// Connection counts per type: connection_type → row count.
    pub connections: BTreeMap<String, usize>,
    /// Total files written (CSVs + blueprint.json).
    pub files_written: usize,
    /// Log lines for verbose output.
    pub log_lines: Vec<String>,
}

/// Export the graph (or selection) to an organized CSV directory tree.
///
/// Creates:
/// - `nodes/<Type>.csv` for each node type (sub-nodes nested under parent)
/// - `connections/<Type>.csv` for each connection type
/// - `blueprint.json` for round-trip re-import via `from_blueprint()`
pub fn to_csv_dir(
    graph: &DirGraph,
    output_dir: &str,
    selection: Option<&CurrentSelection>,
    parent_types: &HashMap<String, String>,
) -> Result<ExportSummary, String> {
    let output = Path::new(output_dir);
    let mut log_lines = Vec::new();
    log_lines.push(format!("Exporting to {}...", output_dir));

    // ── 1. Collect selected node indices ─────────────────────────
    let node_indices = selected_node_indices(graph, selection);
    let node_set: HashSet<_> = node_indices.iter().copied().collect();

    // ── 2. Group nodes by type ───────────────────────────────────
    // type_name → Vec<NodeIndex>
    let mut nodes_by_type: BTreeMap<String, Vec<petgraph::graph::NodeIndex>> = BTreeMap::new();
    for &idx in &node_indices {
        if let Some(node) = graph.graph.node_weight(idx) {
            nodes_by_type
                .entry(node.node_type_str(&graph.interner).to_string())
                .or_default()
                .push(idx);
        }
    }

    // ── 3. Collect edges between selected nodes, grouped by type ─
    // conn_type → Vec<(source_idx, target_idx, edge_ref)>
    struct EdgeInfo {
        source_idx: petgraph::graph::NodeIndex,
        target_idx: petgraph::graph::NodeIndex,
        properties: HashMap<String, Value>,
    }
    let mut edges_by_type: BTreeMap<String, Vec<EdgeInfo>> = BTreeMap::new();
    for &source_idx in &node_indices {
        for edge in {
            let g = &graph.graph;
            g.edges(source_idx)
        } {
            let target_idx = edge.target();
            if node_set.contains(&target_idx) {
                let w = edge.weight();
                edges_by_type
                    .entry(w.connection_type_str(&graph.interner).to_string())
                    .or_default()
                    .push(EdgeInfo {
                        source_idx,
                        target_idx,
                        properties: w.properties_cloned(&graph.interner),
                    });
            }
        }
    }

    // ── 4. Create directories ────────────────────────────────────
    let nodes_dir = output.join("nodes");
    let connections_dir = output.join("connections");
    std::fs::create_dir_all(&nodes_dir)
        .map_err(|e| format!("Failed to create nodes directory: {}", e))?;
    if !edges_by_type.is_empty() {
        std::fs::create_dir_all(&connections_dir)
            .map_err(|e| format!("Failed to create connections directory: {}", e))?;
    }

    // Create sub-node directories
    for parent in parent_types.values() {
        if nodes_by_type.contains_key(parent) {
            let sub_dir = nodes_dir.join(parent);
            std::fs::create_dir_all(&sub_dir)
                .map_err(|e| format!("Failed to create sub-node directory: {}", e))?;
        }
    }

    let mut summary = ExportSummary {
        output_dir: output_dir.to_string(),
        nodes: BTreeMap::new(),
        connections: BTreeMap::new(),
        files_written: 0,
        log_lines: Vec::new(),
    };

    // ── 5. Write node CSVs ───────────────────────────────────────
    // Track property columns and types per node type for the blueprint
    let mut node_type_columns: BTreeMap<String, Vec<String>> = BTreeMap::new();
    let mut node_type_prop_types: BTreeMap<String, BTreeMap<String, String>> = BTreeMap::new();

    for (node_type, indices) in &nodes_by_type {
        // First pass: collect the union of all property names
        let mut prop_names: BTreeSet<String> = BTreeSet::new();
        for &idx in indices {
            if let Some(node) = graph.graph.node_weight(idx) {
                for key in node.property_keys(&graph.interner) {
                    prop_names.insert(key.to_string());
                }
            }
        }
        let prop_cols: Vec<String> = prop_names.into_iter().collect();

        // Infer property types from first non-null value of each property
        let mut prop_types: BTreeMap<String, String> = BTreeMap::new();
        for col in &prop_cols {
            for &idx in indices {
                if let Some(node) = graph.graph.node_weight(idx) {
                    if let Some(val) = node.get_property(col) {
                        if !matches!(*val, Value::Null) {
                            prop_types.insert(col.clone(), value_type_name(&val));
                            break;
                        }
                    }
                }
            }
        }

        // Build CSV
        let mut csv = String::with_capacity(4096);
        // Header: id, title, then properties
        csv.push_str("id,title");
        for col in &prop_cols {
            csv.push(',');
            csv.push_str(&escape_csv(col));
        }
        csv.push('\n');

        // Rows
        for &idx in indices {
            if let Some(node) = graph.graph.node_weight(idx) {
                csv.push_str(&escape_csv(&crate::datatypes::values::raw_string(
                    &node.id(),
                )));
                csv.push(',');
                csv.push_str(&escape_csv(&crate::datatypes::values::raw_string(
                    &node.title(),
                )));
                for col in &prop_cols {
                    csv.push(',');
                    if let Some(val) = node.get_property(col) {
                        csv.push_str(&escape_csv(&crate::datatypes::values::raw_string(&val)));
                    }
                }
                csv.push('\n');
            }
        }

        // Determine file path (nested under parent if sub-node)
        let csv_path = if let Some(parent) = parent_types.get(node_type) {
            nodes_dir.join(parent).join(format!("{}.csv", node_type))
        } else {
            nodes_dir.join(format!("{}.csv", node_type))
        };

        let relative_path = csv_path
            .strip_prefix(output)
            .unwrap_or(&csv_path)
            .to_string_lossy()
            .to_string();

        std::fs::write(&csv_path, &csv)
            .map_err(|e| format!("Failed to write {}: {}", relative_path, e))?;

        log_lines.push(format!(
            "  {}: {} nodes, {} properties",
            relative_path,
            indices.len(),
            prop_cols.len()
        ));

        summary.nodes.insert(node_type.clone(), indices.len());
        summary.files_written += 1;
        node_type_columns.insert(node_type.clone(), prop_cols);
        node_type_prop_types.insert(node_type.clone(), prop_types);
    }

    // ── 6. Write connection CSVs ─────────────────────────────────
    // Track connection metadata for blueprint
    let mut conn_meta: BTreeMap<String, ConnMeta> = BTreeMap::new(); // conn_type -> (source_type, target_type, prop_cols, prop_types)

    for (conn_type, edges) in &edges_by_type {
        // Collect property names across all edges of this type
        let mut prop_names: BTreeSet<String> = BTreeSet::new();
        for edge in edges {
            for key in edge.properties.keys() {
                prop_names.insert(key.clone());
            }
        }
        let prop_cols: Vec<String> = prop_names.into_iter().collect();

        // Infer property types
        let mut prop_types: BTreeMap<String, String> = BTreeMap::new();
        for col in &prop_cols {
            for edge in edges {
                if let Some(val) = edge.properties.get(col) {
                    if !matches!(val, Value::Null) {
                        prop_types.insert(col.clone(), value_type_name(val));
                        break;
                    }
                }
            }
        }

        // Detect source and target types (use first edge)
        let source_type = edges
            .first()
            .and_then(|e| graph.graph.node_weight(e.source_idx))
            .map(|n| n.node_type_str(&graph.interner).to_string())
            .unwrap_or_default();
        let target_type = edges
            .first()
            .and_then(|e| graph.graph.node_weight(e.target_idx))
            .map(|n| n.node_type_str(&graph.interner).to_string())
            .unwrap_or_default();

        // Build CSV
        let mut csv = String::with_capacity(4096);
        csv.push_str("source_id,source_type,target_id,target_type");
        for col in &prop_cols {
            csv.push(',');
            csv.push_str(&escape_csv(col));
        }
        csv.push('\n');

        for edge in edges {
            let source_id = graph
                .graph
                .node_weight(edge.source_idx)
                .map(|n| crate::datatypes::values::raw_string(&n.id()))
                .unwrap_or_default();
            let src_type = graph
                .graph
                .node_weight(edge.source_idx)
                .map(|n| n.node_type_str(&graph.interner).to_string())
                .unwrap_or_default();
            let target_id = graph
                .graph
                .node_weight(edge.target_idx)
                .map(|n| crate::datatypes::values::raw_string(&n.id()))
                .unwrap_or_default();
            let tgt_type = graph
                .graph
                .node_weight(edge.target_idx)
                .map(|n| n.node_type_str(&graph.interner).to_string())
                .unwrap_or_default();

            csv.push_str(&escape_csv(&source_id));
            csv.push(',');
            csv.push_str(&escape_csv(&src_type));
            csv.push(',');
            csv.push_str(&escape_csv(&target_id));
            csv.push(',');
            csv.push_str(&escape_csv(&tgt_type));
            for col in &prop_cols {
                csv.push(',');
                if let Some(val) = edge.properties.get(col) {
                    csv.push_str(&escape_csv(&crate::datatypes::values::raw_string(val)));
                }
            }
            csv.push('\n');
        }

        let csv_path = connections_dir.join(format!("{}.csv", conn_type));
        let relative_path = csv_path
            .strip_prefix(output)
            .unwrap_or(&csv_path)
            .to_string_lossy()
            .to_string();

        std::fs::write(&csv_path, &csv)
            .map_err(|e| format!("Failed to write {}: {}", relative_path, e))?;

        log_lines.push(format!("  {}: {} edges", relative_path, edges.len()));

        summary.connections.insert(conn_type.clone(), edges.len());
        summary.files_written += 1;
        conn_meta.insert(
            conn_type.clone(),
            (source_type, target_type, prop_cols, prop_types),
        );
    }

    // ── 7. Generate blueprint.json ───────────────────────────────
    let blueprint = build_blueprint(
        &nodes_by_type,
        &node_type_columns,
        &node_type_prop_types,
        parent_types,
        &conn_meta,
        output,
    );
    let blueprint_path = output.join("blueprint.json");
    std::fs::write(&blueprint_path, &blueprint)
        .map_err(|e| format!("Failed to write blueprint.json: {}", e))?;
    log_lines.push("  blueprint.json".to_string());
    summary.files_written += 1;

    // Summary line
    let total_nodes: usize = summary.nodes.values().sum();
    let total_edges: usize = summary.connections.values().sum();
    log_lines.push(format!(
        "Done: {} nodes, {} edges, {} files written",
        total_nodes, total_edges, summary.files_written
    ));

    summary.log_lines = log_lines;
    Ok(summary)
}

/// Extract the selected node indices (or all nodes if no selection).
fn selected_node_indices(
    graph: &DirGraph,
    selection: Option<&CurrentSelection>,
) -> Vec<petgraph::graph::NodeIndex> {
    let g = &graph.graph;
    if let Some(sel) = selection {
        let level_idx = sel.get_level_count().saturating_sub(1);
        if let Some(level) = sel.get_level(level_idx) {
            level.get_all_nodes()
        } else {
            g.node_indices().collect()
        }
    } else {
        g.node_indices().collect()
    }
}

/// Map a Value variant to a blueprint property type string.
fn value_type_name(value: &Value) -> String {
    match value {
        Value::String(_) => "string".to_string(),
        Value::Int64(_) => "int".to_string(),
        Value::Float64(_) => "float".to_string(),
        Value::Boolean(_) => "bool".to_string(),
        Value::DateTime(_) => "date".to_string(),
        Value::UniqueId(_) => "int".to_string(),
        Value::Point { .. } => "string".to_string(), // serialized as string in CSV
        // Durations are query-time-only — CSV export serializes as string.
        Value::Duration { .. } => "string".to_string(),
        Value::Null => "string".to_string(),
        Value::NodeRef(_) => "int".to_string(),
        // Phase A.1 — collection / graph-entity values serialize as
        // JSON-ish strings in CSV (round-trip is best-effort).
        Value::List(_)
        | Value::Map(_)
        | Value::Node(_)
        | Value::Relationship(_)
        | Value::Path(_) => "string".to_string(),
    }
}

/// Build a blueprint.json string for round-trip re-import.
fn build_blueprint(
    nodes_by_type: &BTreeMap<String, Vec<petgraph::graph::NodeIndex>>,
    _node_type_columns: &BTreeMap<String, Vec<String>>,
    node_type_prop_types: &BTreeMap<String, BTreeMap<String, String>>,
    parent_types: &HashMap<String, String>,
    conn_meta: &BTreeMap<String, ConnMeta>,
    _output_dir: &Path,
) -> String {
    let mut json = String::with_capacity(4096);
    json.push_str("{\n  \"settings\": {\n    \"root\": \".\"\n  },\n  \"nodes\": {");

    let mut first_node = true;
    for node_type in nodes_by_type.keys() {
        if !first_node {
            json.push(',');
        }
        first_node = false;

        // Determine CSV path relative to output_dir
        let csv_rel = if let Some(parent) = parent_types.get(node_type) {
            format!("nodes/{}/{}.csv", parent, node_type)
        } else {
            format!("nodes/{}.csv", node_type)
        };

        json.push_str(&format!("\n    {}: {{\n", json_string(node_type)));
        json.push_str(&format!("      \"csv\": {},\n", json_string(&csv_rel)));
        json.push_str("      \"pk\": \"id\",\n");
        json.push_str("      \"title\": \"title\"");

        // Properties
        if let Some(prop_types) = node_type_prop_types.get(node_type) {
            if !prop_types.is_empty() {
                json.push_str(",\n      \"properties\": {");
                let mut first_prop = true;
                for (col, typ) in prop_types {
                    if !first_prop {
                        json.push(',');
                    }
                    first_prop = false;
                    json.push_str(&format!(
                        "\n        {}: {}",
                        json_string(col),
                        json_string(typ)
                    ));
                }
                json.push_str("\n      }");
            }
        }

        // Sub-node parent reference
        if let Some(parent) = parent_types.get(node_type) {
            json.push_str(&format!(",\n      \"parent\": {}", json_string(parent)));
        }

        // Connection definitions for this node type
        let node_conns: Vec<_> = conn_meta
            .iter()
            .filter(|(_, (src, _, _, _))| src == node_type)
            .collect();

        if !node_conns.is_empty() {
            json.push_str(",\n      \"connections\": {\n        \"junction_edges\": {");
            let mut first_conn = true;
            for (conn_type, (_, target_type, prop_cols, _prop_types)) in &node_conns {
                if !first_conn {
                    json.push(',');
                }
                first_conn = false;
                let conn_csv = format!("connections/{}.csv", conn_type);
                json.push_str(&format!(
                    "\n          {}: {{\n            \"csv\": {},\n            \"source_fk\": \"source_id\",\n            \"target\": {},\n            \"target_fk\": \"target_id\"",
                    json_string(conn_type),
                    json_string(&conn_csv),
                    json_string(target_type)
                ));
                // Edge properties (exclude the 4 standard columns)
                if !prop_cols.is_empty() {
                    json.push_str(",\n            \"properties\": [");
                    let mut first_p = true;
                    for p in prop_cols.iter() {
                        if !first_p {
                            json.push_str(", ");
                        }
                        first_p = false;
                        json.push_str(&json_string(p));
                    }
                    json.push(']');
                }
                json.push_str("\n          }");
            }
            json.push_str("\n        }\n      }");
        }

        json.push_str("\n    }");
    }

    json.push_str("\n  }\n}\n");

    // Pretty-format via serde_json for consistency (the string is already valid JSON)
    if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(&json) {
        if let Ok(pretty) = serde_json::to_string_pretty(&parsed) {
            return pretty;
        }
    }
    json
}

// Helper functions

fn escape_xml(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&apos;")
}

fn escape_csv(s: &str) -> String {
    if s.contains(',') || s.contains('"') || s.contains('\n') {
        format!("\"{}\"", s.replace('"', "\"\""))
    } else {
        s.to_string()
    }
}

fn json_string(s: &str) -> String {
    format!(
        "\"{}\"",
        s.replace('\\', "\\\\")
            .replace('"', "\\\"")
            .replace('\n', "\\n")
    )
}

fn json_value(value: &Value) -> String {
    match value {
        Value::String(s) => json_string(s),
        Value::Int64(n) => n.to_string(),
        Value::Float64(f) => {
            if f.is_nan() || f.is_infinite() {
                "null".to_string()
            } else {
                f.to_string()
            }
        }
        Value::Boolean(b) => b.to_string(),
        Value::DateTime(dt) => json_string(&dt.to_string()),
        Value::UniqueId(id) => id.to_string(),
        Value::Point { lat, lon } => format!("{{\"lat\":{},\"lon\":{}}}", lat, lon),
        Value::Duration {
            months,
            days,
            seconds,
        } => format!(
            "{{\"months\":{},\"days\":{},\"seconds\":{}}}",
            months, days, seconds
        ),
        Value::Null => "null".to_string(),
        Value::NodeRef(idx) => idx.to_string(),
        // Phase A.1 — JSON-encode the new variants recursively via
        // serde_json (List, Map, Node, Relationship, Path all derive
        // Serialize). Fallback to "null" if serialisation fails (it
        // shouldn't — these are owned structures with no cycles).
        Value::List(_)
        | Value::Map(_)
        | Value::Node(_)
        | Value::Relationship(_)
        | Value::Path(_) => serde_json::to_string(value).unwrap_or_else(|_| "null".to_string()),
    }
}

fn properties_to_json<'a>(properties: impl Iterator<Item = (&'a str, &'a Value)>) -> String {
    let pairs: Vec<String> = properties
        .map(|(k, v)| format!("{}:{}", json_string(k), json_value(v)))
        .collect();
    format!("{{{}}}", pairs.join(","))
}