dbtui 0.2.3

Terminal database client with Vim-style navigation
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
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use vimltui::EditorAction;

use crate::ui::state::AppState;
use crate::ui::tabs::{SubView, TabKind};

use super::Action;

pub(super) fn handle_tab_editor(state: &mut AppState, key: KeyEvent) -> Action {
    let tab_idx = state.active_tab_idx;
    if tab_idx >= state.tabs.len() {
        return Action::None;
    }

    let tab = &mut state.tabs[tab_idx];
    let tab_id = tab.id;

    let is_script = matches!(tab.kind, TabKind::Script { .. });
    let db_type = state.conn.db_type;

    // Determine if this is a source code tab (Package/Function/Procedure)
    let is_source_tab = matches!(
        tab.kind,
        TabKind::Package { .. } | TabKind::Function { .. } | TabKind::Procedure { .. }
    );

    let in_insert = tab
        .active_editor()
        .is_some_and(|e| matches!(e.mode, vimltui::VimMode::Insert | vimltui::VimMode::Replace));

    let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);

    // --- Completion keys in Insert mode ---
    // Ctrl+Space: open/refresh completion
    // Ctrl+N: next item, Ctrl+P: previous item, Ctrl+Y: accept
    if in_insert {
        if ctrl {
            match key.code {
                KeyCode::Char(' ') => {
                    update_completion_impl(state, true);
                    return Action::Render;
                }
                KeyCode::Char('n') => {
                    if let Some(ref mut cmp) = state.engine.completion {
                        cmp.next();
                    }
                    return Action::Render;
                }
                KeyCode::Char('p') => {
                    if let Some(ref mut cmp) = state.engine.completion {
                        cmp.prev();
                    }
                    return Action::Render;
                }
                KeyCode::Char('y') => {
                    if let Some(cmp) = state.engine.completion.take() {
                        accept_completion(state, &cmp);
                        // Re-trigger completion (e.g., after alias. -> show columns)
                        if let Some(action) = update_completion_impl(state, false) {
                            return action;
                        }
                    }
                    return Action::Render;
                }
                _ => {}
            }
        }
        // Enter accepts completion if popup is open
        if key.code == KeyCode::Enter && state.engine.completion.is_some() {
            if let Some(cmp) = state.engine.completion.take() {
                accept_completion(state, &cmp);
                // Re-trigger completion (e.g., after alias. -> show columns)
                if let Some(action) = update_completion_impl(state, false) {
                    return action;
                }
            }
            return Action::Render;
        }
        // Escape closes completion
        if key.code == KeyCode::Esc && state.engine.completion.is_some() {
            state.engine.completion = None;
        }
    }

    // --- Normal mode: diagnostic navigation, sub-views, hover ---
    if !in_insert {
        // Clear hover on any keypress
        state.engine.diagnostic_hover = None;

        // Ctrl+] / Ctrl+[ → navigate diagnostics
        if key.modifiers.contains(KeyModifiers::CONTROL)
            && matches!(key.code, KeyCode::Char(']') | KeyCode::Char('['))
            && !state.engine.diagnostics.is_empty()
        {
            let cur = state.engine.diagnostic_list_cursor;
            let len = state.engine.diagnostics.len();
            let idx = if matches!(key.code, KeyCode::Char(']')) {
                if cur + 1 < len { cur + 1 } else { 0 }
            } else if cur == 0 {
                len - 1
            } else {
                cur - 1
            };
            state.engine.diagnostic_list_cursor = idx;
            let target_row = state.engine.diagnostics[idx].row;
            let target_col = state.engine.diagnostics[idx].col_start;
            if let Some(editor) = state.tabs[tab_idx].active_editor_mut() {
                editor.cursor_row = target_row;
                editor.cursor_col = target_col;
                editor.ensure_cursor_visible();
            }
            return Action::Render;
        }

        // ] / [ sub-view switching is handled globally in handle_global_normal_keys
    }

    // Pass key to editor and collect result + state
    let (action, still_insert, needs_diag) = {
        let tab = &mut state.tabs[tab_idx];
        if let Some(editor) = tab.active_editor_mut() {
            let action = match editor.handle_key(key) {
                EditorAction::Handled => Action::Render,
                EditorAction::Unhandled(_) => Action::None,
                EditorAction::Save => {
                    if is_source_tab {
                        Action::ValidateAndSave { tab_id }
                    } else {
                        Action::SaveScript
                    }
                }
                EditorAction::Close => Action::CloseTab,
                EditorAction::ForceClose => Action::CloseTab,
                EditorAction::SaveAndClose => {
                    if is_script {
                        return Action::SaveScript;
                    }
                    Action::CloseTab
                }
                EditorAction::ToggleComment => {
                    toggle_line_comment(editor, db_type);
                    Action::Render
                }
                EditorAction::ToggleBlockComment { start_row, end_row } => {
                    toggle_block_comment(editor, db_type, start_row, end_row);
                    Action::Render
                }
                EditorAction::Hover => {
                    // K → show diagnostic hover for current line
                    let row = editor.cursor_row;
                    if let Some(diag) = state.engine.diagnostics.iter().find(|d| d.row == row) {
                        state.engine.diagnostic_hover =
                            Some((row, format!("[{}] {}", diag.source.label(), diag.message)));
                    }
                    Action::Render
                }
                EditorAction::GoToDefinition => {
                    // Future: jump to table/object definition
                    Action::None
                }
            };
            // Auto-pair: insert closing bracket after opening bracket
            if matches!(editor.mode, vimltui::VimMode::Insert)
                && let KeyCode::Char(ch) = key.code
                && !key.modifiers.contains(KeyModifiers::CONTROL)
            {
                let close = match ch {
                    '(' => Some(')'),
                    '[' => Some(']'),
                    '{' => Some('}'),
                    '\'' => Some('\''),
                    _ => None,
                };
                if let Some(c) = close {
                    let row = editor.cursor_row;
                    let col = editor.cursor_col;
                    if row < editor.lines.len() {
                        let line = &mut editor.lines[row];
                        if col <= line.len() {
                            line.insert(col, c);
                            // Cursor stays between the pair
                        }
                    }
                }
            }
            let still_insert = matches!(editor.mode, vimltui::VimMode::Insert);
            // Only run diagnostics on Insert->Normal transition and if metadata is loaded
            let needs_diag = !still_insert && in_insert && editor.modified && state.metadata_ready;
            (action, still_insert, needs_diag)
        } else {
            return Action::None;
        }
    };
    // tab/editor borrows are dropped here

    // Check if content reverted to saved state (undo back to original)
    if !still_insert && in_insert {
        state.tabs[tab_idx].check_modified();
    }

    // Update diff signs for source editors (packages, functions, procedures)
    {
        let tab = &state.tabs[tab_idx];
        let original = match &tab.active_sub_view {
            Some(SubView::PackageDeclaration) | Some(SubView::TypeDeclaration) => {
                tab.original_decl.clone()
            }
            Some(SubView::PackageBody) | Some(SubView::TypeBody) => tab.original_body.clone(),
            None if matches!(
                tab.kind,
                TabKind::Function { .. } | TabKind::Procedure { .. }
            ) =>
            {
                tab.original_source.clone()
            }
            _ => None,
        };
        if let Some(orig) = original {
            let tab = &mut state.tabs[tab_idx];
            if let Some(editor) = tab.active_editor_mut() {
                let signs = super::compute_diff_signs(&orig, &editor.lines);
                if signs.is_empty() {
                    editor.gutter = None;
                } else {
                    let mut config = editor.gutter.take().unwrap_or_default();
                    config.signs = signs;
                    editor.gutter = Some(config);
                }
            }
        }
    }

    if still_insert {
        // Auto-update completion while typing in Insert mode
        if let Some(cache_action) = update_completion(state) {
            return cache_action;
        }
    } else {
        state.engine.completion = None;
    }

    if needs_diag {
        let tab = &state.tabs[tab_idx];
        // Skip diagnostics for PL/SQL source tabs (sqlparser can't parse them)
        let is_plsql = matches!(
            tab.kind,
            TabKind::Package { .. } | TabKind::Function { .. } | TabKind::Procedure { .. }
        );
        if is_plsql {
            state.engine.diagnostics.clear();
        } else {
            let lines = tab
                .active_editor()
                .map(|e| e.lines.clone())
                .unwrap_or_default();
            // Use the new sql_engine diagnostic provider
            let eff_conn = state
                .tabs
                .get(tab_idx)
                .and_then(|t| t.kind.conn_name().map(|s| s.to_string()))
                .or_else(|| state.conn.name.clone());
            let empty_idx = crate::sql_engine::metadata::MetadataIndex::new();
            let metadata_idx = eff_conn
                .as_ref()
                .and_then(|cn| state.engine.metadata_indexes.get(cn))
                .unwrap_or(&empty_idx);
            let db_type = metadata_idx.db_type();
            let dialect_box = db_type
                .map(crate::sql_engine::dialect::dialect_for)
                .unwrap_or_else(|| Box::new(crate::sql_engine::dialect::OracleDialect));
            let provider = crate::sql_engine::diagnostics::DiagnosticProvider::new(
                dialect_box.as_ref(),
                metadata_idx,
            );
            let engine_diags = provider.check_local(&lines);
            state.engine.diagnostics = engine_diags
                .into_iter()
                .map(crate::ui::diagnostics::Diagnostic::from_engine)
                .collect();

            // Build gutter signs from diagnostics
            apply_diagnostic_gutter_signs(state, tab_idx);
        }
    }

    action
}

