ratado 0.2.0

A fast, keyboard-driven terminal task manager built with Rust and Ratatui
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
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
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
//! Quick Capture dialog for rapid task entry.
//!
//! Provides a spotlight-style overlay that lets users create tasks from a single
//! line using inline syntax: `<title> [@project] [#tag ...] [!priority] [due:date]`.
//!
//! ## Syntax
//!
//! | Token | Meaning | Examples |
//! |-------|---------|---------|
//! | `@Name` | Project (fuzzy matched) | `@Work`, `@back` → "Backend" |
//! | `#tag` | Tag | `#urgent`, `#bug` |
//! | `!1`–`!4` | Priority (1=urgent, 4=low) | `!1` |
//! | `due:val` | Due date | `due:tomorrow`, `due:fri` |
//! | `\@` `\#` | Escaped literals | `\@email` stays in title |
//! | Everything else | Title | Joined remaining words |

use std::collections::HashMap;

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::{
    layout::{Constraint, Layout, Rect},
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Clear, Paragraph},
    Frame,
};

use super::add_task::parse_due_date;
use super::AddTaskDialog;
use crate::models::{Priority, Project, Task};
use crate::storage::Tag;
use crate::ui::input::TextInput;
use crate::ui::theme;

// ─────────────────────────────────────────────────────────────────────────────
// Suggestion types
// ─────────────────────────────────────────────────────────────────────────────

/// What kind of autocomplete suggestions are currently showing.
#[derive(Debug, Clone, PartialEq, Eq)]
enum SuggestionMode {
    /// No suggestions visible
    None,
    /// Showing project suggestions (triggered by `@`)
    Projects,
    /// Showing tag suggestions (triggered by `#`)
    Tags,
    /// Showing priority suggestions (triggered by `!`)
    Priorities,
}

// ─────────────────────────────────────────────────────────────────────────────
// Parser
// ─────────────────────────────────────────────────────────────────────────────

/// Parsed result from quick capture input.
///
/// Contains the extracted title, project, tags, priority, and due date
/// from a single-line capture string.
#[derive(Debug, Clone, Default)]
pub struct ParsedCapture {
    /// The task title (remaining words after token extraction)
    pub title: String,
    /// Project name from `@Name` token
    pub project_name: Option<String>,
    /// Tags from `#tag` tokens
    pub tags: Vec<String>,
    /// Priority from `!1`–`!4` token
    pub priority: Option<Priority>,
    /// Raw due date text from `due:value` token
    pub due_date_text: Option<String>,
    /// Parsed due date
    pub due_date: Option<chrono::DateTime<chrono::Utc>>,
}

/// Parses a quick capture input string into structured components.
///
/// Walks each whitespace-delimited word and classifies it:
/// - `@word` → project name
/// - `#word` → tag
/// - `!1`–`!4` → priority (1=urgent, 4=low)
/// - `due:rest` → due date text, parsed via `parse_due_date()`
/// - `\@word` / `\#word` → unescaped into title
/// - Everything else → title word
///
/// Tokens are only recognized after whitespace or at position 0.
///
/// # Arguments
///
/// * `input` - The raw capture input string
///
/// # Returns
///
/// A [`ParsedCapture`] with extracted fields
pub fn parse_capture_input(input: &str) -> ParsedCapture {
    let mut result = ParsedCapture::default();
    let mut title_words: Vec<String> = Vec::new();

    for word in input.split_whitespace() {
        if let Some(name) = word.strip_prefix('@') {
            if !name.is_empty() {
                result.project_name = Some(name.to_string());
            } else {
                title_words.push(word.to_string());
            }
        } else if let Some(tag) = word.strip_prefix('#') {
            if !tag.is_empty() {
                result.tags.push(tag.to_string());
            } else {
                title_words.push(word.to_string());
            }
        } else if let Some(level) = word.strip_prefix('!') {
            match level {
                "1" => result.priority = Some(Priority::Urgent),
                "2" => result.priority = Some(Priority::High),
                "3" => result.priority = Some(Priority::Medium),
                "4" => result.priority = Some(Priority::Low),
                _ => title_words.push(word.to_string()),
            }
        } else if let Some(date_str) = word.strip_prefix("due:") {
            if !date_str.is_empty() {
                result.due_date_text = Some(date_str.to_string());
                result.due_date = parse_due_date(date_str);
            }
        } else if let Some(rest) = word.strip_prefix("\\@") {
            title_words.push(format!("@{}", rest));
        } else if let Some(rest) = word.strip_prefix("\\#") {
            title_words.push(format!("#{}", rest));
        } else {
            title_words.push(word.to_string());
        }
    }

    result.title = title_words.join(" ");
    result
}

// ─────────────────────────────────────────────────────────────────────────────
// Dialog
// ─────────────────────────────────────────────────────────────────────────────

/// Actions that can result from quick capture interaction.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QuickCaptureAction {
    /// No action needed, dialog continues
    None,
    /// User submitted the task (Enter)
    Submit,
    /// User cancelled (Esc)
    Cancel,
    /// User wants to expand to full AddTaskDialog (Tab)
    ExpandToFull,
}

/// Quick Capture dialog for rapid task entry.
///
/// Provides a spotlight-style overlay where users type a single line
/// with inline syntax tokens that are parsed in real-time. Supports
/// autocomplete dropdowns for projects (`@`) and tags (`#`).
#[derive(Debug, Clone)]
pub struct QuickCaptureDialog {
    /// Text input field
    pub input: TextInput,
    /// Parsed result from current input
    pub parsed: ParsedCapture,
    /// Available projects for fuzzy matching
    pub projects: Vec<Project>,
    /// Available tags for reference
    pub all_tags: Vec<Tag>,
    /// Project matched from `@name` token
    pub matched_project: Option<Project>,
    /// Suggestion dropdown items
    suggestions: Vec<String>,
    /// Currently highlighted suggestion index
    selected_suggestion: Option<usize>,
    /// What kind of suggestions are showing
    suggestion_mode: SuggestionMode,
    /// Explicitly selected project (from dropdown, survives reparse)
    explicit_project: Option<Project>,
    /// Map of project_id → tag names used in that project's tasks
    project_tag_map: HashMap<String, Vec<String>>,
}

