clin-rs 0.2.1

Encrypted terminal note-taking app
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
use crate::constants::*;
use crate::events::get_title_text;
use crate::events::make_title_editor;
use crate::storage::AppSettings;
use crate::ui::help_page_text;
use crate::ui::text_area_from_content;
use crate::ui::{now_unix_secs, open_in_file_manager};
use ratatui::style::{Color, Style};
use ratatui::text::Text;
use ratatui::widgets::ListState;
use std::borrow::Cow;
use std::time::Duration;
use std::time::Instant;

use crate::keybinds::Keybinds;
use crate::storage::{Note, NoteSummary, Storage};
use crate::templates::{Template, TemplateSummary};
use anyhow::Result;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use tui_textarea::TextArea;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ViewMode {
    List,
    Edit,
    Help,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ListFocus {
    Notes,
    EncryptionToggle,
}

pub struct ContextMenu {
    pub x: u16,
    pub y: u16,
    pub selected: usize,
}

/// Template selection popup state
pub struct TemplatePopup {
    pub templates: Vec<TemplateSummary>,
    pub selected: usize,
}

pub struct TagPopup {
    pub note_id: String,
    pub input: TextArea<'static>,
}

pub enum FolderPopupMode {
    Create,
    Rename { old_path: String },
}

pub struct FolderPopup {
    pub mode: FolderPopupMode,
    pub input: TextArea<'static>,
}

pub struct FolderPicker {
    pub note_id: String,
    pub folders: Vec<String>,
    pub selected: usize,
}

#[derive(Debug, Clone)]
pub enum VisualItem {
    Folder {
        path: String,
        name: String,
        depth: usize,
        is_expanded: bool,
        note_count: usize,
    },
    Note {
        id: String,
        summary_idx: usize,
        depth: usize,
        is_clin: bool,
    },
    CreateNew {
        path: String,
        depth: usize,
    },
}

pub struct App {
    pub storage: Storage,
    pub keybinds: Keybinds,
    pub notes: Vec<NoteSummary>,
    pub visual_list: Vec<VisualItem>,
    pub visual_index: usize,
    pub list_focus: ListFocus,
    pub mode: ViewMode,
    pub editing_id: Option<String>,
    pub title_editor: TextArea<'static>,
    pub editor: TextArea<'static>,
    pub encryption_enabled: bool,
    pub status: Cow<'static, str>,
    pub status_until: Option<Instant>,
    pub pending_delete_note_id: Option<String>,
    pub pending_encrypt_note_id: Option<String>,
    pub help_scroll: u16,
    pub context_menu: Option<ContextMenu>,
    pub template_popup: Option<TemplatePopup>,
    pub tag_popup: Option<TagPopup>,
    pub folder_popup: Option<FolderPopup>,
    pub folder_picker: Option<FolderPicker>,
    pub folder_expanded: HashSet<String>,
    pub filter_tags: Vec<String>,
    pub filter_popup: Option<TextArea<'static>>,
    /// Cached help page text (rebuilt when keybinds change)
    pub help_text_cache: Option<Text<'static>>,
    pub list_state: ListState,
}

pub enum CliCommand {
    Run {
        edit_title: Option<String>,
    },
    NewAndOpen {
        title: Option<String>,
        template: Option<String>,
    },
    QuickNote {
        content: String,
        title: Option<String>,
    },
    ListNoteTitles,
    Help,
    // Storage path commands
    ShowStoragePath,
    SetStoragePath {
        path: PathBuf,
    },
    ResetStoragePath,
    // Keybind commands
    ShowKeybinds,
    ExportKeybinds,
    ResetKeybinds,
    // Template commands
    ListTemplates,
    CreateExampleTemplates,
}

impl App {
    pub fn new(storage: Storage) -> Result<Self> {
        let settings = storage.load_settings();
        let keybinds = storage.load_keybinds();

        let mut app = Self {
            storage,
            keybinds,
            notes: Vec::new(),
            visual_list: Vec::new(),
            visual_index: 0,
            list_focus: ListFocus::Notes,
            mode: ViewMode::List,
            editing_id: None,
            title_editor: make_title_editor(""),
            editor: TextArea::default(),
            encryption_enabled: settings.encryption_enabled,
            status: Cow::Borrowed(LIST_HELP_HINTS),
            status_until: None,
            pending_delete_note_id: None,
            pending_encrypt_note_id: None,
            help_scroll: 0,
            context_menu: None,
            template_popup: None,
            tag_popup: None,
            folder_popup: None,
            folder_picker: None,
            folder_expanded: HashSet::new(),
            filter_tags: Vec::new(),
            filter_popup: None,
            help_text_cache: None,
            list_state: ListState::default(),
        };
        app.context_menu = None;
        app.template_popup = None;
        app.refresh_notes()?;
        Ok(app)
    }

    pub fn refresh_notes(&mut self) -> Result<()> {
        let mut summaries = Vec::new();
        for id in self.storage.list_note_ids()? {
            if let Ok(summary) = self.storage.load_note_summary(&id) {
                // Apply tag filter
                if !self.filter_tags.is_empty() {
                    let mut matches = false;
                    for tag in &self.filter_tags {
                        if summary.tags.contains(tag) {
                            matches = true;
                            break;
                        }
                    }
                    if !matches {
                        continue;
                    }
                }
                summaries.push(summary);
            }
        }
        summaries.sort_by(|a, b| {
            let a_clin = a.id.ends_with(".clin");
            let b_clin = b.id.ends_with(".clin");
            b_clin.cmp(&a_clin).then(b.updated_at.cmp(&a.updated_at))
        });
        self.notes = summaries;

        self.refresh_visual_list();
        Ok(())
    }

    pub fn refresh_visual_list(&mut self) {
        let mut visual = Vec::new();

        // Notes are currently flattened. Let's group them by folder.
        // We'll construct a simple tree.
        let mut by_folder: HashMap<String, Vec<(usize, &NoteSummary)>> = HashMap::new();
        for (i, note) in self.notes.iter().enumerate() {
            by_folder
                .entry(note.folder.clone())
                .or_default()
                .push((i, note));
        }

        // Always show root folder "Vault"
        visual.push(VisualItem::Folder {
            path: "".to_string(),
            name: "Vault".to_string(),
            depth: 0,
            is_expanded: self.folder_expanded.contains(""),
            note_count: by_folder
                .get("")
                .map_or(0, |v: &Vec<(usize, &NoteSummary)>| v.len()),
        });

        if self.folder_expanded.contains("") {
            if let Some(notes) = by_folder.get("") {
                for (idx, note) in notes {
                    visual.push(VisualItem::Note {
                        id: note.id.clone(),
                        summary_idx: *idx,
                        depth: 1,
                        is_clin: note.id.ends_with(".clin"),
                    });
                }
            }
            visual.push(VisualItem::CreateNew {
                path: "".to_string(),
                depth: 1,
            });
        }

        // Get all other folders sorted
        let mut subfolders: Vec<String> = by_folder
            .keys()
            .filter(|k: &&String| !k.is_empty())
            .cloned()
            .collect();
        subfolders.sort();

        // Wait, what if a parent folder has no notes but has subfolders?
        // We should really build a proper tree from `storage.list_folders()`.
        if let Ok(all_folders) = self.storage.list_folders() {
            for folder in all_folders {
                let parts: Vec<&str> = folder.split('/').collect();
                let depth = parts.len();
                let name = parts.last().unwrap_or(&"").to_string();

                // Only show if parent is expanded
                let parent_path = if parts.len() > 1 {
                    parts[..parts.len() - 1].join("/")
                } else {
                    "".to_string()
                };

                // Fast check if parent is expanded
                let mut is_visible = true;
                let mut current_parent = parent_path.clone();
                while !current_parent.is_empty() {
                    if !self.folder_expanded.contains(&current_parent) {
                        is_visible = false;
                        break;
                    }
                    if let Some(slash) = current_parent.rfind('/') {
                        current_parent = current_parent[..slash].to_string();
                    } else {
                        current_parent = "".to_string();
                    }
                }

                // Finally check root
                if !self.folder_expanded.contains("") {
                    is_visible = false;
                }

                if is_visible {
                    let is_expanded = self.folder_expanded.contains(&folder);
                    visual.push(VisualItem::Folder {
                        path: folder.clone(),
                        name,
                        depth,
                        is_expanded,
                        note_count: by_folder
                            .get(&folder)
                            .map_or(0, |v: &Vec<(usize, &NoteSummary)>| v.len()),
                    });

                    if is_expanded {
                        if let Some(notes) = by_folder.get(&folder) {
                            for (idx, note) in notes {
                                visual.push(VisualItem::Note {
                                    id: note.id.clone(),
                                    summary_idx: *idx,
                                    depth: depth + 1,
                                    is_clin: note.id.ends_with(".clin"),
                                });
                            }
                        }
                        visual.push(VisualItem::CreateNew {
                            path: folder.clone(),
                            depth: depth + 1,
                        });
                    }
                }
            }
        }

        self.visual_list = visual;
    }

    pub fn open_selected(&mut self) {
        if self.visual_list.is_empty() {
            return;
        }

        // Clamp index
        if self.visual_index >= self.visual_list.len() {
            self.visual_index = self.visual_list.len().saturating_sub(1);
        }

        match &self.visual_list[self.visual_index] {
            VisualItem::CreateNew { path, .. } => {
                self.start_new_note(path.clone());
            }
            VisualItem::Folder { path, .. } => {
                // Toggle expand/collapse
                let p = path.clone();
                if self.folder_expanded.contains(&p) {
                    self.folder_expanded.remove(&p);
                } else {
                    self.folder_expanded.insert(p);
                }
                self.refresh_visual_list();
            }
            VisualItem::Note { summary_idx, .. } => {
                let note_id = if let Some(summary) = self.notes.get(*summary_idx) {
                    let is_clin = summary.id.ends_with(".clin");
                    if !self.encryption_enabled && is_clin {
                        self.status = Cow::Borrowed(
                            "Cannot open encrypted notes while encryption is disabled.",
                        );
                        return;
                    }

                    if self.encryption_enabled && !is_clin {
                        self.pending_encrypt_note_id = Some(summary.id.clone());
                        self.status_until = None;
                        self.status = Cow::Borrowed(
                            "WARNING: This action will encrypt the file! y confirm, n cancel",
                        );
                        return;
                    }
                    Some(summary.id.clone())
                } else {
                    None
                };

                if let Some(id) = note_id {
                    self.load_and_open_note(&id);
                }
            }
        }
    }

    pub fn load_and_open_note(&mut self, note_id: &str) {
        if let Ok(note) = self.storage.load_note(note_id) {
            self.editing_id = Some(note_id.to_string());
            self.title_editor = make_title_editor(&note.title);
            self.editor = text_area_from_content(&note.content);
            self.mode = ViewMode::Edit;
            self.status = Cow::Borrowed(EDIT_HELP_HINTS);
        } else {
            self.status = Cow::Borrowed("Failed to load note!");
        }
    }

    pub fn confirm_encrypt_warning(&mut self) {
        if let Some(id) = self.pending_encrypt_note_id.take() {
            self.load_and_open_note(&id);
        }
    }

    pub fn cancel_encrypt_warning(&mut self) {
        self.pending_encrypt_note_id = None;
        self.set_default_status();
    }

    pub fn collapse_selected_folder(&mut self) {
        if self.visual_list.is_empty() {
            return;
        }

        if self.visual_index >= self.visual_list.len() {
            self.visual_index = self.visual_list.len().saturating_sub(1);
        }

        match &self.visual_list[self.visual_index] {
            VisualItem::Folder {
                path, is_expanded, ..
            } => {
                if *is_expanded {
                    self.folder_expanded.remove(path);
                    self.refresh_visual_list();
                } else {
                    // Navigate to parent folder
                    if !path.is_empty() {
                        let parent_path = if let Some(slash) = path.rfind('/') {
                            &path[..slash]
                        } else {
                            "" // root
                        };

                        if let Some(idx) = self.visual_list.iter().position(|v| {
                            if let VisualItem::Folder { path: p, .. } = v {
                                p == parent_path
                            } else {
                                false
                            }
                        }) {
                            self.visual_index = idx;
                        }
                    }
                }
            }
            VisualItem::Note { .. } | VisualItem::CreateNew { .. } => {
                // Determine folder path and navigate to parent folder
                let item_path = match &self.visual_list[self.visual_index] {
                    VisualItem::Note { summary_idx, .. } => &self.notes[*summary_idx].folder,
                    VisualItem::CreateNew { path, .. } => path,
                    _ => unreachable!(),
                };

                if let Some(idx) = self.visual_list.iter().position(|v| {
                    if let VisualItem::Folder { path: p, .. } = v {
                        p == item_path
                    } else {
                        false
                    }
                }) {
                    self.visual_index = idx;
                }
            }
        }
    }

    pub fn expand_selected_folder(&mut self) {
        if self.visual_list.is_empty() {
            return;
        }

        if self.visual_index >= self.visual_list.len() {
            self.visual_index = self.visual_list.len().saturating_sub(1);
        }

        match &self.visual_list[self.visual_index] {
            VisualItem::Folder {
                path, is_expanded, ..
            } => {
                if !is_expanded {
                    self.folder_expanded.insert(path.clone());
                    self.refresh_visual_list();
                } else {
                    // Navigate to first child
                    if self.visual_index + 1 < self.visual_list.len() {
                        self.visual_index += 1;
                    }
                }
            }
            VisualItem::Note { .. } | VisualItem::CreateNew { .. } => {
                self.open_selected();
            }
        }
    }

    pub fn open_note_by_title(&mut self, title: &str) -> bool {
        let query = title.trim();
        if query.is_empty() {
            return false;
        }

        if let Some(index) = self
            .notes
            .iter()
            .position(|note| note.title.eq_ignore_ascii_case(query))
        {
            // Now we need to find its visual index...
            if let Some(v_idx) = self.visual_list.iter().position(|v| match v {
                VisualItem::Note { summary_idx, .. } => *summary_idx == index,
                _ => false,
            }) {
                self.visual_index = v_idx;
                self.open_selected();
                return true;
            }
        }

        false
    }

    pub fn start_new_note(&mut self, folder: String) {
        // Check if default template exists and use it
        let template_manager = self.storage.template_manager();
        if let Some(default_template) = template_manager.load_default() {
            self.start_note_from_template(&default_template, folder);
        } else {
            self.start_blank_note(folder);
        }
    }

    pub fn start_blank_note(&mut self, folder: String) {
        self.mode = ViewMode::Edit;
        let mut new_id = self.storage.new_note_id();
        if !folder.is_empty() {
            new_id = format!("{}/{}", folder, new_id);
        }
        self.editing_id = Some(new_id);
        self.title_editor = make_title_editor("");
        self.editor = TextArea::default();
        self.editor
            .set_cursor_style(Style::default().fg(Color::Black).bg(Color::Cyan));
        self.editor
            .set_cursor_line_style(Style::default().bg(Color::Rgb(32, 36, 44)));
        self.set_default_status();
    }

    pub fn start_note_from_template(&mut self, template: &Template, folder: String) {
        let rendered = template.render();

        self.mode = ViewMode::Edit;
        let mut new_id = self.storage.new_note_id();
        if !folder.is_empty() {
            new_id = format!("{}/{}", folder, new_id);
        }
        self.editing_id = Some(new_id);

        self.title_editor = make_title_editor(rendered.title.as_deref().unwrap_or(""));
        self.editor = text_area_from_content(&rendered.content);

        self.editor
            .set_cursor_style(Style::default().fg(Color::Black).bg(Color::Cyan));
        self.editor
            .set_cursor_line_style(Style::default().bg(Color::Rgb(32, 36, 44)));

        self.set_default_status();
    }

    pub fn open_template_popup(&mut self) {
        let template_manager = self.storage.template_manager();
        match template_manager.list() {
            Ok(templates) => {
                if templates.is_empty() {
                    self.set_temporary_status(
                        "No templates found. Create templates in the templates directory.",
                    );
                } else {
                    self.template_popup = Some(TemplatePopup {
                        templates,
                        selected: 0,
                    });
                }
            }
            Err(_) => {
                self.set_temporary_status("Failed to load templates");
            }
        }
    }

    pub fn close_template_popup(&mut self) {
        self.template_popup = None;
    }

    pub fn select_template(&mut self) {
        if let Some(popup) = self.template_popup.take()
            && let Some(summary) = popup.templates.get(popup.selected)
        {
            let template_manager = self.storage.template_manager();
            if let Ok(template) = template_manager.load(&summary.filename) {
                self.start_note_from_template(&template, String::new());
                return;
            }
        }
        // Fallback to blank note if something went wrong
        self.start_blank_note(String::new());
    }

    pub fn autosave(&mut self) {
        let mut title = get_title_text(&self.title_editor).trim().to_string();
        if title.is_empty() {
            title = String::from("Untitled note");
        }
        let content = self.editor.lines().join("\n");
        let id = match &self.editing_id {
            Some(id) => id.clone(),
            None => return,
        };
        let note = Note {
            title: title.clone(),
            content,
            updated_at: self
                .storage
                .load_note(&id)
                .map(|n| n.updated_at)
                .unwrap_or_else(|_| now_unix_secs()),
            tags: self
                .storage
                .load_note(&id)
                .map(|n| n.tags)
                .unwrap_or_default(),
        };
        if let Ok(saved_id) = self.storage.save_note(&id, &note, self.encryption_enabled) {
            self.editing_id = Some(saved_id);
        }
    }

    pub fn back_to_list(&mut self) {
        self.mode = ViewMode::List;
        self.editing_id = None;
        self.list_focus = ListFocus::Notes;
        self.title_editor = make_title_editor("");
        self.editor = TextArea::default();
        self.pending_delete_note_id = None;
        self.pending_encrypt_note_id = None;
        let _ = self.refresh_notes();
        self.set_default_status();
    }

    pub fn handle_menu_action(&mut self, action: usize, focus: &mut EditFocus) {
        match action {
            0 => match focus {
                EditFocus::Title => {
                    self.title_editor.copy();
                }
                EditFocus::Body => {
                    self.editor.copy();
                }
                _ => {}
            },
            1 => match focus {
                EditFocus::Title => {
                    self.title_editor.cut();
                }
                EditFocus::Body => {
                    self.editor.cut();
                }
                _ => {}
            },
            2 => match focus {
                EditFocus::Title => {
                    self.title_editor.paste();
                }
                EditFocus::Body => {
                    self.editor.paste();
                }
                _ => {}
            },
            3 => match focus {
                EditFocus::Title => {
                    self.title_editor.select_all();
                }
                EditFocus::Body => {
                    self.editor.select_all();
                }
                _ => {}
            },
            _ => {}
        }
    }

    pub fn begin_delete_selected(&mut self) {
        if self.visual_index >= self.visual_list.len() {
            self.set_temporary_status("No item selected to delete");
            return;
        }

        match &self.visual_list[self.visual_index].clone() {
            VisualItem::Note { summary_idx, .. } => {
                if let Some(note) = self.notes.get(*summary_idx) {
                    self.pending_delete_note_id = Some(note.id.clone());
                    self.status_until = None;
                    self.status =
                        Cow::Owned(format!("Delete \"{}\"? y confirm, n cancel", note.title));
                }
            }
            VisualItem::Folder { path, .. } => {
                if path.is_empty() {
                    self.set_temporary_status("Cannot delete Vault root");
                    return;
                }
                if let Err(e) = self.storage.delete_folder(path) {
                    self.set_temporary_status(&format!("Failed to delete folder: {e}"));
                } else {
                    let _ = self.refresh_notes();
                    self.set_temporary_status("Folder deleted");
                }
            }
            _ => {
                self.set_temporary_status("Cannot delete this item");
            }
        }
    }

    pub fn cancel_delete_prompt(&mut self) {
        self.pending_delete_note_id = None;
        self.set_default_status();
    }

    pub fn confirm_delete_selected(&mut self) {
        let id = match self.pending_delete_note_id.take() {
            Some(id) => id,
            None => return,
        };

        match self.storage.delete_note(&id) {
            Ok(()) => {
                let _ = self.refresh_notes();
                if self.visual_index >= self.visual_list.len() && !self.visual_list.is_empty() {
                    self.visual_index = self.visual_list.len() - 1;
                }
                self.set_temporary_status("Note deleted");
            }
            Err(err) => {
                self.pending_delete_note_id = None;
                self.set_temporary_status(&format!("Delete failed: {err:#}"));
            }
        }
    }

    pub fn begin_create_folder(&mut self) {
        let mut input = TextArea::default();
        input.set_block(
            ratatui::widgets::Block::default()
                .borders(ratatui::widgets::Borders::ALL)
                .title("Create Folder (e.g. 'work/projects') - Esc to cancel, Enter to save"),
        );
        self.folder_popup = Some(FolderPopup {
            mode: FolderPopupMode::Create,
            input,
        });
    }

    pub fn begin_rename_folder(&mut self) {
        if let Some(VisualItem::Folder { path, .. }) = self.visual_list.get(self.visual_index) {
            if path.is_empty() {
                self.set_temporary_status("Cannot rename Vault root");
                return;
            }
            let mut input = TextArea::default();
            input.insert_str(path);
            input.set_block(
                ratatui::widgets::Block::default()
                    .borders(ratatui::widgets::Borders::ALL)
                    .title("Rename Folder - Esc to cancel, Enter to save"),
            );
            self.folder_popup = Some(FolderPopup {
                mode: FolderPopupMode::Rename {
                    old_path: path.clone(),
                },
                input,
            });
        } else {
            self.set_temporary_status("Select a folder to rename");
        }
    }

    pub fn confirm_folder_popup(&mut self) {
        if let Some(popup) = self.folder_popup.take() {
            let text = popup.input.lines().join("");
            let text = text.trim();
            if text.is_empty() {
                self.set_temporary_status("Folder name cannot be empty");
                return;
            }

            match &popup.mode {
                FolderPopupMode::Create => {
                    if let Err(e) = self.storage.create_folder(text) {
                        self.set_temporary_status(&format!("Failed to create folder: {e}"));
                    } else {
                        let _ = self.refresh_notes();
                        self.set_temporary_status("Folder created");
                    }
                }
                FolderPopupMode::Rename { old_path } => {
                    if let Err(e) = self.storage.rename_folder(old_path, text) {
                        self.set_temporary_status(&format!("Failed to rename folder: {e}"));
                    } else {
                        let _ = self.refresh_notes();
                        self.set_temporary_status("Folder renamed");
                    }
                }
            }
        }
    }

    pub fn begin_move_note(&mut self) {
        if let Some(VisualItem::Note { summary_idx, .. }) = self.visual_list.get(self.visual_index)
        {
            let note = &self.notes[*summary_idx];
            if let Ok(folders) = self.storage.list_folders() {
                let mut all_folders = vec!["".to_string()]; // Root folder
                all_folders.extend(folders);
                self.folder_picker = Some(FolderPicker {
                    note_id: note.id.clone(),
                    folders: all_folders,
                    selected: 0,
                });
            } else {
                self.set_temporary_status("Failed to list folders");
            }
        } else {
            self.set_temporary_status("Select a note to move");
        }
    }

    pub fn confirm_move_note(&mut self) {
        if let Some(picker) = self.folder_picker.take()
            && let Some(target_folder) = picker.folders.get(picker.selected)
        {
            if let Err(e) = self.storage.move_note(&picker.note_id, target_folder) {
                self.set_temporary_status(&format!("Failed to move note: {e}"));
            } else {
                let _ = self.refresh_notes();
                self.set_temporary_status("Note moved");
            }
        }
    }

    pub fn begin_manage_tags(&mut self) {
        if let Some(VisualItem::Note { summary_idx, .. }) = self.visual_list.get(self.visual_index)
        {
            let note = &self.notes[*summary_idx];
            let current_tags = note.tags.clone();

            let mut input = TextArea::default();
            input.insert_str(current_tags.join(", "));
            input.set_block(
                ratatui::widgets::Block::default()
                    .borders(ratatui::widgets::Borders::ALL)
                    .title("Manage Tags (comma separated) - Esc to cancel, Enter to save"),
            );

            self.tag_popup = Some(TagPopup {
                note_id: note.id.clone(),
                input,
            });
        } else {
            self.set_temporary_status("Select a note to manage tags");
        }
    }

    pub fn confirm_manage_tags(&mut self) {
        if let Some(popup) = self.tag_popup.take() {
            let text = popup.input.lines().join("");
            let tags: Vec<String> = text
                .split(',')
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
                .collect();

            if let Ok(mut note) = self.storage.load_note(&popup.note_id) {
                note.tags = tags.clone();
                let is_clin = popup.note_id.ends_with(".clin");
                if let Err(e) = self.storage.save_note(&popup.note_id, &note, is_clin) {
                    self.set_temporary_status(&format!("Failed to save tags: {e}"));
                } else {
                    let mut all_tags = self.storage.load_tag_cache();
                    let mut changed = false;
                    for t in &tags {
                        if !all_tags.contains(t) {
                            all_tags.push(t.clone());
                            changed = true;
                        }
                    }
                    if changed {
                        all_tags.sort();
                        let _ = self.storage.save_tag_cache(&all_tags);
                    }
                    let _ = self.refresh_notes();
                    self.set_temporary_status("Tags updated");
                }
            } else {
                self.set_temporary_status("Failed to load note to update tags");
            }
        }
    }

    pub fn begin_filter_tags(&mut self) {
        let mut input = TextArea::default();
        input.insert_str(self.filter_tags.join(", "));
        input.set_block(
            ratatui::widgets::Block::default()
                .borders(ratatui::widgets::Borders::ALL)
                .title("Filter Tags (comma separated OR logic) - Esc to clear, Enter to apply"),
        );
        self.filter_popup = Some(input);
    }

    pub fn confirm_filter_tags(&mut self) {
        if let Some(input) = self.filter_popup.take() {
            let text = input.lines().join("");
            let tags: Vec<String> = text
                .split(',')
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
                .collect();

            self.filter_tags = tags;
            let _ = self.refresh_notes();
            self.visual_index = 0;
        }
    }

    pub fn cancel_filter_tags(&mut self) {
        self.filter_popup = None;
    }

    pub fn open_selected_note_location(&mut self) {
        if self.visual_index >= self.visual_list.len() {
            self.set_temporary_status("No note selected for location");
            return;
        }

        let summary_idx = match &self.visual_list[self.visual_index] {
            VisualItem::Note { summary_idx, .. } => *summary_idx,
            _ => {
                self.set_temporary_status("Selected item is not a note");
                return;
            }
        };

        let Some(note) = self.notes.get(summary_idx) else {
            self.set_temporary_status("No note selected for location");
            return;
        };

        let note_path = self.storage.note_path(&note.id);
        let Some(parent) = note_path.parent() else {
            self.set_temporary_status("Could not determine note directory");
            return;
        };

        match open_in_file_manager(parent) {
            Ok(()) => self.set_temporary_status("Opened note file location"),
            Err(err) => self.set_temporary_status(&format!("Open location failed: {err:#}")),
        }
    }

    pub fn toggle_encryption_mode(&mut self) {
        self.encryption_enabled = !self.encryption_enabled;
        self.set_default_status();
        self.storage.save_settings(&AppSettings {
            encryption_enabled: self.encryption_enabled,
        });
    }

    pub fn open_help_page(&mut self) {
        self.mode = ViewMode::Help;
        self.help_scroll = 0;
        self.status = Cow::Borrowed(HELP_PAGE_HINTS);
        self.status_until = None;
    }

    pub fn close_help_page(&mut self) {
        self.mode = ViewMode::List;
        self.help_scroll = 0;
        self.set_default_status();
    }

    pub fn default_status_text(&self) -> &'static str {
        match self.mode {
            ViewMode::List => LIST_HELP_HINTS,
            ViewMode::Edit => EDIT_HELP_HINTS,
            ViewMode::Help => HELP_PAGE_HINTS,
        }
    }

    pub fn set_default_status(&mut self) {
        self.status = Cow::Borrowed(self.default_status_text());
        self.status_until = None;
    }

    pub fn set_temporary_status(&mut self, message: &str) {
        self.status = Cow::Owned(message.to_string());
        self.status_until = Some(Instant::now() + Duration::from_secs(2));
    }

    pub fn tick_status(&mut self) {
        if let Some(until) = self.status_until
            && Instant::now() >= until
        {
            self.set_default_status();
        }
    }

    /// Get cached help text, building it if necessary
    pub fn get_help_text(&mut self) -> Text<'static> {
        if let Some(ref text) = self.help_text_cache {
            return text.clone();
        }
        let text = help_page_text(&self.keybinds);
        self.help_text_cache = Some(text.clone());
        text
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EditFocus {
    Title,
    Body,
    EncryptionToggle,
}