mach-tui 0.3.2

A terminal-first task manager for people who live in the shell and work with agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
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
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
//! SQLite-backed persistence for tasks, categories, and settings.
//!
//! A [`Store`] is an explicit instance: callers can open independent data
//! directories in one process. Writes use `BEGIN IMMEDIATE`, validate a fresh
//! snapshot, and commit tasks/categories/settings/attachment metadata plus a
//! monotonic revision in one transaction. Image bytes are immutable,
//! content-addressed files beside the database. Legacy JSON is imported once
//! and left untouched.

use std::collections::{HashMap, HashSet};
use std::fs;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::time::Duration;

use chrono::{Local, NaiveDateTime};
use image::ImageFormat;
use rusqlite::limits::Limit;
use rusqlite::{Connection, OptionalExtension, Transaction, TransactionBehavior, params};
use serde::Deserialize;
use serde::de::DeserializeOwned;
use sha2::{Digest, Sha256};
use unicode_normalization::UnicodeNormalization;
use unicode_segmentation::UnicodeSegmentation;

use crate::due;
use crate::model::{
    Block, Category, MAX_BODY_LINES, MAX_CATEGORY_COUNT, MAX_CATEGORY_DESC_LINE_LEN,
    MAX_CATEGORY_DESC_LINES, MAX_CATEGORY_NAME_LEN, MAX_IMPORTANCE, MAX_NOTES_LINE_LEN,
    MAX_TASK_COUNT, MAX_TITLE_LEN, SCHEMA_VERSION, Task, caseless_key, text_byte_limit,
};
use crate::settings::{DATE_FORMATS, PREVIEW_POSITIONS, SORTS, Settings, THEMES};

const DATABASE_FILE: &str = "mach.db";
const DATABASE_SCHEMA_VERSION: i64 = 1;
const LEGACY_MIGRATION_KEY: &str = "legacy_json_migrated";
const BUSY_TIMEOUT: Duration = Duration::from_secs(10);
const ID_MAX_BYTES: usize = 128;
const DUE_MAX_BYTES: usize = 128;
const CREATED_MAX_BYTES: usize = 64;
const SETTINGS_VALUE_MAX_BYTES: usize = 128;
const MAX_LEGACY_JSON_BYTES: u64 = 128 * 1024 * 1024;
const MAX_SQLITE_VALUE_BYTES: i32 = 8 * 1024 * 1024;
const MAX_ATTACHMENT_BYTES: u64 = 128 * 1024 * 1024;
const ATTACHMENT_ID_LEN: usize = 64;

#[derive(Debug)]
pub enum StoreError {
    Io {
        operation: &'static str,
        path: PathBuf,
        source: std::io::Error,
    },
    Json {
        path: PathBuf,
        source: serde_json::Error,
    },
    Database(rusqlite::Error),
    UnsupportedLegacySchema {
        path: PathBuf,
        found: u32,
        expected: u32,
    },
    UnsupportedDatabaseSchema {
        path: PathBuf,
        found: i64,
        expected: i64,
    },
    Conflict {
        expected: u64,
        actual: u64,
    },
    StaleEntity {
        entity: &'static str,
        id: String,
    },
    NotFound {
        entity: &'static str,
        query: String,
    },
    Ambiguous {
        entity: &'static str,
        query: String,
        matches: Vec<String>,
    },
    Validation(String),
    Corrupt(String),
}

impl StoreError {
    pub fn validation(message: impl Into<String>) -> Self {
        Self::Validation(message.into())
    }

    pub(crate) fn io(operation: &'static str, path: &Path, source: std::io::Error) -> Self {
        Self::Io {
            operation,
            path: path.to_path_buf(),
            source,
        }
    }
}

impl std::fmt::Display for StoreError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Io {
                operation,
                path,
                source,
            } => write!(f, "could not {operation} {}: {source}", path.display()),
            Self::Json { path, source } => {
                write!(f, "could not parse {}: {source}", path.display())
            }
            Self::Database(source) => write!(f, "database error: {source}"),
            Self::UnsupportedLegacySchema {
                path,
                found,
                expected,
            } => write!(
                f,
                "{} uses unsupported schema {found} (expected {expected})",
                path.display()
            ),
            Self::UnsupportedDatabaseSchema {
                path,
                found,
                expected,
            } => write!(
                f,
                "{} uses unsupported database schema {found} (expected {expected})",
                path.display()
            ),
            Self::Conflict { expected, actual } => write!(
                f,
                "store changed since it was loaded (expected revision {expected}, found {actual})"
            ),
            Self::StaleEntity { entity, id } => {
                write!(f, "{entity} {id:?} changed since it was loaded")
            }
            Self::NotFound { entity, query } => {
                write!(f, "no {entity} matching {query:?}")
            }
            Self::Ambiguous {
                entity,
                query,
                matches,
            } => write!(
                f,
                "ambiguous {entity} {query:?}; matches: {}",
                matches.join(", ")
            ),
            Self::Validation(message) => write!(f, "invalid data: {message}"),
            Self::Corrupt(message) => write!(f, "corrupt database: {message}"),
        }
    }
}

impl std::error::Error for StoreError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io { source, .. } => Some(source),
            Self::Json { source, .. } => Some(source),
            Self::Database(source) => Some(source),
            _ => None,
        }
    }
}

impl From<rusqlite::Error> for StoreError {
    fn from(value: rusqlite::Error) -> Self {
        Self::Database(value)
    }
}

#[derive(Debug, Clone, Default)]
pub struct StoreData {
    pub revision: u64,
    pub categories: Vec<Category>,
    pub tasks: Vec<Task>,
    pub settings: Settings,
    pub(crate) attachments: Vec<Attachment>,
}

/// Immutable metadata for one content-addressed image owned by this store.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Attachment {
    pub id: String,
    pub sha256: String,
    pub media_type: String,
    pub byte_len: u64,
    pub storage_name: String,
}

#[derive(Debug, Clone, Default)]
pub struct TaskPatch {
    pub title: Option<String>,
    pub body: Option<Vec<Block>>,
    pub due: Option<String>,
    pub done: Option<bool>,
    pub importance: Option<u8>,
    /// `None` leaves the category unchanged; `Some(None)` clears it.
    pub category_id: Option<Option<String>>,
}

