cfait 1.1.8

Powerful, fast and elegant task / TODO manager. (GUI & TUI, CalDAV & local)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
// SPDX-License-Identifier: GPL-3.0-or-later
// File: ./src/config.rs
// Handles configuration loading, saving, and defaults.
use crate::context::AppContext;
use crate::storage::LocalStorage;
use anyhow::{Error, Result};
use chrono;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::fs;
use std::path::PathBuf;
use std::sync::{LazyLock, RwLock};
use strum::EnumIter;

static CONFIG_CACHE: LazyLock<RwLock<Option<(PathBuf, Config)>>> =
    LazyLock::new(|| RwLock::new(None));

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, EnumIter)]
#[serde(rename_all = "lowercase")]
pub enum FirstDayOfWeek {
    #[default]
    Monday,
    Sunday,
}

impl fmt::Display for FirstDayOfWeek {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FirstDayOfWeek::Monday => write!(f, "{}", rust_i18n::t!("monday")),
            FirstDayOfWeek::Sunday => write!(f, "{}", rust_i18n::t!("sunday")),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, EnumIter)]
#[serde(rename_all = "lowercase")]
pub enum GoalType {
    Count,
    Duration,
}

impl fmt::Display for GoalType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            GoalType::Count => write!(f, "Count"),
            GoalType::Duration => write!(f, "Duration"),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, EnumIter)]
#[serde(rename_all = "lowercase")]
pub enum IntervalUnit {
    Days,
    Weeks,
    Months,
    Years,
}

impl fmt::Display for IntervalUnit {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            IntervalUnit::Days => write!(f, "Days"),
            IntervalUnit::Weeks => write!(f, "Weeks"),
            IntervalUnit::Months => write!(f, "Months"),
            IntervalUnit::Years => write!(f, "Years"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Interval {
    pub amount: u32,
    pub unit: IntervalUnit,
}

impl Interval {
    pub fn get_period_bounds(&self, now: chrono::DateTime<chrono::Utc>, offset: i32) -> (i64, i64) {
        use chrono::{Datelike, NaiveDate, NaiveTime};
        let local_now = now.with_timezone(&chrono::Local).date_naive();

        let amount_i32 = self.amount.max(1) as i32;
        let amount_i64 = self.amount.max(1) as i64;

        let reference_date = match self.unit {
            IntervalUnit::Days => local_now + chrono::Duration::days(offset as i64 * amount_i64),
            IntervalUnit::Weeks => {
                local_now + chrono::Duration::days(offset as i64 * amount_i64 * 7)
            }
            IntervalUnit::Months => {
                let mut m = local_now.month0() as i32 + offset * amount_i32;
                let mut y = local_now.year();
                while m < 0 {
                    m += 12;
                    y -= 1;
                }
                while m > 11 {
                    m -= 12;
                    y += 1;
                }
                NaiveDate::from_ymd_opt(y, (m as u32) + 1, 1).unwrap_or(local_now)
            }
            IntervalUnit::Years => {
                NaiveDate::from_ymd_opt(local_now.year() + offset * amount_i32, 1, 1)
                    .unwrap_or(local_now)
            }
        };

        let start_date = match self.unit {
            IntervalUnit::Days => {
                let epoch_days =
                    (reference_date - NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()).num_days();
                let cycle = (epoch_days / amount_i64) * amount_i64;
                NaiveDate::from_ymd_opt(1970, 1, 1).unwrap() + chrono::Duration::days(cycle)
            }
            IntervalUnit::Weeks => {
                let days_since =
                    (reference_date - NaiveDate::from_ymd_opt(1970, 1, 5).unwrap()).num_days();
                let weeks = if days_since >= 0 {
                    days_since / 7
                } else {
                    (days_since - 6) / 7
                };
                let cycle = (weeks / amount_i64) * amount_i64;
                NaiveDate::from_ymd_opt(1970, 1, 5).unwrap() + chrono::Duration::days(cycle * 7)
            }
            IntervalUnit::Months => {
                let months = (reference_date.year() - 1970) * 12 + reference_date.month0() as i32;
                let cycle = (months / amount_i32) * amount_i32;
                let y = 1970 + cycle / 12;
                let m = (cycle % 12) as u32 + 1;
                NaiveDate::from_ymd_opt(y, m, 1).unwrap()
            }
            IntervalUnit::Years => {
                let years = reference_date.year() - 1970;
                let cycle = (years / amount_i32) * amount_i32;
                NaiveDate::from_ymd_opt(1970 + cycle, 1, 1).unwrap()
            }
        };

        let end_date = match self.unit {
            IntervalUnit::Days => start_date + chrono::Duration::days(amount_i64),
            IntervalUnit::Weeks => start_date + chrono::Duration::days(amount_i64 * 7),
            IntervalUnit::Months => {
                let m = start_date.month0() + self.amount.max(1);
                let y = start_date.year() + (m / 12) as i32;
                NaiveDate::from_ymd_opt(y, (m % 12) + 1, 1).unwrap()
            }
            IntervalUnit::Years => {
                NaiveDate::from_ymd_opt(start_date.year() + amount_i32, 1, 1).unwrap()
            }
        };

        let start_ts = crate::model::item::safe_local_to_utc(
            start_date,
            NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
        )
        .timestamp();
        let end_ts = crate::model::item::safe_local_to_utc(
            end_date,
            NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
        )
        .timestamp();
        (start_ts, end_ts)
    }

    pub fn format_short(&self) -> String {
        let unit_str = match self.unit {
            IntervalUnit::Days => "d",
            IntervalUnit::Weeks => "w",
            IntervalUnit::Months => "mo",
            IntervalUnit::Years => "y",
        };
        if self.amount == 1 {
            unit_str.to_string()
        } else {
            format!("{}{}", self.amount, unit_str)
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Goal {
    pub goal_type: GoalType,
    pub target: u32,
    pub interval: Interval,
}

impl Goal {
    pub fn format_target_display(&self, target_str: &str) -> String {
        if target_str == "1" && self.interval.amount == 1 {
            match self.interval.unit {
                IntervalUnit::Days => rust_i18n::t!("goal_period_daily")
                    .to_string()
                    .to_lowercase(),
                IntervalUnit::Weeks => rust_i18n::t!("goal_period_weekly")
                    .to_string()
                    .to_lowercase(),
                IntervalUnit::Months => rust_i18n::t!("goal_period_monthly")
                    .to_string()
                    .to_lowercase(),
                IntervalUnit::Years => rust_i18n::t!("goal_period_yearly")
                    .to_string()
                    .to_lowercase(),
            }
        } else {
            format!("{}/{}", target_str, self.interval.format_short())
        }
    }
}

impl<'de> Deserialize<'de> for Goal {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(untagged)]
        enum GoalFormat {
            Modern {
                goal_type: GoalType,
                target: u32,
                interval: Interval,
            },
            Legacy {
                goal_type: GoalType,
                target: u32,
                period: String,
            },
        }
        match GoalFormat::deserialize(deserializer)? {
            GoalFormat::Modern {
                goal_type,
                target,
                interval,
            } => Ok(Goal {
                goal_type,
                target,
                interval,
            }),
            GoalFormat::Legacy {
                goal_type,
                target,
                period,
            } => {
                let mut amount = 1;
                let unit = match period.to_lowercase().as_str() {
                    "daily" => IntervalUnit::Days,
                    "monthly" => IntervalUnit::Months,
                    "quarterly" => {
                        amount = 3;
                        IntervalUnit::Months
                    }
                    "halfyearly" | "semiannual" => {
                        amount = 6;
                        IntervalUnit::Months
                    }
                    "yearly" => IntervalUnit::Years,
                    _ => IntervalUnit::Weeks,
                };
                Ok(Goal {
                    goal_type,
                    target,
                    interval: Interval { amount, unit },
                })
            }
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, EnumIter)]
#[serde(rename_all = "lowercase")]
pub enum PausedSortBehavior {
    #[default]
    Tiebreak,
    Top,
    None,
}

impl fmt::Display for PausedSortBehavior {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PausedSortBehavior::Tiebreak => write!(f, "Tie-breaker (within rank/date)"),
            PausedSortBehavior::Top => write!(f, "Top of list (above unstarted)"),
            PausedSortBehavior::None => write!(f, "Off (treat as unstarted)"),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, EnumIter)]
pub enum LogLevel {
    #[default]
    Error,
    Warn,
    Info,
    Debug,
    Trace,
}

impl fmt::Display for LogLevel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            LogLevel::Error => write!(f, "Error"),
            LogLevel::Warn => write!(f, "Warn"),
            LogLevel::Info => write!(f, "Info"),
            LogLevel::Debug => write!(f, "Debug"),
            LogLevel::Trace => write!(f, "Trace"),
        }
    }
}