/// Set diagnostic signs on the active editor's gutter (left of line numbers).
/// Uses the new `DiagnosticSign` API — separate from diff `GutterSign` (right of numbers).
fn apply_diagnostic_gutter_signs(state: &mut AppState, tab_idx: usize) {
    use crate::ui::diagnostics::Severity;
    use std::collections::HashMap;
    let mut diag_signs: HashMap<usize, vimltui::Diagnostic> = HashMap::new();
    for d in &state.engine.diagnostics {
        let sev = match d.severity {
            Severity::Error => vimltui::DiagnosticSeverity::Error,
            Severity::Warning | Severity::Info | Severity::Hint => {
                vimltui::DiagnosticSeverity::Warning
            }
        };
        diag_signs
            .entry(d.row)
            .and_modify(|existing| {
                if sev == vimltui::DiagnosticSeverity::Error {
                    existing.severity = vimltui::DiagnosticSeverity::Error;
                    existing.message = Some(d.message.clone());
                }
            })
            .or_insert(vimltui::Diagnostic {
                severity: sev,
                message: Some(d.message.clone()),
            });
    }

    if let Some(tab) = state.tabs.get_mut(tab_idx)
        && let Some(editor) = tab.active_editor_mut()
    {
        if diag_signs.is_empty() && editor.gutter.is_none() {
            return;
        }
        let mut config = editor.gutter.take().unwrap_or_default();
        config.diagnostics = diag_signs;
        if config.signs.is_empty() && config.diagnostics.is_empty() {
            editor.gutter = None;
        } else {
            editor.gutter = Some(config);
        }
    }
}

