gitwig 2.1.1

a rust based tui, an alternative to sourcetree and gitui
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
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
//! Application state and the main run loop.
//!
//! `App` owns everything mutable about a session: the current config, where
//! to persist it back to, where the cursor is, what mode we're in, and any
//! transient status message. The drawing layer (`ui`) reads `App` but never
//! mutates it. Key handling (`input`) calls back into `App` methods so the
//! state-mutation logic stays in one place.

use std::error::Error;
use std::path::PathBuf;

use crossterm::event::{self, Event};
use ratatui::Terminal;
use ratatui::layout::{Margin, Rect};

use crate::config::{Config, SortOrder, save_config};
use crate::input;
use crate::repo::{self, ItemDetail, ItemStatus};
use crate::ui;
use crate::ui_detail::DetailAreas;

/// Height of each item row inside the bordered list area.
/// Borders (top + bottom) take 2 rows; the remaining 2 inner rows hold
/// the item path and the branch name respectively.
pub const ITEM_HEIGHT: u16 = 4;

/// What operation the remote picker was opened for.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum RemotePickerAction {
    PushBranch,
    PushTag,
    PushAllTags,
    DeleteRemoteTag,
    FetchRemote,
}

/// Interaction modes for the item list.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Mode {
    /// Browsing the list.
    Normal,
    #[allow(dead_code)]
    Adding,
    /// Typing replacement text for the selected item.
    Editing,
    /// Asking the user to confirm deletion of the selected item.
    ConfirmDelete,
    /// Showing the full shortcut reference as a centered overlay.
    Help,
    /// Showing the full-screen detail view for the selected item.
    Detail,
    /// Showing the shortcut reference overlay inside the detail view.
    DetailHelp,
    /// Typing a commit message.
    CommitInput,
    /// Typing a branch name to create.
    BranchCreateInput,
    /// Typing a tag name to create.
    TagCreateInput,
    /// Confirming deletion of a branch.
    BranchDeleteConfirm,
    /// Confirming push of a branch.
    BranchPushConfirm,
    /// Confirming deletion of a tag.
    TagDeleteConfirm,
    /// Confirming push of a tag.
    TagPushConfirm,
    /// Confirming push of all tags.
    TagPushAllConfirm,
    /// Confirming deletion of a stash.
    StashDeleteConfirm,
    /// Confirming apply of a stash.
    StashApplyConfirm,
    /// Typing a stash name/message to create.
    StashCreateInput,
    /// Showing the stashing UI panel with options and file list.
    StashingUI,
    /// Picking a remote when multiple are available.
    RemotePicker,
    /// Typing a search query for commits.
    #[allow(dead_code)]
    CommitSearchInput,
    /// Confirming merge of a branch.
    BranchMergeConfirm,
    /// Confirming rebase onto a branch.
    BranchRebaseConfirm,
    /// Confirming interactive rebase onto a branch.
    BranchInteractiveRebaseConfirm,
    /// Confirming discarding changes in a file.
    DiscardChangesConfirm,
    /// Inspecting a selected commit.
    Inspect,
    /// Settings page.
    Settings,
    /// Debug logs view.
    DebugLogs,
    /// Typing an import URL.
    ImportUrlInput,
    /// Typing an import destination path.
    ImportDestInput,
    /// Typing an import name.
    ImportNameInput,
    /// Typing a directory to bulk add its subdirectories.
    BulkAddInput,
    /// Choosing which columns to filter on.
    SearchColumnPicker,
    /// Typing a remote name to add.
    RemoteAddNameInput,
    /// Typing a remote URL to add.
    RemoteAddUrlInput,
    /// Confirming deletion of a remote.
    RemoteDeleteConfirm,
    /// logs UI with commits only.
    Logs,
    /// Search input in the logs UI.
    LogsSearchInput,
    /// Confirming checkout of a branch.
    BranchCheckoutConfirm,
    /// Confirming checkout of a tag.
    TagCheckoutConfirm,
    /// Search input for repositories on the home page.
    RepoSearchInput,
    /// Confirming aborting of a merge.
    MergeAbortConfirm,
    /// Confirming continuation of a merge.
    MergeContinueConfirm,
    /// Showing the about popup / creator profile.
    About,
    /// Confirming cherry-pick of a commit.
    CherryPickConfirm,
    /// Confirming revert of a commit.
    RevertConfirm,
    /// Per-file history view.
    FileHistory,
    /// Editing repository labels.
    LabelInput,
}

/// Which panel in the detail view currently has keyboard focus.
/// Tab cycles through them in order.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum DetailSection {
    Commits,
    Staged,
    Unstaged,
    Conflicts,
    CommitDetails,
    StagingDetails,
    ConflictDiff,
    LocalBranches,
    RemoteBranches,
    LocalTags,
    RemoteTags,
    Files,
    FileContent,
    Remotes,
    Stashes,
    StashedFiles,
}

/// Resizable splitter identifier.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Splitter {
    InspectHorizontal,  // Left panel vs Right (Diff) panel
    InspectVertical,    // Top sub-panel vs Bottom sub-panel in left panel
    WorkspaceMain,      // Commits list vs staging/files details (vertical split, top/bottom)
    FilesHorizontal,    // Files view: left (tree) vs right (preview)
    BranchesHorizontal, // Branches view: left (local) vs right (remote)
    StashesHorizontal,  // Stashes view: left (lists) vs right (diff)
    StashesVertical,    // Stashes view: top list vs bottom files list
    OverviewHorizontal, // Overview view: left (info) vs right (stats)
    CommitPopupWidth,   // Dragging vertical border of commit popup
    CommitPopupHeight,  // Dragging horizontal border of commit popup
    CommitPopupBoth,    // Dragging corner of commit popup
}

impl DetailSection {
    /// Advance to the next section in the cycle.
    pub fn next(self) -> Self {
        match self {
            Self::Commits => Self::Staged,
            Self::Staged => Self::Unstaged,
            Self::Unstaged => Self::Conflicts,
            Self::Conflicts => Self::CommitDetails,
            Self::CommitDetails => Self::StagingDetails,
            Self::StagingDetails => Self::ConflictDiff,
            Self::ConflictDiff => Self::Commits,
            Self::LocalBranches => Self::RemoteBranches,
            Self::RemoteBranches => Self::LocalBranches,
            Self::LocalTags => Self::RemoteTags,
            Self::RemoteTags => Self::LocalTags,
            Self::Files => Self::FileContent,
            Self::FileContent => Self::Files,
            Self::Remotes => Self::Remotes,
            Self::Stashes => Self::Stashes,
            Self::StashedFiles => Self::StashedFiles,
        }
    }

    /// Move back to the previous section in the cycle.
    pub fn prev(self) -> Self {
        match self {
            Self::Commits => Self::ConflictDiff,
            Self::Staged => Self::Commits,
            Self::Unstaged => Self::Staged,
            Self::Conflicts => Self::Unstaged,
            Self::CommitDetails => Self::Conflicts,
            Self::StagingDetails => Self::CommitDetails,
            Self::ConflictDiff => Self::StagingDetails,
            Self::LocalBranches => Self::RemoteBranches,
            Self::RemoteBranches => Self::LocalBranches,
            Self::LocalTags => Self::RemoteTags,
            Self::RemoteTags => Self::LocalTags,
            Self::Files => Self::FileContent,
            Self::FileContent => Self::Files,
            Self::Remotes => Self::Remotes,
            Self::Stashes => Self::Stashes,
            Self::StashedFiles => Self::StashedFiles,
        }
    }
}

#[derive(Debug, Clone)]
pub struct DetailCache {
    pub detail: repo::ItemDetail,
    pub loaded_at: std::time::Instant,
}

