frame 0.1.5

A markdown task tracker with a terminal UI for humans and a CLI for agents
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
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

use crate::model::SectionKind;
use crate::model::task::Metadata;
use crate::ops::task_ops::{self};

use crate::tui::app::{
    App, AutocompleteKind, AutocompleteState, DetailRegion, EditHistory, EditTarget, FlatItem,
    Mode, MoveState, PendingMove, PendingMoveKind, RepeatableAction, TriageSource, View,
    resolve_task_from_flat,
};
use crate::tui::undo::Operation;

use super::*;

/// Enter SELECT mode and toggle the task under the cursor.
pub(super) fn enter_select_mode(app: &mut App) {
    if let Some((_, task_id, _)) = app.cursor_task_id() {
        app.selection.insert(task_id);
        app.mode = Mode::Select;
    }
}

/// Begin range selection: set anchor at current cursor position.
pub(super) fn begin_range_select(app: &mut App) {
    let track_id = match app.current_track_id() {
        Some(id) => id.to_string(),
        None => return,
    };
    let cursor = app.track_states.get(&track_id).map_or(0, |s| s.cursor);

    // Select current task and set anchor
    if let Some((_, task_id, _)) = app.cursor_task_id() {
        app.selection.insert(task_id);
    }
    app.range_anchor = Some(cursor);
    app.mode = Mode::Select;
}

/// Finalize range selection: select all items between anchor and cursor, clear anchor.
pub(super) fn finalize_range_select(app: &mut App) {
    let track_id = match app.current_track_id() {
        Some(id) => id.to_string(),
        None => return,
    };
    let flat_items = app.build_flat_items(&track_id);
    let cursor = app.track_states.get(&track_id).map_or(0, |s| s.cursor);
    let anchor = match app.range_anchor {
        Some(a) => a,
        None => return,
    };

    let track = match App::find_track_in_project(&app.project, &track_id) {
        Some(t) => t,
        None => return,
    };

    let (start, end) = if cursor <= anchor {
        (cursor, anchor)
    } else {
        (anchor, cursor)
    };

    for i in start..=end {
        if let Some(FlatItem::Task {
            section,
            path,
            is_context,
            ..
        }) = flat_items.get(i)
        {
            if *is_context {
                continue;
            }
            if let Some(task) = resolve_task_from_flat(track, *section, path)
                && let Some(id) = &task.id
            {
                app.selection.insert(id.clone());
            }
        }
    }

    app.range_anchor = None;
}

/// Select all visible (non-context, non-separator) tasks in the current track view.
pub(super) fn select_all(app: &mut App) {
    let track_id = match app.current_track_id() {
        Some(id) => id.to_string(),
        None => return,
    };
    let flat_items = app.build_flat_items(&track_id);
    let track = match App::find_track_in_project(&app.project, &track_id) {
        Some(t) => t,
        None => return,
    };

    app.selection.clear();
    for item in &flat_items {
        if let FlatItem::Task {
            section,
            path,
            is_context,
            ..
        } = item
        {
            if *is_context {
                continue;
            }
            if let Some(task) = resolve_task_from_flat(track, *section, path)
                && let Some(id) = &task.id
            {
                app.selection.insert(id.clone());
            }
        }
    }

    if !app.selection.is_empty() {
        app.mode = Mode::Select;
    }
}