/// Update completion popup (auto-trigger, requires prefix).
pub(super) fn update_completion(state: &mut AppState) -> Option<Action> {
    update_completion_impl(state, false)
}

/// Update completion popup. `force=true` opens even without prefix (Ctrl+Space).
pub(super) fn update_completion_impl(state: &mut AppState, force: bool) -> Option<Action> {
    use crate::sql_engine::analyzer::SemanticAnalyzer;
    use crate::sql_engine::completion::{CompletionItemKind, CompletionProvider};
    use crate::sql_engine::dialect;
    use crate::sql_engine::tokenizer;
    use crate::ui::completion::{CompletionItem, CompletionKind, CompletionState};

    let tab = match state.tabs.get(state.active_tab_idx) {
        Some(t) => t,
        None => {
            state.engine.completion = None;
            return None;
        }
    };
    let editor = match tab.active_editor() {
        Some(e) => e,
        None => {
            state.engine.completion = None;
            return None;
        }
    };

    let row = editor.cursor_row;
    let col = editor.cursor_col;
    let line = editor.current_line();
    // Extract prefix from the current line directly
    let bytes = line.as_bytes();
    let end = col.min(bytes.len());
    let mut pstart = end;
    while pstart > 0 && (bytes[pstart - 1].is_ascii_alphanumeric() || bytes[pstart - 1] == b'_') {
        pstart -= 1;
    }
    let prefix = line[pstart..end].to_string();
    let start_col = pstart;
    let dot_mode = prefix.is_empty() && col > 0 && bytes.get(col - 1) == Some(&b'.');
    // After * in SELECT list: user likely wants to replace * with columns
    let star_mode = prefix.is_empty() && col > 0 && bytes.get(col - 1) == Some(&b'*');

    // Allow empty prefix for dot completions, star replacement, or forced mode (Ctrl+Space)
    if prefix.is_empty() && !dot_mode && !star_mode && !force {
        // Auto-correct case if the old prefix is an exact case-insensitive match
        if let Some(cmp) = state.engine.completion.take() {
            let old_prefix_upper = cmp.prefix.to_uppercase();
            if !old_prefix_upper.is_empty() {
                let exact: Vec<_> = cmp
                    .items
                    .iter()
                    .filter(|item| item.label.to_uppercase() == old_prefix_upper)
                    .collect();
                if exact.len() == 1 && exact[0].label != cmp.prefix {
                    let tab = state.tabs.get_mut(state.active_tab_idx);
                    if let Some(editor) = tab.and_then(|t| t.active_editor_mut()) {
                        let r = cmp.origin_row;
                        if r < editor.lines.len() {
                            let line = &editor.lines[r];
                            let start = cmp.origin_col.min(line.len());
                            let end = (start + cmp.prefix.len()).min(line.len());
                            let mut new_line = String::with_capacity(line.len());
                            new_line.push_str(&line[..start]);
                            new_line.push_str(&exact[0].label);
                            new_line.push_str(&line[end..]);
                            editor.lines[r] = new_line;
                            let diff = exact[0].label.len() as isize - cmp.prefix.len() as isize;
                            if editor.cursor_row == r && editor.cursor_col > start {
                                editor.cursor_col =
                                    (editor.cursor_col as isize + diff).max(0) as usize;
                            }
                        }
                    }
                }
            }
        }
        return None;
    }

    // Clone only the query block lines (not the entire file)
    let editor_row = row;
    let total_lines = editor.lines.len();
    let mut block_start = row;
    while block_start > 0 && !editor.lines[block_start - 1].trim().is_empty() {
        block_start -= 1;
    }
    let mut block_end = row + 1;
    while block_end < total_lines && !editor.lines[block_end].trim().is_empty() {
        block_end += 1;
    }
    let lines: Vec<String> = editor.lines[block_start..block_end].to_vec();
    let block_row = row - block_start;

    // Find the effective connection for this tab
    let eff_conn = state
        .active_tab()
        .and_then(|t| t.kind.conn_name().map(|s| s.to_string()))
        .or_else(|| state.conn.name.clone());

    let empty_idx = crate::sql_engine::metadata::MetadataIndex::new();
    let metadata_idx = eff_conn
        .as_ref()
        .and_then(|cn| state.engine.metadata_indexes.get(cn))
        .unwrap_or(&empty_idx);

    let db_type = metadata_idx.db_type();
    let dialect_box = db_type
        .map(dialect::dialect_for)
        .unwrap_or_else(|| Box::new(dialect::OracleDialect));
    let analyzer = SemanticAnalyzer::new(dialect_box.as_ref(), metadata_idx);
    let ctx = analyzer.analyze(&lines, block_row, col);
    let provider = CompletionProvider::new(dialect_box.as_ref(), metadata_idx);
    let scored_items = provider.complete(&ctx);

    // Determine if this is a table reference context (FROM/JOIN) for alias generation
    let is_table_ref = matches!(
        ctx.cursor_context,
        crate::sql_engine::context::CursorContext::TableRef
    );
    // Collect existing aliases in the query to avoid conflicts
    let existing_aliases: Vec<String> = ctx.aliases.keys().cloned().collect();

    // Convert ScoredItem -> UI CompletionItem
    let items: Vec<CompletionItem> = scored_items
        .into_iter()
        .map(|si| {
            let kind = match si.kind {
                CompletionItemKind::Keyword => CompletionKind::Keyword,
                CompletionItemKind::Schema => CompletionKind::Schema,
                CompletionItemKind::Table => CompletionKind::Table,
                CompletionItemKind::View => CompletionKind::View,
                CompletionItemKind::Column => CompletionKind::Column,
                CompletionItemKind::Package => CompletionKind::Package,
                CompletionItemKind::Function => CompletionKind::Function,
                CompletionItemKind::Procedure => CompletionKind::Procedure,
                CompletionItemKind::Alias => CompletionKind::Alias,
                CompletionItemKind::ForeignKeyJoin => CompletionKind::Table,
            };
            CompletionItem {
                label: si.label,
                kind,
            }
        })
        .collect();

    // If no column completions found and cursor is in a dot context,
    // trigger on-demand column loading
    let has_dot = dot_mode || {
        let before = &lines[block_row][..col.min(lines[block_row].len())];
        tokenizer::identifier_before_dot(before).is_some()
    };

    // Trigger on-demand column loading for SET clause (UPDATE table SET ...)
    let set_table_cache =
        if let crate::sql_engine::context::CursorContext::SetClause { ref target_table } =
            ctx.cursor_context
        {
            let has_cols = ctx
                .columns_for(&target_table.name, &|s| dialect_box.normalize_identifier(s))
                .is_empty();
            if has_cols {
                resolve_table_for_cache(state, &lines, block_row, &target_table.name).and_then(
                    |(schema, table)| {
                        let key = format!("{}.{}", schema.to_uppercase(), table.to_uppercase());
                        if !state.engine.column_cache.contains_key(&key)
                            && !metadata_idx.has_columns_cached(&schema, &table)
                        {
                            Some(Action::CacheColumns { schema, table })
                        } else {
                            None
                        }
                    },
                )
            } else {
                None
            }
        } else {
            None
        };

    let cache_action = if items.is_empty() && has_dot {
        let before = &lines[block_row][..col.min(lines[block_row].len())];
        if let Some((table_ref, _)) = tokenizer::identifier_before_dot(before) {
            if metadata_idx.is_known_schema(table_ref) {
                // Schema is known but has no objects loaded yet -- trigger on-demand load
                Some(Action::CacheSchemaObjects {
                    schema: table_ref.to_string(),
                })
            } else {
                // Not a schema -- try to resolve as table/alias for column loading
                resolve_table_for_cache(state, &lines, block_row, table_ref).and_then(
                    |(schema, table)| {
                        let key = format!("{}.{}", schema.to_uppercase(), table.to_uppercase());
                        if !state.engine.column_cache.contains_key(&key)
                            && !metadata_idx.has_columns_cached(&schema, &table)
                        {
                            Some(Action::CacheColumns { schema, table })
                        } else {
                            None
                        }
                    },
                )
            }
        } else {
            None
        }
    } else {
        None
    };

    let cache_action = cache_action.or(set_table_cache);

    if items.is_empty() {
        state.engine.completion = None;
        return cache_action;
    }

    let prev_cursor = state
        .engine
        .completion
        .as_ref()
        .map(|c| c.cursor.min(items.len().saturating_sub(1)))
        .unwrap_or(0);

    state.engine.completion = Some(CompletionState {
        items,
        cursor: prev_cursor,
        prefix,
        origin_row: editor_row,
        origin_col: start_col,
        table_ref_context: is_table_ref,
        existing_aliases,
    });

    cache_action
}