/// All mutable session state.
pub struct App {
    pub config: Config,
    pub config_path: PathBuf,
    /// Filesystem classification per item, parallel to `config.items`.
    /// Recomputed on add/edit/delete so it never drifts from the list.
    pub statuses: Vec<ItemStatus>,
    pub selected_index: usize,
    pub scroll_top: usize,
    pub mode: Mode,
    pub input_buffer: String,
    pub status_message: Option<String>,
    pub error_message: Option<String>,
    pub current_detail: Option<ItemDetail>,
    /// Cache of repository detail views mapped by their path.
    pub detail_cache: std::collections::HashMap<String, DetailCache>,
    /// Which panel is focused inside the detail view.
    pub detail_focus: DetailSection,
    pub queue: crate::queue::Queue,
    pub file_tree: crate::components::file_tree::FileTreeComponent,
    pub branch_list: crate::components::branch_list::BranchListComponent,
    pub tag_list: crate::components::tag_list::TagListComponent,
    pub stash_list: crate::components::stash_list::StashListComponent,
    /// Selected row index inside the Commits panel (0 = top row).
    pub commit_list: crate::components::commit_list::CommitListComponent,
    pub commit_popup: crate::popups::commit::CommitPopup,
    pub confirm_popup: crate::popups::confirm::ConfirmPopup,
    pub generic_input_popup: crate::popups::commit::GenericInputPopup,
    /// Dynamic commit limit for pagination

    /// Active query for filtering commits in the commits panel

    /// Active query for filtering repositories in the home page list
    pub repo_search_query: Option<String>,
    /// Selected file index inside the Changed Files panel (real commits).

    /// Selected file index inside the Staged/Unstaged sub-panels (uncommitted view).

    /// Cached unified-diff lines for the currently selected file.
    pub diff: crate::components::diff::DiffComponent,
    /// Vertical scroll offset for the diff panel (StagingDetails focus).

    /// Selected hunk index for stage/unstage by hunk (StagingDetails focus).

    /// Whether we are selecting lines (true) or hunks (false) in StagingDetails.

    /// Selected line index in the file_diff.

    /// Selected conflict file index in Conflicts panel.

    /// Vertical scroll offset for the commit details panel (CommitDetails focus).

    /// Vertical scroll offset for the commit input popup.
    pub commit_input_scroll: usize,
    /// Selected local branch index in Branches tab.
    /// Selected remote branch index in Branches tab.
    /// Selected local tag index in Tags/Branches tabs.
    /// Selected remote tag index in Tags/Branches tabs.
    /// Selected remote index in Remotes tab.
    /// Selected stash index in Stashes tab.
    /// Selected file index in the Stashes tab stashed files list.
    /// Scroll offset for the help overlays.
    pub help_scroll: usize,
    /// Panel bounding boxes recorded after each draw, used for mouse hit-testing.
    pub detail_areas: DetailAreas,
    /// Main panel item bounding boxes recorded after each draw, used for mouse hit-testing.
    pub main_areas: Vec<Rect>,

    pub status_list: crate::components::status_list::StatusListComponent,

    /// Timestamp and selected index of the last mouse click for double-click detection.
    pub last_click: Option<(std::time::Instant, usize)>,
    /// Active tab in the detail view (0 = Details, 1 = Graph, 2 = Branches, 3 = Files).
    pub detail_tab: usize,
    /// Selected file index in the Files tab.
    /// Vertical scroll offset for the file content preview in Files tab.
    /// Set of expanded folder paths.
    /// Flattened visible files inside the Files tab.
    /// Vertical scroll offset for the git history graph view (Graph tab).
    pub graph_scroll: usize,
    /// Whether the status bar is expanded.
    pub status_expanded: bool,
    /// Whether the settings panel focus is on the sidebar categories.
    pub settings_focus_sidebar: bool,
    /// Sender for background task events.
    pub tx: std::sync::mpsc::Sender<String>,
    /// Receiver for background task events.
    pub rx: std::sync::mpsc::Receiver<String>,
    /// Whether a background fetch is active.
    pub fetching: bool,
    /// Whether external Git application launch is pending.
    pub pending_git_app: bool,
    /// Whether fzf search launch is pending.
    pub pending_fzf: bool,
    /// Whether bulk fzf search launch is pending.
    pub pending_bulk_fzf: bool,
    /// Whether fzf files search launch is pending.
    pub pending_files_fzf: bool,
    /// Whether interactive rebase is pending.
    pub pending_interactive_rebase: Option<(PathBuf, String)>,
    /// Whether we are currently viewing logs UI.
    pub in_logs_ui: bool,
    /// Whether we are in full-screen diff mode under inspect view.
    pub inspect_full_diff: bool,
    /// Selection in search column picker.
    pub search_column_selection: usize,
    /// Columns to include in search.
    pub search_columns_sha: bool,
    pub search_columns_message: bool,
    pub search_columns_author: bool,
    pub search_columns_date: bool,
    /// Target branch name and remote flag for deletion/creation actions.
    pub branch_action_target: Option<(String, bool)>,
    /// Target commit OID for tag creation.
    pub tag_action_target_oid: Option<String>,
    /// Target tag name and remote flag for deletion action.
    pub tag_delete_target: Option<(String, bool)>,
    /// Target tag name for checkout action.
    pub tag_checkout_target: Option<String>,
    /// Target tag name for push action.
    pub tag_push_target: Option<String>,
    /// Target file path and staged flag for discard/revert action.
    pub discard_target: Option<(String, bool)>,
    /// Target commit (hash, summary) for cherry-pick.
    pub cherry_pick_target: Option<(String, String)>,
    /// Selected destination branch index for the cherry-pick popup.
    pub cherry_pick_dest_selection: usize,
    /// List of local branch names available for cherry-pick destination.
    pub cherry_pick_dest_branches: Vec<String>,
    /// Target commit (hash, summary) for revert.
    pub revert_target: Option<(String, String)>,
    /// Simulated fetch progress percentage.
    pub fetch_progress: u16,
    /// Option to delete the stash after applying.
    pub stash_apply_delete_after: bool,
    /// Option to stash untracked files.
    pub stash_untracked: bool,
    /// Option to keep the index after stashing.
    pub stash_keep_index: bool,
    /// Selection index in the Stashing UI file list.
    pub stashing_ui_selection: usize,
    /// Preserved original order of repository items from the config.
    pub original_items: Vec<String>,
    /// Which action the remote picker was opened for.
    pub remote_picker_action: Option<RemotePickerAction>,
    /// Selected row in the remote picker popup.
    pub remote_picker_selection: usize,
    /// Percentage width of the left panel in the Inspect view (default: 40).
    pub inspect_horizontal_split_pct: u16,
    /// Percentage height of the top left sub-panel in the Inspect view (default: 50).
    pub inspect_vertical_split_pct: u16,
    /// Percentage height of the commits list in the Workspace tab (default: 50).
    pub workspace_main_split_pct: u16,
    /// Percentage width of the files tree in the Files tab (default: 45).
    pub files_horizontal_split_pct: u16,
    /// Percentage width of the local branches list in the Branches tab (default: 50).
    pub branches_horizontal_split_pct: u16,
    /// Percentage width of the left list column in the Stashes tab (default: 35).
    pub stashes_horizontal_split_pct: u16,
    /// Percentage height of the top stashes list in the Stashes tab (default: 50).
    pub stashes_vertical_split_pct: u16,
    /// Percentage width of the left overview panel in the Overview tab (default: 50).
    pub overview_horizontal_split_pct: u16,
    /// Percentage width of the commit message popup (default: 80).
    pub commit_popup_width_pct: u16,
    /// Percentage height of the commit message popup (default: 45).
    pub commit_popup_height_pct: u16,
    /// Active drag splitter if dragging is in progress.
    pub active_drag_splitter: Option<Splitter>,
    pub settings_selected_index: usize,
    pub settings_editing: bool,
    pub settings_theme_list: Vec<String>,
    pub settings_theme_index: usize,
    pub debug_log_scroll: usize,
    pub import_url: String,
    pub import_dest: String,
    pub import_name: String,
    pub remote_add_name: String,
    pub remote_add_url: String,
    pub remote_action_target: Option<String>,
    pub last_staging_focus: DetailSection,
    pub force_fzf_missing: Option<bool>,
    pub loading_repo_path: Option<String>,
    pub detail_tx: std::sync::mpsc::Sender<(String, repo::ItemDetail)>,
    pub detail_rx: std::sync::mpsc::Receiver<(String, repo::ItemDetail)>,
    pub tab_tx: std::sync::mpsc::Sender<(String, usize, repo::TabPayload)>,
    pub tab_rx: std::sync::mpsc::Receiver<(String, usize, repo::TabPayload)>,
    pub file_history_revisions: Vec<repo::FileRevision>,
    pub file_history_selection: usize,
    pub file_history_diff: Vec<repo::DiffLine>,
    pub file_history_diff_scroll: usize,
    pub file_history_path: String,
    pub file_history_focus: usize,
    pub cpu_tracker: std::sync::Mutex<Option<(f64, std::time::Instant, f64, f64)>>,
    pub watcher: Option<notify::RecommendedWatcher>,
}