/// Handle keys in SELECT mode.
pub(super) fn handle_select(app: &mut App, key: KeyEvent) {
    // Conflict popup intercepts Esc
    if app.conflict_text.is_some() {
        if matches!(key.code, KeyCode::Esc) {
            if let Some(ref text) = app.conflict_text {
                crate::io::recovery::log_recovery(
                    &app.project.frame_dir,
                    crate::io::recovery::RecoveryEntry {
                        timestamp: chrono::Utc::now(),
                        category: crate::io::recovery::RecoveryCategory::Conflict,
                        description: "dismissed conflict".to_string(),
                        fields: vec![],
                        body: text.clone(),
                    },
                );
            }
            app.conflict_text = None;
        }
        return;
    }

    // Help overlay intercepts ? and Esc, plus scroll keys
    if app.show_help {
        match key.code {
            KeyCode::Char('?') | KeyCode::Esc => {
                app.show_help = false;
            }
            KeyCode::Char('j') | KeyCode::Down => {
                app.help_scroll = app.help_scroll.saturating_add(1);
            }
            KeyCode::Char('k') | KeyCode::Up => {
                app.help_scroll = app.help_scroll.saturating_sub(1);
            }
            KeyCode::Char('g') => {
                app.help_scroll = 0;
            }
            KeyCode::Char('G') => {
                app.help_scroll = usize::MAX;
            }
            _ => {}
        }
        return;
    }

    // Clear any transient status message on keypress
    app.status_message = None;
    app.status_is_error = false;

    // Track consecutive Esc presses; show quit hint after 5
    if matches!(key.code, KeyCode::Esc) {
        app.esc_streak = app.esc_streak.saturating_add(1);
        if app.esc_streak >= 5 {
            app.status_message = Some("type QQ to quit".to_string());
        }
    } else {
        app.esc_streak = 0;
    }

    // QQ quit: second Q confirms, any other key cancels
    if app.quit_pending {
        if matches!(
            (key.modifiers, key.code),
            (KeyModifiers::SHIFT, KeyCode::Char('Q'))
        ) {
            app.should_quit = true;
            return;
        } else {
            app.quit_pending = false;
            return;
        }
    }

    match (key.modifiers, key.code) {
        // Quit: Ctrl+Q
        (m, KeyCode::Char('q')) if m.contains(KeyModifiers::CONTROL) => {
            app.should_quit = true;
        }

        // Quit: Q (first press shows confirmation)
        (KeyModifiers::SHIFT, KeyCode::Char('Q')) => {
            app.quit_pending = true;
            app.status_message = Some("press Q again to quit".to_string());
        }

        // Esc: cancel range mode first; then detail nav; then clear selection
        (_, KeyCode::Esc) => {
            if app.range_anchor.is_some() {
                app.range_anchor = None;
                return;
            }
            if let View::Detail { .. } = &app.view {
                // Return from detail view but keep selection
                if let Some((parent_track, parent_task)) = app.detail_stack.pop() {
                    let return_view = app
                        .detail_state
                        .as_ref()
                        .map(|ds| ds.return_view.clone())
                        .unwrap_or(crate::tui::app::ReturnView::Track(0));
                    app.detail_state = None;
                    app.view = View::Detail {
                        track_id: parent_track.clone(),
                        task_id: parent_task.clone(),
                    };
                    let regions = if let Some(track) =
                        App::find_track_in_project(&app.project, &parent_track)
                    {
                        if let Some(task) =
                            crate::ops::task_ops::find_task_in_track(track, &parent_task)
                        {
                            App::build_detail_regions(task)
                        } else {
                            vec![DetailRegion::Title]
                        }
                    } else {
                        vec![DetailRegion::Title]
                    };
                    let region = if regions.contains(&DetailRegion::Subtasks) {
                        DetailRegion::Subtasks
                    } else {
                        regions.first().copied().unwrap_or(DetailRegion::Title)
                    };
                    app.detail_state = Some(crate::tui::app::DetailState {
                        region,
                        scroll_offset: 0,
                        regions,
                        return_view,
                        editing: false,
                        edit_buffer: String::new(),
                        edit_cursor_line: 0,
                        edit_cursor_col: 0,
                        edit_original: String::new(),
                        subtask_cursor: 0,
                        flat_subtask_ids: Vec::new(),
                        multiline_selection_anchor: None,
                        note_h_scroll: 0,
                        sticky_col: None,
                        total_lines: 0,
                        note_view_line: None,
                        note_header_line: None,
                        note_content_end: 0,
                        regions_populated: Vec::new(),
                    });
                } else {
                    let return_view = app
                        .detail_state
                        .as_ref()
                        .map(|ds| ds.return_view.clone())
                        .unwrap_or(crate::tui::app::ReturnView::Track(0));
                    match return_view {
                        crate::tui::app::ReturnView::Track(idx) => app.view = View::Track(idx),
                        crate::tui::app::ReturnView::Recent => app.view = View::Recent,
                        crate::tui::app::ReturnView::Board => app.view = View::Board,
                    }
                    app.close_detail_fully();
                }
            } else {
                clear_selection(app);
            }
        }

        // v: toggle selection on cursor task
        (KeyModifiers::NONE, KeyCode::Char('v')) => {
            if let Some((_, task_id, _)) = app.cursor_task_id() {
                if app.selection.contains(&task_id) {
                    app.selection.remove(&task_id);
                    // Auto-exit if selection becomes empty
                    if app.selection.is_empty() {
                        app.mode = Mode::Navigate;
                    }
                } else {
                    app.selection.insert(task_id);
                }
            }
        }

        // V: toggle range select — if anchor active, finalize; otherwise begin
        (KeyModifiers::SHIFT, KeyCode::Char('V')) => {
            if app.range_anchor.is_some() {
                finalize_range_select(app);
            } else {
                begin_range_select(app);
            }
        }

        // Select none: N or Ctrl+Shift+A (must be before Ctrl+A)
        (KeyModifiers::SHIFT, KeyCode::Char('N')) => {
            clear_selection(app);
        }
        (m, KeyCode::Char('a'))
            if m.contains(KeyModifiers::CONTROL) && m.contains(KeyModifiers::SHIFT) =>
        {
            clear_selection(app);
        }
        (m, KeyCode::Char('A'))
            if m.contains(KeyModifiers::CONTROL) && m.contains(KeyModifiers::SHIFT) =>
        {
            clear_selection(app);
        }
        // Kitty protocol may report Ctrl+Shift+A as Char('A') with CONTROL only (shift implied by uppercase)
        (m, KeyCode::Char('A')) if m.contains(KeyModifiers::CONTROL) => {
            clear_selection(app);
        }

        // Select all: Ctrl+A or A (shift+a)
        (m, KeyCode::Char('a')) if m.contains(KeyModifiers::CONTROL) => {
            select_all(app);
        }
        (KeyModifiers::SHIFT, KeyCode::Char('A')) => {
            select_all(app);
        }

        // Cursor movement
        (KeyModifiers::NONE, KeyCode::Up | KeyCode::Char('k')) => {
            move_cursor(app, -1);
        }
        (KeyModifiers::NONE, KeyCode::Down | KeyCode::Char('j')) => {
            move_cursor(app, 1);
        }
        (KeyModifiers::NONE, KeyCode::Char('g')) => {
            jump_to_top(app);
        }
        (KeyModifiers::SHIFT, KeyCode::Char('G')) => {
            jump_to_bottom(app);
        }
        (_, KeyCode::Home) => {
            jump_to_top(app);
        }
        (_, KeyCode::End) => {
            jump_to_bottom(app);
        }

        // Expand/collapse
        (KeyModifiers::NONE, KeyCode::Right | KeyCode::Char('l')) => {
            expand_or_enter(app);
        }
        (KeyModifiers::NONE, KeyCode::Left | KeyCode::Char('h')) => {
            collapse_or_parent(app);
        }

        // Enter: open detail view (selection preserved)
        (KeyModifiers::NONE, KeyCode::Enter) => {
            // Temporarily switch to Navigate for the handler, then restore Select
            handle_enter(app);
            // Stay in Select mode (selection preserved across detail drill-in)
            if !matches!(app.view, View::Detail { .. }) {
                // If we didn't enter detail, stay in Select
                app.mode = Mode::Select;
            } else {
                // In detail view, mode stays Select so we return to Select on Esc
                app.mode = Mode::Select;
            }
        }

        // Bulk state changes
        (KeyModifiers::NONE, KeyCode::Char('x')) => {
            bulk_state_change(app, crate::model::TaskState::Done);
        }
        (KeyModifiers::NONE, KeyCode::Char('b')) => {
            bulk_state_change(app, crate::model::TaskState::Blocked);
        }
        (KeyModifiers::NONE, KeyCode::Char('o')) => {
            bulk_state_change(app, crate::model::TaskState::Todo);
        }
        (KeyModifiers::NONE, KeyCode::Char('~')) => {
            bulk_state_change(app, crate::model::TaskState::Parked);
        }

        // Bulk tagging
        (KeyModifiers::NONE, KeyCode::Char('t')) => {
            begin_bulk_tag_edit(app);
        }

        // Bulk dependency edit
        (KeyModifiers::NONE, KeyCode::Char('d')) => {
            begin_bulk_dep_edit(app);
        }

        // Bulk move within track
        (KeyModifiers::NONE, KeyCode::Char('m')) => {
            begin_bulk_move(app);
        }

        // Bulk move to track
        (KeyModifiers::SHIFT, KeyCode::Char('M')) => {
            begin_bulk_cross_track_move(app);
        }

        // Jump to task by ID (preserves selection)
        (KeyModifiers::SHIFT, KeyCode::Char('J')) => {
            begin_jump_to(app);
        }

        // Help overlay
        (KeyModifiers::NONE, KeyCode::Char('?')) => {
            app.show_help = true;
            app.help_scroll = 0;
        }

        // Search
        (KeyModifiers::NONE, KeyCode::Char('/')) => {
            // Allow searching while in select mode (mode will be restored)
            app.mode = Mode::Search;
            app.search_input.clear();
            app.search_draft.clear();
            app.search_history_index = None;
            app.search_wrap_message = None;
            app.search_match_count = None;
            app.search_zero_confirmed = false;
        }

        // Undo/redo
        (KeyModifiers::NONE, KeyCode::Char('u') | KeyCode::Char('z')) => {
            perform_undo(app);
        }
        (m, KeyCode::Char('z')) if m.contains(KeyModifiers::CONTROL) => {
            perform_undo(app);
        }
        (m, KeyCode::Char('z')) if m.contains(KeyModifiers::SUPER) => {
            perform_undo(app);
        }
        (m, KeyCode::Char('y')) if m.contains(KeyModifiers::CONTROL) => {
            perform_redo(app);
        }
        (m, KeyCode::Char('z') | KeyCode::Char('Z'))
            if m.contains(KeyModifiers::SUPER) && m.contains(KeyModifiers::SHIFT) =>
        {
            perform_redo(app);
        }
        (KeyModifiers::SHIFT, KeyCode::Char('Z')) => {
            perform_redo(app);
        }

        // Tab switching clears selection
        (KeyModifiers::NONE, KeyCode::Tab) => {
            clear_selection(app);
            switch_tab(app, 1);
        }
        (KeyModifiers::SHIFT, KeyCode::BackTab) => {
            clear_selection(app);
            switch_tab(app, -1);
        }
        (KeyModifiers::NONE, KeyCode::Char(c @ '1'..='9')) => {
            let idx = (c as usize) - ('1' as usize);
            if idx < app.active_track_ids.len() {
                clear_selection(app);
                app.view = View::Track(idx);
            }
        }
        (KeyModifiers::NONE, KeyCode::Char('i')) => {
            clear_selection(app);
            app.view = View::Inbox;
        }
        (KeyModifiers::NONE, KeyCode::Char('r')) => {
            clear_selection(app);
            app.view = View::Recent;
        }
        (KeyModifiers::NONE, KeyCode::Char('0') | KeyCode::Char('`')) => {
            clear_selection(app);
            app.tracks_name_col_min = 0;
            app.view = View::Tracks;
        }

        // Repeat last action: .
        (KeyModifiers::NONE, KeyCode::Char('.')) => {
            repeat_last_action(app);
        }

        // Quit: Ctrl+Q
        (m, KeyCode::Char('q')) if m.contains(KeyModifiers::CONTROL) => {
            app.should_quit = true;
        }

        _ => {}
    }
}