/// Resolve a table reference to (schema, table) for column cache loading.
pub(super) fn resolve_table_for_cache(
    state: &AppState,
    lines: &[String],
    _row: usize,
    table_ref: &str,
) -> Option<(String, String)> {
    // Try to resolve alias via the old resolver (still works fine)
    let block: Vec<String> = lines.to_vec();
    let resolved = crate::ui::completion::resolve_table_name(&block, table_ref);
    let table_name = resolved.as_deref().unwrap_or(table_ref);

    // Try MetadataIndex first, fall back to tree walk
    let eff_conn = state
        .active_tab()
        .and_then(|t| t.kind.conn_name().map(|s| s.to_string()))
        .or_else(|| state.conn.name.clone());
    let empty_idx = crate::sql_engine::metadata::MetadataIndex::new();
    let metadata_idx = eff_conn
        .as_ref()
        .and_then(|cn| state.engine.metadata_indexes.get(cn))
        .unwrap_or(&empty_idx);
    if let Some(schema) = metadata_idx.resolve_schema_for(table_name) {
        return Some((schema.to_string(), table_name.to_string()));
    }
    let schema = crate::ui::completion::find_schema_for_table(state, table_name)?;
    Some((schema, table_name.to_string()))
}

/// Toggle a line comment (`-- `) on the current line.
/// If the line already starts with `-- `, remove it. Otherwise, add it.
fn toggle_line_comment(
    editor: &mut vimltui::VimEditor,
    _db_type: Option<crate::core::models::DatabaseType>,
) {
    let row = editor.cursor_row;
    if row >= editor.lines.len() {
        return;
    }
    editor.save_undo();
    let line = &editor.lines[row];
    let trimmed = line.trim_start();
    if trimmed.starts_with("-- ") {
        // Remove comment: find the `-- ` and remove it
        if let Some(pos) = line.find("-- ") {
            let mut new_line = String::with_capacity(line.len());
            new_line.push_str(&line[..pos]);
            new_line.push_str(&line[pos + 3..]);
            editor.lines[row] = new_line;
            editor.cursor_col = editor.cursor_col.saturating_sub(3);
        }
    } else if trimmed.starts_with("--") {
        // Remove comment without trailing space
        if let Some(pos) = line.find("--") {
            let mut new_line = String::with_capacity(line.len());
            new_line.push_str(&line[..pos]);
            new_line.push_str(&line[pos + 2..]);
            editor.lines[row] = new_line;
            editor.cursor_col = editor.cursor_col.saturating_sub(2);
        }
    } else {
        // Add comment: preserve leading whitespace
        let indent = line.len() - trimmed.len();
        let mut new_line = String::with_capacity(line.len() + 3);
        new_line.push_str(&line[..indent]);
        new_line.push_str("-- ");
        new_line.push_str(trimmed);
        editor.lines[row] = new_line;
        editor.cursor_col += 3;
    }
    editor.modified = true;
}