impl LogLevel {
    pub const ALL: &'static [LogLevel] = &[
        LogLevel::Error,
        LogLevel::Warn,
        LogLevel::Info,
        LogLevel::Debug,
        LogLevel::Trace,
    ];

    pub fn to_level_filter(&self) -> log::LevelFilter {
        match self {
            LogLevel::Error => log::LevelFilter::Error,
            LogLevel::Warn => log::LevelFilter::Warn,
            LogLevel::Info => log::LevelFilter::Info,
            LogLevel::Debug => log::LevelFilter::Debug,
            LogLevel::Trace => log::LevelFilter::Trace,
        }
    }
}

fn default_true() -> bool {
    true
}

// Configuration version constant for migration handling
const CURRENT_CONFIG_VERSION: u32 = 2;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TaskAction {
    OpenUrl,
    ToggleDetails,
    CompleteAndShift,
    ToggleTimer,
    StopTimer,
    AddSession,
    IncreasePriority,
    DecreasePriority,
    Edit,
    Yank,
    CreateSubtask,
    DuplicateTree,
    Promote,
    Move,
    Cancel,
    Delete,
    DeleteTree,
    OpenLocations,
    OpenCoordinates,
    TogglePin,
    Focus,
    EditTree,
    CompleteTree,
    BrowseRelations,
}

impl TaskAction {
    pub const ALL: &'static [TaskAction] = &[
        TaskAction::OpenUrl,         // First - link out
        TaskAction::OpenCoordinates, // Second - single coordinates
        TaskAction::OpenLocations,   // Third - multiple coordinates (GPX)
        TaskAction::ToggleDetails,
        TaskAction::CompleteAndShift,
        TaskAction::ToggleTimer,
        TaskAction::StopTimer,
        TaskAction::AddSession,
        TaskAction::IncreasePriority,
        TaskAction::DecreasePriority,
        TaskAction::Edit,
        TaskAction::EditTree,
        TaskAction::Yank,
        TaskAction::CreateSubtask,
        TaskAction::TogglePin,
        TaskAction::DuplicateTree,
        TaskAction::Promote,
        TaskAction::Move,
        TaskAction::Cancel,
        TaskAction::Delete,
        TaskAction::DeleteTree,
        TaskAction::Focus,
        TaskAction::CompleteTree,
        TaskAction::BrowseRelations,
    ];

    pub fn label(&self) -> String {
        match self {
            TaskAction::CompleteAndShift => rust_i18n::t!("action_complete_and_shift").to_string(),
            TaskAction::ToggleDetails => rust_i18n::t!("show_details").to_string(),
            TaskAction::ToggleTimer => rust_i18n::t!("start_task").to_string(),
            TaskAction::StopTimer => rust_i18n::t!("stop_reset").to_string(),
            TaskAction::AddSession => rust_i18n::t!("help_metadata_log_time").to_string(),
            TaskAction::IncreasePriority => rust_i18n::t!("increase_priority").to_string(),
            TaskAction::DecreasePriority => rust_i18n::t!("menu_decrease_prio").to_string(),
            TaskAction::Edit => rust_i18n::t!("edit").to_string(),
            TaskAction::Yank => rust_i18n::t!("yank_copy_id").to_string(),
            TaskAction::CreateSubtask => rust_i18n::t!("create_subtask").to_string(),
            TaskAction::DuplicateTree => rust_i18n::t!("duplicate_task").to_string(),
            TaskAction::Promote => rust_i18n::t!("promote_remove_parent").to_string(),
            TaskAction::Move => rust_i18n::t!("menu_move").to_string(),
            TaskAction::Cancel => rust_i18n::t!("cancel").to_string(),
            TaskAction::Delete => rust_i18n::t!("delete").to_string(),
            TaskAction::DeleteTree => rust_i18n::t!("delete_task_tree").to_string(),
            TaskAction::OpenLocations => rust_i18n::t!("action_open_locations").to_string(),
            TaskAction::OpenCoordinates => rust_i18n::t!("open_coordinates").to_string(),
            TaskAction::OpenUrl => rust_i18n::t!("open_url").to_string(),
            TaskAction::TogglePin => rust_i18n::t!("action_toggle_pin").to_string(),
            TaskAction::Focus => rust_i18n::t!("focus_hide_others").to_string(),
            TaskAction::EditTree => rust_i18n::t!("edit_tree_title").to_string(),
            TaskAction::CompleteTree => rust_i18n::t!("action_complete_tree").to_string(),
            TaskAction::BrowseRelations => rust_i18n::t!("jump_to_related_task").to_string(),
        }
    }

    pub fn search_aliases(&self) -> &'static [&'static str] {
        match self {
            TaskAction::ToggleDetails => &["l", "details"],
            TaskAction::CompleteAndShift => &["r", "rep", "repeat"],
            TaskAction::ToggleTimer => &["s", "start", "pause"],
            TaskAction::StopTimer => &["stop"],
            TaskAction::AddSession => &["t", "log"],
            TaskAction::IncreasePriority => &["+", "up"],
            TaskAction::DecreasePriority => &["-", "down"],
            TaskAction::Edit => &["e"],
            TaskAction::EditTree => &["tree", "edit"],
            TaskAction::Yank => &["y", "copy"],
            TaskAction::TogglePin => &["p", "pin"],
            TaskAction::CreateSubtask => &["c", "sub"],
            TaskAction::DuplicateTree => &["d", "dup"],
            TaskAction::CompleteTree => &["tree", "complete"],
            TaskAction::Promote => &["<", "outdent"],
            TaskAction::Move => &["m"],
            TaskAction::Cancel => &["x"],
            TaskAction::Delete | TaskAction::DeleteTree => &["del", "rm"],
            TaskAction::OpenUrl => &["o", "url", "link"],
            TaskAction::OpenCoordinates | TaskAction::OpenLocations => &["g", "map"],
            TaskAction::Focus => &["f", "focus"],
            TaskAction::BrowseRelations => &["l", "link", "relation", "wiki"],
        }
    }

    /// Returns the direct keyboard shortcut for this action in Normal mode, if any.
    pub fn shortcut(&self) -> Option<&'static str> {
        match self {
            TaskAction::ToggleTimer => Some("s"),
            TaskAction::StopTimer => Some("S"),
            TaskAction::AddSession => Some("t"),
            TaskAction::IncreasePriority => Some("+"),
            TaskAction::DecreasePriority => Some("-"),
            TaskAction::Edit => Some("e"),
            TaskAction::EditTree => Some("Ctrl+E"),
            TaskAction::Yank => Some("y"),
            TaskAction::CreateSubtask => Some("C"),
            TaskAction::TogglePin => Some("p"),
            TaskAction::DuplicateTree => Some("D"),
            TaskAction::Promote => Some("<"),
            TaskAction::Move => Some("m"),
            TaskAction::Cancel => Some("x"),
            TaskAction::Delete => Some("Del"),
            TaskAction::DeleteTree => Some("Ctrl+Del"),
            TaskAction::Focus => Some("f"),
            TaskAction::CompleteAndShift => Some("Shift+Space"),
            TaskAction::CompleteTree => Some("Shift+Space"),
            TaskAction::OpenUrl => Some("o"),
            TaskAction::ToggleDetails => None,
            TaskAction::BrowseRelations => Some("L"),
            TaskAction::OpenCoordinates | TaskAction::OpenLocations => None,
        }
    }
}