/// Apply an absolute state change to all selected tasks (B4).
pub(super) fn bulk_state_change(app: &mut App, target_state: crate::model::TaskState) {
    app.range_anchor = None;
    let track_id = match app.current_track_id() {
        Some(id) => id.to_string(),
        None => return,
    };
    let selected: Vec<String> = app.selection.iter().cloned().collect();
    if selected.is_empty() {
        return;
    }

    let mut ops: Vec<Operation> = Vec::new();
    let mut any_changed = false;

    for task_id in &selected {
        let track = match app.find_track_mut(&track_id) {
            Some(t) => t,
            None => continue,
        };
        let task = match task_ops::find_task_mut_in_track(track, task_id) {
            Some(t) => t,
            None => continue,
        };

        let old_state = task.state;
        if old_state == target_state {
            continue;
        }

        let old_resolved = task.metadata.iter().find_map(|m| {
            if let Metadata::Resolved(d) = m {
                Some(d.clone())
            } else {
                None
            }
        });

        // Apply the state change
        match target_state {
            crate::model::TaskState::Done => task_ops::set_done(task),
            crate::model::TaskState::Blocked => task_ops::set_blocked(task),
            crate::model::TaskState::Todo => {
                task_ops::set_state(task, crate::model::TaskState::Todo)
            }
            crate::model::TaskState::Parked => task_ops::set_parked(task),
            crate::model::TaskState::Active => {
                task_ops::set_state(task, crate::model::TaskState::Active)
            }
        }

        let new_state = task.state;
        let new_resolved = task.metadata.iter().find_map(|m| {
            if let Metadata::Resolved(d) = m {
                Some(d.clone())
            } else {
                None
            }
        });

        if old_state != new_state {
            // Cancel any pending ToDone move if un-doing Done
            if old_state == crate::model::TaskState::Done {
                app.cancel_pending_move(&track_id, task_id);
            }

            ops.push(Operation::StateChange {
                track_id: track_id.clone(),
                task_id: task_id.clone(),
                old_state,
                new_state,
                old_resolved,
                new_resolved,
            });

            // Schedule pending move if transitioning to Done
            if new_state == crate::model::TaskState::Done
                && let Some(track) = App::find_track_in_project(&app.project, &track_id)
                && task_ops::is_top_level_in_section(track, task_id, SectionKind::Backlog)
            {
                app.pending_moves.push(PendingMove {
                    kind: PendingMoveKind::ToDone,
                    track_id: track_id.clone(),
                    task_id: task_id.clone(),
                    deadline: std::time::Instant::now() + std::time::Duration::from_secs(5),
                    old_state: Some(old_state),
                });
            }

            any_changed = true;
        }
    }

    if any_changed {
        app.undo_stack.push(Operation::Bulk(ops));
        let _ = app.save_track(&track_id);

        // Record repeatable action
        app.last_action = Some(RepeatableAction::SetState(target_state));
    }
}

