graphrag-search 0.2.11

GraphRAG — Hybrid search engine: vector embeddings + knowledge graph over Markdown notes. 100% local, single Rust binary.
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
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
//! Interactive concept-map TUI.
//!
//! Provides a terminal user interface (TUI) that renders a force-directed
//! graph of notes, entities, and tags, navigable via keyboard and mouse.
//!
//! # Usage
//!
//! Call [`run_tui`] with a pre-built [`AppState`]:
//!
//! ```ignore
//! let state = AppState { nodes, edges, positions, … };
//! run_tui(state)?;
//! ```

use std::io::stdout;

use anyhow::Result;
use crossterm::{
    event::{self, Event, KeyCode, KeyEventKind, MouseEventKind},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{
    backend::CrosstermBackend,
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    symbols::Marker,
    text::{Line as TextLine, Span},
    widgets::{
        canvas::{Canvas, Line, Rectangle},
        Block, Borders, Paragraph,
    },
    Terminal,
};

use crate::map::layout::Position;
use crate::map::{MapEdge, MapNode};

/// Recalculate offset so the selected node is centered in the viewport.
fn recenter_on_selected(state: &mut AppState) {
    let Some(sel) = state.selected_idx else {
        return;
    };
    let (world_w, world_h) = world_bounds(&state.positions);
    if world_w < 1.0 || world_h < 1.0 {
        return;
    }
    let cw = state.last_canvas_w;
    let ch = state.last_canvas_h;
    state.offset_x = cw / 2.0 - state.positions[sel].x * (cw / world_w) * state.zoom;
    state.offset_y = ch / 2.0 - state.positions[sel].y * (ch / world_h) * state.zoom;
}

// ---------------------------------------------------------------------------
// AppState
// ---------------------------------------------------------------------------

/// All the data and UI state needed to render and interact with the concept
/// map.
#[derive(Debug, Clone)]
pub struct AppState {
    /// Nodes loaded from the database.
    pub nodes: Vec<MapNode>,
    /// Edges between nodes.
    pub edges: Vec<MapEdge>,
    /// 2-D layout positions for each node (parallel to `nodes`).
    pub positions: Vec<Position>,
    /// Index of the currently highlighted / selected node, if any.
    pub selected_idx: Option<usize>,
    /// Index of the current "focus" node. Only this node and its direct
    /// neighbors are shown in the Canvas. Set to `None` on init (first
    /// visible node becomes focus).
    pub focus_idx: Option<usize>,
    /// Optional type filter: `None` = all, `Some("note")`, `Some("entity")`,
    /// `Some("tag")`.
    pub filter_type: Option<String>,
    /// Active search query text.
    pub search_query: String,
    /// Whether the user is currently typing a search query.
    pub search_active: bool,
    /// Horizontal viewport offset (panning).
    pub offset_x: f64,
    /// Vertical viewport offset (panning).
    pub offset_y: f64,
    /// Graph depth used when loading subgraphs.
    #[allow(dead_code)]
    pub depth: i32,
    /// Display mode for node labels in the Canvas: "id", "truncated", or "label".
    pub display_mode: String,
    /// Zoom level (0.3 – 3.0).
    pub zoom: f64,
    /// Last canvas dimensions (for recentering on selected node).
    pub last_canvas_w: f64,
    pub last_canvas_h: f64,
    /// Flag: recalculate offset on next render.
    pub needs_recenter: bool,
    /// Path to the SQLite database (for loading preview content).
    pub db_path: String,
    /// Whether the preview panel is visible.
    pub show_preview: bool,
    /// Content to display in the preview panel.
    pub preview_content: String,
    /// Original force-directed positions (restored when focus is cleared).
    pub original_positions: Vec<Position>,
}

impl Default for AppState {
    fn default() -> Self {
        Self {
            nodes: Vec::new(),
            edges: Vec::new(),
            positions: Vec::new(),
            selected_idx: None,
            focus_idx: None,
            filter_type: None,
            search_query: String::new(),
            search_active: false,
            offset_x: 0.0,
            offset_y: 0.0,
            depth: 2,
            display_mode: "id".to_string(),
            zoom: 1.0,
            last_canvas_w: 80.0,
            last_canvas_h: 40.0,
            needs_recenter: false,
            db_path: String::new(),
            show_preview: false,
            preview_content: String::new(),
            original_positions: Vec::new(),
        }
    }
}

// ---------------------------------------------------------------------------
// Helper: world bounds from positions
// ---------------------------------------------------------------------------

/// Return the effective world width and height of the layout.
///
/// Falls back to `(800.0, 600.0)` when there are no positions.
fn world_bounds(positions: &[Position]) -> (f64, f64) {
    if positions.is_empty() {
        return (800.0, 600.0);
    }
    let w = positions
        .iter()
        .map(|p| p.x)
        .fold(f64::NEG_INFINITY, f64::max);
    let h = positions
        .iter()
        .map(|p| p.y)
        .fold(f64::NEG_INFINITY, f64::max);
    // Guard against degenerate cases where all nodes are at 0,0.
    (w.max(100.0), h.max(100.0))
}

/// Compute column layout positions for focus mode.
///
/// Places the focus node at the centre, predecessors on the left, and
/// successors on the right.  Non-visible nodes keep their original positions.
fn compute_column_layout(state: &mut AppState) {
    let Some(focus) = state.focus_idx else { return };

    // Collect unique predecessor and successor indices.
    let focus_id = state.nodes[focus].id;
    let mut preds: Vec<usize> = Vec::new();
    let mut succs: Vec<usize> = Vec::new();

    for edge in &state.edges {
        if edge.target_id == focus_id {
            if let Some(idx) = state.nodes.iter().position(|n| n.id == edge.source_id) {
                if !preds.contains(&idx) {
                    preds.push(idx);
                }
            }
        }
        if edge.source_id == focus_id {
            if let Some(idx) = state.nodes.iter().position(|n| n.id == edge.target_id) {
                if !succs.contains(&idx) {
                    succs.push(idx);
                }
            }
        }
    }

    // World-coordinate bounds (200 x 150 by default from force-directed).
    let (world_w, world_h) = world_bounds(&state.original_positions);
    let center_x = world_w / 2.0;
    let center_y = world_h / 2.0;

    // Reset all positions to original first.
    state.positions.copy_from_slice(&state.original_positions);

    // Place focus node at centre.
    state.positions[focus].x = center_x;
    state.positions[focus].y = center_y;

    let col_gap = world_w / 5.0;
    let vert_spacing = (world_h / (preds.len().max(succs.len()).max(3) as f64 + 1.0)).min(20.0);

    for (i, &pred) in preds.iter().enumerate() {
        let total = vert_spacing * (preds.len()).saturating_sub(1) as f64;
        let start_y = center_y - total / 2.0;
        state.positions[pred].x = center_x - col_gap;
        state.positions[pred].y = start_y + i as f64 * vert_spacing;
    }

    for (i, &succ) in succs.iter().enumerate() {
        let total = vert_spacing * (succs.len()).saturating_sub(1) as f64;
        let start_y = center_y - total / 2.0;
        state.positions[succ].x = center_x + col_gap;
        state.positions[succ].y = start_y + i as f64 * vert_spacing;
    }
}

/// Load the content of a note node from the database for preview.
fn load_note_content(db_path: &str, node_id: i64) -> String {
    use rusqlite::Connection;
    match Connection::open(db_path) {
        Ok(conn) => {
            let sql = "SELECT COALESCE(chunks.text, nodes.metadata) FROM nodes LEFT JOIN chunks ON chunks.note_id = nodes.id WHERE nodes.id = ?1 LIMIT 1";
            match conn.query_row(sql, [node_id], |row| row.get::<_, String>(0)) {
                Ok(text) => {
                    // Try to extract content from JSON metadata.
                    if text.trim_start().starts_with('{') {
                        serde_json::from_str::<serde_json::Value>(&text)
                            .ok()
                            .and_then(|v| {
                                v.get("content")
                                    .and_then(|c| c.as_str().map(|s| s.to_string()))
                            })
                            .unwrap_or(text)
                    } else {
                        text
                    }
                }
                Err(_) => format!("(no content available for node {node_id})"),
            }
        }
        Err(e) => format!("(error opening database: {e})"),
    }
}

// ---------------------------------------------------------------------------
// Helpers: selection & visibility
// ---------------------------------------------------------------------------

/// Find the nearest node in a given direction from the currently selected
/// node.  If no node is selected, returns the first visible node.
pub fn nearest_node_in_direction(state: &AppState, dx: f64, dy: f64) -> Option<usize> {
    let visible: Vec<usize> = visible_nodes(state);
    if visible.is_empty() {
        return None;
    }

    let sel = state.selected_idx.filter(|&i| visible.contains(&i));

    let (sel_idx, sel_pos) = match sel {
        Some(i) => (i, &state.positions[i]),
        None => return visible.first().copied(),
    };

    let target_angle = dy.atan2(dx);

    let mut best_idx = None;
    let mut best_diff = std::f64::consts::TAU; // 2π

    for &i in &visible {
        if i == sel_idx {
            continue;
        }
        let p = &state.positions[i];
        let a = (p.y - sel_pos.y).atan2(p.x - sel_pos.x);
        let mut diff = (a - target_angle).abs();
        if diff > std::f64::consts::PI {
            diff = std::f64::consts::TAU - diff;
        }
        if diff < best_diff {
            best_diff = diff;
            best_idx = Some(i);
        }
    }

    best_idx
}

/// Find the node whose rectangle contains the given canvas coordinates, if
/// any.
///
/// `canvas_x` / `canvas_y` are in the Canvas logical coordinate system
/// (i.e., after the same transform applied in [`render_canvas`]).
pub fn node_at_position(
    state: &AppState,
    canvas_x: f64,
    canvas_y: f64,
    canvas_w: f64,
    canvas_h: f64,
) -> Option<usize> {
    let (world_w, world_h) = world_bounds(&state.positions);
    let zoom = state.zoom;
    for (i, pos) in state.positions.iter().enumerate() {
        let (label_len, _) = match state.display_mode.as_str() {
            "id" => (4, format!("{}", state.nodes[i].id)),
            "truncated" => {
                let max_chars = 12;
                let text = if state.nodes[i].label.len() > max_chars {
                    format!("{}…", &state.nodes[i].label[..max_chars])
                } else {
                    state.nodes[i].label.clone()
                };
                (text.len(), text)
            }
            _ => (state.nodes[i].label.len(), state.nodes[i].label.clone()),
        };
        let node_w = (label_len as f64 * 0.6).max(4.0);
        let node_h = 3.0;
        let sx = pos.x * (canvas_w / world_w) * zoom + state.offset_x;
        let sy = pos.y * (canvas_h / world_h) * zoom + state.offset_y;
        if canvas_x >= sx && canvas_x <= sx + node_w && canvas_y >= sy && canvas_y <= sy + node_h {
            return Some(i);
        }
    }
    None
}

/// Return the indices of nodes visible in focus mode: the focus node itself
/// plus all its direct neighbors, filtered by type.
pub fn visible_nodes(state: &AppState) -> Vec<usize> {
    let focus = match state.focus_idx {
        Some(i) => i,
        None => {
            // No focus set → return first node (will be set on first render)
            if state.nodes.is_empty() {
                return vec![];
            }
            return vec![0];
        }
    };

    let focus_id = state.nodes[focus].id;

    // Start with the focus node itself, then add neighbors
    let mut result = vec![focus];

    // Collect all neighbor indices (directly connected by any edge)
    let neighbor_indices: Vec<usize> = state
        .edges
        .iter()
        .filter_map(|edge| {
            if edge.source_id == focus_id {
                // target is the neighbor — find its index
                state.nodes.iter().position(|n| n.id == edge.target_id)
            } else if edge.target_id == focus_id {
                // source is the neighbor
                state.nodes.iter().position(|n| n.id == edge.source_id)
            } else {
                None
            }
        })
        .collect();

    result.extend(neighbor_indices);

    // Apply filter_type
    let filter = state.filter_type.as_deref();
    result
        .into_iter()
        .filter(|&i| match filter {
            None => true,
            Some(ft) => state.nodes[i].type_.eq_ignore_ascii_case(ft),
        })
        .collect()
}

/// Return the indices of edges whose *both* endpoints are visible.
#[allow(dead_code)]
pub fn visible_edges(state: &AppState) -> Vec<usize> {
    let visible: Vec<i64> = visible_nodes(state)
        .iter()
        .map(|&i| state.nodes[i].id)
        .collect();
    state
        .edges
        .iter()
        .enumerate()
        .filter(|(_, e)| visible.contains(&e.source_id) && visible.contains(&e.target_id))
        .map(|(i, _)| i)
        .collect()
}

/// Return the color for a node based on its type, using a bright variant when
/// the node is selected.
fn node_color(type_: &str, selected: bool) -> Color {
    match (type_.to_lowercase().as_str(), selected) {
        ("note", false) => Color::Blue,
        ("note", true) => Color::LightBlue,
        ("entity", false) => Color::Green,
        ("entity", true) => Color::LightGreen,
        ("tag", false) => Color::Yellow,
        ("tag", true) => Color::LightYellow,
        (_, false) => Color::White,
        (_, true) => Color::LightMagenta,
    }
}

// ---------------------------------------------------------------------------
// Rendering
// ---------------------------------------------------------------------------

/// Build the [`Canvas`] widget for the graph pane.
///
/// Takes owned data so the paint closure can capture it without borrowing
/// constraints.
#[allow(clippy::too_many_arguments)]
fn render_canvas<'a>(
    nodes: Vec<MapNode>,
    edges: Vec<MapEdge>,
    positions: Vec<Position>,
    selected_idx: Option<usize>,
    focus_idx: Option<usize>,
    filter_type: Option<String>,
    search_query: String,
    search_active: bool,
    offset_x: f64,
    offset_y: f64,
    display_mode: String,
    zoom: f64,
    canvas_area: Rect,
) -> Canvas<'a, impl Fn(&mut ratatui::widgets::canvas::Context)> {
    let canvas_w = canvas_area.width.max(1) as f64;
    let canvas_h = canvas_area.height.max(1) as f64;
    let (world_w, world_h) = world_bounds(&positions);

    // Pre-compute visibility: focus node + its direct neighbors.
    let visible_idx: Vec<usize> = {
        let tmp = AppState {
            focus_idx,
            nodes: nodes.clone(),
            edges: edges.clone(),
            filter_type,
            ..Default::default()
        };
        visible_nodes(&tmp)
    };

    // Compute visible IDs set for filtering edges.
    let visible_ids_set: Vec<i64> = visible_idx.iter().map(|&i| nodes[i].id).collect();

    let search_text = search_query.to_lowercase();
    let has_search = search_active && !search_text.is_empty();

    Canvas::default()
        .x_bounds([0.0, canvas_w])
        .y_bounds([0.0, canvas_h])
        .marker(Marker::HalfBlock)
        .background_color(Color::Black)
        .paint(move |ctx| {
            // ---- Edges ----
            // Show all edges between visible nodes (the set is small with focus mode)
            for edge in edges.iter() {
                if !visible_ids_set.contains(&edge.source_id)
                    || !visible_ids_set.contains(&edge.target_id)
                {
                    continue;
                }
                let src_idx = nodes.iter().position(|n| n.id == edge.source_id);
                let tgt_idx = nodes.iter().position(|n| n.id == edge.target_id);
                let (Some(si), Some(ti)) = (src_idx, tgt_idx) else {
                    continue;
                };
                let src_pos = &positions[si];
                let tgt_pos = &positions[ti];

                let x1 = src_pos.x * (canvas_w / world_w) * zoom + offset_x;
                let y1 = src_pos.y * (canvas_h / world_h) * zoom + offset_y;
                let x2 = tgt_pos.x * (canvas_w / world_w) * zoom + offset_x;
                let y2 = tgt_pos.y * (canvas_h / world_h) * zoom + offset_y;

                let on_screen =
                    (x1 >= -5.0 && x1 <= canvas_w + 5.0) || (x2 >= -5.0 && x2 <= canvas_w + 5.0);
                if !on_screen {
                    continue;
                }

                ctx.draw(&Line {
                    x1,
                    y1,
                    x2,
                    y2,
                    color: Color::Gray,
                });

                let mid_x = (x1 + x2) / 2.0;
                let mid_y = (y1 + y2) / 2.0;
                let dist = ((x2 - x1).powi(2) + (y2 - y1).powi(2)).sqrt();
                if display_mode == "id" && dist > 8.0 && edge.weight > 0.0 {
                    let weight_label = format!("{:.2}", edge.weight)
                        .trim_start_matches('0')
                        .to_string();
                    ctx.print(mid_x, mid_y, weight_label);
                }
            }

            // ---- Nodes ----
            for &ni in &visible_idx {
                let node = &nodes[ni];
                let pos = &positions[ni];

                let node_h = 3.0;

                let (display_text, display_width) = match display_mode.as_str() {
                    "id" => {
                        let text = format!("{}", node.id);
                        let w = (text.len() as f64 * 0.6).max(4.0);
                        (text, w)
                    }
                    "truncated" => {
                        let max_chars = 12;
                        let text = if node.label.len() > max_chars {
                            format!("{}…", &node.label[..max_chars])
                        } else {
                            node.label.clone()
                        };
                        let w = (text.len() as f64 * 0.6).max(4.0);
                        (text, w)
                    }
                    _ => {
                        // "label" mode (full label)
                        let w = (node.label.len() as f64 * 0.6).max(4.0);
                        (node.label.clone(), w)
                    }
                };

                let sx = pos.x * (canvas_w / world_w) * zoom + offset_x;
                let sy = pos.y * (canvas_h / world_h) * zoom + offset_y;

                if sx + display_width < 0.0 || sx > canvas_w || sy + node_h < 0.0 || sy > canvas_h {
                    continue;
                }

                let sel = selected_idx == Some(ni);
                let color = node_color(&node.type_, sel);
                let search_match = has_search && node.label.to_lowercase().contains(&search_text);

                if sel || search_match {
                    let border_color = if search_match {
                        Color::Magenta
                    } else {
                        Color::White
                    };
                    ctx.draw(&Rectangle {
                        x: sx - 0.5,
                        y: sy - 0.5,
                        width: display_width + 1.0,
                        height: node_h + 1.0,
                        color: border_color,
                    });
                }

                ctx.draw(&Rectangle {
                    x: sx,
                    y: sy,
                    width: display_width,
                    height: node_h,
                    color,
                });

                let text_color = if search_match { Color::Magenta } else { color };
                let label_line = TextLine::from(vec![Span::styled(
                    display_text.clone(),
                    Style::default().fg(text_color),
                )]);
                ctx.print(sx + 0.5, sy + 0.5, label_line);
            }

            // ---- Empty state ----
            if visible_idx.is_empty() {
                let msg = "No nodes to display".to_string();
                let cx = canvas_w / 2.0 - (msg.len() as f64 * 0.3);
                let cy = canvas_h / 2.0;
                ctx.print(
                    cx,
                    cy,
                    TextLine::from(vec![Span::styled(msg, Style::default().fg(Color::Gray))]),
                );
            }
        })
}