#[derive(Debug, Clone, Default)]
pub struct CategoryPatch {
    pub name: Option<String>,
    pub description: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PurgeScope {
    All,
    Category(String),
    Uncategorized,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RelativePosition {
    Before,
    After,
}

impl StoreData {
    pub fn attachments(&self) -> &[Attachment] {
        &self.attachments
    }

    /// Resolve a task by full id or unique id prefix.
    pub fn resolve_task_id(&self, query: &str) -> Result<String, StoreError> {
        let query = query.trim();
        if query.is_empty() {
            return Err(StoreError::validation("task id cannot be empty"));
        }
        validate_byte_limit(query, ID_MAX_BYTES, "task id query")?;
        if let Some(task) = self.tasks.iter().find(|task| task.id == query) {
            return Ok(task.id.clone());
        }
        let matches: Vec<_> = self
            .tasks
            .iter()
            .filter(|task| task.id.starts_with(query))
            .map(|task| task.id.clone())
            .collect();
        match matches.as_slice() {
            [id] => Ok(id.clone()),
            [] => Err(StoreError::NotFound {
                entity: "task",
                query: query.to_string(),
            }),
            _ => Err(StoreError::Ambiguous {
                entity: "task id",
                query: query.to_string(),
                matches,
            }),
        }
    }

    /// Resolve a category by id, Unicode-caseless name, or unique name prefix.
    pub fn resolve_category_id(&self, query: &str) -> Result<String, StoreError> {
        let query = query.trim();
        if query.is_empty() {
            return Err(StoreError::validation("category name cannot be empty"));
        }
        if let Some(category) = self.categories.iter().find(|category| category.id == query) {
            return Ok(category.id.clone());
        }
        validate_byte_limit(
            query,
            text_byte_limit(MAX_CATEGORY_NAME_LEN),
            "category query",
        )?;
        let folded = category_name_key(query);
        if let Some(category) = self
            .categories
            .iter()
            .find(|category| category_name_key(&category.name) == folded)
        {
            return Ok(category.id.clone());
        }
        let matches: Vec<_> = self
            .categories
            .iter()
            .filter(|category| category_name_has_prefix(&category.name, &folded))
            .collect();
        match matches.as_slice() {
            [category] => Ok(category.id.clone()),
            [] => Err(StoreError::NotFound {
                entity: "category",
                query: query.to_string(),
            }),
            _ => Err(StoreError::Ambiguous {
                entity: "category",
                query: query.to_string(),
                matches: matches
                    .into_iter()
                    .map(|category| category.name.clone())
                    .collect(),
            }),
        }
    }

    pub fn task(&self, id: &str) -> Result<&Task, StoreError> {
        self.tasks
            .iter()
            .find(|task| task.id == id)
            .ok_or_else(|| StoreError::NotFound {
                entity: "task",
                query: id.to_string(),
            })
    }

    pub fn category(&self, id: &str) -> Result<&Category, StoreError> {
        self.categories
            .iter()
            .find(|category| category.id == id)
            .ok_or_else(|| StoreError::NotFound {
                entity: "category",
                query: id.to_string(),
            })
    }

    pub fn create_task(
        &mut self,
        title: impl Into<String>,
        body: Vec<Block>,
        due: impl Into<String>,
        importance: u8,
        category_id: Option<String>,
    ) -> Result<Task, StoreError> {
        if importance > MAX_IMPORTANCE {
            return Err(StoreError::validation(format!(
                "importance must be 0-{MAX_IMPORTANCE}"
            )));
        }
        let title = title.into();
        let due = due.into();
        let mut task = Task::new(&title, importance, category_id, &due);
        task.body = body;
        self.insert_task(task)
    }

    pub fn insert_task(&mut self, task: Task) -> Result<Task, StoreError> {
        let index = self.tasks.len();
        self.tasks.push(task);
        if let Err(error) = self.normalize_and_validate_new_write() {
            self.tasks.truncate(index);
            return Err(error);
        }
        Ok(self.tasks[index].clone())
    }

    pub fn edit_task(&mut self, id: &str, patch: TaskPatch) -> Result<Task, StoreError> {
        let index = self
            .tasks
            .iter()
            .position(|task| task.id == id)
            .ok_or_else(|| StoreError::NotFound {
                entity: "task",
                query: id.to_string(),
            })?;
        let before = self.tasks[index].clone();
        {
            let task = &mut self.tasks[index];
            if let Some(title) = patch.title {
                task.title = title;
            }
            if let Some(body) = patch.body {
                task.body = body;
            }
            if let Some(due) = patch.due {
                task.due = due;
            }
            if let Some(done) = patch.done {
                task.done = done;
            }
            if let Some(importance) = patch.importance {
                task.importance = importance;
            }
            if let Some(category_id) = patch.category_id {
                task.category_id = category_id;
            }
        }
        if let Err(error) = self.normalize_and_validate_new_write() {
            self.tasks[index] = before;
            return Err(error);
        }
        Ok(self.tasks[index].clone())
    }

    /// Apply only the fields represented by `patch`, but fail if one of those
    /// fields changed since `expected` was loaded. Unrelated concurrent edits
    /// (for example toggling `done` while a title form is open) are preserved.
    pub fn edit_task_if_unchanged(
        &mut self,
        expected: &Task,
        patch: TaskPatch,
    ) -> Result<Task, StoreError> {
        let current = self
            .tasks
            .iter()
            .find(|task| task.id == expected.id)
            .ok_or_else(|| StoreError::StaleEntity {
                entity: "task",
                id: expected.id.clone(),
            })?;
        let stale = field_conflicts(patch.title.as_ref(), &current.title, &expected.title)
            || field_conflicts(patch.body.as_ref(), &current.body, &expected.body)
            || field_conflicts(patch.due.as_ref(), &current.due, &expected.due)
            || field_conflicts(patch.done.as_ref(), &current.done, &expected.done)
            || field_conflicts(
                patch.importance.as_ref(),
                &current.importance,
                &expected.importance,
            )
            || field_conflicts(
                patch.category_id.as_ref(),
                &current.category_id,
                &expected.category_id,
            );
        if stale {
            return Err(StoreError::StaleEntity {
                entity: "task",
                id: expected.id.clone(),
            });
        }
        self.edit_task(&expected.id, patch)
    }

    pub fn delete_task(&mut self, id: &str) -> Result<Task, StoreError> {
        let index = self
            .tasks
            .iter()
            .position(|task| task.id == id)
            .ok_or_else(|| StoreError::NotFound {
                entity: "task",
                query: id.to_string(),
            })?;
        Ok(self.tasks.remove(index))
    }

    pub fn set_task_done(&mut self, id: &str, done: bool) -> Result<Task, StoreError> {
        self.edit_task(
            id,
            TaskPatch {
                done: Some(done),
                ..TaskPatch::default()
            },
        )
    }

    pub fn toggle_task_done(&mut self, id: &str) -> Result<Task, StoreError> {
        let done = !self.task(id)?.done;
        self.set_task_done(id, done)
    }

    pub fn set_task_importance(&mut self, id: &str, importance: u8) -> Result<Task, StoreError> {
        self.edit_task(
            id,
            TaskPatch {
                importance: Some(importance),
                ..TaskPatch::default()
            },
        )
    }

    pub fn set_task_category(
        &mut self,
        id: &str,
        category_id: Option<String>,
    ) -> Result<Task, StoreError> {
        self.edit_task(
            id,
            TaskPatch {
                category_id: Some(category_id),
                ..TaskPatch::default()
            },
        )
    }

    pub fn move_task(&mut self, id: &str, target: usize) -> Result<(), StoreError> {
        let index = self
            .tasks
            .iter()
            .position(|task| task.id == id)
            .ok_or_else(|| StoreError::NotFound {
                entity: "task",
                query: id.to_string(),
            })?;
        if target >= self.tasks.len() {
            return Err(StoreError::validation(format!(
                "task target index {target} is out of range"
            )));
        }
        if self.tasks[index].category_id != self.tasks[target].category_id {
            return Err(StoreError::validation(
                "tasks can only be reordered within the same category",
            ));
        }
        let task = self.tasks.remove(index);
        self.tasks.insert(target, task);
        Ok(())
    }

    pub fn move_task_relative(
        &mut self,
        id: &str,
        target_id: &str,
        position: RelativePosition,
    ) -> Result<Task, StoreError> {
        let id = id.to_string();
        let target_id = target_id.to_string();
        if id == target_id {
            return Err(StoreError::validation(
                "cannot move a task relative to itself",
            ));
        }
        let index = self
            .tasks
            .iter()
            .position(|task| task.id == id)
            .ok_or_else(|| StoreError::NotFound {
                entity: "task",
                query: id.clone(),
            })?;
        let target_index = self
            .tasks
            .iter()
            .position(|task| task.id == target_id)
            .ok_or_else(|| StoreError::NotFound {
                entity: "task",
                query: target_id.clone(),
            })?;
        if self.tasks[index].category_id != self.tasks[target_index].category_id {
            return Err(StoreError::validation(
                "tasks can only be reordered within the same category",
            ));
        }
        let task = self.tasks.remove(index);
        let target_after_removal = self
            .tasks
            .iter()
            .position(|candidate| candidate.id == target_id)
            .ok_or_else(|| {
                StoreError::Corrupt(format!("task {target_id:?} disappeared during reorder"))
            })?;
        let insertion = match position {
            RelativePosition::Before => target_after_removal,
            RelativePosition::After => target_after_removal + 1,
        };
        self.tasks.insert(insertion, task.clone());
        Ok(task)
    }

    pub fn purge_completed(&mut self, scope: &PurgeScope) -> Result<Vec<Task>, StoreError> {
        if let PurgeScope::Category(id) = scope {
            self.category(id)?;
        }
        let mut removed = Vec::new();
        self.tasks.retain(|task| {
            let in_scope = match scope {
                PurgeScope::All => true,
                PurgeScope::Category(id) => task.category_id.as_deref() == Some(id),
                PurgeScope::Uncategorized => task.category_id.is_none(),
            };
            if task.done && in_scope {
                removed.push(task.clone());
                false
            } else {
                true
            }
        });
        Ok(removed)
    }

    /// Purge only the completed tasks captured by a confirmation prompt.
    pub fn purge_completed_ids(&mut self, ids: &[String]) -> Result<Vec<Task>, StoreError> {
        let ids: HashSet<_> = ids.iter().map(String::as_str).collect();
        let mut removed = Vec::new();
        self.tasks.retain(|task| {
            if task.done && ids.contains(task.id.as_str()) {
                removed.push(task.clone());
                false
            } else {
                true
            }
        });
        Ok(removed)
    }

    pub fn create_category(
        &mut self,
        name: impl Into<String>,
        description: impl Into<String>,
    ) -> Result<Category, StoreError> {
        let name = name.into();
        let mut category = Category::new(&name);
        category.description = description.into();
        self.insert_category(category)
    }

    pub fn insert_category(&mut self, category: Category) -> Result<Category, StoreError> {
        let index = self.categories.len();
        self.categories.push(category);
        if let Err(error) = self.normalize_and_validate_new_write() {
            self.categories.truncate(index);
            return Err(error);
        }
        Ok(self.categories[index].clone())
    }

    pub fn edit_category(
        &mut self,
        id: &str,
        patch: CategoryPatch,
    ) -> Result<Category, StoreError> {
        let index = self
            .categories
            .iter()
            .position(|category| category.id == id)
            .ok_or_else(|| StoreError::NotFound {
                entity: "category",
                query: id.to_string(),
            })?;
        let before = self.categories[index].clone();
        {
            let category = &mut self.categories[index];
            if let Some(name) = patch.name {
                category.name = name;
            }
            if let Some(description) = patch.description {
                category.description = description;
            }
        }
        if let Err(error) = self.normalize_and_validate_new_write() {
            self.categories[index] = before;
            return Err(error);
        }
        Ok(self.categories[index].clone())
    }

    pub fn edit_category_if_unchanged(
        &mut self,
        expected: &Category,
        patch: CategoryPatch,
    ) -> Result<Category, StoreError> {
        let current = self
            .categories
            .iter()
            .find(|category| category.id == expected.id)
            .ok_or_else(|| StoreError::StaleEntity {
                entity: "category",
                id: expected.id.clone(),
            })?;
        let stale = field_conflicts(patch.name.as_ref(), &current.name, &expected.name)
            || field_conflicts(
                patch.description.as_ref(),
                &current.description,
                &expected.description,
            );
        if stale {
            return Err(StoreError::StaleEntity {
                entity: "category",
                id: expected.id.clone(),
            });
        }
        self.edit_category(&expected.id, patch)
    }

    /// Delete a category while preserving its tasks as uncategorized.
    pub fn delete_category(&mut self, id: &str) -> Result<Category, StoreError> {
        let index = self
            .categories
            .iter()
            .position(|category| category.id == id)
            .ok_or_else(|| StoreError::NotFound {
                entity: "category",
                query: id.to_string(),
            })?;
        let category = self.categories.remove(index);
        for task in &mut self.tasks {
            if task.category_id.as_deref() == Some(id) {
                task.category_id = None;
            }
        }
        Ok(category)
    }

    pub fn move_category(&mut self, id: &str, target: usize) -> Result<(), StoreError> {
        let index = self
            .categories
            .iter()
            .position(|category| category.id == id)
            .ok_or_else(|| StoreError::NotFound {
                entity: "category",
                query: id.to_string(),
            })?;
        if target >= self.categories.len() {
            return Err(StoreError::validation(format!(
                "category target index {target} is out of range"
            )));
        }
        let category = self.categories.remove(index);
        self.categories.insert(target, category);
        Ok(())
    }

    pub fn move_category_relative(
        &mut self,
        id: &str,
        target_id: &str,
        position: RelativePosition,
    ) -> Result<Category, StoreError> {
        if id == target_id {
            return Err(StoreError::validation(
                "cannot move a category relative to itself",
            ));
        }
        let index = self
            .categories
            .iter()
            .position(|category| category.id == id)
            .ok_or_else(|| StoreError::NotFound {
                entity: "category",
                query: id.to_string(),
            })?;
        if !self
            .categories
            .iter()
            .any(|category| category.id == target_id)
        {
            return Err(StoreError::NotFound {
                entity: "category",
                query: target_id.to_string(),
            });
        }
        let category = self.categories.remove(index);
        let target_after_removal = self
            .categories
            .iter()
            .position(|candidate| candidate.id == target_id)
            .ok_or_else(|| {
                StoreError::Corrupt(format!("category {target_id:?} disappeared during reorder"))
            })?;
        let insertion = match position {
            RelativePosition::Before => target_after_removal,
            RelativePosition::After => target_after_removal + 1,
        };
        self.categories.insert(insertion, category.clone());
        Ok(category)
    }

    pub fn replace_settings(&mut self, settings: Settings) -> Result<Settings, StoreError> {
        let before = std::mem::replace(&mut self.settings, settings);
        if let Err(error) = validate_settings(&self.settings) {
            self.settings = before;
            return Err(error);
        }
        Ok(self.settings.clone())
    }

    pub fn update_settings(
        &mut self,
        operation: impl FnOnce(&mut Settings),
    ) -> Result<Settings, StoreError> {
        let before = self.settings.clone();
        operation(&mut self.settings);
        if let Err(error) = validate_settings(&self.settings) {
            self.settings = before;
            return Err(error);
        }
        Ok(self.settings.clone())
    }

    fn normalize_and_validate_new_write(&mut self) -> Result<(), StoreError> {
        normalize_and_validate(
            self,
            Local::now().naive_local(),
            DueMode::NewWrite,
            AttachmentMode::Draft,
        )
    }
}

/// A three-way field merge conflicts only when the remote and desired values
/// both diverged from the captured base in different directions.
fn field_conflicts<T: PartialEq>(desired: Option<&T>, current: &T, expected: &T) -> bool {
    desired.is_some_and(|desired| current != expected && current != desired)
}

#[derive(Debug, Clone)]
pub struct Paths {
    pub dir: PathBuf,
    pub database: PathBuf,
    pub tasks: PathBuf,
    pub categories: PathBuf,
    pub settings: PathBuf,
    pub images: PathBuf,
}

impl Paths {
    fn new(dir: PathBuf) -> Self {
        Self {
            database: dir.join(DATABASE_FILE),
            tasks: dir.join("tasks.json"),
            categories: dir.join("categories.json"),
            settings: dir.join("settings.json"),
            images: dir.join("images"),
            dir,
        }
    }
}

pub struct Store {
    connection: Connection,
    paths: Paths,
    persistent_attachments: bool,
}

impl Store {
    pub fn open(dir: impl AsRef<Path>) -> Result<Self, StoreError> {
        let paths = Paths::new(expand_user(dir.as_ref().to_path_buf())?);
        ensure_private_directory(&paths.dir)?;
        prepare_private_database_file(&paths.database)?;
        let mut connection = Connection::open(&paths.database)?;
        set_private_file(&paths.database)?;
        connection.busy_timeout(BUSY_TIMEOUT)?;
        configure_resource_limits(&connection)?;
        initialize_schema(&mut connection, &paths.database)?;
        configure_connection(&connection)?;
        quick_check(&connection)?;
        let mut store = Self {
            connection,
            paths,
            persistent_attachments: true,
        };
        store.migrate_legacy_json()?;
        Ok(store)
    }

    /// Open an ephemeral store with no filesystem persistence.
    ///
    /// `data_dir` is only the logical base for relative image references. The
    /// directory is not created or modified.
    pub fn open_in_memory_with_paths(data_dir: impl AsRef<Path>) -> Result<Self, StoreError> {
        let paths = Paths::new(expand_user(data_dir.as_ref().to_path_buf())?);
        let mut connection = Connection::open_in_memory()?;
        connection.busy_timeout(BUSY_TIMEOUT)?;
        configure_resource_limits(&connection)?;
        initialize_schema(&mut connection, Path::new(":memory:"))?;
        configure_in_memory_connection(&connection)?;
        quick_check(&connection)?;
        connection.execute(
            "INSERT INTO metadata(key, value) VALUES (?1, '1')",
            [LEGACY_MIGRATION_KEY],
        )?;
        Ok(Self {
            connection,
            paths,
            persistent_attachments: false,
        })
    }

    pub fn open_default(explicit: Option<PathBuf>) -> Result<Self, StoreError> {
        Self::open(resolve_data_dir(explicit)?)
    }

    pub fn paths(&self) -> &Paths {
        &self.paths
    }

    pub fn data_dir(&self) -> &Path {
        &self.paths.dir
    }

    pub fn images_dir(&self) -> &Path {
        &self.paths.images
    }

    pub fn database_path(&self) -> &Path {
        &self.paths.database
    }

    /// Cheap external-change probe for a long-running TUI.
    pub fn revision(&self) -> Result<u64, StoreError> {
        read_revision(&self.connection)
    }

    pub fn snapshot(&self) -> Result<StoreData, StoreError> {
        let tx = self.connection.unchecked_transaction()?;
        let data = load_snapshot(&tx)?;
        tx.commit()?;
        Ok(data)
    }

    pub fn load_settings(&self) -> Result<Settings, StoreError> {
        Ok(self.snapshot()?.settings)
    }

    pub fn save_settings(&mut self, settings: &Settings) -> Result<(), StoreError> {
        self.update(|data| {
            data.replace_settings(settings.clone())?;
            Ok(())
        })
    }

    /// Run a read-modify-write against a fresh snapshot under
    /// `BEGIN IMMEDIATE`. Every successful call increments `revision` once.
    pub fn update<R>(
        &mut self,
        operation: impl FnOnce(&mut StoreData) -> Result<R, StoreError>,
    ) -> Result<R, StoreError> {
        self.update_with_snapshot(operation)
            .map(|(result, _)| result)
    }

    /// Commit a mutation and return the exact normalized snapshot that was
    /// persisted, including its new revision.
    pub fn update_with_snapshot<R>(
        &mut self,
        operation: impl FnOnce(&mut StoreData) -> Result<R, StoreError>,
    ) -> Result<(R, StoreData), StoreError> {
        self.update_inner(None, operation)
    }

    /// Apply a mutation only if the caller's snapshot is still current.
    pub fn update_if_revision<R>(
        &mut self,
        expected_revision: u64,
        operation: impl FnOnce(&mut StoreData) -> Result<R, StoreError>,
    ) -> Result<R, StoreError> {
        self.update_if_revision_with_snapshot(expected_revision, operation)
            .map(|(result, _)| result)
    }

    pub fn update_if_revision_with_snapshot<R>(
        &mut self,
        expected_revision: u64,
        operation: impl FnOnce(&mut StoreData) -> Result<R, StoreError>,
    ) -> Result<(R, StoreData), StoreError> {
        self.update_inner(Some(expected_revision), operation)
    }

    fn update_inner<R>(
        &mut self,
        expected_revision: Option<u64>,
        operation: impl FnOnce(&mut StoreData) -> Result<R, StoreError>,
    ) -> Result<(R, StoreData), StoreError> {
        let images_root = self
            .persistent_attachments
            .then(|| self.paths.images.clone());
        let tx = self
            .connection
            .transaction_with_behavior(TransactionBehavior::Immediate)?;
        let before = load_snapshot(&tx)?;
        let base_revision = before.revision;
        if let Some(expected) = expected_revision
            && expected != base_revision
        {
            return Err(StoreError::Conflict {
                expected,
                actual: base_revision,
            });
        }
        let mut data = before.clone();
        let result = operation(&mut data)?;
        import_task_attachments(&mut data, images_root.as_deref())?;
        normalize_and_validate(
            &mut data,
            Local::now().naive_local(),
            DueMode::NewWrite,
            AttachmentMode::Persisted,
        )?;
        let next_revision = base_revision
            .checked_add(1)
            .ok_or_else(|| StoreError::Corrupt("revision overflow".into()))?;
        data.revision = next_revision;
        persist_diff(&tx, &before, &data)?;
        tx.commit()?;
        Ok((result, data))
    }

    fn migrate_legacy_json(&mut self) -> Result<(), StoreError> {
        let images_root = self.paths.images.clone();
        let tx = self
            .connection
            .transaction_with_behavior(TransactionBehavior::Immediate)?;
        if migration_complete(&tx)? {
            tx.commit()?;
            return Ok(());
        }

        let existing = load_snapshot(&tx)?;
        if !existing.categories.is_empty()
            || !existing.tasks.is_empty()
            || !existing.attachments.is_empty()
            || existing.revision != 0
        {
            return Err(StoreError::Corrupt(
                "database contains data but has no completed legacy migration marker".into(),
            ));
        }

        let categories_file = read_optional_json::<CategoriesFile>(&self.paths.categories)?;
        let tasks_file = read_optional_json::<TasksFile>(&self.paths.tasks)?;
        let settings = read_optional_json::<Settings>(&self.paths.settings)?;
        validate_legacy_schema(
            &self.paths.categories,
            categories_file.as_ref().map(|f| f.schema),
        )?;
        validate_legacy_schema(&self.paths.tasks, tasks_file.as_ref().map(|f| f.schema))?;

        let has_legacy = categories_file.is_some() || tasks_file.is_some() || settings.is_some();
        if has_legacy {
            let mut data = StoreData {
                revision: 1,
                categories: categories_file
                    .map(|file| {
                        file.categories
                            .into_iter()
                            .filter(|category| !category.is_all())
                            .collect()
                    })
                    .unwrap_or_default(),
                tasks: tasks_file.map(|file| file.tasks).unwrap_or_default(),
                settings: settings.unwrap_or_default().normalized(),
                attachments: Vec::new(),
            };
            import_task_attachments(&mut data, Some(&images_root))?;
            // Legacy relative values used the reader's current date/year. Freeze
            // that interpretation now so it cannot drift after migration.
            normalize_and_validate(
                &mut data,
                Local::now().naive_local(),
                DueMode::LegacyMigration,
                AttachmentMode::Persisted,
            )?;
            persist_diff(&tx, &existing, &data)?;
        }
        tx.execute(
            "INSERT INTO metadata(key, value) VALUES (?1, '1')",
            [LEGACY_MIGRATION_KEY],
        )?;
        tx.commit()?;
        Ok(())
    }
}

pub fn resolve_data_dir(explicit: Option<PathBuf>) -> Result<PathBuf, StoreError> {
    resolve_data_dir_from(
        explicit,
        std::env::var_os("MACH_DIR").map(PathBuf::from),
        dirs::home_dir(),
    )
}

fn resolve_data_dir_from(
    explicit: Option<PathBuf>,
    configured: Option<PathBuf>,
    home: Option<PathBuf>,
) -> Result<PathBuf, StoreError> {
    if let Some(dir) = explicit {
        return expand_user_with_home(dir, home.as_deref());
    }
    if let Some(dir) = configured {
        return expand_user_with_home(dir, home.as_deref());
    }
    home.map(|home| home.join(".mach")).ok_or_else(|| {
        StoreError::validation("could not determine the home directory; use --dir or set MACH_DIR")
    })
}

fn expand_user(path: PathBuf) -> Result<PathBuf, StoreError> {
    let home = dirs::home_dir();
    expand_user_with_home(path, home.as_deref())
}

fn expand_user_with_home(path: PathBuf, home: Option<&Path>) -> Result<PathBuf, StoreError> {
    if path.as_os_str().is_empty() {
        return Err(StoreError::validation("data directory cannot be empty"));
    }
    let text = path.to_string_lossy();
    if text == "~" {
        return home
            .map(Path::to_path_buf)
            .ok_or_else(|| StoreError::validation("cannot expand ~ without a home directory"));
    }
    if let Some(rest) = text.strip_prefix("~/") {
        return home
            .map(|home| home.join(rest))
            .ok_or_else(|| StoreError::validation("cannot expand ~ without a home directory"));
    }
    Ok(path)
}

pub(crate) fn configure_connection(connection: &Connection) -> Result<(), StoreError> {
    connection.pragma_update(None, "foreign_keys", "ON")?;
    connection.pragma_update(None, "synchronous", "FULL")?;
    let mode: String = connection.query_row("PRAGMA journal_mode=WAL", [], |row| row.get(0))?;
    if !mode.eq_ignore_ascii_case("wal") {
        return Err(StoreError::Corrupt(format!(
            "SQLite refused WAL mode (using {mode})"
        )));
    }
    Ok(())
}

pub(crate) fn configure_resource_limits(connection: &Connection) -> Result<(), StoreError> {
    connection.set_limit(Limit::SQLITE_LIMIT_LENGTH, MAX_SQLITE_VALUE_BYTES)?;
    Ok(())
}

fn configure_in_memory_connection(connection: &Connection) -> Result<(), StoreError> {
    connection.pragma_update(None, "foreign_keys", "ON")?;
    connection.pragma_update(None, "journal_mode", "MEMORY")?;
    Ok(())
}

fn initialize_schema(connection: &mut Connection, path: &Path) -> Result<(), StoreError> {
    let version: i64 = connection.query_row("PRAGMA user_version", [], |row| row.get(0))?;
    if version != 0 && version != DATABASE_SCHEMA_VERSION {
        return Err(StoreError::UnsupportedDatabaseSchema {
            path: path.to_path_buf(),
            found: version,
            expected: DATABASE_SCHEMA_VERSION,
        });
    }

    let tx = connection.transaction_with_behavior(TransactionBehavior::Immediate)?;
    tx.execute_batch(
        "
        CREATE TABLE IF NOT EXISTS metadata (
            key TEXT PRIMARY KEY,
            value TEXT NOT NULL
        ) STRICT;
        CREATE TABLE IF NOT EXISTS app_state (
            id INTEGER PRIMARY KEY CHECK (id = 1),
            revision INTEGER NOT NULL CHECK (revision >= 0),
            settings_json TEXT NOT NULL
        ) STRICT;
        CREATE TABLE IF NOT EXISTS categories (
            id TEXT PRIMARY KEY,
            position INTEGER NOT NULL UNIQUE CHECK (position >= 0),
            name TEXT NOT NULL CHECK (length(trim(name)) > 0),
            name_key TEXT NOT NULL UNIQUE CHECK (length(name_key) > 0),
            description TEXT NOT NULL
        ) STRICT;
        CREATE TABLE IF NOT EXISTS tasks (
            id TEXT PRIMARY KEY,
            position INTEGER NOT NULL UNIQUE CHECK (position >= 0),
            title TEXT NOT NULL CHECK (length(trim(title)) > 0),
            body_json TEXT NOT NULL,
            due TEXT NOT NULL,
            created TEXT NOT NULL,
            done INTEGER NOT NULL CHECK (done IN (0, 1)),
            importance INTEGER NOT NULL CHECK (importance BETWEEN 0 AND 3),
            category_id TEXT REFERENCES categories(id) ON DELETE SET NULL
        ) STRICT;
        CREATE TABLE IF NOT EXISTS attachments (
            id TEXT PRIMARY KEY,
            sha256 TEXT NOT NULL UNIQUE CHECK (sha256 = id),
            media_type TEXT NOT NULL,
            byte_len INTEGER NOT NULL CHECK (byte_len > 0),
            storage_name TEXT NOT NULL UNIQUE
        ) STRICT;
        CREATE TABLE IF NOT EXISTS task_attachments (
            task_id TEXT NOT NULL REFERENCES tasks(id) ON DELETE CASCADE,
            block_index INTEGER NOT NULL CHECK (block_index >= 0),
            attachment_id TEXT NOT NULL REFERENCES attachments(id) ON DELETE RESTRICT,
            PRIMARY KEY (task_id, block_index)
        ) STRICT;
        CREATE INDEX IF NOT EXISTS task_attachments_by_attachment
            ON task_attachments(attachment_id);
        ",
    )?;
    let settings = serde_json::to_string(&Settings::default()).map_err(|source| {
        StoreError::Corrupt(format!("could not encode default settings: {source}"))
    })?;
    tx.execute(
        "INSERT OR IGNORE INTO app_state(id, revision, settings_json) VALUES (1, 0, ?1)",
        [settings],
    )?;
    if version == 0 {
        tx.pragma_update(None, "user_version", DATABASE_SCHEMA_VERSION)?;
    }
    tx.commit()?;
    Ok(())
}

fn quick_check(connection: &Connection) -> Result<(), StoreError> {
    let result: String = connection.query_row("PRAGMA quick_check(1)", [], |row| row.get(0))?;
    if result != "ok" {
        return Err(StoreError::Corrupt(format!(
            "SQLite quick check failed: {result}"
        )));
    }
    Ok(())
}

fn migration_complete(connection: &Connection) -> Result<bool, StoreError> {
    let value: Option<String> = connection
        .query_row(
            "SELECT value FROM metadata WHERE key = ?1",
            [LEGACY_MIGRATION_KEY],
            |row| row.get(0),
        )
        .optional()?;
    Ok(value.as_deref() == Some("1"))
}

fn read_revision(connection: &Connection) -> Result<u64, StoreError> {
    let value: i64 =
        connection.query_row("SELECT revision FROM app_state WHERE id = 1", [], |row| {
            row.get(0)
        })?;
    u64::try_from(value).map_err(|_| StoreError::Corrupt(format!("negative revision {value}")))
}

fn load_snapshot(connection: &Connection) -> Result<StoreData, StoreError> {
    let (revision, settings_json): (i64, String) = connection.query_row(
        "SELECT revision, settings_json FROM app_state WHERE id = 1",
        [],
        |row| Ok((row.get(0)?, row.get(1)?)),
    )?;
    let revision = u64::try_from(revision)
        .map_err(|_| StoreError::Corrupt(format!("negative revision {revision}")))?;
    let settings: Settings = serde_json::from_str(&settings_json)
        .map_err(|error| StoreError::Corrupt(format!("invalid settings JSON: {error}")))?;

    let mut attachment_statement = connection.prepare(
        "SELECT id, sha256, media_type, byte_len, storage_name FROM attachments ORDER BY id",
    )?;
    let attachment_rows = attachment_statement.query_map([], |row| {
        Ok((
            row.get::<_, String>(0)?,
            row.get::<_, String>(1)?,
            row.get::<_, String>(2)?,
            row.get::<_, i64>(3)?,
            row.get::<_, String>(4)?,
        ))
    })?;
    let mut attachments = Vec::new();
    for row in attachment_rows {
        let (id, sha256, media_type, byte_len, storage_name) = row?;
        attachments.push(Attachment {
            id,
            sha256,
            media_type,
            byte_len: u64::try_from(byte_len).map_err(|_| {
                StoreError::Corrupt(format!("attachment has invalid byte length {byte_len}"))
            })?,
            storage_name,
        });
    }

    let mut categories_statement = connection.prepare(
        "SELECT position, id, name, name_key, description FROM categories ORDER BY position",
    )?;
    let category_rows = categories_statement.query_map([], |row| {
        Ok((
            row.get::<_, i64>(0)?,
            Category {
                id: row.get(1)?,
                name: row.get(2)?,
                description: row.get(4)?,
            },
            row.get::<_, String>(3)?,
        ))
    })?;
    let mut categories = Vec::new();
    for (expected_position, row) in category_rows.enumerate() {
        if expected_position >= MAX_CATEGORY_COUNT {
            return Err(StoreError::Corrupt(format!(
                "category count exceeds {MAX_CATEGORY_COUNT}"
            )));
        }
        let (stored_position, category, stored_name_key) = row?;
        validate_stored_position(stored_position, expected_position, "category")?;
        let expected_name_key = category_name_key(&category.name);
        if stored_name_key != expected_name_key {
            return Err(StoreError::Corrupt(format!(
                "category {:?} has identity key {stored_name_key:?}, expected {expected_name_key:?}",
                category.id
            )));
        }
        categories.push(category);
    }

    let mut tasks_statement = connection.prepare(
        "SELECT position, id, title, body_json, due, created, done, importance, category_id
         FROM tasks ORDER BY position",
    )?;
    let rows = tasks_statement.query_map([], |row| {
        Ok((
            row.get::<_, i64>(0)?,
            row.get::<_, String>(1)?,
            row.get::<_, String>(2)?,
            row.get::<_, String>(3)?,
            row.get::<_, String>(4)?,
            row.get::<_, String>(5)?,
            row.get::<_, i64>(6)?,
            row.get::<_, i64>(7)?,
            row.get::<_, Option<String>>(8)?,
        ))
    })?;
    let mut tasks = Vec::new();
    for (expected_position, row) in rows.enumerate() {
        if expected_position >= MAX_TASK_COUNT {
            return Err(StoreError::Corrupt(format!(
                "task count exceeds {MAX_TASK_COUNT}"
            )));
        }
        let (stored_position, id, title, body_json, due, created, done, importance, category_id) =
            row?;
        validate_stored_position(stored_position, expected_position, "task")?;
        let body = serde_json::from_str::<Vec<Block>>(&body_json).map_err(|error| {
            StoreError::Corrupt(format!("task {id:?} has invalid body JSON: {error}"))
        })?;
        let importance = u8::try_from(importance).map_err(|_| {
            StoreError::Corrupt(format!("task {id:?} has invalid importance {importance}"))
        })?;
        tasks.push(Task {
            id,
            title,
            body,
            due,
            created,
            done: done != 0,
            importance,
            category_id,
        });
    }
    validate_task_attachment_rows(connection, &tasks)?;
    let mut data = StoreData {
        revision,
        categories,
        tasks,
        settings,
        attachments,
    };
    normalize_and_validate(
        &mut data,
        Local::now().naive_local(),
        DueMode::Stored,
        AttachmentMode::Persisted,
    )
    .map_err(|error| match error {
        StoreError::Validation(message) => StoreError::Corrupt(message),
        other => other,
    })?;
    Ok(data)
}

fn validate_task_attachment_rows(
    connection: &Connection,
    tasks: &[Task],
) -> Result<(), StoreError> {
    let expected: HashSet<(String, usize, String)> = tasks
        .iter()
        .flat_map(|task| {
            task.body
                .iter()
                .enumerate()
                .filter_map(|(block_index, block)| match block {
                    Block::Image { attachment_id } => {
                        Some((task.id.clone(), block_index, attachment_id.clone()))
                    }
                    _ => None,
                })
        })
        .collect();
    let mut statement = connection.prepare(
        "SELECT task_id, block_index, attachment_id
         FROM task_attachments ORDER BY task_id, block_index",
    )?;
    let rows = statement.query_map([], |row| {
        Ok((
            row.get::<_, String>(0)?,
            row.get::<_, i64>(1)?,
            row.get::<_, String>(2)?,
        ))
    })?;
    let mut stored = HashSet::new();
    for row in rows {
        let (task_id, block_index, attachment_id) = row?;
        let block_index = usize::try_from(block_index).map_err(|_| {
            StoreError::Corrupt(format!(
                "task {task_id:?} has invalid attachment reference index {block_index}"
            ))
        })?;
        stored.insert((task_id, block_index, attachment_id));
    }
    if stored != expected {
        return Err(StoreError::Corrupt(
            "task attachment reference rows do not match task body JSON".into(),
        ));
    }
    Ok(())
}

fn validate_stored_position(stored: i64, expected: usize, entity: &str) -> Result<(), StoreError> {
    let expected = i64::try_from(expected)
        .map_err(|_| StoreError::Corrupt(format!("{entity} position exceeds integer range")))?;
    if stored != expected {
        return Err(StoreError::Corrupt(format!(
            "{entity} position {stored} is not contiguous (expected {expected})"
        )));
    }
    Ok(())
}

/// Persist only rows whose identity, content, or position changed.
///
/// Positions and category names have UNIQUE constraints. Rows that move or
/// change names are first assigned transaction-private values outside the
/// validated application domain, which makes swaps and insertions safe without
/// deleting and recreating unrelated rows.
fn persist_diff(
    tx: &Transaction<'_>,
    before: &StoreData,
    after: &StoreData,
) -> Result<(), StoreError> {
    let before_categories: HashMap<&str, (usize, &Category)> = before
        .categories
        .iter()
        .enumerate()
        .map(|(position, category)| (category.id.as_str(), (position, category)))
        .collect();
    let after_categories: HashMap<&str, (usize, &Category)> = after
        .categories
        .iter()
        .enumerate()
        .map(|(position, category)| (category.id.as_str(), (position, category)))
        .collect();
    let before_tasks: HashMap<&str, (usize, &Task)> = before
        .tasks
        .iter()
        .enumerate()
        .map(|(position, task)| (task.id.as_str(), (position, task)))
        .collect();
    let after_tasks: HashMap<&str, (usize, &Task)> = after
        .tasks
        .iter()
        .enumerate()
        .map(|(position, task)| (task.id.as_str(), (position, task)))
        .collect();
    let before_attachments: HashMap<&str, &Attachment> = before
        .attachments
        .iter()
        .map(|attachment| (attachment.id.as_str(), attachment))
        .collect();
    let after_attachments: HashMap<&str, &Attachment> = after
        .attachments
        .iter()
        .map(|attachment| (attachment.id.as_str(), attachment))
        .collect();

    for attachment in &before.attachments {
        if after_attachments.get(attachment.id.as_str()).copied() != Some(attachment) {
            return Err(StoreError::Validation(format!(
                "attachment {:?} metadata is immutable",
                attachment.id
            )));
        }
    }
    for attachment in &after.attachments {
        if !before_attachments.contains_key(attachment.id.as_str()) {
            tx.execute(
                "INSERT INTO attachments(id, sha256, media_type, byte_len, storage_name)
                 VALUES (?1, ?2, ?3, ?4, ?5)",
                params![
                    attachment.id,
                    attachment.sha256,
                    attachment.media_type,
                    sqlite_attachment_size(attachment.byte_len)?,
                    attachment.storage_name,
                ],
            )?;
        }
    }

    // Remove tasks first so deleting a task and its category does not produce
    // an unnecessary ON DELETE SET NULL update.
    for task in &before.tasks {
        if !after_tasks.contains_key(task.id.as_str()) {
            execute_one(
                tx,
                "DELETE FROM tasks WHERE id = ?1",
                [task.id.as_str()],
                "task",
                &task.id,
            )?;
        }
    }

    // Free every old identity key that may be replaced. Control characters are
    // rejected by validation, so these temporary values cannot collide with
    // application data and are never visible outside this transaction.
    let mut temporary_name_index = 0usize;
    for category in &before.categories {
        let name_changed_or_removed = after_categories
            .get(category.id.as_str())
            .is_none_or(|(_, current)| current.name != category.name);
        if name_changed_or_removed {
            let temporary_name = format!("\u{1f}mach-category-{temporary_name_index}");
            temporary_name_index += 1;
            execute_one(
                tx,
                "UPDATE categories SET name = ?1, name_key = ?1 WHERE id = ?2",
                params![temporary_name, category.id],
                "category",
                &category.id,
            )?;
        }
    }

    let category_position_base = before.categories.len().max(after.categories.len());
    let mut category_position_offset = 0usize;
    for (old_position, category) in before.categories.iter().enumerate() {
        if let Some((new_position, _)) = after_categories.get(category.id.as_str()).copied()
            && new_position != old_position
        {
            let temporary = temporary_position(
                category_position_base,
                category_position_offset,
                "categories",
            )?;
            category_position_offset += 1;
            execute_one(
                tx,
                "UPDATE categories SET position = ?1 WHERE id = ?2",
                params![temporary, category.id],
                "category",
                &category.id,
            )?;
        }
    }
    for category in &after.categories {
        if !before_categories.contains_key(category.id.as_str()) {
            let temporary = temporary_position(
                category_position_base,
                category_position_offset,
                "categories",
            )?;
            category_position_offset += 1;
            tx.execute(
                "INSERT INTO categories(id, position, name, name_key, description)
                 VALUES (?1, ?2, ?3, ?4, ?5)",
                params![
                    category.id,
                    temporary,
                    category.name,
                    category_name_key(&category.name),
                    category.description
                ],
            )?;
        }
    }

    let task_position_base = before.tasks.len().max(after.tasks.len());
    let mut task_position_offset = 0usize;
    for (old_position, task) in before.tasks.iter().enumerate() {
        if let Some((new_position, _)) = after_tasks.get(task.id.as_str()).copied()
            && new_position != old_position
        {
            let temporary = temporary_position(task_position_base, task_position_offset, "tasks")?;
            task_position_offset += 1;
            execute_one(
                tx,
                "UPDATE tasks SET position = ?1 WHERE id = ?2",
                params![temporary, task.id],
                "task",
                &task.id,
            )?;
        }
    }

    // New categories now exist, so task foreign keys can safely move to them
    // before obsolete categories are deleted.
    for task in &after.tasks {
        if let Some((_, previous)) = before_tasks.get(task.id.as_str()).copied()
            && previous != task
        {
            let body_json = encode_task_body(task)?;
            execute_one(
                tx,
                "UPDATE tasks SET
                    title = ?1, body_json = ?2, due = ?3, created = ?4,
                    done = ?5, importance = ?6, category_id = ?7
                 WHERE id = ?8",
                params![
                    task.title,
                    body_json,
                    task.due,
                    task.created,
                    i64::from(task.done),
                    i64::from(task.importance),
                    task.category_id,
                    task.id,
                ],
                "task",
                &task.id,
            )?;
        }
    }
    for task in &after.tasks {
        if !before_tasks.contains_key(task.id.as_str()) {
            let temporary = temporary_position(task_position_base, task_position_offset, "tasks")?;
            task_position_offset += 1;
            let body_json = encode_task_body(task)?;
            tx.execute(
                "INSERT INTO tasks(
                    id, position, title, body_json, due, created, done, importance, category_id
                 ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
                params![
                    task.id,
                    temporary,
                    task.title,
                    body_json,
                    task.due,
                    task.created,
                    i64::from(task.done),
                    i64::from(task.importance),
                    task.category_id,
                ],
            )?;
        }
    }

    for task in &after.tasks {
        let body_changed_or_new = before_tasks
            .get(task.id.as_str())
            .is_none_or(|(_, previous)| previous.body != task.body);
        if body_changed_or_new {
            tx.execute(
                "DELETE FROM task_attachments WHERE task_id = ?1",
                [&task.id],
            )?;
            insert_task_attachment_rows(tx, task)?;
        }
    }

    for category in &before.categories {
        if !after_categories.contains_key(category.id.as_str()) {
            execute_one(
                tx,
                "DELETE FROM categories WHERE id = ?1",
                [category.id.as_str()],
                "category",
                &category.id,
            )?;
        }
    }

    for category in &after.categories {
        if let Some((_, previous)) = before_categories.get(category.id.as_str()).copied()
            && previous != category
        {
            execute_one(
                tx,
                "UPDATE categories
                 SET name = ?1, name_key = ?2, description = ?3
                 WHERE id = ?4",
                params![
                    category.name,
                    category_name_key(&category.name),
                    category.description,
                    category.id
                ],
                "category",
                &category.id,
            )?;
        }
    }

    // All rows whose final slots changed are currently at unique temporary
    // positions. Rows omitted here kept the same slot, so final assignment
    // cannot collide with them.
    for (position, category) in after.categories.iter().enumerate() {
        let moved_or_new = before_categories
            .get(category.id.as_str())
            .is_none_or(|(old_position, _)| *old_position != position);
        if moved_or_new {
            let position = sqlite_position(position, "categories")?;
            execute_one(
                tx,
                "UPDATE categories SET position = ?1 WHERE id = ?2",
                params![position, category.id],
                "category",
                &category.id,
            )?;
        }
    }
    for (position, task) in after.tasks.iter().enumerate() {
        let moved_or_new = before_tasks
            .get(task.id.as_str())
            .is_none_or(|(old_position, _)| *old_position != position);
        if moved_or_new {
            let position = sqlite_position(position, "tasks")?;
            execute_one(
                tx,
                "UPDATE tasks SET position = ?1 WHERE id = ?2",
                params![position, task.id],
                "task",
                &task.id,
            )?;
        }
    }

    let settings = (before.settings != after.settings).then_some(&after.settings);
    persist_app_state(tx, after.revision, settings)?;
    Ok(())
}

fn execute_one<P: rusqlite::Params>(
    tx: &Transaction<'_>,
    sql: &str,
    params: P,
    entity: &str,
    id: &str,
) -> Result<(), StoreError> {
    let changed = tx.execute(sql, params)?;
    if changed != 1 {
        return Err(StoreError::Corrupt(format!(
            "expected to change one {entity} {id:?}, changed {changed}"
        )));
    }
    Ok(())
}

fn temporary_position(base: usize, offset: usize, entity: &str) -> Result<i64, StoreError> {
    let position = base
        .checked_add(offset)
        .ok_or_else(|| StoreError::Validation(format!("too many {entity}")))?;
    sqlite_position(position, entity)
}

fn sqlite_position(position: usize, entity: &str) -> Result<i64, StoreError> {
    i64::try_from(position).map_err(|_| StoreError::Validation(format!("too many {entity}")))
}

fn sqlite_attachment_size(byte_len: u64) -> Result<i64, StoreError> {
    i64::try_from(byte_len)
        .map_err(|_| StoreError::Validation("attachment byte length exceeds integer range".into()))
}

fn insert_task_attachment_rows(tx: &Transaction<'_>, task: &Task) -> Result<(), StoreError> {
    let mut statement = tx.prepare(
        "INSERT INTO task_attachments(task_id, block_index, attachment_id)
         VALUES (?1, ?2, ?3)",
    )?;
    for (block_index, block) in task.body.iter().enumerate() {
        let Block::Image { attachment_id } = block else {
            continue;
        };
        statement.execute(params![
            task.id,
            sqlite_position(block_index, "task attachment blocks")?,
            attachment_id,
        ])?;
    }
    Ok(())
}

fn encode_task_body(task: &Task) -> Result<String, StoreError> {
    serde_json::to_string(&task.body).map_err(|error| {
        StoreError::Corrupt(format!("could not encode task {:?}: {error}", task.id))
    })
}

fn persist_app_state(
    tx: &Transaction<'_>,
    revision: u64,
    settings: Option<&Settings>,
) -> Result<(), StoreError> {
    let revision = i64::try_from(revision)
        .map_err(|_| StoreError::Corrupt("revision exceeds SQLite integer range".into()))?;
    let changed = if let Some(settings) = settings {
        let settings_json = serde_json::to_string(settings)
            .map_err(|error| StoreError::Corrupt(format!("could not encode settings: {error}")))?;
        tx.execute(
            "UPDATE app_state SET revision = ?1, settings_json = ?2 WHERE id = 1",
            params![revision, settings_json],
        )?
    } else {
        tx.execute(
            "UPDATE app_state SET revision = ?1 WHERE id = 1",
            [revision],
        )?
    };
    if changed != 1 {
        return Err(StoreError::Corrupt(format!(
            "expected to update app state, changed {changed} rows"
        )));
    }
    Ok(())
}

fn import_task_attachments(
    data: &mut StoreData,
    images_root: Option<&Path>,
) -> Result<(), StoreError> {
    let mut known: HashMap<String, Attachment> = data
        .attachments
        .iter()
        .cloned()
        .map(|attachment| (attachment.id.clone(), attachment))
        .collect();

    for task in &mut data.tasks {
        for block in &mut task.body {
            let Block::Image { attachment_id } = block else {
                continue;
            };
            if known.contains_key(attachment_id) {
                continue;
            }
            if is_attachment_id(attachment_id) {
                return Err(StoreError::Validation(format!(
                    "task {:?} refers to unknown attachment {attachment_id:?}",
                    task.id
                )));
            }
            let Some(images_root) = images_root else {
                return Err(StoreError::Validation(
                    "image attachments require a persistent store".into(),
                ));
            };
            let imported = import_attachment(attachment_id, images_root)?;
            if let Some(existing) = known.get(&imported.id) {
                if existing != &imported {
                    return Err(StoreError::Corrupt(format!(
                        "attachment {:?} metadata does not match imported content",
                        imported.id
                    )));
                }
            } else {
                known.insert(imported.id.clone(), imported.clone());
                data.attachments.push(imported.clone());
            }
            *attachment_id = imported.id;
        }
    }
    data.attachments
        .sort_by(|left, right| left.id.cmp(&right.id));
    Ok(())
}

fn import_attachment(reference: &str, images_root: &Path) -> Result<Attachment, StoreError> {
    let source_path = crate::image::expand_in(reference, images_root);
    let mut source = fs::File::open(&source_path)
        .map_err(|error| StoreError::io("open image attachment", &source_path, error))?;
    let metadata = source
        .metadata()
        .map_err(|error| StoreError::io("inspect image attachment", &source_path, error))?;
    if !metadata.is_file() {
        return Err(StoreError::Validation(format!(
            "image attachment {} is not a regular file",
            source_path.display()
        )));
    }

    ensure_private_directory(images_root)?;
    let temp_path = images_root.join(format!(".mach-attachment-{}.tmp", uuid::Uuid::new_v4()));
    let mut temp = open_private_attachment_temp(&temp_path)?;
    let result = (|| {
        let mut hasher = Sha256::new();
        let mut byte_len = 0_u64;
        let mut prefix = [0_u8; 32];
        let mut prefix_len = 0usize;
        let mut buffer = [0_u8; 64 * 1024];
        loop {
            let read = source
                .read(&mut buffer)
                .map_err(|error| StoreError::io("read image attachment", &source_path, error))?;
            if read == 0 {
                break;
            }
            byte_len = byte_len
                .checked_add(read as u64)
                .ok_or_else(|| StoreError::Validation("image attachment is too large".into()))?;
            if byte_len > MAX_ATTACHMENT_BYTES {
                return Err(StoreError::Validation(format!(
                    "image attachment {} exceeds the {} MiB safety limit",
                    source_path.display(),
                    MAX_ATTACHMENT_BYTES / 1024 / 1024
                )));
            }
            if prefix_len < prefix.len() {
                let copy = (prefix.len() - prefix_len).min(read);
                prefix[prefix_len..prefix_len + copy].copy_from_slice(&buffer[..copy]);
                prefix_len += copy;
            }
            hasher.update(&buffer[..read]);
            temp.write_all(&buffer[..read]).map_err(|error| {
                StoreError::io("write managed image attachment", &temp_path, error)
            })?;
        }
        if byte_len == 0 {
            return Err(StoreError::Validation(format!(
                "image attachment {} is empty",
                source_path.display()
            )));
        }
        let format = image::guess_format(&prefix[..prefix_len]).map_err(|_| {
            StoreError::Validation(format!(
                "image attachment {} is not a supported PNG, JPEG, GIF, or WebP image",
                source_path.display()
            ))
        })?;
        let (extension, media_type) = attachment_format(format).ok_or_else(|| {
            StoreError::Validation(format!(
                "image attachment {} is not a supported PNG, JPEG, GIF, or WebP image",
                source_path.display()
            ))
        })?;
        temp.sync_all()
            .map_err(|error| StoreError::io("sync managed image attachment", &temp_path, error))?;
        drop(temp);
        crate::image::load_dynamic(&temp_path).map_err(StoreError::Validation)?;

        let id = format!("{:x}", hasher.finalize());
        let storage_name = format!("{id}.{extension}");
        let destination = images_root.join(&storage_name);
        if destination.exists() {
            let (stored_hash, stored_len) = hash_attachment_file(&destination)?;
            if stored_hash != id || stored_len != byte_len {
                return Err(StoreError::Corrupt(format!(
                    "managed attachment {} does not match its content address",
                    destination.display()
                )));
            }
            fs::remove_file(&temp_path).map_err(|error| {
                StoreError::io("remove duplicate image attachment", &temp_path, error)
            })?;
        } else {
            fs::rename(&temp_path, &destination).map_err(|error| {
                StoreError::io("install managed image attachment", &destination, error)
            })?;
            set_private_file(&destination)?;
            fs::File::open(images_root)
                .and_then(|directory| directory.sync_all())
                .map_err(|error| StoreError::io("sync image directory", images_root, error))?;
        }
        Ok(Attachment {
            id: id.clone(),
            sha256: id,
            media_type: media_type.into(),
            byte_len,
            storage_name,
        })
    })();
    if result.is_err() {
        let _ = fs::remove_file(&temp_path);
    }
    result
}

fn open_private_attachment_temp(path: &Path) -> Result<fs::File, StoreError> {
    let mut options = fs::OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(0o600);
    }
    options
        .open(path)
        .map_err(|error| StoreError::io("create managed image attachment", path, error))
}

fn hash_attachment_file(path: &Path) -> Result<(String, u64), StoreError> {
    let mut file = fs::File::open(path)
        .map_err(|error| StoreError::io("open managed image attachment", path, error))?;
    let mut hasher = Sha256::new();
    let mut byte_len = 0_u64;
    let mut buffer = [0_u8; 64 * 1024];
    loop {
        let read = file
            .read(&mut buffer)
            .map_err(|error| StoreError::io("read managed image attachment", path, error))?;
        if read == 0 {
            break;
        }
        byte_len = byte_len
            .checked_add(read as u64)
            .ok_or_else(|| StoreError::Corrupt("managed attachment is too large".into()))?;
        if byte_len > MAX_ATTACHMENT_BYTES {
            return Err(StoreError::Corrupt(format!(
                "managed attachment {} exceeds the safety limit",
                path.display()
            )));
        }
        hasher.update(&buffer[..read]);
    }
    Ok((format!("{:x}", hasher.finalize()), byte_len))
}

fn attachment_format(format: ImageFormat) -> Option<(&'static str, &'static str)> {
    match format {
        ImageFormat::Png => Some(("png", "image/png")),
        ImageFormat::Jpeg => Some(("jpg", "image/jpeg")),
        ImageFormat::Gif => Some(("gif", "image/gif")),
        ImageFormat::WebP => Some(("webp", "image/webp")),
        _ => None,
    }
}

fn is_attachment_id(value: &str) -> bool {
    value.len() == ATTACHMENT_ID_LEN
        && value
            .bytes()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
}

#[derive(Clone, Copy)]
enum DueMode {
    NewWrite,
    LegacyMigration,
    Stored,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum AttachmentMode {
    Draft,
    Persisted,
}

fn normalize_and_validate(
    data: &mut StoreData,
    now: NaiveDateTime,
    due_mode: DueMode,
    attachment_mode: AttachmentMode,
) -> Result<(), StoreError> {
    // This compatibility-only field moved to the application-level updater
    // store. Never let it re-enter persisted task settings.
    data.settings.last_update_check_at = None;
    if data.categories.len() > MAX_CATEGORY_COUNT {
        return Err(StoreError::Validation(format!(
            "category limit is {MAX_CATEGORY_COUNT}"
        )));
    }
    if data.tasks.len() > MAX_TASK_COUNT {
        return Err(StoreError::Validation(format!(
            "task limit is {MAX_TASK_COUNT}"
        )));
    }

    let attachment_ids = validate_attachments(&data.attachments)?;

    let mut category_ids = HashSet::new();
    let mut category_names = HashSet::new();
    for category in &data.categories {
        validate_single_line(&category.id, "category id")?;
        validate_byte_limit(&category.id, ID_MAX_BYTES, "category id")?;
        if category.is_all() {
            return Err(StoreError::Validation(
                "real category id cannot be empty".into(),
            ));
        }
        if !category_ids.insert(category.id.as_str()) {
            return Err(StoreError::Validation(format!(
                "category id {:?} must be unique",
                category.id
            )));
        }
        validate_single_line(&category.name, "category name")?;
        validate_byte_limit(
            &category.name,
            text_byte_limit(MAX_CATEGORY_NAME_LEN),
            "category name",
        )?;
        let name = category.name.trim();
        if name.is_empty() {
            return Err(StoreError::Validation(
                "category name cannot be empty".into(),
            ));
        }
        if name.graphemes(true).count() > MAX_CATEGORY_NAME_LEN {
            return Err(StoreError::Validation(format!(
                "category name {:?} exceeds {MAX_CATEGORY_NAME_LEN} characters",
                category.name
            )));
        }
        if !category_names.insert(category_name_key(name)) {
            return Err(StoreError::Validation(format!(
                "category names must be unique (duplicate {:?})",
                category.name
            )));
        }
        validate_multiline(
            &category.description,
            MAX_CATEGORY_DESC_LINES,
            MAX_CATEGORY_DESC_LINE_LEN,
            "category description",
        )?;
    }

    let mut task_ids = HashSet::new();
    for task in &mut data.tasks {
        validate_single_line(&task.id, "task id")?;
        validate_byte_limit(&task.id, ID_MAX_BYTES, "task id")?;
        if task.id.is_empty() || !task_ids.insert(task.id.as_str()) {
            return Err(StoreError::Validation(format!(
                "task id {:?} must be nonempty and unique",
                task.id
            )));
        }
        validate_single_line(&task.title, "task title")?;
        validate_byte_limit(&task.title, text_byte_limit(MAX_TITLE_LEN), "task title")?;
        if task.title.trim().is_empty() {
            return Err(StoreError::Validation(format!(
                "task {:?} title cannot be empty",
                task.id
            )));
        }
        if task.title.graphemes(true).count() > MAX_TITLE_LEN {
            return Err(StoreError::Validation(format!(
                "task {:?} title exceeds {MAX_TITLE_LEN} characters",
                task.id
            )));
        }
        if task.importance > MAX_IMPORTANCE {
            return Err(StoreError::Validation(format!(
                "task {:?} importance must be 0-{MAX_IMPORTANCE}",
                task.id
            )));
        }
        if task.body.len() > MAX_BODY_LINES {
            return Err(StoreError::Validation(format!(
                "task {:?} body exceeds {MAX_BODY_LINES} blocks",
                task.id
            )));
        }
        for block in &task.body {
            validate_block(block, &task.id)?;
            if let Block::Image { attachment_id } = block {
                let known = attachment_ids.contains(attachment_id.as_str());
                if attachment_mode == AttachmentMode::Persisted && !known {
                    return Err(StoreError::Validation(format!(
                        "task {:?} refers to unknown attachment {attachment_id:?}",
                        task.id
                    )));
                }
                if attachment_mode == AttachmentMode::Draft
                    && is_attachment_id(attachment_id)
                    && !known
                {
                    return Err(StoreError::Validation(format!(
                        "task {:?} refers to unknown attachment {attachment_id:?}",
                        task.id
                    )));
                }
            }
        }
        if let Some(category_id) = task.category_id.as_deref() {
            validate_single_line(category_id, "task category id")?;
            validate_byte_limit(category_id, ID_MAX_BYTES, "task category id")?;
            if !category_ids.contains(category_id) {
                return Err(StoreError::Validation(format!(
                    "task {:?} refers to unknown category {category_id:?}",
                    task.id
                )));
            }
        }
        validate_single_line(&task.due, "task due")?;
        validate_byte_limit(&task.due, DUE_MAX_BYTES, "task due")?;
        let normalized_due = match due_mode {
            DueMode::NewWrite => due::normalize_for_write_at(&task.due, now),
            DueMode::LegacyMigration => due::normalize_legacy_at(&task.due, now),
            DueMode::Stored => due::normalize_for_write_at(&task.due, now),
        }
        .map_err(|error| StoreError::Validation(format!("task {:?} has {error}", task.id)))?;
        if matches!(due_mode, DueMode::Stored) && normalized_due != task.due {
            return Err(StoreError::Validation(format!(
                "task {:?} has noncanonical due value {:?}",
                task.id, task.due
            )));
        }
        task.due = normalized_due;
        validate_single_line(&task.created, "task creation timestamp")?;
        validate_byte_limit(&task.created, CREATED_MAX_BYTES, "task creation timestamp")?;
        NaiveDateTime::parse_from_str(&task.created, "%Y-%m-%d %H:%M:%S").map_err(|_| {
            StoreError::Validation(format!(
                "task {:?} has invalid creation timestamp {:?}",
                task.id, task.created
            ))
        })?;
    }
    validate_settings(&data.settings)
}

fn validate_block(block: &Block, task_id: &str) -> Result<(), StoreError> {
    let (kind, value) = match block {
        Block::Text { text } => ("text", text),
        Block::Todo { text, .. } => ("subtask", text),
        Block::Bullet { text } => ("bullet", text),
        Block::Number { text } => ("number", text),
        Block::Link { url } => ("link", url),
        Block::Image { attachment_id } => ("image attachment", attachment_id),
    };
    validate_single_line(value, kind)?;
    validate_byte_limit(value, text_byte_limit(MAX_NOTES_LINE_LEN), kind)?;
    if value.graphemes(true).count() > MAX_NOTES_LINE_LEN {
        return Err(StoreError::Validation(format!(
            "task {task_id:?} {kind} exceeds {MAX_NOTES_LINE_LEN} characters"
        )));
    }
    Ok(())
}

fn validate_attachments(attachments: &[Attachment]) -> Result<HashSet<&str>, StoreError> {
    let mut ids = HashSet::new();
    let mut storage_names = HashSet::new();
    for attachment in attachments {
        if !is_attachment_id(&attachment.id) || attachment.sha256 != attachment.id {
            return Err(StoreError::Validation(format!(
                "attachment {:?} has an invalid content address",
                attachment.id
            )));
        }
        if !ids.insert(attachment.id.as_str()) {
            return Err(StoreError::Validation(format!(
                "attachment id {:?} must be unique",
                attachment.id
            )));
        }
        if attachment.byte_len == 0 || attachment.byte_len > MAX_ATTACHMENT_BYTES {
            return Err(StoreError::Validation(format!(
                "attachment {:?} has invalid byte length {}",
                attachment.id, attachment.byte_len
            )));
        }
        let extension = match attachment.media_type.as_str() {
            "image/png" => "png",
            "image/jpeg" => "jpg",
            "image/gif" => "gif",
            "image/webp" => "webp",
            other => {
                return Err(StoreError::Validation(format!(
                    "attachment {:?} has unsupported media type {other:?}",
                    attachment.id
                )));
            }
        };
        let expected_storage_name = format!("{}.{}", attachment.id, extension);
        if attachment.storage_name != expected_storage_name {
            return Err(StoreError::Validation(format!(
                "attachment {:?} has invalid storage name {:?}",
                attachment.id, attachment.storage_name
            )));
        }
        if !storage_names.insert(attachment.storage_name.as_str()) {
            return Err(StoreError::Validation(format!(
                "attachment storage name {:?} must be unique",
                attachment.storage_name
            )));
        }
    }
    Ok(ids)
}

fn validate_multiline(
    value: &str,
    max_lines: usize,
    max_line_len: usize,
    label: &str,
) -> Result<(), StoreError> {
    let max_line_bytes = text_byte_limit(max_line_len);
    let max_total_bytes = max_lines.saturating_mul(max_line_bytes.saturating_add(1));
    validate_byte_limit(value, max_total_bytes, label)?;
    if value
        .chars()
        .any(|character| character.is_control() && character != '\n')
    {
        return Err(StoreError::Validation(format!(
            "{label} contains a control character"
        )));
    }
    for (index, line) in value.split('\n').enumerate() {
        if index >= max_lines {
            return Err(StoreError::Validation(format!(
                "{label} exceeds {max_lines} lines"
            )));
        }
        if line.len() > max_line_bytes {
            return Err(StoreError::Validation(format!(
                "{label} line exceeds {max_line_bytes} bytes"
            )));
        }
        if line.graphemes(true).count() > max_line_len {
            return Err(StoreError::Validation(format!(
                "{label} line exceeds {max_line_len} characters"
            )));
        }
    }
    Ok(())
}

fn validate_single_line(value: &str, label: &str) -> Result<(), StoreError> {
    if value.chars().any(char::is_control) {
        return Err(StoreError::Validation(format!(
            "{label} contains a control character"
        )));
    }
    Ok(())
}

fn validate_byte_limit(value: &str, max_bytes: usize, label: &str) -> Result<(), StoreError> {
    if value.len() > max_bytes {
        return Err(StoreError::Validation(format!(
            "{label} exceeds {max_bytes} bytes"
        )));
    }
    Ok(())
}

/// Unicode compatibility normalization followed by full default case folding
/// defines category identity. A final normalization makes the key stable when
/// folding introduces decomposed characters.
fn category_name_key(value: &str) -> String {
    caseless_key(value.trim())
}

fn category_name_has_prefix(name: &str, folded_query: &str) -> bool {
    let normalized: String = name.trim().nfkc().collect();
    normalized
        .char_indices()
        .skip(1)
        .map(|(index, _)| index)
        .chain(std::iter::once(normalized.len()))
        .any(|end| category_name_key(&normalized[..end]) == folded_query)
}

fn validate_settings(settings: &Settings) -> Result<(), StoreError> {
    validate_single_line(&settings.date_format, "date format")?;
    validate_byte_limit(
        &settings.date_format,
        SETTINGS_VALUE_MAX_BYTES,
        "date format",
    )?;
    validate_single_line(&settings.selected_color, "theme")?;
    validate_byte_limit(&settings.selected_color, SETTINGS_VALUE_MAX_BYTES, "theme")?;
    validate_single_line(&settings.sort, "sort")?;
    validate_byte_limit(&settings.sort, SETTINGS_VALUE_MAX_BYTES, "sort")?;
    validate_single_line(&settings.preview_position, "preview position")?;
    validate_byte_limit(
        &settings.preview_position,
        SETTINGS_VALUE_MAX_BYTES,
        "preview position",
    )?;
    if let Some(version) = settings.last_run_version.as_deref() {
        validate_single_line(version, "last-run version")?;
        validate_byte_limit(version, SETTINGS_VALUE_MAX_BYTES, "last-run version")?;
    }
    if !DATE_FORMATS.contains(&settings.date_format.as_str()) {
        return Err(StoreError::Validation(format!(
            "unknown date format {:?}",
            settings.date_format
        )));
    }
    if !THEMES.contains(&settings.selected_color.as_str()) {
        return Err(StoreError::Validation(format!(
            "unknown theme {:?}",
            settings.selected_color
        )));
    }
    if !SORTS.contains(&settings.sort.as_str()) {
        return Err(StoreError::Validation(format!(
            "unknown sort {:?}",
            settings.sort
        )));
    }
    if !PREVIEW_POSITIONS.contains(&settings.preview_position.as_str()) {
        return Err(StoreError::Validation(format!(
            "unknown preview position {:?}",
            settings.preview_position
        )));
    }
    Ok(())
}

fn read_optional_json<T: DeserializeOwned>(path: &Path) -> Result<Option<T>, StoreError> {
    let file = match fs::File::open(path) {
        Ok(file) => file,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(error) => return Err(StoreError::io("read", path, error)),
    };
    let size = file
        .metadata()
        .map_err(|error| StoreError::io("inspect", path, error))?
        .len();
    if size > MAX_LEGACY_JSON_BYTES {
        return Err(StoreError::Validation(format!(
            "legacy file {} is larger than the {} MiB safety limit",
            path.display(),
            MAX_LEGACY_JSON_BYTES / 1024 / 1024
        )));
    }
    serde_json::from_reader(std::io::BufReader::new(file))
        .map(Some)
        .map_err(|source| StoreError::Json {
            path: path.to_path_buf(),
            source,
        })
}

fn validate_legacy_schema(path: &Path, schema: Option<u32>) -> Result<(), StoreError> {
    if let Some(found) = schema
        && found != SCHEMA_VERSION
    {
        return Err(StoreError::UnsupportedLegacySchema {
            path: path.to_path_buf(),
            found,
            expected: SCHEMA_VERSION,
        });
    }
    Ok(())
}

#[derive(Debug, Deserialize)]
struct TasksFile {
    schema: u32,
    tasks: Vec<Task>,
}

#[derive(Debug, Deserialize)]
struct CategoriesFile {
    schema: u32,
    categories: Vec<Category>,
}

pub(crate) fn ensure_private_directory(path: &Path) -> Result<(), StoreError> {
    let created = match fs::create_dir(path) {
        Ok(()) => true,
        Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists && path.is_dir() => false,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
            if let Some(parent) = path
                .parent()
                .filter(|parent| !parent.as_os_str().is_empty())
            {
                fs::create_dir_all(parent).map_err(|parent_error| {
                    StoreError::io("create parent directory", parent, parent_error)
                })?;
            }
            match fs::create_dir(path) {
                Ok(()) => true,
                Err(retry)
                    if retry.kind() == std::io::ErrorKind::AlreadyExists && path.is_dir() =>
                {
                    false
                }
                Err(retry) => {
                    return Err(StoreError::io("create directory", path, retry));
                }
            }
        }
        Err(error) => return Err(StoreError::io("create directory", path, error)),
    };
    if created {
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            fs::set_permissions(path, fs::Permissions::from_mode(0o700))
                .map_err(|error| StoreError::io("set permissions on", path, error))?;
        }
    }
    Ok(())
}