/// Toggle block comments on a range of lines.
/// If ALL lines are commented, uncomment them. Otherwise, comment all.
fn toggle_block_comment(
    editor: &mut vimltui::VimEditor,
    _db_type: Option<crate::core::models::DatabaseType>,
    start_row: usize,
    end_row: usize,
) {
    let end = end_row.min(editor.lines.len().saturating_sub(1));
    if start_row > end {
        return;
    }
    editor.save_undo();

    // Check if all lines in the range are already commented
    let all_commented = (start_row..=end).all(|r| {
        let trimmed = editor.lines[r].trim_start();
        trimmed.starts_with("--")
    });

    for r in start_row..=end {
        let line = &editor.lines[r];
        let trimmed = line.trim_start();
        let indent = line.len() - trimmed.len();

        if all_commented {
            // Uncomment
            let uncommented = trimmed
                .strip_prefix("-- ")
                .or_else(|| trimmed.strip_prefix("--"));
            if let Some(rest) = uncommented {
                let mut new_line = String::with_capacity(line.len());
                new_line.push_str(&line[..indent]);
                new_line.push_str(rest);
                editor.lines[r] = new_line;
            }
        } else {
            // Comment
            if !trimmed.starts_with("--") {
                let mut new_line = String::with_capacity(line.len() + 3);
                new_line.push_str(&line[..indent]);
                new_line.push_str("-- ");
                new_line.push_str(trimmed);
                editor.lines[r] = new_line;
            }
        }
    }
    editor.modified = true;
}