#[derive(Clone, Debug)]
pub struct FileTreeItem {
    pub name: String,
    pub full_path: String,
    pub is_dir: bool,
    pub depth: usize,
    pub is_expanded: bool,
}

struct TempNode {
    name: String,
    full_path: String,
    is_dir: bool,
    children: std::collections::BTreeMap<String, TempNode>,
}

enum LogsNavDirection {
    Up,
    Down,
    PageUp(usize),
    PageDown(usize),
}

mod actions;
mod git;
mod navigation;
#[cfg(test)]
mod tests;
mod workspace;

impl App {
    pub fn setup_watcher(&mut self) {
        use notify::{RecursiveMode, Watcher};

        self.watcher = None;

        let tx = self.tx.clone();
        let mut watcher =
            match notify::recommended_watcher(move |res: Result<notify::Event, notify::Error>| {
                if let Ok(event) = res {
                    for path in event.paths {
                        let path_str = path.to_string_lossy();
                        let clean_path =
                            path_str.replace("\\.git\\", "/.git/").replace("\\.git", "/.git");
                        if let Some(pos) = clean_path.find("/.git") {
                            let repo_root = &clean_path[..pos];
                            if !path_str.ends_with(".lock")
                                && (path_str.contains("/.git/refs/")
                                    || path_str.ends_with("/.git/index")
                                    || path_str.ends_with("/.git/HEAD"))
                            {
                                let _ = tx.send(format!("REFRESH_REPO:{}", repo_root));
                            }
                        }
                    }
                }
            }) {
                Ok(w) => w,
                Err(e) => {
                    crate::debug_log::warn(format!("Failed to initialize file watcher: {}", e));
                    return;
                }
            };

        for item in &self.config.items {
            let canon = match std::fs::canonicalize(item) {
                Ok(c) => c,
                Err(_) => PathBuf::from(item),
            };
            let git_dir = canon.join(".git");
            if git_dir.exists() && git_dir.is_dir() {
                if let Err(e) = watcher.watch(&git_dir, RecursiveMode::Recursive) {
                    crate::debug_log::warn(format!(
                        "Failed to watch repository {:?}: {}",
                        git_dir, e
                    ));
                }
            }
        }

        self.watcher = Some(watcher);
    }