/// Open the inline editor for bulk tag editing (B5).
pub(super) fn begin_bulk_tag_edit(app: &mut App) {
    app.range_anchor = None;
    if app.selection.is_empty() {
        return;
    }
    app.edit_buffer = String::new();
    app.edit_cursor = 0;
    app.edit_selection_anchor = None;
    app.edit_target = Some(EditTarget::BulkTags);
    app.edit_history = Some(EditHistory::new("", 0, 0));

    // Activate tag autocomplete
    let candidates = app.collect_all_tags();
    if !candidates.is_empty() {
        let mut ac = AutocompleteState::new(AutocompleteKind::Tag, candidates);
        ac.filter("");
        app.autocomplete = Some(ac);
    }

    app.mode = Mode::Edit;
}

/// Confirm bulk tag edit: parse +tag/-tag tokens and apply to all selected tasks (B5).
pub(super) fn confirm_bulk_tag_edit(app: &mut App) {
    let buffer = app.edit_buffer.clone();
    let track_id = match app.current_track_id() {
        Some(id) => id.to_string(),
        None => return,
    };

    // Parse tokens: +tag adds, -tag removes, bare = add
    let (adds, removes) = parse_bulk_tokens(&buffer);
    if adds.is_empty() && removes.is_empty() {
        app.mode = Mode::Select;
        return;
    }

    let selected: Vec<String> = app.selection.iter().cloned().collect();
    let mut ops: Vec<Operation> = Vec::new();

    for task_id in &selected {
        let track = match App::find_track_in_project(&app.project, &track_id) {
            Some(t) => t,
            None => continue,
        };
        let task = match task_ops::find_task_in_track(track, task_id) {
            Some(t) => t,
            None => continue,
        };

        let old_tags = task
            .tags
            .iter()
            .map(|t| format!("#{}", t))
            .collect::<Vec<_>>()
            .join(" ");
        let mut new_tags = task.tags.clone();

        for tag in &adds {
            let clean = tag.strip_prefix('#').unwrap_or(tag).to_string();
            if !new_tags.contains(&clean) {
                new_tags.push(clean);
            }
        }
        for tag in &removes {
            let clean = tag.strip_prefix('#').unwrap_or(tag).to_string();
            new_tags.retain(|t| t != &clean);
        }

        let new_tags_str = new_tags
            .iter()
            .map(|t| format!("#{}", t))
            .collect::<Vec<_>>()
            .join(" ");
        if old_tags != new_tags_str {
            // Apply the change
            let track_mut = app.find_track_mut(&track_id).unwrap();
            let task_mut = task_ops::find_task_mut_in_track(track_mut, task_id).unwrap();
            task_mut.tags = new_tags;
            task_mut.mark_dirty();

            ops.push(Operation::FieldEdit {
                track_id: track_id.clone(),
                task_id: task_id.clone(),
                field: "tags".to_string(),
                old_value: old_tags,
                new_value: new_tags_str,
            });
        }
    }

    if !ops.is_empty() {
        app.undo_stack.push(Operation::Bulk(ops));
        let _ = app.save_track(&track_id);

        // Record repeatable action
        app.last_action = Some(RepeatableAction::TagEdit {
            adds: adds.clone(),
            removes: removes.clone(),
        });
    }

    app.mode = Mode::Select;
}