impl QuickCaptureDialog {
    /// Creates a new Quick Capture dialog.
    ///
    /// # Arguments
    ///
    /// * `projects` - Available projects for `@name` fuzzy matching
    /// * `tags` - Available tags for reference
    /// * `tasks` - Current tasks, used to build project-tag associations
    pub fn new(projects: Vec<Project>, tags: Vec<Tag>, tasks: &[Task]) -> Self {
        // Precompute which tags are used in each project's tasks
        let mut project_tag_map: HashMap<String, Vec<String>> = HashMap::new();
        for task in tasks {
            if let Some(ref pid) = task.project_id {
                let entry = project_tag_map.entry(pid.clone()).or_default();
                for tag in &task.tags {
                    if !entry.contains(tag) {
                        entry.push(tag.clone());
                    }
                }
            }
        }

        Self {
            input: TextInput::new().with_placeholder("Task title @project #tag !priority due:date"),
            parsed: ParsedCapture::default(),
            projects,
            all_tags: tags,
            matched_project: None,
            suggestions: Vec::new(),
            selected_suggestion: None,
            suggestion_mode: SuggestionMode::None,
            explicit_project: None,
            project_tag_map,
        }
    }

    /// Sets the pre-selected project (e.g. from the currently active sidebar selection).
    pub fn set_project(&mut self, project: Project) {
        self.explicit_project = Some(project);
    }

    /// Returns the explicitly selected project, if any.
    pub fn project(&self) -> Option<&Project> {
        self.explicit_project.as_ref()
    }

    /// Handles a key event and returns the resulting action.
    ///
    /// When suggestions are visible, Tab accepts the selected suggestion,
    /// and Up/Down navigate the dropdown. Otherwise Tab expands to full dialog.
    pub fn handle_key(&mut self, key: KeyEvent) -> QuickCaptureAction {
        let has_suggestions = !self.suggestions.is_empty();

        match key.code {
            KeyCode::Enter => {
                self.reparse();
                QuickCaptureAction::Submit
            }
            KeyCode::Esc => QuickCaptureAction::Cancel,
            KeyCode::Tab => {
                if has_suggestions {
                    self.accept_suggestion();
                    QuickCaptureAction::None
                } else {
                    self.reparse();
                    QuickCaptureAction::ExpandToFull
                }
            }
            KeyCode::Up if has_suggestions => {
                if let Some(idx) = self.selected_suggestion {
                    self.selected_suggestion = if idx == 0 {
                        Some(self.suggestions.len() - 1)
                    } else {
                        Some(idx - 1)
                    };
                }
                QuickCaptureAction::None
            }
            KeyCode::Down if has_suggestions => {
                if let Some(idx) = self.selected_suggestion {
                    self.selected_suggestion = Some((idx + 1) % self.suggestions.len());
                }
                QuickCaptureAction::None
            }
            // Word navigation
            KeyCode::Char('b') if key.modifiers.contains(KeyModifiers::ALT) => {
                self.input.move_word_left();
                self.reparse();
                self.update_suggestions();
                QuickCaptureAction::None
            }
            KeyCode::Char('f') if key.modifiers.contains(KeyModifiers::ALT) => {
                self.input.move_word_right();
                self.reparse();
                self.update_suggestions();
                QuickCaptureAction::None
            }
            KeyCode::Backspace if key.modifiers.contains(KeyModifiers::ALT) => {
                self.input.delete_word_backward();
                self.reparse();
                self.update_suggestions();
                QuickCaptureAction::None
            }
            KeyCode::Char(c) => {
                self.input.insert(c);
                self.reparse();
                self.update_suggestions();
                QuickCaptureAction::None
            }
            KeyCode::Backspace => {
                self.input.delete_backward();
                self.reparse();
                self.update_suggestions();
                QuickCaptureAction::None
            }
            KeyCode::Delete => {
                self.input.delete_forward();
                self.reparse();
                self.update_suggestions();
                QuickCaptureAction::None
            }
            KeyCode::Left if key.modifiers.contains(KeyModifiers::ALT) => {
                self.input.move_word_left();
                self.update_suggestions();
                QuickCaptureAction::None
            }
            KeyCode::Right if key.modifiers.contains(KeyModifiers::ALT) => {
                self.input.move_word_right();
                self.update_suggestions();
                QuickCaptureAction::None
            }
            KeyCode::Left => {
                self.input.move_left();
                self.update_suggestions();
                QuickCaptureAction::None
            }
            KeyCode::Right => {
                self.input.move_right();
                self.update_suggestions();
                QuickCaptureAction::None
            }
            KeyCode::Home => {
                self.input.move_home();
                self.update_suggestions();
                QuickCaptureAction::None
            }
            KeyCode::End => {
                self.input.move_end();
                self.update_suggestions();
                QuickCaptureAction::None
            }
            _ => QuickCaptureAction::None,
        }
    }

    /// Re-parses the current input and updates parsed state and matched project.
    ///
    /// If `explicit_project` is set and no new `@token` appears in input,
    /// keeps the explicit project. If a new `@token` appears, clears it
    /// (user wants to change project).
    fn reparse(&mut self) {
        self.parsed = parse_capture_input(self.input.value());
        self.matched_project = self
            .parsed
            .project_name
            .as_ref()
            .and_then(|name| self.fuzzy_match_project(name));

        // If user typed a new @token, clear the explicit project
        if self.parsed.project_name.is_some() && self.explicit_project.is_some() {
            self.explicit_project = None;
        }
    }

    /// Finds the active `@partial` or `#partial` token at the cursor position.
    ///
    /// Scans backwards from the cursor to find if the cursor is currently
    /// inside a `@` or `#` token. Returns the mode and the partial text
    /// (without the prefix character), along with the byte offsets of the
    /// full token (including prefix) for replacement.
    ///
    /// # Returns
    ///
    /// `Some((mode, partial, start, end))` if cursor is in a token, `None` otherwise.
    fn active_token_at_cursor(&self) -> Option<(SuggestionMode, String, usize, usize)> {
        let value = self.input.value();
        let cursor = self.input.cursor();

        if cursor == 0 || value.is_empty() {
            return None;
        }

        let bytes = value.as_bytes();

        // Scan backwards from cursor to find the start of the current "word"
        let mut start = cursor;
        while start > 0 && bytes[start - 1] != b' ' {
            start -= 1;
        }

        // The token text from start to cursor
        let token = &value[start..cursor];

        if let Some(partial) = token.strip_prefix('@') {
            Some((SuggestionMode::Projects, partial.to_string(), start, cursor))
        } else if let Some(partial) = token.strip_prefix('#') {
            Some((SuggestionMode::Tags, partial.to_string(), start, cursor))
        } else {
            token
                .strip_prefix('!')
                .map(|partial| (SuggestionMode::Priorities, partial.to_string(), start, cursor))
        }
    }