    pub fn drain_queue(&mut self) {
        while let Some(ev) = self.queue.pop() {
            match ev {
                crate::queue::InternalEvent::ClosePopup => self.mode = Mode::Detail,

                crate::queue::InternalEvent::ConfirmYes => match self.mode {
                    Mode::BranchDeleteConfirm => self.confirm_branch_delete(),
                    Mode::BranchPushConfirm => self.confirm_branch_push(),
                    Mode::BranchMergeConfirm => self.confirm_branch_merge(),
                    Mode::MergeAbortConfirm => self.confirm_abort_merge(),
                    Mode::MergeContinueConfirm => self.confirm_continue_merge(),
                    Mode::BranchRebaseConfirm => self.confirm_branch_rebase(),
                    Mode::BranchInteractiveRebaseConfirm => {
                        self.confirm_branch_interactive_rebase()
                    }
                    Mode::DiscardChangesConfirm => self.confirm_discard_changes(),
                    Mode::RevertConfirm => self.confirm_revert(),
                    Mode::TagDeleteConfirm => self.confirm_tag_delete(),
                    Mode::TagPushConfirm => self.confirm_tag_push(),
                    Mode::TagPushAllConfirm => self.confirm_tag_push_all(),
                    Mode::StashDeleteConfirm => self.confirm_stash_delete(),
                    Mode::BranchCheckoutConfirm => self.confirm_branch_checkout(),
                    Mode::TagCheckoutConfirm => self.confirm_tag_checkout(),
                    Mode::RemoteDeleteConfirm => self.confirm_remote_delete(),
                    _ => {}
                },
                crate::queue::InternalEvent::ConfirmNo => match self.mode {
                    Mode::BranchDeleteConfirm => self.cancel_branch_delete(),
                    Mode::BranchPushConfirm => self.cancel_branch_push(),
                    Mode::BranchMergeConfirm => self.cancel_branch_merge(),
                    Mode::MergeAbortConfirm => {
                        self.mode = Mode::Detail;
                    }
                    Mode::MergeContinueConfirm => {
                        self.mode = Mode::Detail;
                    }
                    Mode::BranchRebaseConfirm => self.cancel_branch_rebase(),
                    Mode::BranchInteractiveRebaseConfirm => self.cancel_branch_interactive_rebase(),
                    Mode::DiscardChangesConfirm => self.cancel_discard_changes(),
                    Mode::RevertConfirm => self.cancel_revert(),
                    Mode::TagDeleteConfirm => self.cancel_tag_delete(),
                    Mode::TagPushConfirm => self.cancel_tag_push(),
                    Mode::TagPushAllConfirm => self.cancel_tag_push_all(),
                    Mode::StashDeleteConfirm => self.cancel_stash_delete(),
                    Mode::BranchCheckoutConfirm => self.cancel_branch_checkout(),
                    Mode::TagCheckoutConfirm => self.cancel_tag_checkout(),
                    Mode::RemoteDeleteConfirm => {
                        self.remote_action_target = None;
                        self.mode = Mode::Detail;
                    }
                    _ => {
                        self.mode = Mode::Detail;
                    }
                },
                crate::queue::InternalEvent::InputChar(c) => self.input_char(c),
                crate::queue::InternalEvent::InputBackspace => self.input_backspace(),
                crate::queue::InternalEvent::InputEnter => match self.mode {
                    Mode::BranchCreateInput => self.commit_branch_create(),
                    Mode::TagCreateInput => self.commit_tag_create(),
                    Mode::StashCreateInput => self.commit_stash_create(),
                    Mode::RemoteAddNameInput => self.commit_remote_add_name(),
                    Mode::RemoteAddUrlInput => self.commit_remote_add_url(),
                    _ => {}
                },
                crate::queue::InternalEvent::InputEsc => match self.mode {
                    Mode::BranchCreateInput => self.cancel_branch_create(),
                    Mode::TagCreateInput => {
                        self.tag_action_target_oid = None;
                        self.mode = Mode::Detail;
                    }
                    Mode::StashCreateInput => {
                        self.mode = Mode::Detail;
                    }
                    Mode::RemoteAddNameInput => {
                        self.mode = Mode::Detail;
                    }
                    Mode::RemoteAddUrlInput => {
                        self.mode = Mode::Detail;
                    }
                    _ => {
                        self.mode = Mode::Detail;
                    }
                },
                // simplified
                crate::queue::InternalEvent::Commit => {
                    self.commit_git_changes();
                }
                crate::queue::InternalEvent::SearchColumnPicker => {
                    self.search_column_selection = 0;
                    self.mode = Mode::SearchColumnPicker;
                }
                crate::queue::InternalEvent::StartCommit => self.start_commit(),
                crate::queue::InternalEvent::StartCommitAmend => self.start_commit_amend(),
                crate::queue::InternalEvent::StartTagCreate => self.start_tag_create(),
                crate::queue::InternalEvent::RunInteractiveRebase => self.run_interactive_rebase(),
                crate::queue::InternalEvent::RequestCherryPick => self.request_cherry_pick(),
                crate::queue::InternalEvent::YankSelectedCommitHash => {
                    self.yank_selected_commit_hash()
                }
                crate::queue::InternalEvent::RequestRevert => self.request_revert(),
                crate::queue::InternalEvent::InspectCommit => {
                    self.mode = Mode::Inspect;
                    if self.is_uncommitted_selected() {
                        self.detail_focus = DetailSection::Staged;
                        self.last_staging_focus = DetailSection::Staged;
                        self.status_list.staging_file_selection = 0;
                    } else {
                        self.detail_focus = DetailSection::Staged;
                        self.last_staging_focus = DetailSection::Staged;
                        self.status_list.file_selection = 0;
                    }
                    self.diff.diff_scroll = 0;
                    self.refresh_file_diff();
                }
                crate::queue::InternalEvent::CommitSelectionUp => self.detail_commit_up(),
                crate::queue::InternalEvent::CommitSelectionDown => self.detail_commit_down(),
                crate::queue::InternalEvent::CommitSelectionPageUp => {
                    let page = self.config.page_size;
                    self.detail_commit_page_up(page);
                }

                crate::queue::InternalEvent::CommitSelectionTop => self.detail_commit_to_top(),
                crate::queue::InternalEvent::CommitSelectionBottom => {
                    self.detail_commit_to_bottom()
                }
                crate::queue::InternalEvent::LoadMoreCommits => {
                    if self.commit_list.limit > 0 {
                        let add_amount =
                            if self.config.max_commits > 0 { self.config.max_commits } else { 200 };
                        self.commit_list.limit = self.commit_list.limit.saturating_add(add_amount);
                        self.resync_detail();
                        self.status_message = Some("Loading more commits...".to_string());
                    }
                }
                crate::queue::InternalEvent::CommitDetailsUp => {
                    self.commit_list.details_scroll_up()
                }
                crate::queue::InternalEvent::CommitDetailsDown => {
                    self.commit_list.details_scroll_down()
                }
                crate::queue::InternalEvent::StagingFileUp => {
                    if self.is_uncommitted_selected() {
                        self.staging_file_up()
                    } else {
                        self.detail_file_up()
                    }
                }
                crate::queue::InternalEvent::StagingFileDown => {
                    if self.is_uncommitted_selected() {
                        self.staging_file_down()
                    } else {
                        self.detail_file_down()
                    }
                }
                crate::queue::InternalEvent::ConflictFileUp => self.conflict_file_up(),
                crate::queue::InternalEvent::ConflictFileDown => self.conflict_file_down(),
                crate::queue::InternalEvent::StageSelectedFile => self.stage_selected_file(),
                crate::queue::InternalEvent::UnstageSelectedFile => self.unstage_selected_file(),
                crate::queue::InternalEvent::ResolveConflictOurs => self.resolve_conflict_ours(),
                crate::queue::InternalEvent::ResolveConflictTheirs => {
                    self.resolve_conflict_theirs()
                }
                crate::queue::InternalEvent::MarkConflictResolved => self.mark_conflict_resolved(),
                crate::queue::InternalEvent::MergeAbortConfirm => {
                    self.mode = Mode::MergeAbortConfirm
                }
                crate::queue::InternalEvent::MergeContinueConfirm => {
                    self.mode = Mode::MergeContinueConfirm
                }
                crate::queue::InternalEvent::StageSelectedHunk => self.stage_selected_hunk(),
                crate::queue::InternalEvent::UnstageSelectedHunk => self.unstage_selected_hunk(),
                crate::queue::InternalEvent::StageAllChanges => self.stage_all_changes(),
                crate::queue::InternalEvent::UnstageAllChanges => self.unstage_all_changes(),
                crate::queue::InternalEvent::RequestDiscardChanges => {
                    self.request_discard_changes()
                }
                crate::queue::InternalEvent::RequestDiscardAllChanges => {
                    self.request_discard_all_changes()
                }
                crate::queue::InternalEvent::StartStashCreate => self.start_stash_create(),
                crate::queue::InternalEvent::DiffScrollUp => self.diff.diff_scroll_up(),
                crate::queue::InternalEvent::DiffScrollDown => self.diff.diff_scroll_down(),
                crate::queue::InternalEvent::DiffScrollPageUp => {
                    let page = self.config.page_size;
                    self.diff.diff_scroll_page_up(page);
                }
                crate::queue::InternalEvent::DiffScrollPageDown => {
                    let page = self.config.page_size;
                    self.diff.diff_scroll_page_down(page);
                }
                crate::queue::InternalEvent::DiffScrollTop => self.diff.diff_scroll_to_top(),
                crate::queue::InternalEvent::DiffScrollBottom => self.diff.diff_scroll_to_bottom(),

                // FileTree
                crate::queue::InternalEvent::FileTreeUp => self.file_list_up(),
                crate::queue::InternalEvent::FileTreeDown => self.file_list_down(),
                crate::queue::InternalEvent::FileTreePageUp => {
                    let p = self.config.page_size;
                    self.file_list_page_up(p)
                }
                crate::queue::InternalEvent::FileTreePageDown => {
                    let p = self.config.page_size;
                    self.file_list_page_down(p)
                }
                crate::queue::InternalEvent::FileTreeTop => self.file_list_to_top(),
                crate::queue::InternalEvent::FileTreeBottom => self.file_list_to_bottom(),
                crate::queue::InternalEvent::FileContentUp => {
                    self.file_tree.file_content_scroll_up()
                }
                crate::queue::InternalEvent::FileContentDown => {
                    self.file_tree.file_content_scroll_down()
                }
                crate::queue::InternalEvent::FileContentPageUp => {
                    let p = self.config.page_size;
                    self.file_tree.file_content_scroll_page_up(p)
                }
                crate::queue::InternalEvent::FileContentPageDown => {
                    let p = self.config.page_size;
                    self.file_tree.file_content_scroll_page_down(p)
                }
                crate::queue::InternalEvent::FileContentTop => {
                    self.file_tree.file_content_scroll_to_top()
                }
                crate::queue::InternalEvent::FileContentBottom => {
                    self.file_tree.file_content_scroll_to_bottom()
                }
                crate::queue::InternalEvent::ToggleFolderExpanded => self.toggle_folder_expanded(),
                crate::queue::InternalEvent::CollapseAllFolders => self.collapse_all_folders(),
                crate::queue::InternalEvent::RequestDiscardFile => self.request_discard_changes(),

                // BranchList
                crate::queue::InternalEvent::LocalBranchUp => self.local_branch_up(),
                crate::queue::InternalEvent::LocalBranchDown => self.local_branch_down(),
                crate::queue::InternalEvent::LocalBranchPageUp => {
                    let p = self.config.page_size;
                    self.local_branch_page_up(p)
                }
                crate::queue::InternalEvent::LocalBranchPageDown => {
                    let p = self.config.page_size;
                    self.local_branch_page_down(p)
                }
                crate::queue::InternalEvent::LocalBranchTop => self.local_branch_to_top(),
                crate::queue::InternalEvent::LocalBranchBottom => self.local_branch_to_bottom(),
                crate::queue::InternalEvent::RemoteBranchUp => self.remote_branch_up(),
                crate::queue::InternalEvent::RemoteBranchDown => self.remote_branch_down(),
                crate::queue::InternalEvent::RemoteBranchPageUp => {
                    let p = self.config.page_size;
                    self.remote_branch_page_up(p)
                }
                crate::queue::InternalEvent::RemoteBranchPageDown => {
                    let p = self.config.page_size;
                    self.remote_branch_page_down(p)
                }
                crate::queue::InternalEvent::RemoteBranchTop => self.remote_branch_to_top(),
                crate::queue::InternalEvent::RemoteBranchBottom => self.remote_branch_to_bottom(),
                crate::queue::InternalEvent::CheckoutBranch => self.request_branch_checkout(),
                crate::queue::InternalEvent::RequestDeleteBranch => self.request_branch_delete(),
                crate::queue::InternalEvent::StartBranchCreate => self.start_branch_create(),
                crate::queue::InternalEvent::StartBranchMerge => self.request_branch_merge(),
                crate::queue::InternalEvent::StartBranchRebase => self.request_branch_rebase(),
                crate::queue::InternalEvent::RequestBranchPush => self.request_branch_push(),
                crate::queue::InternalEvent::FetchRemote => {
                    let remote_name = if let Some(crate::repo::ItemDetail::Repo { info, .. }) =
                        &self.current_detail
                    {
                        info.remotes
                            .get(self.branch_list.remote_selection)
                            .or_else(|| info.remotes.first())
                            .map(|r| r.name.clone())
                    } else {
                        None
                    };
                    if let Some(name) = remote_name {
                        self.fetch_remote(&name);
                    }
                }
                crate::queue::InternalEvent::StartRemoteAdd => self.start_remote_add(),
                crate::queue::InternalEvent::RequestDeleteRemote => self.request_remote_delete(),

                // TagList
                crate::queue::InternalEvent::TagUp => self.local_tag_up(),
                crate::queue::InternalEvent::TagDown => self.local_tag_down(),
                crate::queue::InternalEvent::TagPageUp => {
                    let p = self.config.page_size;
                    self.local_tag_page_up(p)
                }
                crate::queue::InternalEvent::TagPageDown => {
                    let p = self.config.page_size;
                    self.local_tag_page_down(p)
                }
                crate::queue::InternalEvent::TagTop => self.local_tag_to_top(),
                crate::queue::InternalEvent::TagBottom => self.local_tag_to_bottom(),
                crate::queue::InternalEvent::CheckoutTag => self.request_tag_checkout(),
                crate::queue::InternalEvent::RequestDeleteTag => self.request_tag_delete(),
                crate::queue::InternalEvent::RequestPushTag => self.request_tag_push(),
                crate::queue::InternalEvent::RequestPushAllTags => self.request_tag_push_all(),
                crate::queue::InternalEvent::FetchRemoteTags => self.fetch_remote_tags(true),

                // StashList
                crate::queue::InternalEvent::StashUp => self.stash_up(),
                crate::queue::InternalEvent::StashDown => self.stash_down(),
                crate::queue::InternalEvent::StashPageUp => {
                    let p = self.config.page_size;
                    self.stash_page_up(p)
                }
                crate::queue::InternalEvent::StashPageDown => {
                    let p = self.config.page_size;
                    self.stash_page_down(p)
                }
                crate::queue::InternalEvent::StashTop => self.stash_to_top(),
                crate::queue::InternalEvent::StashBottom => self.stash_to_bottom(),
                crate::queue::InternalEvent::StashFileUp => self.stash_file_up(),
                crate::queue::InternalEvent::StashFileDown => self.stash_file_down(),
                crate::queue::InternalEvent::StashFilePageUp => {
                    let p = self.config.page_size;
                    self.stash_file_page_up(p)
                }
                crate::queue::InternalEvent::StashFilePageDown => {
                    let p = self.config.page_size;
                    self.stash_file_page_down(p)
                }
                crate::queue::InternalEvent::StashFileTop => self.stash_file_to_top(),
                crate::queue::InternalEvent::StashFileBottom => self.stash_file_to_bottom(),
                crate::queue::InternalEvent::RequestDeleteStash => self.request_stash_delete(),
                crate::queue::InternalEvent::RequestApplyStash => self.request_stash_apply(),

                crate::queue::InternalEvent::CommitSelectionPageDown => {
                    let page = self.config.page_size;
                    self.detail_commit_page_down(page);
                }
                _ => {}
            }
        }
    }