/// Build the details [`Paragraph`] widget.
fn render_details<'a>(state: &'a AppState) -> Paragraph<'a> {
    match state.selected_idx {
        None => Paragraph::new(TextLine::from(vec![Span::styled(
            "No node selected",
            Style::default().fg(Color::Gray),
        )])),
        Some(idx) => {
            let node = &state.nodes[idx];
            let pos = &state.positions[idx];

            // Node info lines.
            let mut lines: Vec<TextLine> = Vec::new();
            lines.push(TextLine::from(vec![
                Span::styled("Node: ", Style::default().add_modifier(Modifier::BOLD)),
                Span::raw(&node.label),
            ]));
            lines.push(TextLine::from(vec![
                Span::styled("Type: ", Style::default().add_modifier(Modifier::BOLD)),
                Span::raw(&node.type_),
            ]));
            lines.push(TextLine::from(vec![
                Span::styled("ID: ", Style::default().add_modifier(Modifier::BOLD)),
                Span::raw(format!("{}", node.id)),
            ]));
            lines.push(TextLine::from(vec![
                Span::styled("Pos: ", Style::default().add_modifier(Modifier::BOLD)),
                Span::raw(format!("({:.1}, {:.1})", pos.x, pos.y)),
            ]));

            // Separator.
            lines.push(TextLine::from(Span::raw("─".repeat(20))));

            // Neighbors.
            lines.push(TextLine::from(vec![
                Span::styled("Neighbors:", Style::default().add_modifier(Modifier::BOLD)),
                Span::raw(""),
            ]));

            for edge in &state.edges {
                let is_source = edge.source_id == node.id;
                let is_target = edge.target_id == node.id;
                if !is_source && !is_target {
                    continue;
                }
                let neighbor_id = if is_source {
                    edge.target_id
                } else {
                    edge.source_id
                };
                // Look up neighbor label.
                let neighbor_label = state
                    .nodes
                    .iter()
                    .find(|n| n.id == neighbor_id)
                    .map(|n| n.label.as_str())
                    .unwrap_or("?");
                let direction = if is_source { "→" } else { "←" };
                lines.push(TextLine::from(vec![
                    Span::raw(format!("  {neighbor_label} {direction} {}", edge.type_)),
                    Span::styled(
                        format!(" (w={:.2})", edge.weight),
                        Style::default().fg(Color::DarkGray),
                    ),
                ]));
            }

            Paragraph::new(lines)
        }
    }
    .block(Block::default().borders(Borders::ALL).title(" Details "))
}