/// Controls the priority order of task sorting within the "urgent" bucket (rank 1-3).
/// This determines which criterion (urgent priority, started status, or due soon) takes precedence.
/// - `UrgentStartedDue`: Urgent tasks first, then started, then due soon
/// - `UrgentDueStarted`: Urgent tasks first, then due soon, then started
/// - `StartedUrgentDue`: Started tasks first, then urgent, then due soon
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, EnumIter)]
pub enum SortPreset {
    #[default]
    UrgentStartedDue,
    UrgentDueStarted,
    StartedUrgentDue,
}

impl fmt::Display for SortPreset {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SortPreset::UrgentStartedDue => write!(f, "Urgent > Ongoing > Due Soon"),
            SortPreset::UrgentDueStarted => write!(f, "Urgent > Due Soon > Ongoing"),
            SortPreset::StartedUrgentDue => write!(f, "Ongoing > Urgent > Due Soon"),
        }
    }
}

impl std::str::FromStr for SortPreset {
    type Err = ();
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "Urgent > Started > Due Soon" | "Urgent > Ongoing > Due Soon" => {
                Ok(SortPreset::UrgentStartedDue)
            }
            "Urgent > Due Soon > Started" | "Urgent > Due Soon > Ongoing" => {
                Ok(SortPreset::UrgentDueStarted)
            }
            "Started > Urgent > Due Soon" | "Ongoing > Urgent > Due Soon" => {
                Ok(SortPreset::StartedUrgentDue)
            }
            _ => Err(()),
        }
    }
}

fn default_pinned_actions() -> Vec<TaskAction> {
    vec![
        TaskAction::OpenUrl, // First action - open URL
        TaskAction::OpenCoordinates,
        TaskAction::ToggleDetails,
        TaskAction::ToggleTimer,
        TaskAction::IncreasePriority,
        TaskAction::DecreasePriority,
        TaskAction::Cancel,
        TaskAction::Edit,
        TaskAction::EditTree,
        TaskAction::Yank,
        TaskAction::CreateSubtask,
        TaskAction::Focus,
    ]
}

pub fn set_locale_with_fallback(locale: &str) {
    let available = rust_i18n::available_locales!();
    if available.iter().any(|l| l == locale) {
        rust_i18n::set_locale(locale);
    } else if let Some(primary) = locale.split(&['_', '-'][..]).next() {
        if available.iter().any(|l| l == primary) {
            rust_i18n::set_locale(primary);
        } else {
            rust_i18n::set_locale(locale);
        }
    } else {
        rust_i18n::set_locale(locale);
    }
    crate::model::parser::rebuild_lexicon();
}

pub fn init_locale(ctx: &dyn crate::context::AppContext) {
    // Initialize locale using the persistent config if present, otherwise fall back
    // to the system locale (primary language subtag). Android will pass its locale
    // at startup via UniFFI so this will pick that up if it's saved in the Config.
    let config = Config::load(ctx).unwrap_or_default();
    if let Some(lang) = config.language {
        set_locale_with_fallback(&lang);
    } else if let Some(sys_lang) = sys_locale::get_locale() {
        set_locale_with_fallback(&sys_lang);
    } else {
        crate::model::parser::rebuild_lexicon();
    }
}

fn default_urgent_days() -> u32 {
    1
}
fn default_urgent_prio() -> u8 {
    1
}

fn default_start_grace_period() -> u32 {
    1
}

fn default_priority() -> u8 {
    5
}

fn default_auto_remind() -> bool {
    true
}
fn default_remind_time() -> String {
    "08:00".to_string()
}
fn default_snooze_1() -> u32 {
    60
}
fn default_snooze_2() -> u32 {
    1440
}

fn default_create_events() -> bool {
    false
}

fn default_delete_events_on_completion() -> bool {
    false
}

fn default_max_done_roots() -> usize {
    20
}

fn default_max_done_subtasks() -> usize {
    5
}

// Quick Filter defaults
fn default_quick_filter_term() -> String {
    "is:ready".to_string()
}
fn default_quick_filter_icon() -> String {
    "f0fa9".to_string()
}

// Default trash retention moved to advanced settings; default shortened to 14 days.
fn default_trash_retention() -> u32 {
    14
}

fn default_duration_goal_mins() -> u32 {
    60
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, EnumIter)]
pub enum AppTheme {
    #[default]
    RustyDark,
    Random,
    Light,
    Dark,
    Dracula,
    Nord,
    SolarizedLight,
    SolarizedDark,
    GruvboxLight,
    GruvboxDark,
    CatppuccinLatte,
    CatppuccinFrappe,
    CatppuccinMacchiato,
    CatppuccinMocha,
    TokyoNight,
    TokyoNightStorm,
    TokyoNightLight,
    KanagawaWave,
    KanagawaDragon,
    KanagawaLotus,
    Moonfly,
    Nightfly,
    Oxocarbon,
    Ferra,
}

impl AppTheme {
    pub fn is_dark(&self) -> bool {
        !matches!(
            self,
            AppTheme::Light
                | AppTheme::SolarizedLight
                | AppTheme::GruvboxLight
                | AppTheme::CatppuccinLatte
                | AppTheme::TokyoNightLight
        )
    }
}

impl fmt::Display for AppTheme {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AppTheme::RustyDark => write!(f, "Rusty Dark"),
            AppTheme::Random => write!(f, "Random"),
            AppTheme::Light => write!(f, "Light"),
            AppTheme::Dark => write!(f, "Dark"),
            AppTheme::Dracula => write!(f, "Dracula"),
            AppTheme::Nord => write!(f, "Nord"),
            AppTheme::SolarizedLight => write!(f, "Solarized Light"),
            AppTheme::SolarizedDark => write!(f, "Solarized Dark"),
            AppTheme::GruvboxLight => write!(f, "Gruvbox Light"),
            AppTheme::GruvboxDark => write!(f, "Gruvbox Dark"),
            AppTheme::CatppuccinLatte => write!(f, "Catppuccin Latte"),
            AppTheme::CatppuccinFrappe => write!(f, "Catppuccin Frappé"),
            AppTheme::CatppuccinMacchiato => write!(f, "Catppuccin Macchiato"),
            AppTheme::CatppuccinMocha => write!(f, "Catppuccin Mocha"),
            AppTheme::TokyoNight => write!(f, "Tokyo Night"),
            AppTheme::TokyoNightStorm => write!(f, "Tokyo Night Storm"),
            AppTheme::TokyoNightLight => write!(f, "Tokyo Night Light"),
            AppTheme::KanagawaWave => write!(f, "Kanagawa Wave"),
            AppTheme::KanagawaDragon => write!(f, "Kanagawa Dragon"),
            AppTheme::KanagawaLotus => write!(f, "Kanagawa Lotus"),
            AppTheme::Moonfly => write!(f, "Moonfly"),
            AppTheme::Nightfly => write!(f, "Nightfly"),
            AppTheme::Oxocarbon => write!(f, "Oxocarbon"),
            AppTheme::Ferra => write!(f, "Ferra"),
        }
    }
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(default)]
pub struct Config {
    /// IMPORTANT FOR DEVELOPERS:
    /// If you add a new action to `default_pinned_actions()` or modify a default
    /// collection that existing users should inherit, you MUST increment
    /// `CURRENT_CONFIG_VERSION` and add a migration step in `Config::load()`.
    /// Otherwise, existing users' saved configs will override your new defaults.
    pub config_version: u32,