/// Open the inline editor for bulk dependency editing (B6).
pub(super) fn begin_bulk_dep_edit(app: &mut App) {
    app.range_anchor = None;
    if app.selection.is_empty() {
        return;
    }
    app.edit_buffer = String::new();
    app.edit_cursor = 0;
    app.edit_selection_anchor = None;
    app.edit_target = Some(EditTarget::BulkDeps);
    app.edit_history = Some(EditHistory::new("", 0, 0));

    // Activate task ID autocomplete
    let candidates = app.collect_all_task_ids();
    if !candidates.is_empty() {
        let mut ac = AutocompleteState::new(AutocompleteKind::TaskId, candidates);
        ac.filter("");
        app.autocomplete = Some(ac);
    }

    app.mode = Mode::Edit;
}

/// Confirm bulk dep edit: parse +ID/-ID tokens and apply to all selected tasks (B6).
pub(super) fn confirm_bulk_dep_edit(app: &mut App) {
    let buffer = app.edit_buffer.clone();
    let track_id = match app.current_track_id() {
        Some(id) => id.to_string(),
        None => return,
    };

    let (adds, removes) = parse_bulk_tokens(&buffer);
    if adds.is_empty() && removes.is_empty() {
        app.mode = Mode::Select;
        return;
    }

    let selected: Vec<String> = app.selection.iter().cloned().collect();
    let mut ops: Vec<Operation> = Vec::new();

    for task_id in &selected {
        let track = match App::find_track_in_project(&app.project, &track_id) {
            Some(t) => t,
            None => continue,
        };
        let task = match task_ops::find_task_in_track(track, task_id) {
            Some(t) => t,
            None => continue,
        };

        // Get current deps
        let old_deps: Vec<String> = task
            .metadata
            .iter()
            .find_map(|m| {
                if let Metadata::Dep(deps) = m {
                    Some(deps.clone())
                } else {
                    None
                }
            })
            .unwrap_or_default();
        let old_value = old_deps.join(", ");

        let mut new_deps = old_deps.clone();
        for dep in &adds {
            if !new_deps.contains(dep) {
                new_deps.push(dep.clone());
            }
        }
        for dep in &removes {
            new_deps.retain(|d| d != dep);
        }

        let new_value = new_deps.join(", ");
        if old_value != new_value {
            let track_mut = app.find_track_mut(&track_id).unwrap();
            let task_mut = task_ops::find_task_mut_in_track(track_mut, task_id).unwrap();
            task_mut.metadata.retain(|m| !matches!(m, Metadata::Dep(_)));
            if !new_deps.is_empty() {
                task_mut.metadata.push(Metadata::Dep(new_deps));
            }
            task_mut.mark_dirty();

            ops.push(Operation::FieldEdit {
                track_id: track_id.clone(),
                task_id: task_id.clone(),
                field: "deps".to_string(),
                old_value,
                new_value,
            });
        }
    }

    if !ops.is_empty() {
        app.undo_stack.push(Operation::Bulk(ops));
        let _ = app.save_track(&track_id);

        // Record repeatable action
        app.last_action = Some(RepeatableAction::DepEdit {
            adds: adds.clone(),
            removes: removes.clone(),
        });
    }

    app.mode = Mode::Select;
}