/// SQL reserved words that must never be used as aliases.
const RESERVED_ALIAS_WORDS: &[&str] = &[
    "SELECT",
    "FROM",
    "WHERE",
    "AND",
    "OR",
    "NOT",
    "IN",
    "ON",
    "AS",
    "IS",
    "BY",
    "IF",
    "DO",
    "SET",
    "ALL",
    "ANY",
    "FOR",
    "TOP",
    "END",
    "ASC",
    "DESC",
    "JOIN",
    "LEFT",
    "RIGHT",
    "FULL",
    "INTO",
    "NULL",
    "LIKE",
    "CASE",
    "WHEN",
    "THEN",
    "ELSE",
    "WITH",
    "OVER",
    "ORDER",
    "GROUP",
    "HAVING",
    "LIMIT",
    "UNION",
    "UPDATE",
    "DELETE",
    "INSERT",
    "CREATE",
    "ALTER",
    "DROP",
    "TABLE",
    "VIEW",
    "INDEX",
    "BETWEEN",
    "EXISTS",
    "DISTINCT",
    "VALUES",
    "INNER",
    "OUTER",
    "CROSS",
    "NATURAL",
    "USING",
    "OFFSET",
    "EXCEPT",
    "INTERSECT",
    "PRIMARY",
    "FOREIGN",
    "REFERENCES",
    "CONSTRAINT",
    "DEFAULT",
    "CHECK",
    "UNIQUE",
    "CASCADE",
    "TRUNCATE",
    "GRANT",
    "REVOKE",
    "BEGIN",
    "COMMIT",
    "ROLLBACK",
    "DECLARE",
    "FUNCTION",
    "PROCEDURE",
    "TRIGGER",
    "RETURN",
    "TO",
    "NO",
    "GO",
];