    /// Directory for local data files (journal, local calendars, locks).
    /// When set, overrides the XDG default (`~/.local/share/cfait`). Useful for
    /// placing data inside a sync folder (e.g. syncthing). Config and cache
    /// directories are not affected.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data_dir: Option<String>,

    pub url: String,
    pub username: String,

    #[serde(skip_serializing)]
    pub password: String,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub tls_client_cert_path: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tls_client_key_path: Option<String>,

    pub allow_insecure_certs: bool,
    pub disabled_calendars: Vec<String>,
    pub default_calendar: Option<String>,
    pub enable_local_mode: bool,
    pub hide_completed: bool,
    pub strikethrough_completed: bool,
    pub hide_fully_completed_tags: bool,
    pub hide_aliases_in_sidebar: bool,
    pub show_inline_descriptions: bool,
    pub ui_scale: f32,
    pub sort_cutoff_days: Option<u32>,
    /// When `true`, rank-4 (standard tasks with a due date within the cutoff) are sorted
    /// by priority first, then by due date.  Default is `false` (date-first).
    pub sort_standard_by_priority: bool,
    pub paused_sort_behavior: PausedSortBehavior,
    pub sort_tiebreak_recent: bool,
    /// Priority order for sorting tasks within the urgent/due soon/started ranks.
    /// See `SortPreset` enum for available options.
    pub sort_preset: SortPreset,
    pub theme: AppTheme,

    // Optional language/locale selection. None = use system default.
    pub language: Option<String>,
    pub first_day_of_week: FirstDayOfWeek,
    pub urgent_days_horizon: u32,
    pub urgent_priority_threshold: u8,
    pub default_priority: u8,
    pub start_grace_period_days: u32,
    pub auto_reminders: bool,
    pub default_reminder_time: String, // Format "HH:MM"
    pub snooze_short_mins: u32,
    pub snooze_long_mins: u32,
    pub create_events_for_tasks: bool,
    pub delete_events_on_completion: bool,
    pub auto_refresh_interval_mins: u32,

    // Default retention for local trash. This setting has been moved to the
    // Advanced Settings UI; default value reduced to 14 days.
    pub trash_retention_days: u32, // Integer: Days to keep items in local trash before permanent delete. 0 to disable trash.
    pub default_duration_goal_mins: u32,
    pub sessions_count_as_completions: bool,
    pub max_done_roots: usize,
    pub max_done_subtasks: usize,
    pub show_ongoing_notifications: bool,
    pub show_priority_numbers: bool,
    pub pinned_actions: Vec<TaskAction>,
    pub quick_filter_term: String,
    pub quick_filter_icon: String,
    pub show_quick_filter: bool,
    pub show_calendars_tab: bool,
    pub show_tags_tab: bool,
    pub show_locations_tab: bool,
    pub show_goals_tab: bool,
    pub show_journal_tab: bool,
    pub show_task_goals_in_sidebar: bool,
    pub show_undo_snackbar: bool,
    pub sidebar_is_hidden: bool,
    pub blur_when_unfocused: bool,
    pub description_editor: String,

    // Logging level for both file and terminal output
    pub log_level: LogLevel,

    // Maps are typically at the end in TOML
    pub hidden_calendars: Vec<String>,
    pub collection_order: Vec<String>,
    pub tag_aliases: HashMap<String, Vec<String>>,
    pub goals: HashMap<String, Goal>,