/// Build the preview [`Paragraph`] widget showing note content.
fn render_preview<'a>(state: &'a AppState) -> Paragraph<'a> {
    let lines: Vec<TextLine> = state
        .preview_content
        .lines()
        .map(|l| TextLine::from(Span::raw(l)))
        .collect();
    Paragraph::new(lines)
        .block(Block::default().borders(Borders::ALL).title(format!(
            " Preview: {} ",
            state
                .selected_idx
                .and_then(|i| state.nodes.get(i))
                .map(|n| n.label.as_str())
                .unwrap_or("")
        )))
        .style(Style::default().fg(Color::White))
}

/// Build the status bar [`Paragraph`] widget.
fn render_status<'a>(state: &AppState) -> Paragraph<'a> {
    let total = state.nodes.len();
    let visible = visible_nodes(state).len();

    // Node count.
    let node_info = format!("{visible}/{total} nodes");

    // Filter info.
    let filter_info = match state.filter_type.as_deref() {
        None => "filter: all",
        Some(ft) => {
            // Use short labels for the three allowed types.
            match ft {
                "note" => "filter: notes only",
                "entity" => "filter: entities only",
                "tag" => "filter: tags only",
                other => {
                    return Paragraph::new(TextLine::from(Span::raw(format!("filter: {other}"))))
                }
            }
        }
    };

    // Display mode info.
    let display_info = format!("display: {}", state.display_mode);

    // Zoom level.
    let zoom_info = format!("zoom: {:.1}x", state.zoom);

    // Search info.
    let search_info = if state.search_active && !state.search_query.is_empty() {
        format!("/{}", state.search_query)
    } else if state.search_active {
        "/_".to_string()
    } else {
        String::new()
    };

    // Keybinding hints.
    let hints = if state.show_preview {
        "↵ close preview   q/esc quit"
    } else {
        "↑↓←→ navigate  ↵ preview  t filter  i display  +/- zoom  / search  q quit"
    };

    let mut parts: Vec<String> = vec![node_info, display_info, zoom_info, filter_info.to_string()];
    if !search_info.is_empty() {
        parts.push(search_info);
    }
    parts.push(hints.to_string());

    Paragraph::new(TextLine::from(Span::styled(
        parts.join("  │  "),
        Style::default().fg(Color::White),
    )))
    .style(Style::default().bg(Color::DarkGray))
}