fn generate_table_alias(table_name: &str, existing: &[String]) -> String {
    let lower = table_name.to_lowercase();
    let parts: Vec<&str> = lower.split('_').filter(|p| !p.is_empty()).collect();

    let conflicts = |candidate: &str| -> bool {
        existing.iter().any(|a| a.eq_ignore_ascii_case(candidate))
            || RESERVED_ALIAS_WORDS
                .iter()
                .any(|r| r.eq_ignore_ascii_case(candidate))
    };

    // Build candidate list ordered by preference
    let mut candidates: Vec<String> = Vec::new();

    if parts.len() > 1 {
        // Multi-word: initials (2-3 chars)
        let initials: String = parts.iter().filter_map(|p| p.chars().next()).collect();
        // 2-char version
        if initials.len() >= 2 {
            candidates.push(initials.chars().take(2).collect());
        }
        // 3-char version
        if initials.len() >= 3 {
            candidates.push(initials.chars().take(3).collect());
        }
        // Fallback: first part 2 chars
        if parts[0].len() >= 2 {
            candidates.push(parts[0].chars().take(2).collect());
        }
        if parts[0].len() >= 3 {
            candidates.push(parts[0].chars().take(3).collect());
        }
    } else {
        // Single word: build progressively longer abbreviations
        let chars: Vec<char> = lower.chars().collect();
        // 2-char: first + next consonant
        if let Some(c) = chars[1..].iter().find(|c| !"aeiou".contains(**c)) {
            candidates.push(format!("{}{c}", chars[0]));
        }
        // 2-char: first 2 chars
        if chars.len() >= 2 {
            candidates.push(chars[..2].iter().collect());
        }
        // 3-char: first + 2 consonants
        let consonants: Vec<char> = chars[1..]
            .iter()
            .filter(|c| !"aeiou".contains(**c) && c.is_ascii_alphabetic())
            .copied()
            .collect();
        if consonants.len() >= 2 {
            candidates.push(format!("{}{}{}", chars[0], consonants[0], consonants[1]));
        }
        // 3-char: first 3 chars
        if chars.len() >= 3 {
            candidates.push(chars[..3].iter().collect());
        }
    }

    // Pick the first non-conflicting candidate
    for c in &candidates {
        if !conflicts(c) {
            return c.clone();
        }
    }

    // Last resort: first candidate + digit suffix
    let base = candidates
        .first()
        .cloned()
        .unwrap_or_else(|| "tb".to_string());
    for n in 2..100 {
        let candidate = format!("{base}{n}");
        if !conflicts(&candidate) {
            return candidate;
        }
    }

    base
}