    pub fn sym(&self, key: &str) -> &'static str {
        self.config.sym(key)
    }

    pub fn new(config: Config, config_path: PathBuf) -> Self {
        crate::debug_log::info("Initializing Gitwig application state");
        crate::ui::update_theme(&config.theme);
        let original_items = config.items.clone();
        let max_commits = config.max_commits;
        let statuses = config.items.iter().map(|s| repo::inspect_summary(s)).collect();
        let (tx, rx) = std::sync::mpsc::channel();
        let (detail_tx, detail_rx) = std::sync::mpsc::channel();
        let (tab_tx, tab_rx) = std::sync::mpsc::channel();
        let queue = crate::queue::Queue::default();
        let mut app = Self {
            queue: queue.clone(),
            original_items,
            config,
            config_path,
            statuses,
            selected_index: 0,
            scroll_top: 0,
            mode: Mode::Normal,
            input_buffer: String::new(),
            status_message: None,
            error_message: None,
            current_detail: None,
            detail_cache: std::collections::HashMap::new(),
            detail_focus: DetailSection::Commits,
            file_tree: crate::components::file_tree::FileTreeComponent::new(queue.clone()),
            branch_list: crate::components::branch_list::BranchListComponent::new(queue.clone()),
            tag_list: crate::components::tag_list::TagListComponent::new(queue.clone()),
            stash_list: crate::components::stash_list::StashListComponent::new(queue.clone()),
            commit_list: crate::components::commit_list::CommitListComponent {
                limit: max_commits,
                queue: queue.clone(),
                ..Default::default()
            },
            commit_popup: crate::popups::commit::CommitPopup::new(queue.clone()),
            confirm_popup: crate::popups::confirm::ConfirmPopup::new(queue.clone()),
            generic_input_popup: crate::popups::commit::GenericInputPopup::new(queue.clone()),

            repo_search_query: None,

            diff: crate::components::diff::DiffComponent::new(queue.clone()),

            commit_input_scroll: 0,
            help_scroll: 0,
            detail_areas: DetailAreas::default(),
            main_areas: Vec::new(),

            status_list: crate::components::status_list::StatusListComponent::new(queue.clone()),

            last_click: None,
            detail_tab: 0,
            graph_scroll: 0,
            status_expanded: false,
            settings_focus_sidebar: true,
            tx,
            rx,
            fetching: false,
            pending_git_app: false,
            pending_fzf: false,
            pending_bulk_fzf: false,
            pending_files_fzf: false,
            pending_interactive_rebase: None,
            in_logs_ui: false,
            inspect_full_diff: false,
            search_column_selection: 0,
            search_columns_sha: true,
            search_columns_message: true,
            search_columns_author: true,
            search_columns_date: true,
            branch_action_target: None,
            tag_action_target_oid: None,
            tag_delete_target: None,
            tag_checkout_target: None,
            tag_push_target: None,
            discard_target: None,
            cherry_pick_target: None,
            cherry_pick_dest_selection: 0,
            cherry_pick_dest_branches: Vec::new(),
            revert_target: None,
            fetch_progress: 0,
            stash_apply_delete_after: true,
            stash_untracked: true,
            stash_keep_index: false,
            stashing_ui_selection: 0,
            remote_picker_action: None,
            remote_picker_selection: 0,
            inspect_horizontal_split_pct: 38,
            inspect_vertical_split_pct: 38,
            workspace_main_split_pct: 38,
            files_horizontal_split_pct: 38,
            branches_horizontal_split_pct: 50,
            stashes_horizontal_split_pct: 38,
            stashes_vertical_split_pct: 38,
            overview_horizontal_split_pct: 38,
            commit_popup_width_pct: 80,
            commit_popup_height_pct: 45,
            active_drag_splitter: None,
            settings_selected_index: 0,
            settings_editing: false,
            settings_theme_list: Vec::new(),
            settings_theme_index: 0,
            debug_log_scroll: 0,
            import_url: String::new(),
            import_dest: String::new(),
            import_name: String::new(),
            remote_add_name: String::new(),
            remote_add_url: String::new(),
            remote_action_target: None,
            last_staging_focus: DetailSection::Staged,
            force_fzf_missing: None,
            loading_repo_path: None,
            detail_tx,
            detail_rx,
            tab_tx,
            tab_rx,
            file_history_revisions: Vec::new(),
            file_history_selection: 0,
            file_history_diff: Vec::new(),
            file_history_diff_scroll: 0,
            file_history_path: String::new(),
            file_history_focus: 0,
            cpu_tracker: std::sync::Mutex::new(None),
            watcher: None,
        };

        if app.config.sort_by != SortOrder::Custom {
            app.sort_items_in_place();
        }

        // Detect update / initial setup
        let current_version = env!("CARGO_PKG_VERSION");
        let version_path = app.config_path.parent().unwrap_or(&app.config_path).join(".version");
        let mut is_first_run = false;

        let last_version = if version_path.exists() {
            std::fs::read_to_string(&version_path).map(|s| s.trim().to_string()).unwrap_or_default()
        } else {
            is_first_run = true;
            String::new()
        };

        if last_version != current_version {
            // 1. Back up config if it is an update and config exists
            if !is_first_run && app.config_path.exists() {
                let backup_path = app.config_path.with_extension("toml.bak");
                let _ = std::fs::copy(&app.config_path, backup_path);
                crate::debug_log::info(format!(
                    "Backed up configuration to {:?}",
                    app.config_path.with_extension("toml.bak")
                ));
            }

            // 2. Perform updates or auto-detections
            let gitui_installed = is_tool_installed("gitui");
            let lazygit_installed = is_tool_installed("lazygit");
            let fzf_installed = is_tool_installed("fzf");

            if app.config.git_app == "gitui" && !gitui_installed && lazygit_installed {
                app.config.git_app = "lazygit".to_string();
                crate::debug_log::info("Auto-configured git_app to lazygit as gitui was not found");
            }

            if app.config.fzf.enabled && !fzf_installed {
                crate::debug_log::warn(
                    "fzf is enabled in configuration but not found in your system PATH.",
                );
            }

            // 3. Write new version file
            let _ = std::fs::write(&version_path, current_version);

            // 4. Save config to persist migration changes
            let _ = crate::config::save_config(&app.config, &app.config_path);

            // 5. Update UI status message to inform user
            if is_first_run {
                app.status_message = Some(format!("Welcome to Gitwig v{}!", current_version));
            } else {
                app.status_message = Some(format!(
                    "Gitwig updated to v{}! Configuration verified and backed up.",
                    current_version
                ));
            }
        }

        app.setup_watcher();

        app
    }
}