    /// Updates the suggestion list based on the current token at the cursor.
    ///
    /// Called after every keystroke. Populates `suggestions` and sets
    /// `suggestion_mode` based on the active `@` or `#` token.
    fn update_suggestions(&mut self) {
        match self.active_token_at_cursor() {
            Some((SuggestionMode::Projects, partial, _, _)) => {
                let lower = partial.to_lowercase();
                let mut matches: Vec<String> = Vec::new();

                if lower.is_empty() {
                    // Show all projects
                    matches = self.projects.iter().map(|p| p.name.clone()).collect();
                } else {
                    // Exact matches first
                    for p in &self.projects {
                        if p.name.to_lowercase() == lower {
                            matches.push(p.name.clone());
                        }
                    }
                    // Prefix matches
                    for p in &self.projects {
                        if p.name.to_lowercase().starts_with(&lower)
                            && !matches.contains(&p.name)
                        {
                            matches.push(p.name.clone());
                        }
                    }
                    // Substring matches
                    for p in &self.projects {
                        if p.name.to_lowercase().contains(&lower)
                            && !matches.contains(&p.name)
                        {
                            matches.push(p.name.clone());
                        }
                    }
                }

                self.suggestions = matches;
                self.suggestion_mode = SuggestionMode::Projects;
                self.selected_suggestion = if self.suggestions.is_empty() {
                    None
                } else {
                    Some(0)
                };
            }
            Some((SuggestionMode::Tags, partial, _, _)) => {
                let lower = partial.to_lowercase();
                let already_parsed: Vec<String> = self
                    .parsed
                    .tags
                    .iter()
                    .map(|t| t.to_lowercase())
                    .collect();

                // Build candidate list: project-scoped tags first, then others
                let mut scoped_tags: Vec<String> = Vec::new();
                let mut other_tags: Vec<String> = Vec::new();

                // Get project-scoped tags if a project is selected
                let project_id = self
                    .explicit_project
                    .as_ref()
                    .or(self.matched_project.as_ref())
                    .map(|p| p.id.clone());

                let scoped_tag_set: Vec<String> = project_id
                    .as_ref()
                    .and_then(|pid| self.project_tag_map.get(pid))
                    .cloned()
                    .unwrap_or_default();

                for tag in &self.all_tags {
                    // Skip tags already in the input
                    if already_parsed.contains(&tag.name.to_lowercase()) {
                        continue;
                    }

                    if scoped_tag_set.iter().any(|s| s.to_lowercase() == tag.name.to_lowercase()) {
                        scoped_tags.push(tag.name.clone());
                    } else {
                        other_tags.push(tag.name.clone());
                    }
                }

                // Filter by partial
                let filter = |tags: &[String]| -> Vec<String> {
                    if lower.is_empty() {
                        tags.to_vec()
                    } else {
                        tags.iter()
                            .filter(|t| t.to_lowercase().contains(&lower))
                            .cloned()
                            .collect()
                    }
                };

                let mut matches = filter(&scoped_tags);
                matches.extend(filter(&other_tags));

                self.suggestions = matches;
                self.suggestion_mode = SuggestionMode::Tags;
                self.selected_suggestion = if self.suggestions.is_empty() {
                    None
                } else {
                    Some(0)
                };
            }
            Some((SuggestionMode::Priorities, partial, _, _)) => {
                // Priority options: !1 Urgent, !2 High, !3 Medium, !4 Low
                let all_priorities = vec![
                    "1".to_string(),
                    "2".to_string(),
                    "3".to_string(),
                    "4".to_string(),
                ];

                let matches: Vec<String> = if partial.is_empty() {
                    all_priorities
                } else {
                    all_priorities
                        .into_iter()
                        .filter(|p| p.starts_with(&partial))
                        .collect()
                };

                self.suggestions = matches;
                self.suggestion_mode = SuggestionMode::Priorities;
                self.selected_suggestion = if self.suggestions.is_empty() {
                    None
                } else {
                    Some(0)
                };
            }
            _ => {
                self.suggestions.clear();
                self.selected_suggestion = None;
                self.suggestion_mode = SuggestionMode::None;
            }
        }
    }

    /// Fuzzy matches a project name against available projects.
    ///
    /// Matching priority: exact (case-insensitive) → prefix → substring.
    fn fuzzy_match_project(&self, name: &str) -> Option<Project> {
        let lower = name.to_lowercase();

        // Exact match (case-insensitive)
        if let Some(p) = self
            .projects
            .iter()
            .find(|p| p.name.to_lowercase() == lower)
        {
            return Some(p.clone());
        }

        // Prefix match
        if let Some(p) = self
            .projects
            .iter()
            .find(|p| p.name.to_lowercase().starts_with(&lower))
        {
            return Some(p.clone());
        }

        // Substring match
        if let Some(p) = self
            .projects
            .iter()
            .find(|p| p.name.to_lowercase().contains(&lower))
        {
            return Some(p.clone());
        }

        None
    }

    /// Accepts the currently selected suggestion.
    ///
    /// For projects: removes the `@token` from input and sets `explicit_project`.
    /// For tags: replaces `#partial` with `#exactname` in input.
    fn accept_suggestion(&mut self) {
        let selected_idx = match self.selected_suggestion {
            Some(idx) if idx < self.suggestions.len() => idx,
            _ => return,
        };

        let selected = self.suggestions[selected_idx].clone();

        match self.suggestion_mode {
            SuggestionMode::Projects => {
                // Find the project
                if let Some(project) = self.projects.iter().find(|p| p.name == selected) {
                    self.explicit_project = Some(project.clone());
                }

                // Remove the @token from input (including trailing space if any)
                if let Some((_, _, start, end)) = self.active_token_at_cursor() {
                    self.replace_token_in_input(start, end, "");
                }

                self.suggestions.clear();
                self.selected_suggestion = None;
                self.suggestion_mode = SuggestionMode::None;
                self.reparse();
            }
            SuggestionMode::Tags => {
                // Replace #partial with #exactname
                if let Some((_, _, start, end)) = self.active_token_at_cursor() {
                    let replacement = format!("#{}", selected);
                    self.replace_token_in_input(start, end, &replacement);
                }

                self.suggestions.clear();
                self.selected_suggestion = None;
                self.suggestion_mode = SuggestionMode::None;
                self.reparse();
            }
            SuggestionMode::Priorities => {
                // Replace !partial with !N
                if let Some((_, _, start, end)) = self.active_token_at_cursor() {
                    let replacement = format!("!{}", selected);
                    self.replace_token_in_input(start, end, &replacement);
                }

                self.suggestions.clear();
                self.selected_suggestion = None;
                self.suggestion_mode = SuggestionMode::None;
                self.reparse();
            }
            SuggestionMode::None => {}
        }
    }