// ---------------------------------------------------------------------------
// Event loop
// ---------------------------------------------------------------------------

/// Run the interactive concept-map TUI.
///
/// Sets up the terminal, enters the event loop, and restores the terminal on
/// exit.
///
/// # Errors
///
/// Returns an error if terminal setup or the event loop fails unexpectedly.
pub fn run_tui(mut state: AppState) -> Result<()> {
    let mut stdout = stdout();

    // --- Terminal setup ---
    enable_raw_mode()?;
    execute!(stdout, EnterAlternateScreen)?;
    // Enable mouse capture so we can handle click events.
    execute!(stdout, crossterm::event::EnableMouseCapture)?;

    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;
    terminal.clear()?;
    terminal.hide_cursor()?;

    // --- Main event loop ---
    let result = event_loop(&mut terminal, &mut state);

    // --- Terminal teardown ---
    terminal.show_cursor()?;
    execute!(
        terminal.backend_mut(),
        crossterm::event::DisableMouseCapture,
        LeaveAlternateScreen
    )?;
    disable_raw_mode()?;

    result
}

/// The inner event loop, factored out so `run_tui` can always perform proper
/// cleanup on both success and error.
fn event_loop(
    terminal: &mut Terminal<CrosstermBackend<std::io::Stdout>>,
    state: &mut AppState,
) -> Result<()> {
    loop {
        // Auto-set focus to first visible node on startup
        if state.focus_idx.is_none() && !state.nodes.is_empty() {
            state.focus_idx = Some(0);
            state.selected_idx = Some(0);
            compute_column_layout(state);
        }

        // Re-center if needed (after zoom or focus change)
        if state.needs_recenter {
            recenter_on_selected(state);
            state.needs_recenter = false;
        }

        // Compute canvas area for dimension tracking
        let term_size = terminal.size().ok();
        if let Some(size) = term_size {
            let area = ratatui::prelude::Rect::new(0, 0, size.width, size.height);
            let vert = Layout::default()
                .direction(Direction::Vertical)
                .constraints([
                    Constraint::Length(1),
                    Constraint::Min(0),
                    Constraint::Length(1),
                ])
                .split(area);
            let horiz = Layout::default()
                .direction(Direction::Horizontal)
                .constraints([Constraint::Percentage(70), Constraint::Percentage(30)])
                .split(vert[1]);
            state.last_canvas_w = horiz[0].width.max(1) as f64;
            state.last_canvas_h = horiz[0].height.max(1) as f64;
        }

        // --- Render ---
        terminal.draw(|f| {
            let area = f.area();

            // Vertical layout: title | main | status
            let vert = Layout::default()
                .direction(Direction::Vertical)
                .constraints([
                    Constraint::Length(1),
                    Constraint::Min(0),
                    Constraint::Length(1),
                ])
                .split(area);

            // Title bar.
            let title_block = Block::default()
                .title(" Concept Map ")
                .borders(Borders::ALL)
                .style(Style::default().fg(Color::Cyan));
            f.render_widget(title_block, vert[0]);

            // Main area: canvas | detail panel
            let horiz = Layout::default()
                .direction(Direction::Horizontal)
                .constraints([Constraint::Percentage(70), Constraint::Percentage(30)])
                .split(vert[1]);

            // Graph canvas.
            let canvas_widget = render_canvas(
                state.nodes.clone(),
                state.edges.clone(),
                state.positions.clone(),
                state.selected_idx,
                state.focus_idx,
                state.filter_type.clone(),
                state.search_query.clone(),
                state.search_active,
                state.offset_x,
                state.offset_y,
                state.display_mode.clone(),
                state.zoom,
                horiz[0],
            );
            f.render_widget(canvas_widget, horiz[0]);

            // Details / Preview panel.
            if state.show_preview {
                let preview_widget = render_preview(state);
                f.render_widget(preview_widget, horiz[1]);
            } else {
                let details_widget = render_details(state);
                f.render_widget(details_widget, horiz[1]);
            }

            // Status bar.
            let status_widget = render_status(state);
            f.render_widget(status_widget, vert[2]);
        })?;

        // --- Event handling ---
        let event = event::read()?;

        match event {
            Event::Key(key_event) => {
                if key_event.kind == KeyEventKind::Press && handle_key(state, key_event.code) {
                    break; // quit
                }
            }
            Event::Mouse(mouse_event) => {
                if mouse_event.kind == MouseEventKind::Down(crossterm::event::MouseButton::Left) {
                    handle_mouse(state, mouse_event.column, mouse_event.row, terminal);
                }
            }
            Event::Resize(..) => {
                // Just re-render on resize — handled by next draw call.
            }
            _ => {}
        }
    }

    Ok(())
}