/// Parse a multi-token bulk edit string: "+foo -bar baz" → adds: [foo, baz], removes: [bar]
pub(super) fn parse_bulk_tokens(input: &str) -> (Vec<String>, Vec<String>) {
    let mut adds = Vec::new();
    let mut removes = Vec::new();
    for token in input.split_whitespace() {
        if let Some(tag) = token.strip_prefix('-') {
            let clean = tag.strip_prefix('#').unwrap_or(tag);
            if !clean.is_empty() {
                removes.push(clean.to_string());
            }
        } else if let Some(tag) = token.strip_prefix('+') {
            let clean = tag.strip_prefix('#').unwrap_or(tag);
            if !clean.is_empty() {
                adds.push(clean.to_string());
            }
        } else {
            let clean = token.strip_prefix('#').unwrap_or(token);
            if !clean.is_empty() {
                adds.push(clean.to_string());
            }
        }
    }
    (adds, removes)
}

/// Enter move mode for bulk-selected tasks (B7).
pub(super) fn begin_bulk_move(app: &mut App) {
    app.range_anchor = None;
    let track_id = match app.current_track_id() {
        Some(id) => id.to_string(),
        None => return,
    };
    if app.selection.is_empty() {
        return;
    }

    // Snapshot selected IDs before mutable borrow
    let selected_ids: Vec<String> = app.selection.iter().cloned().collect();
    let cursor = app.track_states.get(&track_id).map_or(0, |s| s.cursor);

    let track = match app.find_track_mut(&track_id) {
        Some(t) => t,
        None => return,
    };
    let backlog = match track.section_tasks_mut(SectionKind::Backlog) {
        Some(t) => t,
        None => return,
    };

    // Collect indices of selected top-level tasks
    let mut to_remove_indices: Vec<usize> = Vec::new();
    for (i, task) in backlog.iter().enumerate() {
        if let Some(id) = &task.id
            && selected_ids.contains(id)
        {
            to_remove_indices.push(i);
        }
    }

    if to_remove_indices.is_empty() {
        return;
    }

    // Remove tasks in reverse order to preserve indices
    let mut removed_tasks: Vec<(usize, crate::model::Task)> = Vec::new();
    for &idx in to_remove_indices.iter().rev() {
        let task = backlog.remove(idx);
        removed_tasks.push((idx, task));
    }
    removed_tasks.reverse(); // Restore original order

    // Determine initial insertion position (at the cursor's current position in the reduced list)
    let insert_pos = cursor.min(backlog.len());

    app.move_state = Some(MoveState::BulkTask {
        track_id: track_id.clone(),
        removed_tasks,
        insert_pos,
    });
    app.mode = Mode::Move;
}