    /// Replaces a range in the input text and adjusts the cursor.
    fn replace_token_in_input(&mut self, start: usize, end: usize, replacement: &str) {
        let value = self.input.value().to_string();
        let mut new_value = String::new();
        new_value.push_str(&value[..start]);
        new_value.push_str(replacement);
        new_value.push_str(&value[end..]);

        // Trim leading/trailing spaces and collapse double spaces
        let new_value = new_value
            .split_whitespace()
            .collect::<Vec<_>>()
            .join(" ");

        let new_cursor = if replacement.is_empty() {
            // For project removal, put cursor at start position (adjusted for collapsed spaces)
            start.min(new_value.len())
        } else {
            // For tag replacement, put cursor after the replacement
            (start + replacement.len()).min(new_value.len())
        };

        self.input.set_value(&new_value);
        // set_value puts cursor at end; adjust to desired position
        self.input.move_home();
        for _ in 0..new_cursor {
            self.input.move_right();
        }
    }

    /// Creates a Task from the parsed fields.
    ///
    /// Prefers `explicit_project` (set via dropdown) over `matched_project` (fuzzy).
    /// Returns `None` if the title is empty.
    pub fn to_task(&self) -> Option<Task> {
        let title = self.parsed.title.trim();
        if title.is_empty() {
            return None;
        }

        let mut task = Task::new(title);
        task.priority = self.parsed.priority.unwrap_or(Priority::Medium);
        task.due_date = self.parsed.due_date;
        task.tags = self.parsed.tags.clone();

        let project = self.explicit_project.as_ref().or(self.matched_project.as_ref());
        if let Some(project) = project {
            task.project_id = Some(project.id.clone());
        }

        Some(task)
    }

    /// Converts the quick capture state into a pre-populated AddTaskDialog.
    ///
    /// Used when the user presses Tab to expand to the full dialog.
    /// Prefers `explicit_project` over `matched_project`.
    pub fn to_add_task_dialog(&self) -> AddTaskDialog {
        let mut dialog = AddTaskDialog::new().with_available_tags(self.all_tags.clone());

        // Set title
        if !self.parsed.title.is_empty() {
            dialog.title.set_value(&self.parsed.title);
        }

        // Set priority
        dialog.priority = self.parsed.priority.unwrap_or(Priority::Medium);

        // Set due date text
        if let Some(ref date_text) = self.parsed.due_date_text {
            dialog.due_date.set_value(date_text);
        }

        // Set project (prefer explicit selection)
        let project = self.explicit_project.as_ref().or(self.matched_project.as_ref());
        if let Some(project) = project {
            dialog.project_id = Some(project.id.clone());
        }

        // Set tags
        dialog.tags = crate::ui::tag_input::TagInput::with_tags(self.parsed.tags.clone());

        dialog
    }

    /// Renders the Quick Capture spotlight overlay.
    pub fn render(&self, frame: &mut Frame) {
        let area = frame.area();

        // Dim background
        frame.render_widget(Clear, area);
        frame.render_widget(
            Paragraph::new("").style(Style::default().bg(theme::BG_DARK)),
            area,
        );

        // Calculate overlay dimensions
        let dialog_width = 60.min((area.width * 60 / 100).max(45)).max(45);
        let dialog_height: u16 = 6;

        // Position in upper 1/5th of screen
        let x = (area.width.saturating_sub(dialog_width)) / 2;
        let y = area.height / 5;
        let dialog_area = Rect::new(
            area.x + x,
            area.y + y,
            dialog_width.min(area.width.saturating_sub(x)),
            dialog_height.min(area.height.saturating_sub(y)),
        );

        // Dialog block
        let title_span = Span::styled(
            format!(" {} Quick Capture ", theme::icons::SPARKLE),
            Style::default()
                .fg(theme::ACCENT)
                .add_modifier(Modifier::BOLD),
        );
        let block = Block::default()
            .title(title_span)
            .borders(Borders::ALL)
            .border_set(ratatui::symbols::border::ROUNDED)
            .border_style(Style::default().fg(theme::PRIMARY_LIGHT))
            .style(Style::default().bg(theme::BG_ELEVATED));

        let inner = block.inner(dialog_area);
        frame.render_widget(block, dialog_area);

        if inner.height < 4 || inner.width < 10 {
            return;
        }

        // Inner layout: input line, separator, title preview, metadata badges
        let chunks = Layout::vertical([
            Constraint::Length(1), // Input line
            Constraint::Length(1), // Separator
            Constraint::Length(1), // Title preview
            Constraint::Length(1), // Metadata badges
        ])
        .split(inner);

        // 1. Input line with syntax highlighting
        self.render_input_line(frame, chunks[0]);

        // 2. Separator
        let sep = "".repeat(chunks[1].width as usize);
        frame.render_widget(
            Paragraph::new(sep).style(Style::default().fg(theme::BORDER_MUTED)),
            chunks[1],
        );

        // 3. Title preview
        self.render_title_preview(frame, chunks[2]);

        // 4. Metadata badges
        self.render_badges(frame, chunks[3]);

        // 5. Suggestion dropdown (rendered last to overlay on top)
        if !self.suggestions.is_empty() {
            self.render_suggestions(frame, dialog_area);
        }
    }