/// Move focus to the selected node and recompute column layout.
fn move_focus_to_selected(state: &mut AppState) {
    if state.selected_idx != state.focus_idx {
        state.focus_idx = state.selected_idx;
        compute_column_layout(state);
        state.needs_recenter = true;
    }
}

/// Handle a key-press event.  Returns `true` if the application should quit.
fn handle_key(state: &mut AppState, code: KeyCode) -> bool {
    if state.search_active {
        return handle_search_key(state, code);
    }

    match code {
        KeyCode::Char('q') => {
            if state.show_preview {
                state.show_preview = false;
                false
            } else {
                true
            }
        }
        KeyCode::Esc => {
            if state.show_preview {
                state.show_preview = false;
                false
            } else {
                true
            }
        }
        KeyCode::Up | KeyCode::Char('k') => {
            state.selected_idx = nearest_node_in_direction(state, 0.0, 1.0);
            move_focus_to_selected(state);
            false
        }
        KeyCode::Down | KeyCode::Char('j') => {
            state.selected_idx = nearest_node_in_direction(state, 0.0, -1.0);
            move_focus_to_selected(state);
            false
        }
        KeyCode::Left | KeyCode::Char('h') => {
            state.selected_idx = nearest_node_in_direction(state, -1.0, 0.0);
            move_focus_to_selected(state);
            false
        }
        KeyCode::Right | KeyCode::Char('l') => {
            state.selected_idx = nearest_node_in_direction(state, 1.0, 0.0);
            move_focus_to_selected(state);
            false
        }
        KeyCode::Enter => {
            if state.show_preview {
                state.show_preview = false;
            } else if let Some(sel) = state.selected_idx {
                let node = &state.nodes[sel];
                if node.type_.eq_ignore_ascii_case("note") {
                    state.preview_content = load_note_content(&state.db_path, node.id);
                    state.show_preview = true;
                }
            }
            false
        }
        KeyCode::Char('t') => {
            cycle_filter(state);
            false
        }
        KeyCode::Char('/') => {
            state.search_active = true;
            state.search_query.clear();
            false
        }
        KeyCode::Char('i') => {
            cycle_display_mode(state);
            false
        }
        KeyCode::Char('+') | KeyCode::Char('=') => {
            state.zoom = (state.zoom + 0.2).min(3.0);
            state.needs_recenter = true;
            false
        }
        KeyCode::Char('-') | KeyCode::Char('_') => {
            state.zoom = (state.zoom - 0.2).max(0.3);
            state.needs_recenter = true;
            false
        }
        _ => false,
    }
}