    // UI State
    pub expanded_tags: Vec<String>,
    pub expanded_locations: Vec<String>,
    pub sync_settings: bool,
    pub settings_updated_at: i64,
    pub window_width: f32,
    pub window_height: f32,
    pub sort_collections_by_size: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
pub struct SyncableConfig {
    #[serde(default)]
    pub default_calendar: Option<String>,
    #[serde(default)]
    pub disabled_calendars: Vec<String>,
    #[serde(default)]
    pub collection_order: Vec<String>,
    #[serde(default)]
    pub tag_aliases: HashMap<String, Vec<String>>,
    #[serde(default)]
    pub goals: HashMap<String, Goal>,
    #[serde(default)]
    pub hide_completed: bool,
    #[serde(default)]
    pub hide_fully_completed_tags: bool,
    #[serde(default)]
    pub hide_aliases_in_sidebar: bool,
    #[serde(default = "default_true")]
    pub show_inline_descriptions: bool,
    #[serde(default)]
    pub sort_cutoff_days: Option<u32>,
    #[serde(default)]
    pub sort_standard_by_priority: bool,
    #[serde(default)]
    pub paused_sort_behavior: PausedSortBehavior,
    #[serde(default)]
    pub sort_tiebreak_recent: bool,
    #[serde(default)]
    pub sort_preset: SortPreset,
    #[serde(default = "default_urgent_days")]
    pub urgent_days_horizon: u32,
    #[serde(default = "default_urgent_prio")]
    pub urgent_priority_threshold: u8,
    #[serde(default = "default_priority")]
    pub default_priority: u8,
    #[serde(default = "default_start_grace_period")]
    pub start_grace_period_days: u32,
    #[serde(default = "default_auto_remind")]
    pub auto_reminders: bool,
    #[serde(default = "default_remind_time")]
    pub default_reminder_time: String,
    #[serde(default = "default_snooze_1")]
    pub snooze_short_mins: u32,
    #[serde(default = "default_snooze_2")]
    pub snooze_long_mins: u32,
    #[serde(default = "default_create_events")]
    pub create_events_for_tasks: bool,
    #[serde(default = "default_delete_events_on_completion")]
    pub delete_events_on_completion: bool,
    #[serde(default = "default_trash_retention")]
    pub trash_retention_days: u32,
    #[serde(default = "default_duration_goal_mins")]
    pub default_duration_goal_mins: u32,
    #[serde(default)]
    pub sessions_count_as_completions: bool,
    #[serde(default = "default_max_done_roots")]
    pub max_done_roots: usize,
    #[serde(default = "default_max_done_subtasks")]
    pub max_done_subtasks: usize,
    #[serde(default = "default_true")]
    pub show_ongoing_notifications: bool,
    #[serde(default = "default_true")]
    pub show_priority_numbers: bool,
    #[serde(default = "default_pinned_actions")]
    pub pinned_actions: Vec<TaskAction>,
    #[serde(default = "default_quick_filter_term")]
    pub quick_filter_term: String,
    #[serde(default = "default_quick_filter_icon")]
    pub quick_filter_icon: String,
    #[serde(default = "default_true")]
    pub show_quick_filter: bool,
    #[serde(default = "default_true")]
    pub show_calendars_tab: bool,
    #[serde(default = "default_true")]
    pub show_tags_tab: bool,
    #[serde(default = "default_true")]
    pub show_locations_tab: bool,
    #[serde(default = "default_true")]
    pub show_goals_tab: bool,
    #[serde(default = "default_true")]
    pub show_journal_tab: bool,
    #[serde(default = "default_true")]
    pub show_task_goals_in_sidebar: bool,
    #[serde(default = "default_true")]
    pub show_undo_snackbar: bool,
    #[serde(default)]
    pub blur_when_unfocused: bool,
    #[serde(default = "default_true")]
    pub sort_collections_by_size: bool,
    #[serde(default)]
    pub first_day_of_week: FirstDayOfWeek,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct SettingsPayload {
    #[serde(default)]
    pub updated_at: i64,
    #[serde(default)]
    pub config: SyncableConfig,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            config_version: CURRENT_CONFIG_VERSION,
            data_dir: None,
            url: String::new(),
            username: String::new(),
            password: String::new(),
            tls_client_cert_path: None,
            tls_client_key_path: None,
            default_calendar: None,
            enable_local_mode: true,
            allow_insecure_certs: false,
            hidden_calendars: Vec::new(),
            collection_order: Vec::new(),
            disabled_calendars: Vec::new(),
            hide_completed: false,
            hide_fully_completed_tags: true,
            hide_aliases_in_sidebar: true,
            show_inline_descriptions: true,
            ui_scale: 1.0,
            sort_cutoff_days: Some(30),
            sort_standard_by_priority: false,
            paused_sort_behavior: PausedSortBehavior::default(),
            sort_tiebreak_recent: true,
            sort_preset: SortPreset::default(),
            tag_aliases: HashMap::new(),
            language: None,
            theme: AppTheme::default(),
            urgent_days_horizon: 1,
            urgent_priority_threshold: 1,
            default_priority: 5,
            start_grace_period_days: 1,
            auto_reminders: true,
            default_reminder_time: "08:00".to_string(),
            snooze_short_mins: 60,
            snooze_long_mins: 1440,
            create_events_for_tasks: false,
            delete_events_on_completion: false,
            auto_refresh_interval_mins: 30,
            trash_retention_days: 14,
            default_duration_goal_mins: 60,
            sessions_count_as_completions: false,
            strikethrough_completed: false,
            max_done_roots: 20,
            max_done_subtasks: 5,
            show_ongoing_notifications: true,
            show_priority_numbers: true,
            pinned_actions: vec![
                TaskAction::OpenUrl,
                TaskAction::OpenCoordinates,
                TaskAction::ToggleDetails,
                TaskAction::ToggleTimer,
                TaskAction::IncreasePriority,
                TaskAction::DecreasePriority,
                TaskAction::Cancel,
                TaskAction::Edit,
                TaskAction::EditTree,
                TaskAction::Yank,
                TaskAction::CreateSubtask,
                TaskAction::Focus,
            ],
            quick_filter_term: "is:ready".to_string(),
            quick_filter_icon: "f0fa9".to_string(),
            show_quick_filter: true,
            show_calendars_tab: true,
            show_tags_tab: true,
            show_locations_tab: true,
            show_goals_tab: true,
            show_journal_tab: true,
            show_task_goals_in_sidebar: true,
            show_undo_snackbar: true,
            blur_when_unfocused: false,
            sidebar_is_hidden: false,
            first_day_of_week: FirstDayOfWeek::default(),
            description_editor: String::new(),
            log_level: LogLevel::Warn,
            expanded_tags: vec!["j:pages".to_string()],
            expanded_locations: Vec::new(),
            sync_settings: true,
            settings_updated_at: 0,
            goals: HashMap::new(),
            window_width: 1024.0,
            window_height: 768.0,
            sort_collections_by_size: true,
        }
    }
}

impl Config {
    pub fn get_syncable(&self) -> SyncableConfig {
        SyncableConfig {
            default_calendar: self.default_calendar.clone(),
            disabled_calendars: self.disabled_calendars.clone(),
            collection_order: self.collection_order.clone(),
            tag_aliases: self.tag_aliases.clone(),
            goals: self.goals.clone(),
            hide_completed: self.hide_completed,
            hide_fully_completed_tags: self.hide_fully_completed_tags,
            hide_aliases_in_sidebar: self.hide_aliases_in_sidebar,
            show_inline_descriptions: self.show_inline_descriptions,
            sort_cutoff_days: self.sort_cutoff_days,
            sort_standard_by_priority: self.sort_standard_by_priority,
            paused_sort_behavior: self.paused_sort_behavior,
            sort_tiebreak_recent: self.sort_tiebreak_recent,
            sort_preset: self.sort_preset,
            urgent_days_horizon: self.urgent_days_horizon,
            urgent_priority_threshold: self.urgent_priority_threshold,
            default_priority: self.default_priority,
            start_grace_period_days: self.start_grace_period_days,
            auto_reminders: self.auto_reminders,
            default_reminder_time: self.default_reminder_time.clone(),
            snooze_short_mins: self.snooze_short_mins,
            snooze_long_mins: self.snooze_long_mins,
            create_events_for_tasks: self.create_events_for_tasks,
            delete_events_on_completion: self.delete_events_on_completion,
            trash_retention_days: self.trash_retention_days,
            default_duration_goal_mins: self.default_duration_goal_mins,
            sessions_count_as_completions: self.sessions_count_as_completions,
            max_done_roots: self.max_done_roots,
            max_done_subtasks: self.max_done_subtasks,
            show_ongoing_notifications: self.show_ongoing_notifications,
            show_priority_numbers: self.show_priority_numbers,
            pinned_actions: self.pinned_actions.clone(),
            quick_filter_term: self.quick_filter_term.clone(),
            quick_filter_icon: self.quick_filter_icon.clone(),
            show_quick_filter: self.show_quick_filter,
            show_calendars_tab: self.show_calendars_tab,
            show_tags_tab: self.show_tags_tab,
            show_locations_tab: self.show_locations_tab,
            show_goals_tab: self.show_goals_tab,
            show_journal_tab: self.show_journal_tab,
            show_task_goals_in_sidebar: self.show_task_goals_in_sidebar,
            show_undo_snackbar: self.show_undo_snackbar,
            blur_when_unfocused: self.blur_when_unfocused,
            sort_collections_by_size: self.sort_collections_by_size,
            first_day_of_week: self.first_day_of_week,
        }
    }