pub(super) fn begin_bulk_cross_track_move(app: &mut App) {
    app.range_anchor = None;
    let source_track_id = match app.current_track_id() {
        Some(id) => id.to_string(),
        None => return,
    };
    if app.selection.is_empty() {
        return;
    }

    // Build candidate tracks: all non-archived tracks except current (show prefix)
    let candidates: Vec<String> = app
        .project
        .config
        .tracks
        .iter()
        .filter(|t| t.state != "archived" && t.id != source_track_id)
        .map(|t| {
            let prefix = app
                .project
                .config
                .ids
                .prefixes
                .get(&t.id)
                .map(|p| p.to_uppercase())
                .unwrap_or_else(|| t.id.to_uppercase());
            format!("{} ({})", t.name, prefix)
        })
        .collect();

    if candidates.is_empty() {
        app.status_message = Some("No other tracks to move to".to_string());
        return;
    }

    app.edit_buffer.clear();
    app.edit_cursor = 0;
    app.autocomplete = Some(AutocompleteState::new(AutocompleteKind::Tag, candidates));
    if let Some(ac) = &mut app.autocomplete {
        ac.filter("");
    }

    app.triage_state = Some(crate::tui::app::TriageState {
        source: TriageSource::BulkCrossTrackMove { source_track_id },
        step: crate::tui::app::TriageStep::SelectTrack,
        popup_anchor: None,
        position_cursor: 1, // default to Bottom
    });
    app.mode = Mode::Triage;
}