    /// Renders the suggestion dropdown below the input line.
    ///
    /// The dropdown appears directly below the dialog, overlaying content beneath it.
    /// Shows up to 5 items with the selected item highlighted.
    fn render_suggestions(&self, frame: &mut Frame, dialog_area: Rect) {
        let max_items: usize = 5;
        let visible_count = self.suggestions.len().min(max_items);
        if visible_count == 0 {
            return;
        }

        // Position dropdown directly below the dialog
        let dropdown_height = visible_count as u16 + 2; // +2 for borders
        let dropdown_y = dialog_area.y + dialog_area.height;
        let screen = frame.area();

        // Don't render past screen bottom
        if dropdown_y + dropdown_height > screen.y + screen.height {
            return;
        }

        let dropdown_area = Rect::new(
            dialog_area.x,
            dropdown_y,
            dialog_area.width,
            dropdown_height,
        );

        // Clear the area first
        frame.render_widget(Clear, dropdown_area);

        // Dropdown border
        let block = Block::default()
            .borders(Borders::ALL)
            .border_set(ratatui::symbols::border::ROUNDED)
            .border_style(Style::default().fg(theme::BORDER_MUTED))
            .style(Style::default().bg(theme::BG_ELEVATED));

        let inner = block.inner(dropdown_area);
        frame.render_widget(block, dropdown_area);

        // Determine scroll offset to keep selected item visible
        let selected = self.selected_suggestion.unwrap_or(0);
        let scroll_offset = if selected >= max_items {
            selected - max_items + 1
        } else {
            0
        };

        // Render items
        for (i, suggestion) in self
            .suggestions
            .iter()
            .skip(scroll_offset)
            .take(max_items)
            .enumerate()
        {
            let actual_idx = i + scroll_offset;
            let is_selected = self.selected_suggestion == Some(actual_idx);
            let y = inner.y + i as u16;

            if y >= inner.y + inner.height {
                break;
            }

            let item_area = Rect::new(inner.x, y, inner.width, 1);

            let (prefix, label, color) = match self.suggestion_mode {
                SuggestionMode::Projects => (
                    format!("{} ", theme::icons::DIAMOND),
                    suggestion.clone(),
                    theme::PROJECT,
                ),
                SuggestionMode::Tags => (
                    "#".to_string(),
                    suggestion.clone(),
                    theme::TAG,
                ),
                SuggestionMode::Priorities => {
                    match suggestion.as_str() {
                        "1" => (
                            format!("{} ", theme::icons::PRIORITY_URGENT),
                            "Urgent".to_string(),
                            theme::PRIORITY_URGENT,
                        ),
                        "2" => (
                            format!("{} ", theme::icons::PRIORITY_HIGH),
                            "High".to_string(),
                            theme::PRIORITY_HIGH,
                        ),
                        "3" => (
                            "".to_string(),
                            "Medium".to_string(),
                            theme::INFO,
                        ),
                        "4" => (
                            format!("{} ", theme::icons::PRIORITY_LOW),
                            "Low".to_string(),
                            theme::PRIORITY_LOW,
                        ),
                        _ => (
                            "".to_string(),
                            suggestion.clone(),
                            theme::TEXT_SECONDARY,
                        ),
                    }
                }
                SuggestionMode::None => (
                    "".to_string(),
                    suggestion.clone(),
                    theme::TEXT_SECONDARY,
                ),
            };

            let style = if is_selected {
                Style::default()
                    .bg(theme::PRIMARY)
                    .fg(theme::TEXT_PRIMARY)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(color)
            };

            let text = format!("{}{}", prefix, label);
            // Truncate to fit
            let max_len = inner.width as usize;
            let display: String = text.chars().take(max_len).collect();

            frame.render_widget(Paragraph::new(display).style(style), item_area);
        }
    }

    /// Renders the input line with syntax highlighting and cursor.
    fn render_input_line(&self, frame: &mut Frame, area: Rect) {
        let prompt_span = Span::styled("> ", Style::default().fg(theme::ACCENT));

        let input_value = self.input.value();
        if input_value.is_empty() {
            // Show placeholder
            let line = Line::from(vec![
                prompt_span,
                Span::styled(
                    "Task title @project #tag !priority due:date",
                    Style::default().fg(theme::TEXT_MUTED),
                ),
            ]);
            frame.render_widget(Paragraph::new(line), area);
        } else {
            // Syntax-highlighted input
            let mut spans = vec![prompt_span];
            let highlighted = highlight_input(input_value);
            spans.extend(highlighted);
            let line = Line::from(spans);
            frame.render_widget(Paragraph::new(line), area);
        }

        // Render cursor
        let cursor_x = area.x + 2 + self.input.cursor() as u16; // 2 for "> "
        if cursor_x < area.x + area.width {
            let cursor_char = if self.input.cursor() < input_value.len() {
                input_value.chars().nth(self.input.cursor()).unwrap_or(' ')
            } else {
                ' '
            };
            let buf = frame.buffer_mut();
            buf[(cursor_x, area.y)]
                .set_char(cursor_char)
                .set_style(Style::default().bg(theme::ACCENT).fg(ratatui::style::Color::Black));
        }
    }

    /// Renders the title preview line.
    fn render_title_preview(&self, frame: &mut Frame, area: Rect) {
        let title = self.parsed.title.trim();
        if title.is_empty() {
            let line = Paragraph::new(Span::styled(
                "Title will appear here...",
                Style::default().fg(theme::TEXT_MUTED),
            ));
            frame.render_widget(line, area);
        } else {
            let line = Paragraph::new(Span::styled(
                title,
                Style::default()
                    .fg(theme::TEXT_PRIMARY)
                    .add_modifier(Modifier::BOLD),
            ));
            frame.render_widget(line, area);
        }
    }

    /// Renders the metadata badges line.
    fn render_badges(&self, frame: &mut Frame, area: Rect) {
        let mut spans: Vec<Span> = Vec::new();

        // Project badge (prefer explicit_project, fall back to parsed)
        if let Some(ref project) = self.explicit_project {
            spans.push(Span::styled(
                format!("{} {} ", theme::icons::DIAMOND, project.name),
                Style::default().fg(theme::PROJECT),
            ));
        } else if let Some(ref project_name) = self.parsed.project_name {
            if let Some(ref matched) = self.matched_project {
                spans.push(Span::styled(
                    format!("{} {} ", theme::icons::DIAMOND, matched.name),
                    Style::default().fg(theme::PROJECT),
                ));
            } else {
                spans.push(Span::styled(
                    format!("{} {}? ", theme::icons::DIAMOND, project_name),
                    Style::default().fg(theme::TEXT_MUTED),
                ));
            }
        }

        // Priority badge
        if let Some(priority) = self.parsed.priority {
            let (icon, label, color) = match priority {
                Priority::Urgent => (theme::icons::PRIORITY_URGENT, "Urgent", theme::PRIORITY_URGENT),
                Priority::High => (theme::icons::PRIORITY_HIGH, "High", theme::PRIORITY_HIGH),
                Priority::Medium => ("", "Medium", theme::INFO),
                Priority::Low => (theme::icons::PRIORITY_LOW, "Low", theme::PRIORITY_LOW),
            };
            if !spans.is_empty() {
                spans.push(Span::raw("  "));
            }
            spans.push(Span::styled(
                format!("{} {} ", icon, label),
                Style::default().fg(color),
            ));
        }

        // Tag badges
        for tag in &self.parsed.tags {
            if !spans.is_empty() {
                spans.push(Span::raw("  "));
            }
            spans.push(Span::styled(
                format!("#{} ", tag),
                Style::default().fg(theme::TAG),
            ));
        }

        // Due date badge
        if let Some(ref date_text) = self.parsed.due_date_text {
            if !spans.is_empty() {
                spans.push(Span::raw("  "));
            }
            let color = if self.parsed.due_date.is_some() {
                theme::INFO
            } else {
                theme::TEXT_MUTED
            };
            spans.push(Span::styled(
                format!("{} {} ", theme::icons::CHECKBOX_PROGRESS, date_text),
                Style::default().fg(color),
            ));
        }

        // Empty state hint
        if spans.is_empty() {
            spans.push(Span::styled(
                "@project #tag !priority due:date",
                Style::default().fg(theme::TEXT_DISABLED),
            ));
        }

        let line = Line::from(spans);
        frame.render_widget(Paragraph::new(line), area);
    }
}