/// Handle a key-press while search mode is active.
fn handle_search_key(state: &mut AppState, code: KeyCode) -> bool {
    match code {
        KeyCode::Esc => {
            state.search_active = false;
            state.search_query.clear();
            false
        }
        KeyCode::Enter => {
            state.search_active = false;
            // Keep the query for highlighting.
            false
        }
        KeyCode::Backspace => {
            state.search_query.pop();
            false
        }
        KeyCode::Char(c) if c.is_ascii_graphic() || c == ' ' => {
            state.search_query.push(c);
            false
        }
        _ => false,
    }
}

/// Handle a mouse click — attempt to select the node under the cursor.
fn handle_mouse(
    state: &mut AppState,
    col: u16,
    row: u16,
    terminal: &Terminal<CrosstermBackend<std::io::Stdout>>,
) {
    // We need the canvas-area rectangle to map screen coords → canvas coords.
    // Grab the terminal's current size and build a full-area Rect.
    let size = terminal
        .size()
        .ok()
        .unwrap_or_else(|| ratatui::layout::Size::new(80, 24));
    let area = Rect::new(0, 0, size.width, size.height);

    // Recompute the same layout as in `event_loop`.
    let vert = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1),
            Constraint::Min(0),
            Constraint::Length(1),
        ])
        .split(area);
    let horiz = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Percentage(70), Constraint::Percentage(30)])
        .split(vert[1]);

    let canvas_area = horiz[0];
    if col < canvas_area.left()
        || col >= canvas_area.right()
        || row < canvas_area.top()
        || row >= canvas_area.bottom()
    {
        return;
    }

    // Map terminal cell coordinates to canvas logical coordinates.
    let canvas_w = canvas_area.width.max(1) as f64;
    let canvas_h = canvas_area.height.max(1) as f64;
    let cx = (col - canvas_area.left()) as f64;
    let cy = (row - canvas_area.top()) as f64;

    if let Some(idx) = node_at_position(state, cx, cy, canvas_w, canvas_h) {
        state.selected_idx = Some(idx);
    }
}