/// Main event loop: compute layout, draw, poll input, repeat.
pub fn run<B: ratatui::backend::Backend>(
    terminal: &mut Terminal<B>,
    mut app: App,
) -> Result<(), Box<dyn Error>>
where
    <B as ratatui::backend::Backend>::Error: 'static,
{
    loop {
        while let Ok(msg) = app.rx.try_recv() {
            if let Some(repo_path) = msg.strip_prefix("REFRESH_REPO:") {
                let canon_target =
                    std::fs::canonicalize(repo_path).unwrap_or_else(|_| PathBuf::from(repo_path));
                if let Some(idx) = app.config.items.iter().position(|item| {
                    let canon_item =
                        std::fs::canonicalize(item).unwrap_or_else(|_| PathBuf::from(item));
                    canon_item == canon_target
                }) {
                    app.statuses[idx] = repo::inspect_summary(&app.config.items[idx]);
                    if let Some(repo::ItemDetail::Repo { resolved, .. }) = &app.current_detail {
                        if resolved == &canon_target {
                            app.resync_detail();
                        }
                    }
                }
            } else if let Some(dest_path) = msg.strip_prefix("CLONE_SUCCESS:") {
                app.fetching = false;
                app.status_message = Some("Cloning completed successfully".to_string());
                app.add_repo_path(dest_path.to_string());
            } else if let Some(tags_data) = msg.strip_prefix("REMOTE_TAGS:") {
                let tags = repo::deserialize_tags(tags_data);
                if let Some(repo::ItemDetail::Repo { info, .. }) = &mut app.current_detail {
                    info.remote_tags = repo::TabData::Loaded(tags);
                    info.remote_tags_loaded = true;
                }
                app.fetching = false;
            } else if let Some(err_msg) = msg.strip_prefix("REMOTE_TAGS_ERR:") {
                app.set_error(err_msg.to_string());
                app.fetching = false;
            } else {
                let success_fetch = msg.starts_with("Fetched remote ");
                let is_err = msg.starts_with("Fetch failed:")
                    || msg.starts_with("Pull failed:")
                    || msg.starts_with("Push failed:")
                    || msg.starts_with("Failed to")
                    || msg.contains("failed");

                if is_err {
                    let has_conflict = msg.contains("conflict") || msg.contains("CONFLICT");
                    app.set_error(msg);
                    if has_conflict {
                        app.detail_focus = DetailSection::Conflicts;
                    }
                } else {
                    app.status_message = Some(msg);
                }
                app.fetching = false;
                app.resync_detail();
                if success_fetch {
                    app.fetch_remote_tags(false);
                }
            }
        }

        while let Ok((path, detail)) = app.detail_rx.try_recv() {
            app.detail_cache.insert(
                path.clone(),
                DetailCache { detail: detail.clone(), loaded_at: std::time::Instant::now() },
            );

            let is_currently_loading = Some(&path) == app.loading_repo_path.as_ref();
            let is_currently_open = if let Some(current) = &app.current_detail {
                match current {
                    repo::ItemDetail::Repo { resolved, .. }
                    | repo::ItemDetail::Missing { resolved, .. }
                    | repo::ItemDetail::Directory { resolved, .. }
                    | repo::ItemDetail::Error { resolved, .. } => {
                        resolved.to_string_lossy() == path
                    }
                }
            } else {
                false
            };

            if is_currently_loading || is_currently_open {
                app.apply_detail_snapshot(detail);
                if is_currently_loading {
                    app.loading_repo_path = None;
                }
            }
        }

        let mut tab_updated = false;
        while let Ok((path, tab_idx, payload)) = app.tab_rx.try_recv() {
            crate::debug_log::info(format!(
                "Received tab payload: tab_idx={}, path={}",
                tab_idx, path
            ));
            if let Some(repo::ItemDetail::Repo { resolved, info }) = &mut app.current_detail {
                let resolved_str = resolved.to_string_lossy().to_string();
                if resolved_str == path {
                    crate::debug_log::info(format!("Paths match! Updating tab_idx={}", tab_idx));
                    tab_updated = true;
                    if tab_idx < 8 {
                        info.tab_loading[tab_idx] = false;
                        info.tab_loaded_at[tab_idx] = Some(std::time::Instant::now());
                    }
                    match payload {
                        repo::TabPayload::Files(res) => {
                            info.files = match res {
                                Ok(files) => repo::TabData::Loaded(files),
                                Err(e) => repo::TabData::Error(e),
                            };
                        }
                        repo::TabPayload::Graph(res) => {
                            info.graph_lines = match res {
                                Ok(lines) => repo::TabData::Loaded(lines),
                                Err(e) => repo::TabData::Error(e),
                            };
                        }
                        repo::TabPayload::Branches { local, remote } => {
                            info.local_branches = match local {
                                Ok(b) => repo::TabData::Loaded(b),
                                Err(e) => repo::TabData::Error(e),
                            };
                            info.remote_branches = match remote {
                                Ok(b) => repo::TabData::Loaded(b),
                                Err(e) => repo::TabData::Error(e),
                            };
                        }
                        repo::TabPayload::Tags { local, remote } => {
                            info.local_tags = match local {
                                Ok(t) => repo::TabData::Loaded(t),
                                Err(e) => repo::TabData::Error(e),
                            };
                            if !info.remote_tags_loaded {
                                info.remote_tags = match remote {
                                    Ok(t) => repo::TabData::Loaded(t),
                                    Err(e) => repo::TabData::Error(e),
                                };
                            }
                        }
                        repo::TabPayload::Remotes(res) => {
                            info.remotes = match res {
                                Ok(r) => repo::TabData::Loaded(r),
                                Err(e) => repo::TabData::Error(e),
                            };
                        }
                        repo::TabPayload::Stashes(res) => {
                            info.stashes = match res {
                                Ok(s) => repo::TabData::Loaded(s),
                                Err(e) => repo::TabData::Error(e),
                            };
                        }
                        repo::TabPayload::Overview(res) => match res {
                            Ok((stats, capped)) => {
                                info.committer_stats = repo::TabData::Loaded(stats);
                                info.committer_stats_limit_reached = capped;
                            }
                            Err(e) => {
                                info.committer_stats = repo::TabData::Error(e);
                            }
                        },
                    }
                }
            }
        }
        if tab_updated {
            app.update_cache_from_current_detail();
            app.rebuild_visible_files();
        }

        if app.pending_git_app {
            app.pending_git_app = false;
            if let Some(item) = app.config.items.get(app.selected_index) {
                let path = repo::expand_tilde(item);

                let raw_res = crossterm::terminal::disable_raw_mode();
                let exec_res = crossterm::execute!(
                    std::io::stdout(),
                    crossterm::terminal::LeaveAlternateScreen,
                    crossterm::event::DisableMouseCapture
                );
                let cursor_res = terminal.show_cursor();

                if raw_res.is_ok() && exec_res.is_ok() && cursor_res.is_ok() {
                    let git_app_name = &app.config.git_app;
                    let status =
                        std::process::Command::new(git_app_name).current_dir(&path).status();

                    let _ = crossterm::terminal::enable_raw_mode();
                    let _ = crossterm::execute!(
                        std::io::stdout(),
                        crossterm::terminal::EnterAlternateScreen,
                        crossterm::event::EnableMouseCapture
                    );
                    let _ = terminal.clear();

                    match status {
                        Ok(s) if s.success() => {
                            app.status_message = Some(format!("Returned from {}", git_app_name));
                            app.refresh_selected_status();
                        }
                        Ok(_) => {
                            app.status_message =
                                Some(format!("{} exited with error", git_app_name));
                            app.refresh_selected_status();
                        }
                        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                            app.set_error(format!("{} is not found in the system", git_app_name));
                        }
                        Err(e) => {
                            app.set_error(format!("Could not run {}: {}", git_app_name, e));
                        }
                    }
                }
            }
        }

        if let Some((repo_path, target)) = app.pending_interactive_rebase.take() {
            let raw_res = crossterm::terminal::disable_raw_mode();
            let exec_res = crossterm::execute!(
                std::io::stdout(),
                crossterm::terminal::LeaveAlternateScreen,
                crossterm::event::DisableMouseCapture
            );
            let cursor_res = terminal.show_cursor();

            if raw_res.is_ok() && exec_res.is_ok() && cursor_res.is_ok() {
                let status = std::process::Command::new("git")
                    .env("GIT_TERMINAL_PROMPT", "0")
                    .env("GIT_SSH_COMMAND", "ssh -o StrictHostKeyChecking=accept-new")
                    .arg("rebase")
                    .arg("-i")
                    .arg(&target)
                    .current_dir(&repo_path)
                    .status();

                let _ = crossterm::terminal::enable_raw_mode();
                let _ = crossterm::execute!(
                    std::io::stdout(),
                    crossterm::terminal::EnterAlternateScreen,
                    crossterm::event::EnableMouseCapture
                );
                let _ = terminal.clear();

                match status {
                    Ok(s) if s.success() => {
                        app.status_message =
                            Some("Interactive rebase completed successfully".to_string());
                    }
                    Ok(s) => {
                        app.status_message = Some(format!(
                            "Rebase exited with status: {}. Check terminal/git status.",
                            s
                        ));
                    }
                    Err(e) => {
                        app.status_message = Some(format!("Failed to run git rebase: {}", e));
                    }
                }
                if let Some(item) = app.config.items.get(app.selected_index) {
                    let new_status = repo::inspect_summary(item);
                    if let Some(slot) = app.statuses.get_mut(app.selected_index) {
                        *slot = new_status;
                    }
                }
                app.refresh_detail();
            }
        }

        if app.pending_fzf {
            app.pending_fzf = false;

            let raw_res = crossterm::terminal::disable_raw_mode();
            let exec_res = crossterm::execute!(
                std::io::stdout(),
                crossterm::terminal::LeaveAlternateScreen,
                crossterm::event::DisableMouseCapture
            );
            let cursor_res = terminal.show_cursor();

            if raw_res.is_ok() && exec_res.is_ok() && cursor_res.is_ok() {
                let max_depth = app.config.fzf.max_depth;
                let fd_excludes = app
                    .config
                    .fzf
                    .excludes
                    .iter()
                    .map(|x| format!("--exclude '{}'", x))
                    .collect::<Vec<String>>()
                    .join(" ");
                let find_prunes = app
                    .config
                    .fzf
                    .excludes
                    .iter()
                    .map(|x| format!("-path '*/{}'", x))
                    .collect::<Vec<String>>()
                    .join(" -o ");
                let find_prune_clause = if find_prunes.is_empty() {
                    "".to_string()
                } else {
                    format!("\\( {} \\) -prune -o ", find_prunes)
                };

                let expanded_start_dir = crate::repo::expand_tilde(&app.config.fzf.start_dir);
                let start_dir_str = expanded_start_dir.to_string_lossy().into_owned();
                let start_dir = start_dir_str.replace('\'', "'\\''");

                let cmd = if app.config.fzf.git_only {
                    format!(
                        "if ! command -v fzf >/dev/null 2>&1; then exit 127; fi; (command -v fd >/dev/null 2>&1 && fd -H '^\\.git$' '{}' --max-depth {} {} 2>/dev/null | xargs -I {{}} dirname {{}} || find '{}' -maxdepth {} {} -name .git -type d 2>/dev/null | xargs -I {{}} dirname {{}}) | fzf",
                        start_dir,
                        max_depth + 1,
                        fd_excludes,
                        start_dir,
                        max_depth + 1,
                        find_prune_clause
                    )
                } else {
                    format!(
                        "if ! command -v fzf >/dev/null 2>&1; then exit 127; fi; (command -v fd >/dev/null 2>&1 && fd . '{}' --type d --max-depth {} {} 2>/dev/null || find '{}' -maxdepth {} {} -type d -print 2>/dev/null) | fzf",
                        start_dir, max_depth, fd_excludes, start_dir, max_depth, find_prune_clause
                    )
                };

                let output = std::process::Command::new("sh")
                    .arg("-c")
                    .arg(&cmd)
                    .stdin(std::process::Stdio::inherit())
                    .stderr(std::process::Stdio::inherit())
                    .stdout(std::process::Stdio::piped())
                    .output();

                let _ = crossterm::terminal::enable_raw_mode();
                let _ = crossterm::execute!(
                    std::io::stdout(),
                    crossterm::terminal::EnterAlternateScreen,
                    crossterm::event::EnableMouseCapture
                );
                let _ = terminal.clear();

                match output {
                    Ok(out) => {
                        if out.status.success() {
                            let selected = String::from_utf8_lossy(&out.stdout).trim().to_string();
                            if !selected.is_empty() {
                                app.add_repo_path(selected);
                            }
                        } else if out.status.code() == Some(127) {
                            app.set_error("fzf is not installed. Please install fzf.".to_string());
                        }
                    }
                    Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                        app.set_error("fzf is not installed. Please install fzf.".to_string());
                    }
                    Err(e) => {
                        app.set_error(format!("Could not run fzf: {}", e));
                    }
                }
            }
        }

        if app.pending_bulk_fzf {
            app.pending_bulk_fzf = false;
            app.input_buffer.clear();
            app.mode = Mode::Normal;

            let raw_res = crossterm::terminal::disable_raw_mode();
            let exec_res = crossterm::execute!(
                std::io::stdout(),
                crossterm::terminal::LeaveAlternateScreen,
                crossterm::event::DisableMouseCapture
            );
            let cursor_res = terminal.show_cursor();

            if raw_res.is_ok() && exec_res.is_ok() && cursor_res.is_ok() {
                let max_depth = app.config.fzf.max_depth;
                let fd_excludes = app
                    .config
                    .fzf
                    .excludes
                    .iter()
                    .map(|x| format!("--exclude '{}'", x))
                    .collect::<Vec<String>>()
                    .join(" ");
                let find_prunes = app
                    .config
                    .fzf
                    .excludes
                    .iter()
                    .map(|x| format!("-path '*/{}'", x))
                    .collect::<Vec<String>>()
                    .join(" -o ");
                let find_prune_clause = if find_prunes.is_empty() {
                    "".to_string()
                } else {
                    format!("\\( {} \\) -prune -o ", find_prunes)
                };

                let expanded_start_dir = crate::repo::expand_tilde(&app.config.fzf.start_dir);
                let start_dir_str = expanded_start_dir.to_string_lossy().into_owned();
                let start_dir = start_dir_str.replace('\'', "'\\''");

                let cmd = format!(
                    "if ! command -v fzf >/dev/null 2>&1; then exit 127; fi; (command -v fd >/dev/null 2>&1 && fd . '{}' --type d --max-depth {} {} 2>/dev/null || find '{}' -maxdepth {} {} -type d -print 2>/dev/null) | fzf",
                    start_dir, max_depth, fd_excludes, start_dir, max_depth, find_prune_clause
                );

                let output = std::process::Command::new("sh")
                    .arg("-c")
                    .arg(&cmd)
                    .stdin(std::process::Stdio::inherit())
                    .stderr(std::process::Stdio::inherit())
                    .stdout(std::process::Stdio::piped())
                    .output();

                let _ = crossterm::terminal::enable_raw_mode();
                let _ = crossterm::execute!(
                    std::io::stdout(),
                    crossterm::terminal::EnterAlternateScreen,
                    crossterm::event::EnableMouseCapture
                );
                let _ = terminal.clear();

                match output {
                    Ok(out) => {
                        if out.status.success() {
                            let selected = String::from_utf8_lossy(&out.stdout).trim().to_string();
                            if !selected.is_empty() {
                                app.bulk_add_path(selected);
                            }
                        } else if out.status.code() == Some(127) {
                            app.set_error("fzf is not installed. Please install fzf.".to_string());
                        }
                    }
                    Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                        app.set_error("fzf is not installed. Please install fzf.".to_string());
                    }
                    Err(e) => {
                        app.set_error(format!("Could not run fzf: {}", e));
                    }
                }
            }
        }

        if app.pending_files_fzf {
            app.pending_files_fzf = false;
            if let Some(repo::ItemDetail::Repo { resolved, info }) = &app.current_detail {
                let repo_path = resolved.clone();
                let files = info.files.clone();

                let raw_res = crossterm::terminal::disable_raw_mode();
                let exec_res = crossterm::execute!(
                    std::io::stdout(),
                    crossterm::terminal::LeaveAlternateScreen,
                    crossterm::event::DisableMouseCapture
                );
                let cursor_res = terminal.show_cursor();

                if raw_res.is_ok() && exec_res.is_ok() && cursor_res.is_ok() {
                    let mut child_cmd = std::process::Command::new("fzf");
                    child_cmd.arg("--prompt").arg("Select file> ");
                    let child = child_cmd
                        .current_dir(&repo_path)
                        .stdin(std::process::Stdio::piped())
                        .stdout(std::process::Stdio::piped())
                        .stderr(std::process::Stdio::inherit())
                        .spawn();

                    let output = match child {
                        Ok(mut c) => {
                            if let Some(mut stdin) = c.stdin.take() {
                                use std::io::Write;
                                for file in files.iter() {
                                    let _ = writeln!(stdin, "{}", file);
                                }
                            }
                            c.wait_with_output()
                        }
                        Err(e) => Err(e),
                    };

                    let _ = crossterm::terminal::enable_raw_mode();
                    let _ = crossterm::execute!(
                        std::io::stdout(),
                        crossterm::terminal::EnterAlternateScreen,
                        crossterm::event::EnableMouseCapture
                    );
                    let _ = terminal.clear();

                    match output {
                        Ok(out) => {
                            if out.status.success() {
                                let selected =
                                    String::from_utf8_lossy(&out.stdout).trim().to_string();
                                if !selected.is_empty() {
                                    // Expand the parent directories of the selected file
                                    let parts: Vec<&str> = selected.split('/').collect();
                                    let mut accumulated = String::new();
                                    for part in parts.iter().take(parts.len().saturating_sub(1)) {
                                        if !accumulated.is_empty() {
                                            accumulated.push('/');
                                        }
                                        accumulated.push_str(part);
                                        app.file_tree.expanded_folders.insert(accumulated.clone());
                                    }
                                    app.rebuild_visible_files();
                                    if let Some(pos) = app
                                        .file_tree
                                        .visible_files
                                        .iter()
                                        .position(|item| item.full_path == selected)
                                    {
                                        app.file_tree.file_list_selection = pos;
                                        app.file_tree.file_content_scroll = 0;
                                        app.detail_focus = DetailSection::Files;
                                    }
                                    app.status_message = Some(format!("Selected {}", selected));
                                }
                            } else if out.status.code() == Some(127) {
                                app.status_message =
                                    Some("fzf is not installed. Please install fzf.".to_string());
                            }
                        }
                        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                            app.status_message = Some("fzf is not installed".to_string());
                        }
                        Err(e) => {
                            app.status_message = Some(format!("Could not run fzf: {}", e));
                        }
                    }
                }
            } else {
                app.status_message = Some("Not inside a repository".to_string());
            }
        }

        app.clamp_selection();

        let size = terminal.size()?;
        let area = Rect::new(0, 0, size.width, size.height);
        let inner_area = area.inner(Margin { vertical: 1, horizontal: 1 });

        let available_height = inner_area.height.saturating_sub(app.status_height());
        let visible_count =
            (available_height / ITEM_HEIGHT).min(app.get_items_len() as u16) as usize;
        app.clamp_scroll(visible_count);
        app.clamp_help_scroll(area.height as usize);

        app.trigger_tab_load_if_needed(app.detail_tab);

        // Capture panel rects from the draw pass for mouse hit-testing.
        let mut detail_areas = DetailAreas::default();
        let mut main_areas = Vec::new();
        terminal.draw(|f| {
            ui::draw(f, &app, area, inner_area, visible_count, &mut detail_areas, &mut main_areas)
        })?;
        app.detail_areas = detail_areas;
        app.main_areas = main_areas;

        // Transient feedback disappears after one frame, unless we are fetching.
        if app.fetching {
            if app.status_message.is_none() {
                app.status_message = Some("Executing Git operation...".to_string());
            }
            app.fetch_progress = (app.fetch_progress + 5) % 105;
        } else {
            app.status_message = None;
            app.fetch_progress = 0;
        }

        if event::poll(std::time::Duration::from_millis(app.config.poll_interval_ms))? {
            match event::read()? {
                Event::Key(key) => {
                    if key.kind == crossterm::event::KeyEventKind::Press
                        && !input::handle_key(&mut app, key, visible_count)
                    {
                        return Ok(());
                    }
                }
                Event::Mouse(mouse) => {
                    crate::mouse::handle_mouse(&mut app, mouse);
                }
                _ => {}
            }
        }
    }
}