/// Syntax-highlights a quick capture input string into colored spans.
fn highlight_input(input: &str) -> Vec<Span<'static>> {
    let mut spans = Vec::new();
    let mut first = true;

    for word in input.split(' ') {
        if !first {
            spans.push(Span::raw(" "));
        }
        first = false;

        if word.starts_with('@') && word.len() > 1 {
            spans.push(Span::styled(
                word.to_string(),
                Style::default().fg(theme::PROJECT),
            ));
        } else if word.starts_with('#') && word.len() > 1 {
            spans.push(Span::styled(
                word.to_string(),
                Style::default().fg(theme::TAG),
            ));
        } else if word.starts_with('!') && matches!(word, "!1" | "!2" | "!3" | "!4") {
            let color = match word {
                "!1" => theme::PRIORITY_URGENT,
                "!2" => theme::PRIORITY_HIGH,
                "!3" => theme::INFO,
                _ => theme::PRIORITY_LOW,
            };
            spans.push(Span::styled(
                word.to_string(),
                Style::default().fg(color),
            ));
        } else if word.starts_with("due:") && word.len() > 4 {
            spans.push(Span::styled(
                word.to_string(),
                Style::default().fg(theme::INFO),
            ));
        } else {
            spans.push(Span::styled(
                word.to_string(),
                Style::default().fg(theme::TEXT_PRIMARY),
            ));
        }
    }

    spans
}

#[cfg(test)]
mod tests {
    use super::*;
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    // ─── Parser tests ────────────────────────────────────────────────────

    #[test]
    fn test_parse_title_only() {
        let result = parse_capture_input("Buy groceries");
        assert_eq!(result.title, "Buy groceries");
        assert!(result.project_name.is_none());
        assert!(result.tags.is_empty());
        assert!(result.priority.is_none());
        assert!(result.due_date_text.is_none());
    }

    #[test]
    fn test_parse_with_project() {
        let result = parse_capture_input("Fix bug @Backend");
        assert_eq!(result.title, "Fix bug");
        assert_eq!(result.project_name.as_deref(), Some("Backend"));
    }

    #[test]
    fn test_parse_with_tags() {
        let result = parse_capture_input("Fix bug #urgent #backend");
        assert_eq!(result.title, "Fix bug");
        assert_eq!(result.tags, vec!["urgent", "backend"]);
    }

    #[test]
    fn test_parse_with_priority() {
        let result = parse_capture_input("Fix bug !1");
        assert_eq!(result.title, "Fix bug");
        assert_eq!(result.priority, Some(Priority::Urgent));
    }

    #[test]
    fn test_parse_priority_values() {
        assert_eq!(
            parse_capture_input("t !1").priority,
            Some(Priority::Urgent)
        );
        assert_eq!(parse_capture_input("t !2").priority, Some(Priority::High));
        assert_eq!(
            parse_capture_input("t !3").priority,
            Some(Priority::Medium)
        );
        assert_eq!(parse_capture_input("t !4").priority, Some(Priority::Low));
    }

    #[test]
    fn test_parse_invalid_priority_becomes_title() {
        let result = parse_capture_input("t !5");
        assert_eq!(result.title, "t !5");
        assert!(result.priority.is_none());
    }

    #[test]
    fn test_parse_with_due_date() {
        let result = parse_capture_input("Fix bug due:tomorrow");
        assert_eq!(result.title, "Fix bug");
        assert_eq!(result.due_date_text.as_deref(), Some("tomorrow"));
        assert!(result.due_date.is_some());
    }

    #[test]
    fn test_parse_full_syntax() {
        let result = parse_capture_input("Fix login bug @Backend #bug !1 due:tomorrow");
        assert_eq!(result.title, "Fix login bug");
        assert_eq!(result.project_name.as_deref(), Some("Backend"));
        assert_eq!(result.tags, vec!["bug"]);
        assert_eq!(result.priority, Some(Priority::Urgent));
        assert_eq!(result.due_date_text.as_deref(), Some("tomorrow"));
    }

    #[test]
    fn test_parse_escaped_at() {
        let result = parse_capture_input("Email \\@john about meeting");
        assert_eq!(result.title, "Email @john about meeting");
        assert!(result.project_name.is_none());
    }

    #[test]
    fn test_parse_escaped_hash() {
        let result = parse_capture_input("Fix issue \\#123");
        assert_eq!(result.title, "Fix issue #123");
        assert!(result.tags.is_empty());
    }

    #[test]
    fn test_parse_empty_input() {
        let result = parse_capture_input("");
        assert_eq!(result.title, "");
        assert!(result.project_name.is_none());
        assert!(result.tags.is_empty());
        assert!(result.priority.is_none());
    }

    #[test]
    fn test_parse_only_tokens() {
        let result = parse_capture_input("@Backend #bug !1 due:tomorrow");
        assert_eq!(result.title, "");
        assert_eq!(result.project_name.as_deref(), Some("Backend"));
        assert_eq!(result.tags, vec!["bug"]);
        assert_eq!(result.priority, Some(Priority::Urgent));
    }

    #[test]
    fn test_parse_empty_at_hash_ignored() {
        let result = parse_capture_input("hello @ # world");
        // Bare @ and # without a name are treated as title words
        assert_eq!(result.title, "hello @ # world");
    }

    // ─── Fuzzy matching tests ────────────────────────────────────────────

    #[test]
    fn test_fuzzy_match_exact() {
        let dialog = make_dialog_with_projects();
        let matched = dialog.fuzzy_match_project("Backend");
        assert_eq!(matched.map(|p| p.name), Some("Backend".to_string()));
    }

    #[test]
    fn test_fuzzy_match_prefix() {
        let dialog = make_dialog_with_projects();
        let matched = dialog.fuzzy_match_project("Back");
        assert_eq!(matched.map(|p| p.name), Some("Backend".to_string()));
    }

    #[test]
    fn test_fuzzy_match_case_insensitive() {
        let dialog = make_dialog_with_projects();
        let matched = dialog.fuzzy_match_project("backend");
        assert_eq!(matched.map(|p| p.name), Some("Backend".to_string()));
    }