    pub fn apply_syncable(&mut self, sync: SyncableConfig) {
        // Only overwrite the default calendar if the sync payload contains one,
        // protecting against downgrades from older clients missing this field.
        if sync.default_calendar.is_some() {
            self.default_calendar = sync.default_calendar;
        }
        self.disabled_calendars = sync.disabled_calendars;
        self.collection_order = sync.collection_order;
        self.tag_aliases = sync.tag_aliases;
        self.goals = sync.goals;
        self.hide_completed = sync.hide_completed;
        self.hide_fully_completed_tags = sync.hide_fully_completed_tags;
        self.hide_aliases_in_sidebar = sync.hide_aliases_in_sidebar;
        self.show_inline_descriptions = sync.show_inline_descriptions;
        self.sort_cutoff_days = sync.sort_cutoff_days;
        self.sort_standard_by_priority = sync.sort_standard_by_priority;
        self.paused_sort_behavior = sync.paused_sort_behavior;
        self.sort_tiebreak_recent = sync.sort_tiebreak_recent;
        self.sort_preset = sync.sort_preset;
        self.urgent_days_horizon = sync.urgent_days_horizon;
        self.urgent_priority_threshold = sync.urgent_priority_threshold;
        self.default_priority = sync.default_priority;
        self.start_grace_period_days = sync.start_grace_period_days;
        self.auto_reminders = sync.auto_reminders;
        self.default_reminder_time = sync.default_reminder_time;
        self.snooze_short_mins = sync.snooze_short_mins;
        self.snooze_long_mins = sync.snooze_long_mins;
        self.create_events_for_tasks = sync.create_events_for_tasks;
        self.delete_events_on_completion = sync.delete_events_on_completion;
        self.trash_retention_days = sync.trash_retention_days;
        self.default_duration_goal_mins = sync.default_duration_goal_mins;
        self.sessions_count_as_completions = sync.sessions_count_as_completions;
        self.max_done_roots = sync.max_done_roots;
        self.max_done_subtasks = sync.max_done_subtasks;
        self.show_ongoing_notifications = sync.show_ongoing_notifications;
        self.show_priority_numbers = sync.show_priority_numbers;
        self.pinned_actions = sync.pinned_actions;
        self.quick_filter_term = sync.quick_filter_term;
        self.quick_filter_icon = sync.quick_filter_icon;
        self.show_quick_filter = sync.show_quick_filter;
        self.show_calendars_tab = sync.show_calendars_tab;
        self.show_tags_tab = sync.show_tags_tab;
        self.show_locations_tab = sync.show_locations_tab;
        self.show_goals_tab = sync.show_goals_tab;
        self.show_journal_tab = sync.show_journal_tab;
        self.show_task_goals_in_sidebar = sync.show_task_goals_in_sidebar;
        self.show_undo_snackbar = sync.show_undo_snackbar;
        self.blur_when_unfocused = sync.blur_when_unfocused;
        self.sort_collections_by_size = sync.sort_collections_by_size;
        self.first_day_of_week = sync.first_day_of_week;
    }

    pub fn update_sync_timestamp_if_changed(&mut self, old: &Config) {
        if self.get_syncable() != old.get_syncable() {
            self.settings_updated_at = chrono::Utc::now().timestamp();
        }
    }

    /// Clear the in-memory config cache, forcing the next `load` to read from disk.
    pub fn invalidate_cache() {
        *CONFIG_CACHE.write().unwrap() = None;
    }

    /// Cheap accessor for `tag_aliases` that clones only the alias map from the cache
    /// instead of the entire `Config`. Falls back to a full `load` (which populates the
    /// cache) on a miss, and to an empty map when no config exists. Use this in per-task
    /// hot paths that only need the alias map (e.g. `extract_transient_metadata`).
    pub fn tag_aliases(ctx: &dyn AppContext) -> HashMap<String, Vec<String>> {
        if let Ok(path) = ctx.get_config_file_path() {
            let guard = CONFIG_CACHE.read().unwrap();
            if let Some((cached_path, config)) = guard.as_ref()
                && cached_path == &path
            {
                return config.tag_aliases.clone();
            }
        }
        Self::load(ctx).map(|c| c.tag_aliases).unwrap_or_default()
    }

    /// Load the configuration from disk using an explicit context.
    pub fn load(ctx: &dyn AppContext) -> Result<Self> {
        let path = ctx.get_config_file_path()?;

        if let Some(cache) = CONFIG_CACHE.read().unwrap().as_ref()
            && cache.0 == path
        {
            return Ok(cache.1.clone());
        }

        if !path.exists() {
            return Err(anyhow::anyhow!("Config file not found"));
        }

        let contents = fs::read_to_string(&path).map_err(|e| {
            anyhow::anyhow!("Failed to read config file '{}': {}", path.display(), e)
        })?;

        let mut config: Config = toml::from_str(&contents).map_err(|e| {
            anyhow::anyhow!("Failed to parse config file '{}': {}", path.display(), e)
        })?;

        // --- CONFIG MIGRATIONS ---
        // Apply migrations for older configs to ensure they receive new features/defaults.
        // If you bump `CURRENT_CONFIG_VERSION`, add a new `if` block here.
        if config.config_version == 0 {
            // Migrate OpenCoordinates (added after v0.5.8)
            if !config.pinned_actions.contains(&TaskAction::OpenCoordinates) {
                config.pinned_actions.insert(0, TaskAction::OpenCoordinates);
            }
            // Migrate OpenUrl (added after v0.5.8)
            if !config.pinned_actions.contains(&TaskAction::OpenUrl) {
                config.pinned_actions.insert(0, TaskAction::OpenUrl);
            }
            // Note: Inserting both at 0 one after the other places OpenUrl first,
            // then OpenCoordinates, perfectly matching `default_pinned_actions()`.

            config.config_version = 1;
        }

        if config.config_version == 1 {
            // New default for expanded_tags was added in v2
            if config.expanded_tags.is_empty() {
                config.expanded_tags = vec!["j:pages".to_string()];
            }
            config.config_version = 2;
        }

        *CONFIG_CACHE.write().unwrap() = Some((path, config.clone()));
        Ok(config)
    }

    /// Load the configuration from disk and fetch the password from the OS keyring.
    /// Use this ONLY during app startup, explicit syncing, or opening the settings panel.
    pub fn load_with_credentials(ctx: &dyn AppContext) -> Result<Self> {
        let mut config = Self::load(ctx)?;

        let user_key = if config.username.is_empty() {
            "default"
        } else {
            &config.username
        };

        match keyring_core::Entry::new("cfait", user_key) {
            Ok(entry) => {
                if !config.password.is_empty() {
                    // Migration: plaintext password found in config.toml!
                    // Move it securely into the OS keyring.
                    if let Err(err) = entry.set_password(&config.password) {
                        log::warn!(
                            "Failed to migrate password into keyring for user '{}': {}",
                            user_key,
                            err
                        );
                    }
                } else {
                    match entry.get_password() {
                        Ok(pw) => {
                            // Normal run: fetch the password from the OS keyring.
                            config.password = pw;
                        }
                        Err(keyring_core::Error::NoEntry) => {}
                        Err(err) => {
                            log::warn!(
                                "Failed to load password from keyring for user '{}': {}",
                                user_key,
                                err
                            );
                        }
                    }
                }
            }
            Err(err) => {
                log::warn!(
                    "Failed to initialize keyring entry for user '{}': {}",
                    user_key,
                    err
                );
            }
        }

        Ok(config)
    }

    pub fn is_missing_config_error(err: &Error) -> bool {
        if err.to_string().contains("Config file not found") {
            return true;
        }
        if let Some(io_err) = err.downcast_ref::<std::io::Error>()
            && io_err.kind() == std::io::ErrorKind::NotFound
        {
            return true;
        }
        for cause in err.chain() {
            if let Some(io_err) = cause.downcast_ref::<std::io::Error>()
                && io_err.kind() == std::io::ErrorKind::NotFound
            {
                return true;
            }
        }
        false
    }

    /// Save configuration using an explicit context.
    /// This method post-processes the TOML string to inject documentation comments.
    pub fn save(&self, ctx: &dyn AppContext) -> Result<()> {
        let path = ctx.get_config_file_path()?;

        LocalStorage::with_lock(&path, || {
            let toml_str = toml::to_string_pretty(self)?;
            let documented_toml = Self::inject_documentation(&toml_str);
            LocalStorage::atomic_write(&path, documented_toml)?;
            Ok(())
        })?;

        let mut cached = self.clone();
        cached.password.clear();
        *CONFIG_CACHE.write().unwrap() = Some((path, cached));
        Ok(())
    }