pub(crate) fn prepare_private_database_file(path: &Path) -> Result<(), StoreError> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        match fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .mode(0o600)
            .open(path)
        {
            Ok(file) => drop(file),
            Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {}
            Err(error) => return Err(StoreError::io("create database", path, error)),
        }
    }
    #[cfg(not(unix))]
    let _ = path;
    Ok(())
}

pub(crate) fn set_private_file(path: &Path) -> Result<(), StoreError> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        fs::set_permissions(path, fs::Permissions::from_mode(0o600))
            .map_err(|error| StoreError::io("set permissions on", path, error))?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn default_directory_requires_a_home_when_no_path_is_configured() {
        let error = resolve_data_dir_from(None, None, None)
            .expect_err("missing home must not silently select the working directory");
        assert!(matches!(error, StoreError::Validation(_)));

        assert_eq!(
            resolve_data_dir_from(Some(PathBuf::from("/tmp/mach")), None, None).unwrap(),
            PathBuf::from("/tmp/mach")
        );
        assert_eq!(
            resolve_data_dir_from(None, Some(PathBuf::from("/tmp/configured")), None).unwrap(),
            PathBuf::from("/tmp/configured")
        );
        assert!(resolve_data_dir_from(Some(PathBuf::from("~/.mach")), None, None).is_err());
    }
}