/// Cycle the filter through: all → note → entity → tag → all …
fn cycle_filter(state: &mut AppState) {
    let next = match state.filter_type.as_deref() {
        None => Some("note"),
        Some("note") => Some("entity"),
        Some("entity") => Some("tag"),
        Some("tag") => None,
        Some(_other) => None,
    };
    state.filter_type = next.map(String::from);
}

/// Cycle the display mode through: id → truncated → label → id …
fn cycle_display_mode(state: &mut AppState) {
    state.display_mode = match state.display_mode.as_str() {
        "id" => "truncated".to_string(),
        "truncated" => "label".to_string(),
        _ => "id".to_string(),
    };
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    fn test_state() -> AppState {
        AppState {
            nodes: vec![
                MapNode {
                    id: 1,
                    label: "Python".into(),
                    type_: "note".into(),
                },
                MapNode {
                    id: 2,
                    label: "Rust".into(),
                    type_: "entity".into(),
                },
                MapNode {
                    id: 3,
                    label: "Tokio".into(),
                    type_: "entity".into(),
                },
                MapNode {
                    id: 4,
                    label: "tutorial".into(),
                    type_: "tag".into(),
                },
                MapNode {
                    id: 5,
                    label: "Async".into(),
                    type_: "note".into(),
                },
            ],
            edges: vec![
                MapEdge {
                    source_id: 1,
                    target_id: 2,
                    type_: "related".into(),
                    weight: 0.8,
                    context: None,
                },
                MapEdge {
                    source_id: 2,
                    target_id: 3,
                    type_: "depends".into(),
                    weight: 0.9,
                    context: None,
                },
                MapEdge {
                    source_id: 1,
                    target_id: 4,
                    type_: "tagged".into(),
                    weight: 0.5,
                    context: None,
                },
                MapEdge {
                    source_id: 5,
                    target_id: 2,
                    type_: "related".into(),
                    weight: 0.7,
                    context: None,
                },
            ],
            positions: vec![
                Position { x: 100.0, y: 200.0 },
                Position { x: 300.0, y: 150.0 },
                Position { x: 500.0, y: 400.0 },
                Position { x: 200.0, y: 500.0 },
                Position { x: 400.0, y: 100.0 },
            ],
            selected_idx: None,
            focus_idx: Some(0),
            filter_type: None,
            search_query: String::new(),
            search_active: false,
            offset_x: 0.0,
            offset_y: 0.0,
            depth: 2,
            display_mode: "id".to_string(),
            zoom: 1.0,
            last_canvas_w: 80.0,
            last_canvas_h: 40.0,
            needs_recenter: false,
            db_path: String::new(),
            show_preview: false,
            preview_content: String::new(),
            original_positions: Vec::new(),
        }
    }

    // -----------------------------------------------------------------------
    // test_app_state_initialization
    // -----------------------------------------------------------------------

    #[test]
    fn test_app_state_initialization() {
        let state: AppState = AppState::default();
        assert!(state.nodes.is_empty());
        assert!(state.edges.is_empty());
        assert!(state.positions.is_empty());
        assert_eq!(state.selected_idx, None);
        assert_eq!(state.focus_idx, None);
        assert_eq!(state.filter_type, None);
        assert!(state.search_query.is_empty());
        assert!(!state.search_active);
        assert_eq!(state.offset_x, 0.0);
        assert_eq!(state.offset_y, 0.0);
        assert_eq!(state.depth, 2);
    }

    // -----------------------------------------------------------------------
    // test_visible_nodes_no_filter
    // -----------------------------------------------------------------------

    #[test]
    fn test_visible_nodes_no_filter() {
        let state = test_state();
        let visible = visible_nodes(&state);
        // Focus on Python (idx 0): neighbors are Rust (idx 1) and tutorial (idx 3)
        assert_eq!(
            visible.len(),
            3,
            "focus node + 2 neighbors should be visible"
        );
    }

    // -----------------------------------------------------------------------
    // test_visible_nodes_filter_notes
    // -----------------------------------------------------------------------

    #[test]
    fn test_visible_nodes_filter_notes() {
        let mut state = test_state();
        state.filter_type = Some("note".into());
        let visible = visible_nodes(&state);
        // Focus on Python (idx 0, note type). Visible neighbors: Rust (entity), tutorial (tag).
        // With note filter, only Python itself remains.
        assert_eq!(visible.len(), 1, "only Python (note) among visible set");
        for &i in &visible {
            assert_eq!(state.nodes[i].type_, "note");
        }
    }

    // -----------------------------------------------------------------------
    // test_visible_edges_filtered
    // -----------------------------------------------------------------------

    #[test]
    fn test_visible_edges_filtered() {
        let mut state = test_state();
        // Focus on Rust (idx 1, entity). Neighbors: Python (note), Tokio (entity), Async (note).
        state.focus_idx = Some(1);
        // Filter to "note" only: Python (idx 0) and Async (idx 4) are visible.
        state.filter_type = Some("note".into());
        let visible = visible_edges(&state);
        // Edges whose both endpoints are notes visible from Rust:
        //   None — because no edge connects two notes directly
        //   (Async→Rust has Rust which is entity → filtered out of visible set)
        // So the result should be empty.
        assert_eq!(visible.len(), 0, "no edges between two notes");
    }

    #[test]
    fn test_visible_edges_with_entity_filter() {
        let mut state = test_state();
        // Focus on Rust (idx 1, entity). Neighbors: Python (note), Tokio (entity), Async (note).
        state.focus_idx = Some(1);
        // Filter to "entity" only: Rust (idx 1) and Tokio (idx 2) are visible.
        state.filter_type = Some("entity".into());
        let visible = visible_edges(&state);
        // Edge 2→3 (Rust→Tokio, "depends") has both endpoints as entities → visible.
        assert_eq!(visible.len(), 1, "one edge between two entities");
        assert_eq!(state.edges[visible[0]].type_, "depends");
    }

    // -----------------------------------------------------------------------
    // test_search_matching
    // -----------------------------------------------------------------------

    #[test]
    fn test_search_matching() {
        let state = test_state();
        // The search_active flag alone doesn't filter nodes — it's used for
        // highlighting.  We test that visible_nodes returns all when filter
        // is None, and that case-insensitive matching works on labels.
        let search_query = "rust".to_lowercase();
        let matched: Vec<&str> = state
            .nodes
            .iter()
            .filter(|n| n.label.to_lowercase().contains(&search_query))
            .map(|n| n.label.as_str())
            .collect();
        assert_eq!(matched, vec!["Rust"]);
    }

    // -----------------------------------------------------------------------
    // test_search_no_match
    // -----------------------------------------------------------------------

    #[test]
    fn test_search_no_match() {
        let state = test_state();
        let search_query = "nonexistent".to_lowercase();
        let matched: Vec<&str> = state
            .nodes
            .iter()
            .filter(|n| n.label.to_lowercase().contains(&search_query))
            .map(|n| n.label.as_str())
            .collect();
        assert!(matched.is_empty(), "no labels should match 'nonexistent'");
    }

    // -----------------------------------------------------------------------
    // test_cycle_filter
    // -----------------------------------------------------------------------

    #[test]
    fn test_cycle_filter() {
        let mut state = test_state();

        // Start: all
        assert_eq!(state.filter_type, None);

        cycle_filter(&mut state);
        assert_eq!(state.filter_type.as_deref(), Some("note"));

        cycle_filter(&mut state);
        assert_eq!(state.filter_type.as_deref(), Some("entity"));

        cycle_filter(&mut state);
        assert_eq!(state.filter_type.as_deref(), Some("tag"));

        cycle_filter(&mut state);
        assert_eq!(state.filter_type, None, "cycles back to all");
    }

    // -----------------------------------------------------------------------
    // test_nearest_node_in_direction
    // -----------------------------------------------------------------------

    #[test]
    fn test_nearest_node_in_direction_no_selection() {
        let state = test_state();
        let result = nearest_node_in_direction(&state, 1.0, 0.0);
        // No node selected → returns first visible node (index 0).
        assert_eq!(result, Some(0));
    }

    #[test]
    fn test_nearest_node_in_direction_with_selection() {
        let mut state = test_state();
        state.selected_idx = Some(0); // Python at (100, 200)

        // Direction: right (dx=1, dy=0)
        let right = nearest_node_in_direction(&state, 1.0, 0.0);
        // Python (100,200) → candidates with x > 100:
        //   Rust  (300,150) angle ~ -0.17 rad
        //   tutorial (200,500) angle ~ 1.25 rad
        //   Async (400,100) angle ~ -0.46 rad
        //   Tokio (500,400) angle ~ 0.56 rad
        // Closest to 0 rad = Rust (angle ~ -0.17 → diff = 0.17)
        assert_eq!(right, Some(1), "expected Rust (idx 1) to be nearest right");
    }

    // -----------------------------------------------------------------------
    // test_handle_search_key
    // -----------------------------------------------------------------------

    #[test]
    fn test_handle_search_key_escape() {
        let mut state = test_state();
        state.search_active = true;
        state.search_query = "test".into();

        let quit = handle_search_key(&mut state, KeyCode::Esc);
        assert!(!quit, "Esc should not quit during search");
        assert!(!state.search_active, "search should be cancelled");
        assert!(state.search_query.is_empty(), "query should be cleared");
    }

    #[test]
    fn test_handle_search_key_enter() {
        let mut state = test_state();
        state.search_active = true;
        state.search_query = "rust".into();

        let quit = handle_search_key(&mut state, KeyCode::Enter);
        assert!(!quit, "Enter should not quit");
        assert!(!state.search_active, "search mode should end");
        assert_eq!(state.search_query, "rust", "query should be preserved");
    }

    #[test]
    fn test_handle_search_key_backspace() {
        let mut state = test_state();
        state.search_active = true;
        state.search_query = "rust".into();

        handle_search_key(&mut state, KeyCode::Backspace);
        assert_eq!(state.search_query, "rus");
    }

    #[test]
    fn test_handle_search_key_char() {
        let mut state = test_state();
        state.search_active = true;

        handle_search_key(&mut state, KeyCode::Char('a'));
        assert_eq!(state.search_query, "a");

        handle_search_key(&mut state, KeyCode::Char('b'));
        assert_eq!(state.search_query, "ab");
    }
}