    /// Save configuration and update the OS keyring credential.
    pub fn save_with_credentials(&self, ctx: &dyn AppContext) -> Result<()> {
        let user_key = if self.username.is_empty() {
            "default"
        } else {
            &self.username
        };

        match keyring_core::Entry::new("cfait", user_key) {
            Ok(entry) => {
                if !self.password.is_empty() {
                    if let Err(err) = entry.set_password(&self.password) {
                        log::warn!(
                            "Failed to save password to keyring for user '{}': {}",
                            user_key,
                            err
                        );
                    }
                } else if let Err(err) = entry.delete_credential() {
                    // Delete credential if the user cleared the password.
                    // Missing entries are fine; anything else is worth logging.
                    if !matches!(err, keyring_core::Error::NoEntry) {
                        log::warn!(
                            "Failed to delete keyring credential for user '{}': {}",
                            user_key,
                            err
                        );
                    }
                }
            }
            Err(err) => {
                log::warn!(
                    "Failed to initialize keyring entry for user '{}': {}",
                    user_key,
                    err
                );
            }
        }

        self.save(ctx)
    }

    /// Get the path string using an explicit context.
    pub fn get_path_string(ctx: &dyn AppContext) -> Result<String> {
        let path = ctx.get_config_file_path()?;
        Ok(path.to_string_lossy().to_string())
    }