/// Move the bulk-move stand-in position up or down by one.
pub(super) fn move_bulk_standin(app: &mut App, direction: i32) {
    let (track_id, max_pos) = match &app.move_state {
        Some(MoveState::BulkTask { track_id, .. }) => {
            let tid = track_id.clone();
            let backlog_len = App::find_track_in_project(&app.project, &tid)
                .map(|t| t.backlog().len())
                .unwrap_or(0);
            (tid, backlog_len)
        }
        _ => return,
    };
    if let Some(MoveState::BulkTask { insert_pos, .. }) = &mut app.move_state {
        let new_pos = (*insert_pos as i32 + direction).clamp(0, max_pos as i32) as usize;
        *insert_pos = new_pos;
    }
    // Update cursor to track the stand-in position
    if let Some(MoveState::BulkTask { insert_pos, .. }) = &app.move_state {
        let pos = *insert_pos;
        if let Some(state) = app.track_states.get_mut(&track_id) {
            state.cursor = pos;
        }
    }
}

/// Move the bulk-move stand-in to the top or bottom of the backlog.
pub(super) fn move_bulk_standin_to_boundary(app: &mut App, to_top: bool) {
    let (track_id, max_pos) = match &app.move_state {
        Some(MoveState::BulkTask { track_id, .. }) => {
            let tid = track_id.clone();
            let backlog_len = App::find_track_in_project(&app.project, &tid)
                .map(|t| t.backlog().len())
                .unwrap_or(0);
            (tid, backlog_len)
        }
        _ => return,
    };
    let new_pos = if to_top { 0 } else { max_pos };
    if let Some(MoveState::BulkTask { insert_pos, .. }) = &mut app.move_state {
        *insert_pos = new_pos;
    }
    if let Some(state) = app.track_states.get_mut(&track_id) {
        state.cursor = new_pos;
    }
}