fn is_tool_installed(name: &str) -> bool {
    #[cfg(target_os = "windows")]
    let cmd = "where";
    #[cfg(not(target_os = "windows"))]
    let cmd = "which";

    std::process::Command::new(cmd)
        .arg(name)
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}

fn copy_to_clipboard(text: &str) -> Result<(), String> {
    #[cfg(target_os = "macos")]
    {
        use std::io::Write;
        let mut child = std::process::Command::new("pbcopy")
            .stdin(std::process::Stdio::piped())
            .spawn()
            .map_err(|e| e.to_string())?;
        if let Some(mut stdin) = child.stdin.take() {
            stdin.write_all(text.as_bytes()).map_err(|e| e.to_string())?;
        }
        child.wait().map_err(|e| e.to_string())?;
        Ok(())
    }
    #[cfg(target_os = "windows")]
    {
        use std::io::Write;
        let mut child = std::process::Command::new("clip")
            .stdin(std::process::Stdio::piped())
            .spawn()
            .map_err(|e| e.to_string())?;
        if let Some(mut stdin) = child.stdin.take() {
            stdin.write_all(text.as_bytes()).map_err(|e| e.to_string())?;
        }
        child.wait().map_err(|e| e.to_string())?;
        Ok(())
    }
    #[cfg(not(any(target_os = "macos", target_os = "windows")))]
    {
        use std::io::Write;
        if let Ok(mut child) = std::process::Command::new("xclip")
            .arg("-selection")
            .arg("clipboard")
            .stdin(std::process::Stdio::piped())
            .spawn()
        {
            if let Some(mut stdin) = child.stdin.take() {
                if stdin.write_all(text.as_bytes()).is_ok() {
                    let _ = child.wait();
                    return Ok(());
                }
            }
        }
        if let Ok(mut child) = std::process::Command::new("xsel")
            .arg("-ib")
            .stdin(std::process::Stdio::piped())
            .spawn()
        {
            if let Some(mut stdin) = child.stdin.take() {
                if stdin.write_all(text.as_bytes()).is_ok() {
                    let _ = child.wait();
                    return Ok(());
                }
            }
        }
        Err("Could not find xclip or xsel on Linux system".to_string())
    }
}