    /// Post-process raw TOML string to add comments and headers.
    fn inject_documentation(raw_toml: &str) -> String {
        let mut out = String::with_capacity(raw_toml.len() + 2048);

        // Header Comment
        out.push_str("# Cfait Configuration\n");

        for line in raw_toml.lines() {
            let trimmed = line.trim();

            // -- Section Headers --
            if trimmed.starts_with("data_dir =") {
                out.push_str("\n# --- Storage ---\n");
            } else if trimmed.starts_with("url =") {
                out.push_str("\n# --- Connection Settings ---\n");
            } else if trimmed.starts_with("default_calendar =") {
                out.push_str("\n# --- UI & Behavior ---\n");
            } else if trimmed.starts_with("sort_cutoff_days =") {
                out.push_str("\n# --- Sorting & Ranking Logic ---\n");
            } else if trimmed.starts_with("auto_reminders =") {
                out.push_str("\n# --- Notifications & Reminders ---\n");
            } else if trimmed.starts_with("create_events_for_tasks =") {
                out.push_str("\n# --- Calendar Integration (VEVENT Sync) ---\n");
            } else if trimmed.starts_with("max_done_roots =") {
                out.push_str("\n# --- Advanced Settings ---\n");
            } else if trimmed.starts_with("[tag_aliases]") {
                out.push_str("\n# --- Aliases (Global Templates) ---\n");
                out.push_str("# Map shortcuts to sets of tags/locations/priorities.\n");
                out.push_str("# Example: \"gardening\" = [\"#fun\", \"@@home\"]\n");
            } else if trimmed.starts_with("[goals]") {
                out.push_str("\n# --- Goals & Habit Tracking ---\n");
                out.push_str("# Set tracking goals for specific tags or locations.\n");
                out.push_str("# Example: [goals.\"#reading\"]\n");
                out.push_str("#          goal_type = \"count\" # or \"duration\"\n");
                out.push_str("#          target = 5\n");
                out.push_str("#          period = \"weekly\" # daily, weekly, monthly, yearly\n");
            }

            // -- Inline or Block Comments for specific keys --

            if trimmed.starts_with("data_dir =") {
                out.push_str(line);
                out.push_str(
                    " # String (Optional): Directory for local data files. Overrides the XDG default (~/.local/share/cfait). Tilde (~) is expanded.",
                );
            } else if trimmed.starts_with("url =") {
                out.push_str("# URL: The full address to your CalDAV server endpoint.\n");
                out.push_str(line);
            } else if trimmed.starts_with("tls_client_cert_path =") {
                out.push_str(line);
                out.push_str(
                    " # String (Optional): Path to PEM-encoded client certificate for mTLS.",
                );
            } else if trimmed.starts_with("tls_client_key_path =") {
                out.push_str(line);
                out.push_str(" # String (Optional): Path to PEM-encoded private key for mTLS.");
            } else if trimmed.starts_with("allow_insecure_certs =") {
                out.push_str(line);
                out.push_str(
                    " # Boolean: Set true to bypass SSL verification (e.g. self-signed certs).",
                );
            } else if trimmed.starts_with("sync_settings =") {
                out.push_str(line);
                out.push_str(
                    " # Boolean: If true, sync settings and aliases as a hidden task on the server.",
                );
            } else if trimmed.starts_with("disabled_calendars =") {
                out.push_str("# List of calendar HREFs (strings) to completely disable/ignore.\n");
                out.push_str(line);
            } else if trimmed.starts_with("default_calendar =") {
                out.push_str(line);
                out.push_str(
                    " # String: Target for new tasks. HREF or 'local://default' when local mode is enabled.",
                );
            } else if trimmed.starts_with("enable_local_mode =") {
                out.push_str(line);
                out.push_str(
                    " # Boolean: If false, TUI local/offline calendars are hidden and new tasks target remote calendars only.",
                );
            } else if trimmed.starts_with("hide_completed =") {
                out.push_str(line);
                out.push_str(" # Boolean: If true, Completed/Cancelled tasks are hidden globally.");
            } else if trimmed.starts_with("strikethrough_completed =") {
                out.push_str(line);
                out.push_str(" # Boolean: Apply strikethrough styling to completed task titles.");
            } else if trimmed.starts_with("hide_fully_completed_tags =") {
                out.push_str(line);
                out.push_str(" # Boolean: Hide tags in sidebar if all their tasks are completed.");
            } else if trimmed.starts_with("hide_aliases_in_sidebar =") {
                out.push_str(line);
                out.push_str(" # Boolean: Hide shorthand aliases from the sidebar (showing only their targets).");
            } else if trimmed.starts_with("show_inline_descriptions =") {
                out.push_str(line);
                out.push_str(" # Boolean: Previews up to 3 lines of the description in the list.");
            } else if trimmed.starts_with("ui_scale =") {
                out.push_str(line);
                out.push_str(
                    " # Float: Global UI scale factor (0.5–3.0). Ctrl+/Ctrl-/scroll to change.",
                );
            } else if trimmed.starts_with("theme =") {
                out.push_str(line);
                out.push_str(" # String: App Theme (RustyDark, Light, Dark, etc). In the TUI, light themes adapt text contrast for light terminal backgrounds.");
            } else if trimmed.starts_with("sort_cutoff_days =") {
                out.push_str(line);
                out.push_str(" # Integer/None: Tasks due beyond this many days are ranked lower.");
            } else if trimmed.starts_with("sort_standard_by_priority =") {
                out.push_str(line);
                out.push_str(
                    " # Boolean: If true, regular tasks sort by priority first, then by date.",
                );
            } else if trimmed.starts_with("paused_sort_behavior =") {
                out.push_str(line);
                out.push_str(" # Enum: Paused tasks behavior (Tiebreak, Top, None).");
            } else if trimmed.starts_with("sort_tiebreak_recent =") {
                out.push_str(line);
                out.push_str(
                    " # Boolean: If true, ties in sorting are broken by recently modified rather than alphabetical.",
                );
            } else if trimmed.starts_with("sort_preset =") {
                out.push_str(line);
                out.push_str(" # Enum: Order of urgent buckets (UrgentStartedDue, UrgentDueStarted, StartedUrgentDue).");
            } else if trimmed.starts_with("urgent_days_horizon =") {
                out.push_str(line);
                out.push_str(
                    " # Integer: Tasks due within this many days are considered 'Urgent'.",
                );
            } else if trimmed.starts_with("urgent_priority_threshold =") {
                out.push_str(line);
                out.push_str(" # Integer (1-9): Priorities <= this value are 'Urgent'. (1=High)");
            } else if trimmed.starts_with("default_priority =") {
                out.push_str(line);
                out.push_str(" # Integer (1-9): Default priority for new tasks. 0 maps to this.");
            } else if trimmed.starts_with("start_grace_period_days =") {
                out.push_str(line);
                out.push_str(
                    " # Integer: Future tasks appear in the list this many days before start.",
                );
            } else if trimmed.starts_with("auto_reminders =") {
                out.push_str(line);
                out.push_str(
                    " # Boolean: Auto-remind on Due/Start dates if no explicit alarms exist.",
                );
            } else if trimmed.starts_with("default_reminder_time =") {
                out.push_str(line);
                out.push_str(" # String (HH:MM): Default time for date-only auto-reminders.");
            } else if trimmed.starts_with("snooze_short_mins =") {
                out.push_str(line);
                out.push_str(" # Integer: Minutes for the 'Short Snooze' button.");
            } else if trimmed.starts_with("snooze_long_mins =") {
                out.push_str(line);
                out.push_str(" # Integer: Minutes for the 'Long Snooze' button.");
            } else if trimmed.starts_with("auto_refresh_interval_mins =") {
                out.push_str(line);
                out.push_str(" # Integer: Background sync interval in minutes. 0 to disable.");
            } else if trimmed.starts_with("create_events_for_tasks =") {
                out.push_str(line);
                out.push_str(
                    " # Boolean: Create companion VEVENTs on server for tasks with dates.",
                );
            } else if trimmed.starts_with("delete_events_on_completion =") {
                out.push_str(line);
                out.push_str(" # Boolean: Remove the companion VEVENT when task is completed.");
            } else if trimmed.starts_with("max_done_roots =") {
                out.push_str(line);
                out.push_str(
                    " # Integer: Limit completed root tasks shown before 'Expand' button.",
                );
            } else if trimmed.starts_with("max_done_subtasks =") {
                out.push_str(line);
                out.push_str(
                    " # Integer: Limit completed subtasks shown in a parent before 'Expand'.",
                );
            } else if trimmed.starts_with("show_ongoing_notifications =") {
                out.push_str(line);
                out.push_str(" # Boolean: Display ongoing timer notification for active tasks.");
            } else if trimmed.starts_with("quick_filter_term =") {
                out.push_str(line);
                out.push_str(" # String: The search term toggled by the quick filter button.");
            } else if trimmed.starts_with("quick_filter_icon =") {
                out.push_str(line);
                out.push_str(" # String: Hex code or character for the quick filter button icon.");
            } else if trimmed.starts_with("show_quick_filter =") {
                out.push_str(line);
                out.push_str(" # Boolean: Display the quick filter button in the search bar.");
            } else if trimmed.starts_with("show_calendars_tab =") {
                out.push_str(line);
                out.push_str(" # Boolean: Display the Calendars tab in the sidebar.");
            } else if trimmed.starts_with("show_tags_tab =") {
                out.push_str(line);
                out.push_str(" # Boolean: Display the Tags tab in the sidebar.");
            } else if trimmed.starts_with("show_locations_tab =") {
                out.push_str(line);
                out.push_str(" # Boolean: Display the Locations tab in the sidebar.");
            } else if trimmed.starts_with("show_goals_tab =") {
                out.push_str(line);
                out.push_str(" # Boolean: Display the Goals tab in the sidebar.");
            } else if trimmed.starts_with("show_journal_tab =") {
                out.push_str(line);
                out.push_str(" # Boolean: Display the Journal tab in the sidebar.");
            } else if trimmed.starts_with("show_task_goals_in_sidebar =") {
                out.push_str(line);
                out.push_str(" # Boolean: Display task-specific goals alongside global goals.");
            } else if trimmed.starts_with("sidebar_is_hidden =") {
                out.push_str(line);
                out.push_str(" # Boolean: Hide the left sidebar collections panel.");
            } else if trimmed.starts_with("blur_when_unfocused =") {
                out.push_str(line);
                out.push_str(" # Boolean: Hide contents when window is unfocused (Privacy mode).");
            } else if trimmed.starts_with("description_editor =") {
                out.push_str(line);
                out.push_str(" # String: Editor for task descriptions. Empty uses $VISUAL/$EDITOR. 'builtin' forces internal editor.");
            } else if trimmed.starts_with("show_priority_numbers =") {
                out.push_str(line);
                out.push_str(" # Boolean: Render priority numbers (!X) visually next to tags.");
            } else if trimmed.starts_with("show_undo_snackbar =") {
                out.push_str(line);
                out.push_str(" # Boolean: Show the transient undo notification after task mutations (Android).");
            } else if trimmed.starts_with("first_day_of_week =") {
                out.push_str(line);
                out.push_str(
                    " # Enum: First day of the week for calendar/journal views (Monday or Sunday).",
                );
            } else if trimmed.starts_with("pinned_actions =") {
                out.push_str(line);
                out.push_str(" # Array: Action buttons pinned directly to GUI task rows (e.g. Start, Done, Move).");
            } else if trimmed.starts_with("collection_order =") {
                out.push_str(line);
                out.push_str(" # Array of HREFs defining the custom display order of collections in the sidebar.");
            } else if trimmed.starts_with("hidden_calendars =") {
                out.push_str("# List of calendar HREFs currently toggled 'off' in the sidebar.\n");
                out.push_str(line);
            } else if trimmed.starts_with("sort_collections_by_size =") {
                out.push_str(line);
                out.push_str(
                    " # Boolean: Automatically sort collections from most to least tasks.",
                );
            } else if trimmed.starts_with("expanded_tags =") {
                out.push_str("\n# --- UI Memory State ---\n");
                out.push_str("# Arrays remembering which tree folders are currently expanded.\n");
                out.push_str(line);
            } else if trimmed.starts_with("trash_retention_days =") {
                out.push_str(line);
                out.push_str(" # Integer: Days to keep deleted items in local trash before permanent delete. 0 disables trash.");
            } else if trimmed.starts_with("default_duration_goal_mins =") {
                out.push_str(line);
                out.push_str(" # Integer: Implicit duration added to goals when completing a task without a timer.");
            } else if trimmed.starts_with("sessions_count_as_completions =") {
                out.push_str(line);
                out.push_str(" # Boolean: If true, logging a time session counts as a completion for 'Count' goals.");
            } else if trimmed.starts_with("log_level =") {
                out.push_str(line);
                out.push_str(" # String: Logging verbosity level (Error, Warn, Info, Debug, Trace). Applies to both log file and terminal.");
            } else if trimmed.starts_with("window_width =") {
                out.push_str(line);
                out.push_str(" # Float: Last window width (GUI).");
            } else if trimmed.starts_with("window_height =") {
                out.push_str(line);
                out.push_str(" # Float: Last window height (GUI).");
            } else if trimmed.starts_with("config_version =") {
                out.push_str(
                    "# Internal version for configuration migrations. Do not edit manually.",
                );
                out.push_str(line);
            } else {
                // Pass through unhandled lines
                out.push_str(line);
            }
            out.push('\n');
        }

        out
    }
}