    #[test]
    fn test_fuzzy_match_substring() {
        let dialog = make_dialog_with_projects();
        let matched = dialog.fuzzy_match_project("end");
        assert_eq!(matched.map(|p| p.name), Some("Backend".to_string()));
    }

    #[test]
    fn test_fuzzy_match_no_match() {
        let dialog = make_dialog_with_projects();
        let matched = dialog.fuzzy_match_project("xyz");
        assert!(matched.is_none());
    }

    // ─── Dialog key handling tests ───────────────────────────────────────

    #[test]
    fn test_handle_key_enter_submits() {
        let mut dialog = QuickCaptureDialog::new(vec![], vec![], &[]);
        let key = KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE);
        assert_eq!(dialog.handle_key(key), QuickCaptureAction::Submit);
    }

    #[test]
    fn test_handle_key_esc_cancels() {
        let mut dialog = QuickCaptureDialog::new(vec![], vec![], &[]);
        let key = KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE);
        assert_eq!(dialog.handle_key(key), QuickCaptureAction::Cancel);
    }

    #[test]
    fn test_handle_key_tab_expands() {
        let mut dialog = QuickCaptureDialog::new(vec![], vec![], &[]);
        let key = KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE);
        assert_eq!(dialog.handle_key(key), QuickCaptureAction::ExpandToFull);
    }

    #[test]
    fn test_handle_key_char_input() {
        let mut dialog = QuickCaptureDialog::new(vec![], vec![], &[]);
        let key = KeyEvent::new(KeyCode::Char('H'), KeyModifiers::NONE);
        assert_eq!(dialog.handle_key(key), QuickCaptureAction::None);
        assert_eq!(dialog.input.value(), "H");
    }

    // ─── to_task tests ───────────────────────────────────────────────────

    #[test]
    fn test_to_task_empty_title_returns_none() {
        let dialog = QuickCaptureDialog::new(vec![], vec![], &[]);
        assert!(dialog.to_task().is_none());
    }

    #[test]
    fn test_to_task_with_parsed_data() {
        let mut dialog = make_dialog_with_projects();
        dialog.input.set_value("Fix bug @Backend #urgent !1 due:tomorrow");
        dialog.reparse();

        let task = dialog.to_task().unwrap();
        assert_eq!(task.title, "Fix bug");
        assert_eq!(task.priority, Priority::Urgent);
        assert_eq!(task.tags, vec!["urgent"]);
        assert!(task.project_id.is_some());
        assert!(task.due_date.is_some());
    }

    // ─── to_add_task_dialog tests ────────────────────────────────────────

    #[test]
    fn test_to_add_task_dialog_populates_fields() {
        let mut dialog = make_dialog_with_projects();
        dialog.input.set_value("Fix bug @Backend #urgent !2 due:tomorrow");
        dialog.reparse();

        let add_dialog = dialog.to_add_task_dialog();
        assert_eq!(add_dialog.title.value(), "Fix bug");
        assert_eq!(add_dialog.priority, Priority::High);
        assert!(add_dialog.project_id.is_some());
    }

    // ─── Suggestion tests ─────────────────────────────────────────────

    #[test]
    fn test_active_token_at_cursor_project() {
        let mut dialog = make_dialog_with_projects();
        dialog.input.set_value("Fix @ba");
        // Cursor is at end (position 7), which is inside "@ba"
        let result = dialog.active_token_at_cursor();
        assert!(result.is_some());
        let (mode, partial, start, end) = result.unwrap();
        assert_eq!(mode, SuggestionMode::Projects);
        assert_eq!(partial, "ba");
        assert_eq!(start, 4); // "@ba" starts at index 4
        assert_eq!(end, 7);
    }

    #[test]
    fn test_active_token_at_cursor_tag() {
        let mut dialog = make_dialog_with_projects();
        dialog.input.set_value("Fix #ur");
        let result = dialog.active_token_at_cursor();
        assert!(result.is_some());
        let (mode, partial, _, _) = result.unwrap();
        assert_eq!(mode, SuggestionMode::Tags);
        assert_eq!(partial, "ur");
    }

    #[test]
    fn test_active_token_at_cursor_none() {
        let mut dialog = make_dialog_with_projects();
        dialog.input.set_value("Fix bug");
        let result = dialog.active_token_at_cursor();
        assert!(result.is_none());
    }

    #[test]
    fn test_update_suggestions_projects() {
        let mut dialog = make_dialog_with_projects();
        dialog.input.set_value("Fix @b");
        dialog.reparse();
        dialog.update_suggestions();

        assert_eq!(dialog.suggestion_mode, SuggestionMode::Projects);
        assert!(dialog.suggestions.contains(&"Backend".to_string()));
        assert!(!dialog.suggestions.contains(&"Frontend".to_string()));
    }

    #[test]
    fn test_update_suggestions_tags() {
        let tags = vec![
            Tag { id: "1".to_string(), name: "urgent".to_string() },
            Tag { id: "2".to_string(), name: "bug".to_string() },
            Tag { id: "3".to_string(), name: "feature".to_string() },
        ];
        let mut dialog = QuickCaptureDialog::new(vec![], tags, &[]);
        dialog.input.set_value("Fix #urg");
        dialog.reparse();
        dialog.update_suggestions();

        assert_eq!(dialog.suggestion_mode, SuggestionMode::Tags);
        assert!(dialog.suggestions.contains(&"urgent".to_string()));
        assert_eq!(dialog.suggestions.len(), 1); // Only "urgent" matches "urg"
    }

    #[test]
    fn test_update_suggestions_tags_project_scoped() {
        let projects = vec![Project::new("Backend")];
        let project_id = projects[0].id.clone();
        let tags = vec![
            Tag { id: "1".to_string(), name: "api".to_string() },
            Tag { id: "2".to_string(), name: "frontend".to_string() },
            Tag { id: "3".to_string(), name: "auth".to_string() },
        ];
        // Create a task in the project with the "api" tag
        let mut task = Task::new("API task");
        task.project_id = Some(project_id.clone());
        task.tags = vec!["api".to_string()];

        let mut dialog = QuickCaptureDialog::new(projects, tags, &[task]);
        // Set explicit project so scoping works
        dialog.explicit_project = Some(Project::new("Backend"));
        dialog.explicit_project.as_mut().unwrap().id = project_id;

        dialog.input.set_value("Fix #a");
        dialog.reparse();
        dialog.update_suggestions();

        // "api" should appear first (project-scoped), then "auth"
        assert_eq!(dialog.suggestion_mode, SuggestionMode::Tags);
        assert!(dialog.suggestions.len() >= 2);
        assert_eq!(dialog.suggestions[0], "api"); // scoped tag first
        assert_eq!(dialog.suggestions[1], "auth"); // non-scoped match
    }

    #[test]
    fn test_accept_project_removes_token() {
        let mut dialog = make_dialog_with_projects();
        // Type "@Back"
        dialog.input.set_value("Fix @Back bug");
        // Position cursor after "@Back" (position 9)
        dialog.input.move_home();
        for _ in 0..9 {
            dialog.input.move_right();
        }
        dialog.reparse();
        dialog.update_suggestions();

        assert!(!dialog.suggestions.is_empty());
        assert_eq!(dialog.suggestion_mode, SuggestionMode::Projects);

        // Accept the suggestion (Backend)
        dialog.accept_suggestion();

        // @Back should be removed from input
        assert!(dialog.explicit_project.is_some());
        assert_eq!(dialog.explicit_project.as_ref().unwrap().name, "Backend");
        // Input should not contain @Back anymore
        assert!(!dialog.input.value().contains("@Back"));
        assert!(dialog.input.value().contains("Fix"));
        assert!(dialog.input.value().contains("bug"));
    }

    #[test]
    fn test_accept_tag_replaces_token() {
        let tags = vec![
            Tag { id: "1".to_string(), name: "urgent".to_string() },
        ];
        let mut dialog = QuickCaptureDialog::new(vec![], tags, &[]);
        dialog.input.set_value("Fix #ur bug");
        // Position cursor after "#ur" (position 6)
        dialog.input.move_home();
        for _ in 0..6 {
            dialog.input.move_right();
        }
        dialog.reparse();
        dialog.update_suggestions();

        assert!(!dialog.suggestions.is_empty());
        assert_eq!(dialog.suggestion_mode, SuggestionMode::Tags);

        dialog.accept_suggestion();

        // #ur should be replaced with #urgent
        assert!(dialog.input.value().contains("#urgent"));
        assert!(!dialog.input.value().contains("#ur "));
    }

    #[test]
    fn test_tab_accepts_suggestion() {
        let mut dialog = make_dialog_with_projects();
        // Type "@B" to trigger suggestions
        let key_at = KeyEvent::new(KeyCode::Char('@'), KeyModifiers::NONE);
        dialog.handle_key(key_at);
        let key_b = KeyEvent::new(KeyCode::Char('B'), KeyModifiers::NONE);
        dialog.handle_key(key_b);

        assert!(!dialog.suggestions.is_empty());

        // Tab should accept suggestion instead of expanding
        let key_tab = KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE);
        let action = dialog.handle_key(key_tab);
        assert_eq!(action, QuickCaptureAction::None); // Not ExpandToFull
        assert!(dialog.explicit_project.is_some());
    }

    #[test]
    fn test_explicit_project_cleared_on_new_at() {
        let mut dialog = make_dialog_with_projects();
        // Set an explicit project
        dialog.explicit_project = Some(dialog.projects[0].clone());

        // Type "@F" to start a new project token
        dialog.input.set_value("Fix @F");
        dialog.reparse();

        // The explicit project should be cleared because a new @token appeared
        assert!(dialog.explicit_project.is_none());
    }

    // ─── Priority suggestion tests ──────────────────────────────────────

    #[test]
    fn test_active_token_at_cursor_priority() {
        let mut dialog = make_dialog_with_projects();
        dialog.input.set_value("Fix !");
        let result = dialog.active_token_at_cursor();
        assert!(result.is_some());
        let (mode, partial, start, end) = result.unwrap();
        assert_eq!(mode, SuggestionMode::Priorities);
        assert_eq!(partial, "");
        assert_eq!(start, 4); // "!" starts at index 4
        assert_eq!(end, 5);
    }

    #[test]
    fn test_update_suggestions_priorities_all() {
        let mut dialog = make_dialog_with_projects();
        dialog.input.set_value("Fix !");
        dialog.reparse();
        dialog.update_suggestions();

        assert_eq!(dialog.suggestion_mode, SuggestionMode::Priorities);
        assert_eq!(dialog.suggestions.len(), 4);
        assert_eq!(dialog.suggestions, vec!["1", "2", "3", "4"]);
    }

    #[test]
    fn test_update_suggestions_priorities_filtered() {
        let mut dialog = make_dialog_with_projects();
        dialog.input.set_value("Fix !2");
        dialog.reparse();
        dialog.update_suggestions();

        assert_eq!(dialog.suggestion_mode, SuggestionMode::Priorities);
        assert_eq!(dialog.suggestions.len(), 1);
        assert_eq!(dialog.suggestions[0], "2");
    }

    #[test]
    fn test_accept_priority_replaces_token() {
        let mut dialog = make_dialog_with_projects();
        dialog.input.set_value("Fix ! bug");
        // Position cursor after "!" (position 5)
        dialog.input.move_home();
        for _ in 0..5 {
            dialog.input.move_right();
        }
        dialog.reparse();
        dialog.update_suggestions();

        assert_eq!(dialog.suggestion_mode, SuggestionMode::Priorities);
        // Select "1" (Urgent) - it's the first item
        dialog.accept_suggestion();

        // "!" should be replaced with "!1"
        assert!(dialog.input.value().contains("!1"));
        assert!(dialog.input.value().contains("Fix"));
        assert!(dialog.input.value().contains("bug"));
    }

    #[test]
    fn test_tab_accepts_priority_suggestion() {
        let mut dialog = make_dialog_with_projects();
        // Type "!" to trigger priority suggestions
        let key_bang = KeyEvent::new(KeyCode::Char('!'), KeyModifiers::NONE);
        dialog.handle_key(key_bang);

        assert_eq!(dialog.suggestion_mode, SuggestionMode::Priorities);
        assert_eq!(dialog.suggestions.len(), 4);

        // Navigate down to select "2" (High)
        let key_down = KeyEvent::new(KeyCode::Down, KeyModifiers::NONE);
        dialog.handle_key(key_down);
        assert_eq!(dialog.selected_suggestion, Some(1));

        // Tab should accept the priority suggestion
        let key_tab = KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE);
        let action = dialog.handle_key(key_tab);
        assert_eq!(action, QuickCaptureAction::None); // Not ExpandToFull
        assert!(dialog.input.value().contains("!2"));
    }

    // ─── Test helpers ────────────────────────────────────────────────────

    fn make_dialog_with_projects() -> QuickCaptureDialog {
        let projects = vec![
            Project::new("Backend"),
            Project::new("Frontend"),
            Project::new("Design"),
        ];
        QuickCaptureDialog::new(projects, vec![], &[])
    }
}