/// Accept the selected completion item: replace prefix with completion text.
pub(super) fn accept_completion(
    state: &mut AppState,
    cmp: &crate::ui::completion::CompletionState,
) {
    use crate::ui::completion::CompletionKind;

    let item = match cmp.selected() {
        Some(s) => s.clone(),
        None => return,
    };

    // Append "." for alias/schema, "()" for functions/procedures and keywords that need parens
    let needs_parens = match item.kind {
        CompletionKind::Function | CompletionKind::Procedure => true,
        CompletionKind::Keyword => {
            matches!(
                item.label.as_str(),
                "IN" | "EXISTS" | "NOT IN" | "NOT EXISTS"
            )
        }
        _ => false,
    };
    // cursor_inside_parens: place cursor between () instead of after
    let (insert_text, cursor_inside_parens) = match item.kind {
        CompletionKind::Alias | CompletionKind::Schema => (format!("{}.", item.label), false),
        _ if needs_parens => (format!("{}()", item.label), true),
        // Tables/Views in FROM/JOIN context: append auto-generated alias
        CompletionKind::Table | CompletionKind::View if cmp.table_ref_context => {
            let alias = generate_table_alias(&item.label, &cmp.existing_aliases);
            (format!("{} {alias}", item.label), false)
        }
        _ => (item.label, false),
    };

    let tab = match state.tabs.get_mut(state.active_tab_idx) {
        Some(t) => t,
        None => return,
    };
    let editor = match tab.active_editor_mut() {
        Some(e) => e,
        None => return,
    };

    let row = cmp.origin_row;
    if row >= editor.lines.len() {
        return;
    }

    let line = &editor.lines[row];
    let start = cmp.origin_col.min(line.len());
    let end = editor.cursor_col.min(line.len());

    let mut new_line = String::with_capacity(line.len() + insert_text.len());
    new_line.push_str(&line[..start]);
    new_line.push_str(&insert_text);
    if end < line.len() {
        new_line.push_str(&line[end..]);
    }

    editor.lines[row] = new_line;
    editor.cursor_col = if cursor_inside_parens {
        // Place cursor between the parens: "COUNT(|)"
        start + insert_text.len() - 1
    } else {
        start + insert_text.len()
    };
    editor.modified = true;
}

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

    #[test]
    fn alias_single_word() {
        // "or" is reserved (SQL OR) → skips to next candidate
        let ord = generate_table_alias("orders", &[]);
        assert!(
            !RESERVED_ALIAS_WORDS
                .iter()
                .any(|r| r.eq_ignore_ascii_case(&ord)),
            "alias '{ord}' is a reserved word"
        );
        assert!(ord.len() <= 3, "orders → {ord}");

        let us = generate_table_alias("users", &[]);
        assert!(
            !RESERVED_ALIAS_WORDS
                .iter()
                .any(|r| r.eq_ignore_ascii_case(&us)),
            "alias '{us}' is a reserved word"
        );

        let cs = generate_table_alias("customers", &[]);
        assert_eq!(cs, "cs"); // c + s (not reserved)
    }

    #[test]
    fn alias_avoids_reserved_words() {
        // "or" (orders), "as" (assets), "in" (invoices), "on" (online_orders)
        // — all are SQL reserved words and must be skipped
        let alias = generate_table_alias("orders", &[]);
        assert_ne!(alias.to_uppercase(), "OR", "got {alias}");

        let alias = generate_table_alias("assets", &[]);
        assert_ne!(alias.to_uppercase(), "AS", "got {alias}");

        let alias = generate_table_alias("invoices", &[]);
        assert_ne!(alias.to_uppercase(), "IN", "got {alias}");

        let alias = generate_table_alias("online_orders", &[]);
        assert_ne!(alias.to_uppercase(), "ON", "got {alias}");
        // "oo" is not reserved → should be fine
    }

    #[test]
    fn alias_multi_word() {
        assert_eq!(generate_table_alias("customer_orders", &[]), "co");
        assert_eq!(generate_table_alias("order_line_items", &[]), "ol");
        assert_eq!(generate_table_alias("user_role_mapping", &[]), "ur");
    }

    #[test]
    fn alias_conflict_expands_to_3() {
        let alias = generate_table_alias("customers", &["cs".to_string()]);
        assert!(alias.len() <= 3, "got {alias}");
        assert_ne!(alias, "cs");
    }

    #[test]
    fn alias_no_duplicate_across_tables() {
        let existing = vec!["or".to_string()]; // from "orders"
        let alias = generate_table_alias("organisms", &existing);
        assert_ne!(alias, "or", "should not conflict");
    }

    #[test]
    fn alias_conflict_case_insensitive() {
        let existing = vec!["CO".to_string()];
        let alias = generate_table_alias("customer_orders", &existing);
        assert_ne!(alias.to_lowercase(), "co");
    }
}