snip-sync 0.2.0

gRPC sync server for snp snippet manager
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
use argon2::{
    Algorithm, Argon2, Params, Version,
    password_hash::{PasswordHasher, PasswordVerifier, phc::PasswordHash},
};
use base64::Engine;
use sha2::{Digest, Sha256};
use sqlx::AssertSqlSafe;
use sqlx::sqlite::{SqliteConnectOptions, SqlitePool, SqlitePoolOptions};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::time::Duration;
use thiserror::Error;
use time::OffsetDateTime;
use uuid::Uuid;

const ARGON2_MEMORY_KIB: u32 = 1 << 14; // 16 MiB — OWASP minimum for Argon2id
const ARGON2_ITERATIONS: u32 = 3;
const ARGON2_PARALLELISM: u32 = 4;

#[derive(Debug, Error)]
pub enum DbError {
    #[error("Database error: {0}")]
    Database(#[from] sqlx::Error),
    #[error("Not found: {0}")]
    NotFound(String),
    #[error("Conflict: {0}")]
    Conflict(String),
    #[error("Validation error: {0}")]
    Validation(String),
    #[error("Internal error: {0}")]
    Internal(String),
}

pub type DbResult<T> = Result<T, DbError>;

/// Result of [`Database::upsert_snippet_in_tx`].
///
/// `snippets.id` is a *global* primary key, so an insert that collides with a
/// row owned by a different user or library cannot be written at all. SQLite
/// reports a false `WHERE` on `DO UPDATE` as a silent no-op (0 rows affected,
/// no error), which is why the outcome has to be classified explicitly instead
/// of treating "no rows affected" as success.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UpsertOutcome {
    /// The row was inserted or updated.
    Written,
    /// A newer version of the same record already exists under the same user
    /// and library; last-write-wins correctly kept it.
    Stale,
    /// A row with the same id already belongs to a different user and/or
    /// library. The write was discarded — reporting this as `Stale` would
    /// silently lose the snippet.
    ForeignScope,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Snippet {
    pub id: String,
    pub description: String,
    pub command: String,
    pub tags: Vec<String>,
    pub created_at: i64,
    pub updated_at: i64,
    pub device_id: String,
    pub deleted: bool,
    pub encrypted: bool,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Library {
    pub id: String,
    pub name: String,
    pub created_at: i64,
    pub snippet_count: i64,
}

/// One page of snippets plus the paging facts derived from the page.
#[derive(Debug, Clone)]
pub struct SnippetPage {
    /// The rows of this page (never more than the requested limit).
    pub snippets: Vec<Snippet>,
    /// True when rows exist beyond this page.
    pub has_more: bool,
    /// Exact number of rows in the filtered range.
    pub total: i32,
}

/// The cursor the delta filter selects on.
///
/// The server keeps two cursors side by side. The **timestamp cursor**
/// (`visible_at`) is second-granular and stays the filter for clients that
/// send no sequence cursor. The **write-sequence cursor** (`sequence`) is
/// server-assigned, strictly increasing per library, and exact. The request
/// picks the form: a non-zero sequence cursor means the sequence form, and
/// anything else falls back to the timestamp form.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeltaCursor {
    /// The timestamp cursor: `visible_at >= value`.
    Timestamp(i64),
    /// The write-sequence cursor: `sequence >= value`.
    Sequence(i64),
}

impl DeltaCursor {
    /// The SQL predicate and its bound value for this cursor form.
    ///
    /// The predicate is a literal, never caller input, so the assembled
    /// statement stays free of injected values.
    fn predicate(self) -> (&'static str, i64) {
        match self {
            DeltaCursor::Timestamp(value) => ("AND visible_at >= ?", value),
            DeltaCursor::Sequence(value) => ("AND sequence >= ?", value),
        }
    }
}

#[derive(Clone)]
pub struct Database {
    pool: SqlitePool,
}

fn hash_api_key(api_key: &str) -> DbResult<String> {
    let mut salt_bytes = [0u8; 16];
    getrandom::fill(&mut salt_bytes)
        .map_err(|e| DbError::Internal(format!("Failed to generate salt: {}", e)))?;
    let params = Params::new(
        ARGON2_MEMORY_KIB,
        ARGON2_ITERATIONS,
        ARGON2_PARALLELISM,
        None,
    )
    .map_err(|e| DbError::Internal(format!("Invalid Argon2 params: {}", e)))?;
    let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
    let hash = argon2
        .hash_password_with_salt(api_key.as_bytes(), &salt_bytes)
        .map_err(|e| DbError::Internal(format!("Failed to hash API key: {}", e)))?
        .to_string();
    Ok(hash)
}

fn verify_api_key(api_key: &str, hash: &str) -> bool {
    let parsed = match PasswordHash::new(hash) {
        Ok(p) => p,
        Err(_) => return false,
    };
    let params = match Params::new(
        ARGON2_MEMORY_KIB,
        ARGON2_ITERATIONS,
        ARGON2_PARALLELISM,
        None,
    ) {
        Ok(p) => p,
        Err(_) => return false,
    };
    let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
    argon2.verify_password(api_key.as_bytes(), &parsed).is_ok()
}

/// Computes the first 8 chars of base64(SHA-256(api_key)) for indexed lookup.
fn compute_api_key_prefix(api_key: &str) -> String {
    let hash = Sha256::digest(api_key.as_bytes());
    let encoded = base64::engine::general_purpose::STANDARD_NO_PAD.encode(hash);
    encoded.chars().take(8).collect()
}

/// Derives a stable prefix from a stored hash for legacy users whose
/// `api_key_prefix` is NULL. The prefix is independent of the original
/// API key (which is unrecoverable from the Argon2 hash), so legacy users
/// cannot authenticate after backfill — but the fallback that turned
/// NULL-prefix users into an O(N) Argon2id DoS amplifier is removed.
fn derive_prefix_from_hash(stored_hash: &str) -> String {
    let hash = Sha256::digest(stored_hash.as_bytes());
    let encoded = base64::engine::general_purpose::STANDARD_NO_PAD.encode(hash);
    encoded.chars().take(8).collect()
}

fn saturating_i32(v: i64) -> i32 {
    v.min(i32::MAX as i64) as i32
}

type SnippetRow = (String, String, String, String, i64, i64, String, i32, i32);

/// Count the rows of the filtered range.
///
/// `>=` (not `>`) is required for delta correctness: the watermark returned
/// to the client is the maximum cursor value of the library, so a strict `>`
/// would skip the boundary row itself. Re-delivering boundary rows is safe —
/// merges are idempotent by snippet identity.
async fn count_snippets(
    conn: &mut sqlx::SqliteConnection,
    user_id: &str,
    library_id: &str,
    cursor: DeltaCursor,
    include_deleted: bool,
) -> DbResult<i32> {
    let (predicate, cursor_value) = cursor.predicate();
    // The only dynamic part of the statement is `predicate`, which is a
    // fixed literal chosen by the cursor form — no caller input reaches the
    // SQL text, and every value is bound as a parameter.
    let sql = AssertSqlSafe(format!(
        "SELECT COUNT(*) FROM snippets \
         WHERE user_id = ? \
           AND library_id = ? \
           {predicate} \
           AND (? OR deleted = 0)"
    ));
    let total: (i64,) = sqlx::query_as(sql)
        .bind(user_id)
        .bind(library_id)
        .bind(cursor_value)
        .bind(include_deleted)
        .fetch_one(&mut *conn)
        .await?;

    Ok(saturating_i32(total.0))
}

/// Read one page of the filtered range, newest first.
async fn fetch_snippets(
    conn: &mut sqlx::SqliteConnection,
    user_id: &str,
    library_id: &str,
    cursor: DeltaCursor,
    limit: i32,
    offset: i32,
    include_deleted: bool,
) -> DbResult<Vec<Snippet>> {
    let (predicate, cursor_value) = cursor.predicate();
    // See `count_snippets`: the only dynamic part is a fixed literal.
    let sql = AssertSqlSafe(format!(
        "SELECT id, description, command, tags, created_at, updated_at, device_id, deleted, encrypted \
         FROM snippets \
         WHERE user_id = ? \
           AND library_id = ? \
           {predicate} \
           AND (? OR deleted = 0) \
         ORDER BY updated_at DESC, id DESC \
         LIMIT ? OFFSET ?"
    ));
    let rows: Vec<SnippetRow> = sqlx::query_as(sql)
        .bind(user_id)
        .bind(library_id)
        .bind(cursor_value)
        .bind(include_deleted)
        .bind(limit)
        .bind(offset)
        .fetch_all(&mut *conn)
        .await?;

    rows.into_iter()
        .map(
            |(
                id,
                description,
                command,
                tags_str,
                created_at,
                updated_at,
                device_id,
                deleted,
                encrypted,
            )| {
                // Surface corruption to repair flows instead of
                // silently substituting an empty tag list.
                let tags: Vec<String> = serde_json::from_str(&tags_str).map_err(|e| {
                    DbError::Internal(format!(
                        "corrupt tags JSON for snippet {id} (row must be repaired): {e}"
                    ))
                })?;
                Ok(Snippet {
                    id,
                    description,
                    command,
                    tags,
                    created_at,
                    updated_at,
                    device_id,
                    deleted: deleted != 0,
                    encrypted: encrypted != 0,
                })
            },
        )
        .collect::<DbResult<Vec<Snippet>>>()
}

const MIGRATION_BATCH_SIZE: i64 = 100;

/// Bump the library's write-sequence counter and return the new value.
///
/// The counter row is created on first use. The bump runs inside the
/// caller's transaction, so it is committed with the row write or rolled
/// back with it: a failed write never consumes a sequence value, and a
/// consumed value is never handed out twice.
async fn bump_library_sequence(
    tx: &mut sqlx::Transaction<'_, sqlx::Sqlite>,
    library_id: &str,
) -> DbResult<i64> {
    sqlx::query(
        "INSERT INTO library_sequences (library_id, sequence) VALUES (?, 0) \
         ON CONFLICT(library_id) DO NOTHING",
    )
    .bind(library_id)
    .execute(&mut **tx)
    .await?;

    let (sequence,): (i64,) = sqlx::query_as(
        "UPDATE library_sequences SET sequence = sequence + 1 WHERE library_id = ? RETURNING sequence",
    )
    .bind(library_id)
    .fetch_one(&mut **tx)
    .await?;

    Ok(sequence)
}

/// Create (or tighten) the directory that holds the SQLite database so that it
/// is reachable only by the service account on Unix.
///
/// A newly created directory is created *with* `0o700` as part of the creation
/// call, so there is no window in which it exists with a broader mode. A
/// pre-existing directory may predate this policy: it is tightened when
/// possible, and a failure is a warning naming the observed mode rather than a
/// startup abort, because a read-only or administrator-managed deployment can
/// legitimately refuse the change.
#[cfg(unix)]
fn prepare_data_directory(path: &Path) -> std::io::Result<()> {
    use std::os::unix::fs::{DirBuilderExt, PermissionsExt};

    let existed = path.exists();
    std::fs::DirBuilder::new()
        .recursive(true)
        .mode(0o700)
        .create(path)?;

    if existed {
        let mode = std::fs::metadata(path)?.permissions().mode() & 0o777;
        if mode & 0o077 != 0 {
            match std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o700)) {
                Ok(()) => tracing::info!(
                    "Tightened server data directory {} from mode {:#05o} to 0o700",
                    path.display(),
                    mode
                ),
                Err(error) => tracing::warn!(
                    "Server data directory {} is accessible to other local users \
                     (mode {:#05o}) and could not be tightened to 0o700: {}",
                    path.display(),
                    mode,
                    error
                ),
            }
        }
    }
    Ok(())
}

#[cfg(not(unix))]
fn prepare_data_directory(path: &Path) -> std::io::Result<()> {
    // Windows ACL enforcement is out of scope; the directory is still created.
    std::fs::create_dir_all(path)
}

/// Restrict the database file and its WAL/shared-memory siblings to `0o600`.
///
/// The siblings are created by SQLite on first write, so this runs after the
/// schema has been touched. Contents are ciphertext, but metadata and Argon2id
/// hashes are readable by any local user who can open the file.
#[cfg(unix)]
fn restrict_database_files(filename: &Path) {
    use std::os::unix::fs::PermissionsExt;

    let mut targets = vec![filename.to_path_buf()];
    for suffix in ["-wal", "-shm"] {
        let mut sibling = filename.as_os_str().to_owned();
        sibling.push(suffix);
        targets.push(PathBuf::from(sibling));
    }

    for target in targets {
        let mode = match std::fs::metadata(&target) {
            Ok(metadata) => metadata.permissions().mode() & 0o777,
            // A missing `-wal`/`-shm` is normal: SQLite may not have created it
            // yet, or may have removed it on a clean checkpoint.
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => continue,
            Err(error) => {
                tracing::debug!(
                    "Unable to inspect database file {}: {}",
                    target.display(),
                    error
                );
                continue;
            }
        };
        if mode & 0o077 == 0 {
            continue;
        }
        if let Err(error) =
            std::fs::set_permissions(&target, std::fs::Permissions::from_mode(0o600))
        {
            tracing::warn!(
                "Database file {} is accessible to other local users (mode {:#05o}) \
                 and could not be tightened to 0o600: {}",
                target.display(),
                mode,
                error
            );
        }
    }
}

#[cfg(not(unix))]
fn restrict_database_files(_filename: &Path) {}

impl Database {
    pub async fn connect(url: &str, max_connections: u32) -> DbResult<Self> {
        // The public configuration uses a SQLite file path for convenience,
        // while tests and advanced deployments may provide a full sqlite URL.
        // Sqlx's generic `Pool::connect` leaves file creation disabled, which
        // makes a fresh `snip-sync init` fail on its first start. Normalize
        // both forms and explicitly create the file when needed.
        let options = if url.starts_with("sqlite:") {
            SqliteConnectOptions::from_str(url)?
        } else {
            SqliteConnectOptions::new().filename(url)
        }
        .create_if_missing(true)
        .busy_timeout(Duration::from_secs(5));

        let filename = options.get_filename().to_path_buf();
        if filename != Path::new(":memory:")
            && let Some(parent) = filename.parent()
            && !parent.as_os_str().is_empty()
        {
            prepare_data_directory(parent).map_err(|e| {
                DbError::Internal(format!(
                    "Failed to create database directory {}: {}",
                    parent.display(),
                    e
                ))
            })?;
        }

        // `foreign_keys` is a **per-connection** SQLite setting (unlike
        // `journal_mode`, which SQLite persists in the database file header).
        // Running the pragma against the pool set it on whichever single
        // connection it happened to check out — at most 1 of
        // `max_connections` — leaving the schema's declared FKs unenforced on
        // the rest. `after_connect` runs for every connection the pool opens.
        let pool = SqlitePoolOptions::new()
            .max_connections(max_connections)
            .after_connect(|conn, _meta| {
                Box::pin(async move {
                    sqlx::query("PRAGMA foreign_keys=ON").execute(conn).await?;
                    Ok(())
                })
            })
            .connect_with(options)
            .await?;

        sqlx::query("PRAGMA journal_mode=WAL")
            .execute(&pool)
            .await?;

        sqlx::query(
            "
            CREATE TABLE IF NOT EXISTS users (
                id TEXT PRIMARY KEY,
                api_key TEXT UNIQUE NOT NULL,
                api_key_prefix TEXT,
                created_at INTEGER NOT NULL,
                updated_at INTEGER NOT NULL
            )
            ",
        )
        .execute(&pool)
        .await?;

        sqlx::query("CREATE INDEX IF NOT EXISTS idx_users_api_key_prefix ON users(api_key_prefix)")
            .execute(&pool)
            .await?;

        sqlx::query(
            "
            CREATE TABLE IF NOT EXISTS libraries (
                id TEXT PRIMARY KEY,
                user_id TEXT NOT NULL,
                name TEXT NOT NULL,
                created_at INTEGER NOT NULL,
                deleted_at INTEGER,
                FOREIGN KEY (user_id) REFERENCES users(id),
                UNIQUE(user_id, name)
            )
            ",
        )
        .execute(&pool)
        .await?;

        sqlx::query(
            "
            CREATE TABLE IF NOT EXISTS snippets (
                id TEXT PRIMARY KEY,
                user_id TEXT NOT NULL,
                library_id TEXT NOT NULL,
                description TEXT NOT NULL,
                command TEXT NOT NULL,
                tags TEXT NOT NULL DEFAULT '[]',
                created_at INTEGER NOT NULL,
                updated_at INTEGER NOT NULL,
                device_id TEXT NOT NULL,
                deleted INTEGER NOT NULL DEFAULT 0,
                encrypted INTEGER NOT NULL DEFAULT 0,
                visible_at INTEGER NOT NULL DEFAULT 0,
                sequence INTEGER NOT NULL DEFAULT 0,
                FOREIGN KEY (user_id) REFERENCES users(id),
                FOREIGN KEY (library_id) REFERENCES libraries(id)
            )
            ",
        )
        .execute(&pool)
        .await?;

        // The per-library write-sequence counter. One row per library holds
        // the last sequence the server handed out; the next write takes the
        // next value. The counter lives in the database, so a server restart
        // does not reset it.
        sqlx::query(
            "
            CREATE TABLE IF NOT EXISTS library_sequences (
                library_id TEXT PRIMARY KEY,
                sequence INTEGER NOT NULL DEFAULT 0
            )
            ",
        )
        .execute(&pool)
        .await?;

        sqlx::query("CREATE INDEX IF NOT EXISTS idx_snippets_user ON snippets(user_id)")
            .execute(&pool)
            .await?;
        sqlx::query("CREATE INDEX IF NOT EXISTS idx_snippets_library ON snippets(library_id)")
            .execute(&pool)
            .await?;
        sqlx::query("CREATE INDEX IF NOT EXISTS idx_snippets_updated ON snippets(updated_at)")
            .execute(&pool)
            .await?;
        sqlx::query(
            "CREATE INDEX IF NOT EXISTS idx_snippets_user_library_updated \
             ON snippets(user_id, library_id, updated_at, deleted)",
        )
        .execute(&pool)
        .await?;
        // The delta cursor lives in `visible_at`, not in the client-chosen
        // `updated_at`; this index serves the `visible_at >= since` range.
        sqlx::query(
            "CREATE INDEX IF NOT EXISTS idx_snippets_user_library_visible \
             ON snippets(user_id, library_id, visible_at, deleted)",
        )
        .execute(&pool)
        .await?;
        sqlx::query("CREATE INDEX IF NOT EXISTS idx_libraries_user ON libraries(user_id)")
            .execute(&pool)
            .await?;

        // The write-sequence cursor owns its column, its counter, and its
        // indexes together: the unique index cannot be created before the
        // column exists, and the column cannot be backfilled before the
        // counter table exists. The migration runs inside `connect` so the
        // server never serves a database whose sequence cursor is half
        // installed.
        let db = Self { pool };
        db.migrate_snippet_sequence().await?;

        // Runs after the schema writes so SQLite has created `-wal`/`-shm`.
        restrict_database_files(&filename);

        Ok(db)
    }

    pub async fn ping(&self) -> DbResult<()> {
        sqlx::query("SELECT 1").execute(&self.pool).await?;
        Ok(())
    }

    pub fn pool(&self) -> &SqlitePool {
        &self.pool
    }

    pub async fn create_user(&self, api_key: &str) -> DbResult<String> {
        let user_id = Uuid::new_v4().to_string();
        let now = OffsetDateTime::now_utc().unix_timestamp();
        let api_key_hash = hash_api_key(api_key)?;
        let api_key_prefix = compute_api_key_prefix(api_key);

        let mut tx = self.pool.begin().await?;

        sqlx::query("INSERT INTO users (id, api_key, api_key_prefix, created_at, updated_at) VALUES (?, ?, ?, ?, ?)")
            .bind(&user_id)
            .bind(&api_key_hash)
            .bind(&api_key_prefix)
            .bind(now)
            .bind(now)
            .execute(&mut *tx)
            .await?;

        let default_lib_id = Uuid::new_v4().to_string();
        sqlx::query("INSERT INTO libraries (id, user_id, name, created_at) VALUES (?, ?, ?, ?)")
            .bind(&default_lib_id)
            .bind(&user_id)
            .bind("default")
            .bind(now)
            .execute(&mut *tx)
            .await?;

        tx.commit().await?;

        tracing::info!(
            "Created user {} with default library {}",
            user_id,
            default_lib_id
        );

        Ok(user_id)
    }

    pub async fn get_user_by_api_key(&self, api_key: &str) -> DbResult<Option<String>> {
        let prefix = compute_api_key_prefix(api_key);

        // Prefix is always populated for new users (see create_user) and is
        // backfilled by migrate_plaintext_api_keys. The legacy NULL-prefix
        // fallback was removed because N NULL-prefix users turned every auth
        // attempt into O(N) Argon2id verification — a DoS amplifier.
        let rows: Vec<(String, String)> =
            sqlx::query_as("SELECT id, api_key FROM users WHERE api_key_prefix = ?")
                .bind(&prefix)
                .fetch_all(&self.pool)
                .await?;

        for (user_id, stored_hash) in rows {
            if verify_api_key(api_key, &stored_hash) {
                return Ok(Some(user_id));
            }
        }

        Ok(None)
    }

    pub async fn create_library(&self, user_id: &str, name: &str) -> DbResult<String> {
        if !name
            .chars()
            .all(|c| c.is_alphanumeric() || c == '-' || c == '_')
        {
            return Err(DbError::Validation(
                "Invalid library name. Use only letters, numbers, dash, and underscore."
                    .to_string(),
            ));
        }
        if name.is_empty() || name.len() > 64 {
            return Err(DbError::Validation(
                "Library name must be 1-64 characters".to_string(),
            ));
        }

        let lib_id = Uuid::new_v4().to_string();
        let now = OffsetDateTime::now_utc().unix_timestamp();

        let result = sqlx::query(
            "INSERT OR IGNORE INTO libraries (id, user_id, name, created_at) VALUES (?, ?, ?, ?)",
        )
        .bind(&lib_id)
        .bind(user_id)
        .bind(name)
        .bind(now)
        .execute(&self.pool)
        .await?;

        if result.rows_affected() == 0 {
            return Err(DbError::Conflict(format!(
                "Library '{}' already exists",
                name
            )));
        }

        Ok(lib_id)
    }

    pub async fn list_libraries(
        &self,
        user_id: &str,
        limit: i32,
        offset: i32,
    ) -> DbResult<(Vec<Library>, i32)> {
        let total: (i64,) = sqlx::query_as(
            "SELECT COUNT(*) FROM libraries WHERE user_id = ? AND deleted_at IS NULL",
        )
        .bind(user_id)
        .fetch_one(&self.pool)
        .await?;

        let rows: Vec<(String, String, i64, i64)> = sqlx::query_as(
            "SELECT l.id, l.name, l.created_at, COUNT(s.id) as snippet_count \
             FROM libraries l \
             LEFT JOIN snippets s ON s.library_id = l.id AND s.deleted = 0 \
             WHERE l.user_id = ? AND l.deleted_at IS NULL \
             GROUP BY l.id \
             ORDER BY l.name LIMIT ? OFFSET ?",
        )
        .bind(user_id)
        .bind(limit)
        .bind(offset)
        .fetch_all(&self.pool)
        .await?;

        let libraries = rows
            .into_iter()
            .map(|(id, name, created_at, snippet_count)| Library {
                id,
                name,
                created_at,
                snippet_count,
            })
            .collect();

        Ok((libraries, saturating_i32(total.0)))
    }

    pub async fn delete_library(&self, user_id: &str, library_id: &str) -> DbResult<()> {
        let now = OffsetDateTime::now_utc().unix_timestamp();
        let mut tx = self.pool.begin().await?;

        let result = sqlx::query(
            "UPDATE libraries SET deleted_at = ? WHERE id = ? AND user_id = ? AND deleted_at IS NULL",
        )
        .bind(now)
        .bind(library_id)
        .bind(user_id)
        .execute(&mut *tx)
        .await?;

        if result.rows_affected() == 0 {
            tx.rollback().await?;
            return Err(DbError::NotFound(
                "Library not found or already deleted".to_string(),
            ));
        }

        // Tombstone the rows. Every tombstoned row takes a fresh write
        // sequence, so a client that syncs on the sequence cursor sees the
        // deletion instead of keeping the row it already had.
        let rows: Vec<(String,)> = sqlx::query_as(
            "SELECT id FROM snippets WHERE library_id = ? AND user_id = ? AND deleted = 0",
        )
        .bind(library_id)
        .bind(user_id)
        .fetch_all(&mut *tx)
        .await?;

        for (id,) in rows {
            let sequence = bump_library_sequence(&mut tx, library_id).await?;
            sqlx::query(
                "UPDATE snippets
                 SET deleted = 1, updated_at = MAX(updated_at, ?), visible_at = MAX(visible_at, ?), sequence = ?
                 WHERE id = ?",
            )
            .bind(now)
            .bind(now)
            .bind(sequence)
            .bind(&id)
            .execute(&mut *tx)
            .await?;
        }

        tx.commit().await?;
        Ok(())
    }

    pub async fn get_default_library(&self, user_id: &str) -> DbResult<String> {
        let (lib_id,): (String,) = sqlx::query_as(
            "SELECT id FROM libraries WHERE user_id = ? AND name = 'default' AND deleted_at IS NULL",
        )
        .bind(user_id)
        .fetch_optional(&self.pool)
        .await?
        .ok_or_else(|| DbError::NotFound("Default library not found".to_string()))?;

        Ok(lib_id)
    }

    pub async fn verify_library_ownership(
        &self,
        user_id: &str,
        library_id: &str,
    ) -> DbResult<bool> {
        let (count,): (i64,) = sqlx::query_as(
            "SELECT COUNT(*) FROM libraries WHERE id = ? AND user_id = ? AND deleted_at IS NULL",
        )
        .bind(library_id)
        .bind(user_id)
        .fetch_one(&self.pool)
        .await?;

        Ok(count > 0)
    }

    pub async fn get_snippets(
        &self,
        user_id: &str,
        library_id: &str,
        since: i64,
        limit: i32,
        offset: i32,
        include_deleted: bool,
    ) -> DbResult<(Vec<Snippet>, i32)> {
        // The read-only `GetSnippets` RPC carries a timestamp cursor only, so
        // it always filters on the timestamp cursor.
        let page = self
            .get_snippets_page(
                user_id,
                library_id,
                DeltaCursor::Timestamp(since),
                limit,
                offset,
                include_deleted,
            )
            .await?;
        Ok((page.snippets, page.total))
    }

    /// Fetch one page of snippets together with the paging facts derived
    /// from it.
    ///
    /// The page is read with `LIMIT limit + 1`, so `has_more` comes from the
    /// rows themselves instead of a second `COUNT(*)` scan over the same
    /// range. The exact `total` is taken from the page whenever the page is
    /// the last one (the range then holds exactly `offset + rows` rows); when
    /// the range continues past the page — or the requested offset is past
    /// the end of the range — the range is still counted so `total` keeps
    /// its exact meaning.
    pub async fn get_snippets_page(
        &self,
        user_id: &str,
        library_id: &str,
        cursor: DeltaCursor,
        limit: i32,
        offset: i32,
        include_deleted: bool,
    ) -> DbResult<SnippetPage> {
        let mut tx = self.pool.begin().await?;

        let page_limit = limit.max(0);
        let mut snippets = fetch_snippets(
            &mut tx,
            user_id,
            library_id,
            cursor,
            page_limit.saturating_add(1),
            offset,
            include_deleted,
        )
        .await?;

        let has_more = snippets.len() > page_limit as usize;
        snippets.truncate(page_limit as usize);

        let total = if !has_more && (!snippets.is_empty() || offset == 0) {
            offset.saturating_add(snippets.len() as i32)
        } else {
            count_snippets(&mut tx, user_id, library_id, cursor, include_deleted).await?
        };

        tx.commit().await?;

        Ok(SnippetPage {
            snippets,
            has_more,
            total,
        })
    }

    #[cfg(test)]
    pub async fn upsert_snippet(
        &self,
        snippet: &Snippet,
        user_id: &str,
        library_id: &str,
    ) -> DbResult<()> {
        // Delegate to the production write path so a test fixture gets the
        // same cursor bookkeeping as a real push.
        let mut tx = self.pool.begin().await?;
        self.upsert_snippet_in_tx(&mut tx, snippet, user_id, library_id)
            .await?;
        tx.commit().await?;
        Ok(())
    }

    pub async fn upsert_snippet_in_tx(
        &self,
        tx: &mut sqlx::Transaction<'_, sqlx::Sqlite>,
        snippet: &Snippet,
        user_id: &str,
        library_id: &str,
    ) -> DbResult<UpsertOutcome> {
        let tags_json = serde_json::to_string(&snippet.tags).unwrap_or_else(|_| "[]".to_string());
        let deleted = snippet.deleted as i32;
        let encrypted = snippet.encrypted as i32;

        // The server assigns the write sequence. The counter is bumped
        // **first** so this transaction takes SQLite's write lock before it
        // reads the watermark: two concurrent writes to one library serialize
        // here and receive distinct sequence values instead of both reading
        // the same counter. The bump shares the transaction with the row
        // write, so a failed write consumes no sequence value.
        let sequence = bump_library_sequence(tx, library_id).await?;

        // The timestamp delta cursor is the library's `visible_at` watermark,
        // not the client-chosen `updated_at`: a row stored *below* the
        // watermark would never be delivered to any client, because the
        // timestamp delta pull filters on `visible_at >= since`. The stored
        // `updated_at` keeps the client's logical modification time verbatim
        // — that is what the server's last-write-wins rule and the client's
        // merge order compare — while `visible_at` carries the cursor position
        // the row is delivered at.
        let (watermark,): (i64,) = sqlx::query_as(
            "SELECT COALESCE(MAX(visible_at), 0) FROM snippets WHERE user_id = ? AND library_id = ?",
        )
        .bind(user_id)
        .bind(library_id)
        .fetch_one(&mut **tx)
        .await?;
        let visible_at = snippet.updated_at.max(watermark);

        // Upsert with last-write-wins conflict resolution.
        // Tie-breaking uses device_id string comparison, which is correct for
        // lowercase hex UUIDs (lexicographic == numeric for lowercase hex).
        // When updated_at and device_id are equal, fall back to content
        // comparison (command, description) to provide a deterministic
        // winner that mirrors the client's fingerprint tie-break.
        //
        // The conflict comparison uses the client's raw `updated_at`, so the
        // rule is unchanged by the cursor bookkeeping; only `visible_at` and
        // `sequence` move, and neither moves backwards.
        let result = sqlx::query(
            "INSERT INTO snippets (id, user_id, library_id, description, command, tags, created_at, updated_at, device_id, deleted, encrypted, visible_at, sequence)
              VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
              ON CONFLICT(id) DO UPDATE SET
                 description = excluded.description,
                 command = excluded.command,
                 tags = excluded.tags,
                 updated_at = excluded.updated_at,
                 device_id = excluded.device_id,
                 deleted = excluded.deleted,
                 encrypted = excluded.encrypted,
                 visible_at = MAX(snippets.visible_at, excluded.visible_at),
                 sequence = excluded.sequence
              WHERE excluded.user_id = snippets.user_id AND excluded.library_id = snippets.library_id \
            AND (excluded.updated_at > snippets.updated_at \
                 OR (excluded.updated_at = snippets.updated_at AND excluded.device_id > snippets.device_id) \
                 OR (excluded.updated_at = snippets.updated_at AND excluded.device_id = snippets.device_id AND excluded.command > snippets.command) \
                 OR (excluded.updated_at = snippets.updated_at AND excluded.device_id = snippets.device_id AND excluded.command = snippets.command AND excluded.description > snippets.description))",
        )
        .bind(&snippet.id)
        .bind(user_id)
        .bind(library_id)
        .bind(&snippet.description)
        .bind(&snippet.command)
        .bind(&tags_json)
        .bind(snippet.created_at)
        .bind(snippet.updated_at)
        .bind(&snippet.device_id)
        .bind(deleted)
        .bind(encrypted)
        .bind(visible_at)
        .bind(sequence)
        .execute(&mut **tx)
        .await?;

        if result.rows_affected() > 0 {
            return Ok(UpsertOutcome::Written);
        }

        // Zero rows written. Either the existing record under this same
        // user/library is newer (normal last-write-wins dedupe), or the id is
        // already held by a different scope and the write was dropped. Only a
        // scope probe can tell them apart.
        let foreign: Option<(String, String)> = sqlx::query_as(
            "SELECT user_id, library_id FROM snippets \
             WHERE id = ? AND (user_id != ? OR library_id != ?) LIMIT 1",
        )
        .bind(&snippet.id)
        .bind(user_id)
        .bind(library_id)
        .fetch_optional(&mut **tx)
        .await?;

        Ok(match foreign {
            Some(_) => UpsertOutcome::ForeignScope,
            None => UpsertOutcome::Stale,
        })
    }

    pub async fn get_latest_timestamp(&self, user_id: &str, library_id: &str) -> DbResult<i64> {
        // The cursor is the `visible_at` watermark, not `MAX(updated_at)`:
        // `updated_at` is chosen by the client and may sit below the current
        // watermark, which would make the cursor non-monotone.
        let (timestamp,): (i64,) = sqlx::query_as(
            "SELECT COALESCE(MAX(visible_at), 0) FROM snippets WHERE user_id = ? AND library_id = ?",
        )
        .bind(user_id)
        .bind(library_id)
        .fetch_one(&self.pool)
        .await?;

        Ok(timestamp)
    }

    /// The library's write-sequence watermark.
    ///
    /// This is the cursor the server hands back to a client that syncs on the
    /// sequence path. It is the highest sequence any row of the library
    /// holds, so it never moves backwards and never skips a row.
    pub async fn get_latest_sequence(&self, user_id: &str, library_id: &str) -> DbResult<i64> {
        let (sequence,): (i64,) = sqlx::query_as(
            "SELECT COALESCE(MAX(sequence), 0) FROM snippets WHERE user_id = ? AND library_id = ?",
        )
        .bind(user_id)
        .bind(library_id)
        .fetch_one(&self.pool)
        .await?;

        Ok(sequence)
    }

    /// Add the write-sequence cursor: its column, its per-library counter,
    /// and the unique index that turns a counter race into a loud failure.
    ///
    /// The backfill gives every row a sequence that preserves the
    /// pre-migration `updated_at` ordering (ranked inside the row's library,
    /// ties broken by the row id), so a migrated database selects the same
    /// rows as an unmigrated one for the first sync and every client's stored
    /// cursor stays valid. The counter is then seeded from the highest
    /// sequence of each library, so the next write can never collide with a
    /// backfilled row.
    ///
    /// Idempotent: it is safe to run on every start.
    pub async fn migrate_snippet_sequence(&self) -> DbResult<bool> {
        let columns: Vec<String> =
            sqlx::query_scalar("SELECT name FROM pragma_table_info('snippets')")
                .fetch_all(&self.pool)
                .await?;

        let migrated = if columns.iter().any(|name| name == "sequence") {
            false
        } else {
            sqlx::query("ALTER TABLE snippets ADD COLUMN sequence INTEGER NOT NULL DEFAULT 0")
                .execute(&self.pool)
                .await?;
            // The rank of the row inside its library, ordered by the
            // pre-migration `updated_at` and then by the row id. The
            // backfilled ordering therefore preserves the pre-migration
            // ordering, and every row of a library gets a distinct sequence.
            sqlx::query(
                "WITH ranked AS (
                     SELECT id,
                            ROW_NUMBER() OVER (
                                PARTITION BY user_id, library_id
                                ORDER BY updated_at ASC, id ASC
                            ) AS rn
                     FROM snippets
                 )
                 UPDATE snippets
                 SET sequence = (SELECT rn FROM ranked WHERE ranked.id = snippets.id)",
            )
            .execute(&self.pool)
            .await?;
            true
        };

        // The counter must never hand out a value a row already holds, so it
        // is seeded from the highest sequence of each library. The seed runs
        // through a subquery with a `WHERE` clause: SQLite needs one to tell
        // the upsert's `ON CONFLICT` apart from a join's `ON`.
        sqlx::query(
            "INSERT INTO library_sequences (library_id, sequence)
             SELECT library_id, highest
             FROM (
                 SELECT library_id, MAX(sequence) AS highest
                 FROM snippets
                 GROUP BY library_id
             ) WHERE true
             ON CONFLICT(library_id) DO UPDATE
                SET sequence = MAX(library_sequences.sequence, excluded.sequence)",
        )
        .execute(&self.pool)
        .await?;

        // The unique index is the enforcement mechanism for the concurrent
        // counter race: two rows sharing a (library_id, sequence) are
        // rejected loudly instead of silently making one of them invisible.
        sqlx::query(
            "CREATE UNIQUE INDEX IF NOT EXISTS idx_snippets_library_sequence \
             ON snippets(library_id, sequence)",
        )
        .execute(&self.pool)
        .await?;

        // The range index for the sequence form of the delta filter.
        sqlx::query(
            "CREATE INDEX IF NOT EXISTS idx_snippets_user_library_sequence \
             ON snippets(user_id, library_id, sequence, deleted)",
        )
        .execute(&self.pool)
        .await?;

        if migrated {
            tracing::info!("Migrated snippets table: added the sequence write-cursor column");
        }

        Ok(migrated)
    }

    /// Add the `visible_at` cursor column to an existing database.
    ///
    /// The column carries the delta cursor position of a row; `updated_at`
    /// keeps the client's logical modification time. Existing rows are
    /// backfilled with their current `updated_at`, so a migrated database
    /// selects exactly the same rows as before the migration and every
    /// client's stored cursor stays valid.
    pub async fn migrate_snippet_visible_at(&self) -> DbResult<bool> {
        let columns: Vec<String> =
            sqlx::query_scalar("SELECT name FROM pragma_table_info('snippets')")
                .fetch_all(&self.pool)
                .await?;
        if columns.iter().any(|name| name == "visible_at") {
            return Ok(false);
        }

        sqlx::query("ALTER TABLE snippets ADD COLUMN visible_at INTEGER NOT NULL DEFAULT 0")
            .execute(&self.pool)
            .await?;
        sqlx::query("UPDATE snippets SET visible_at = updated_at")
            .execute(&self.pool)
            .await?;
        sqlx::query(
            "CREATE INDEX IF NOT EXISTS idx_snippets_user_library_visible \
             ON snippets(user_id, library_id, visible_at, deleted)",
        )
        .execute(&self.pool)
        .await?;

        tracing::info!("Migrated snippets table: added the visible_at delta-cursor column");
        Ok(true)
    }

    pub async fn migrate_plaintext_api_keys(&self) -> DbResult<usize> {
        let mut migrated = 0;
        let mut last_id = String::new();

        loop {
            let rows: Vec<(String, String, Option<String>)> = sqlx::query_as(
                "SELECT id, api_key, api_key_prefix FROM users \
                 WHERE id > ? ORDER BY id LIMIT ?",
            )
            .bind(&last_id)
            .bind(MIGRATION_BATCH_SIZE)
            .fetch_all(&self.pool)
            .await?;

            if rows.is_empty() {
                break;
            }

            // Each batch commits atomically: a crash mid-batch leaves every
            // row in the batch at its pre-migration state, so auth never sees
            // a half-migrated user (hash updated but prefix stale).
            let mut tx = self.pool.begin().await?;
            for (user_id, stored, prefix) in rows {
                last_id = user_id.clone();
                let mut needs_update = false;
                let mut new_hash = stored.clone();
                let mut new_prefix = prefix.clone();

                // Migrate plaintext to hashed
                if !stored.starts_with("$argon2") {
                    new_hash = hash_api_key(&stored)?;
                    needs_update = true;
                }

                // Backfill prefix if missing
                if prefix.is_none() {
                    if stored.starts_with("$argon2") {
                        // Legacy hashed key: derive a stable prefix from the stored hash.
                        // The original API key is unrecoverable, so this user will no
                        // longer be able to authenticate — but the NULL-prefix fallback
                        // that amplified auth into an O(N) Argon2id DoS is removed.
                        new_prefix = Some(derive_prefix_from_hash(&stored));
                    } else {
                        new_prefix = Some(compute_api_key_prefix(&stored));
                    }
                    needs_update = true;
                }

                if needs_update {
                    sqlx::query("UPDATE users SET api_key = ?, api_key_prefix = ? WHERE id = ?")
                        .bind(&new_hash)
                        .bind(&new_prefix)
                        .bind(&user_id)
                        .execute(&mut *tx)
                        .await?;
                    migrated += 1;
                }
            }
            tx.commit().await?;
        }

        if migrated > 0 {
            tracing::info!("Migrated {} API keys (hash or prefix update)", migrated);
        }

        Ok(migrated)
    }
}

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

    async fn setup_db() -> Database {
        Database::connect("sqlite::memory:", 5).await.unwrap()
    }

    #[tokio::test]
    async fn test_file_database_is_created_from_path() {
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("nested").join("snippets.db");

        let _db = Database::connect(path.to_str().unwrap(), 1).await.unwrap();

        assert!(path.exists());
    }

    /// The server's data directory holds ciphertext, but also metadata and
    /// Argon2id hashes. On Unix no other local user may traverse or read it.
    #[cfg(unix)]
    #[tokio::test]
    async fn data_directory_is_created_owner_only() {
        use std::os::unix::fs::PermissionsExt;

        let temp = tempfile::tempdir().unwrap();
        let dir = temp.path().join("server-data");
        let path = dir.join("snippets.db");

        let _db = Database::connect(path.to_str().unwrap(), 1).await.unwrap();

        let mode = std::fs::metadata(&dir).unwrap().permissions().mode() & 0o777;
        assert_eq!(
            mode, 0o700,
            "the data directory must be created owner-only, got {mode:#05o}"
        );
    }

    /// A database file that already exists with a broader mode is tightened in
    /// place; the contents are never rewritten by the permission change.
    #[cfg(unix)]
    #[tokio::test]
    async fn database_file_is_restricted_to_owner_only() {
        use std::os::unix::fs::PermissionsExt;

        let temp = tempfile::tempdir().unwrap();
        let dir = temp.path().join("server-data");
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("snippets.db");
        // Simulate a pre-existing world-readable database.
        std::fs::write(&path, b"").unwrap();
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap();

        let _db = Database::connect(path.to_str().unwrap(), 1).await.unwrap();

        let mode = std::fs::metadata(&path).unwrap().permissions().mode() & 0o777;
        assert_eq!(
            mode & 0o077,
            0,
            "the database file must not be group/world accessible, got {mode:#05o}"
        );
    }

    /// A pre-existing broader directory is tightened rather than abandoned.
    #[cfg(unix)]
    #[tokio::test]
    async fn preexisting_broad_directory_is_tightened() {
        use std::os::unix::fs::PermissionsExt;

        let temp = tempfile::tempdir().unwrap();
        let dir = temp.path().join("server-data");
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o755)).unwrap();
        let path = dir.join("snippets.db");

        let _db = Database::connect(path.to_str().unwrap(), 1).await.unwrap();

        let mode = std::fs::metadata(&dir).unwrap().permissions().mode() & 0o777;
        assert_eq!(mode, 0o700, "an existing broad mode must be tightened");
    }

    /// The in-memory database has no filesystem path and must not trip the
    /// permission helpers.
    #[tokio::test]
    async fn memory_database_skips_file_permission_handling() {
        let _db = setup_db().await;
    }

    #[tokio::test]
    async fn test_create_and_verify_user() {
        let db = setup_db().await;
        let api_key = "test-api-key-12345";

        let user_id = db.create_user(api_key).await.unwrap();
        assert!(!user_id.is_empty());

        // Should find user with correct API key
        let found = db.get_user_by_api_key(api_key).await.unwrap();
        assert_eq!(found, Some(user_id));

        // Should not find user with wrong API key
        let not_found = db.get_user_by_api_key("wrong-key").await.unwrap();
        assert_eq!(not_found, None);
    }

    #[tokio::test]
    async fn test_api_key_is_hashed() {
        let db = setup_db().await;
        let api_key = "plaintext-key-should-be-hashed";

        let _user_id = db.create_user(api_key).await.unwrap();

        // Verify the stored key is an Argon2 hash, not plaintext
        let rows: Vec<(String, String, Option<String>)> =
            sqlx::query_as("SELECT id, api_key, api_key_prefix FROM users")
                .fetch_all(&db.pool)
                .await
                .unwrap();

        assert_eq!(rows.len(), 1);
        assert!(
            rows[0].1.starts_with("$argon2"),
            "API key should be stored as Argon2 hash"
        );
        // Verify prefix is stored
        assert!(rows[0].2.is_some(), "API key prefix should be stored");
        assert_eq!(rows[0].2.as_ref().unwrap().len(), 8);
    }

    #[tokio::test]
    async fn test_create_default_library() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();

        let lib_id = db.get_default_library(&user_id).await.unwrap();
        assert!(!lib_id.is_empty());

        let (libraries, total) = db.list_libraries(&user_id, 10, 0).await.unwrap();
        assert_eq!(total, 1);
        assert_eq!(libraries[0].name, "default");
    }

    #[tokio::test]
    async fn test_create_additional_library() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();

        let lib_id = db.create_library(&user_id, "work").await.unwrap();
        assert!(!lib_id.is_empty());

        let (_libraries, total) = db.list_libraries(&user_id, 10, 0).await.unwrap();
        assert_eq!(total, 2);

        // Duplicate should fail with Conflict
        let err = db.create_library(&user_id, "work").await.unwrap_err();
        assert!(matches!(err, DbError::Conflict(_)));
    }

    #[tokio::test]
    async fn test_invalid_library_names() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();

        // Empty name
        assert!(db.create_library(&user_id, "").await.is_err());

        // Name with slash
        assert!(db.create_library(&user_id, "my/lib").await.is_err());

        // Name too long
        assert!(db.create_library(&user_id, &"a".repeat(65)).await.is_err());

        // Valid names
        assert!(db.create_library(&user_id, "valid-name").await.is_ok());
        assert!(db.create_library(&user_id, "valid_name").await.is_ok());
        assert!(db.create_library(&user_id, "ValidName123").await.is_ok());
    }

    #[tokio::test]
    async fn test_upsert_and_get_snippets() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        let snippet = Snippet {
            id: "snip-1".to_string(),
            description: "Test snippet".to_string(),
            command: "echo hello".to_string(),
            tags: vec!["test".to_string()],
            created_at: 100,
            updated_at: 100,
            device_id: "device-1".to_string(),
            deleted: false,
            encrypted: false,
        };

        db.upsert_snippet(&snippet, &user_id, &lib_id)
            .await
            .unwrap();

        let (snippets, total) = db
            .get_snippets(&user_id, &lib_id, 0, 10, 0, false)
            .await
            .unwrap();
        assert_eq!(total, 1);
        assert_eq!(snippets[0].description, "Test snippet");
        assert_eq!(snippets[0].command, "echo hello");
        assert_eq!(snippets[0].tags, vec!["test"]);
    }

    /// Delta queries must include rows at exactly the watermark: the cursor
    /// has second granularity and the client stores `MAX(visible_at)` as
    /// `last_sync`. With strict `>`, a row written in the same second as the
    /// watermark was invisible to every later incremental sync.
    #[tokio::test]
    async fn test_delta_query_includes_watermark_boundary_row() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        let snippet = Snippet {
            id: "boundary-snip".to_string(),
            description: "Boundary".to_string(),
            command: "echo boundary".to_string(),
            tags: vec![],
            created_at: 500,
            updated_at: 500,
            device_id: "device-1".to_string(),
            deleted: false,
            encrypted: false,
        };
        db.upsert_snippet(&snippet, &user_id, &lib_id)
            .await
            .unwrap();

        let latest = db.get_latest_timestamp(&user_id, &lib_id).await.unwrap();
        assert_eq!(latest, 500);

        // A subsequent incremental sync sends `since = MAX(visible_at)`.
        let (snippets, total) = db
            .get_snippets(&user_id, &lib_id, latest, 10, 0, false)
            .await
            .unwrap();
        assert_eq!(total, 1, "row at exactly `since` must be returned");
        assert_eq!(snippets[0].id, "boundary-snip");
    }

    /// A row whose client-chosen `updated_at` sits *below* the library's
    /// watermark must still be delivered: the delta cursor lives in
    /// `visible_at`, which is clamped to the watermark, while `updated_at`
    /// keeps the client's logical modification time.
    #[tokio::test]
    async fn test_insert_below_watermark_is_still_delivered() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        let make = |id: &str, updated_at: i64| Snippet {
            id: id.to_string(),
            description: format!("desc-{id}"),
            command: format!("echo {id}"),
            tags: vec![],
            created_at: updated_at,
            updated_at,
            device_id: "device-1".to_string(),
            deleted: false,
            encrypted: false,
        };

        let mut tx = db.pool.begin().await.unwrap();
        assert_eq!(
            db.upsert_snippet_in_tx(&mut tx, &make("newer", 1000), &user_id, &lib_id)
                .await
                .unwrap(),
            UpsertOutcome::Written
        );
        assert_eq!(
            db.upsert_snippet_in_tx(&mut tx, &make("older", 500), &user_id, &lib_id)
                .await
                .unwrap(),
            UpsertOutcome::Written
        );
        tx.commit().await.unwrap();

        let watermark = db.get_latest_timestamp(&user_id, &lib_id).await.unwrap();
        assert_eq!(watermark, 1000);

        let (snippets, total) = db
            .get_snippets(&user_id, &lib_id, watermark, 10, 0, false)
            .await
            .unwrap();
        assert_eq!(
            total, 2,
            "the row pushed below the watermark must still be delivered by a delta pull"
        );

        // The stored logical time is the client's own value, unchanged.
        let older = snippets
            .iter()
            .find(|s| s.id == "older")
            .expect("the older row must be present");
        assert_eq!(older.updated_at, 500);
    }

    /// The cursor bookkeeping must not change the server's last-write-wins
    /// rule: the comparison uses the client's raw `updated_at`, so a newer
    /// push of a row that was stored below the watermark still wins.
    #[tokio::test]
    async fn test_update_below_the_watermark_still_uses_the_raw_timestamp() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        let make = |id: &str, command: &str, updated_at: i64| Snippet {
            id: id.to_string(),
            description: "desc".to_string(),
            command: command.to_string(),
            tags: vec![],
            created_at: 0,
            updated_at,
            device_id: "device-1".to_string(),
            deleted: false,
            encrypted: false,
        };

        // `other` sets the watermark at 1000.
        let mut tx = db.pool.begin().await.unwrap();
        db.upsert_snippet_in_tx(
            &mut tx,
            &make("other", "echo other", 1000),
            &user_id,
            &lib_id,
        )
        .await
        .unwrap();
        tx.commit().await.unwrap();

        // This row lands below the watermark.
        let mut tx = db.pool.begin().await.unwrap();
        assert_eq!(
            db.upsert_snippet_in_tx(&mut tx, &make("row", "echo one", 500), &user_id, &lib_id)
                .await
                .unwrap(),
            UpsertOutcome::Written
        );
        tx.commit().await.unwrap();

        // A newer push of the same row must win: the comparison reads the raw
        // `updated_at`, not the clamped cursor.
        let mut tx = db.pool.begin().await.unwrap();
        assert_eq!(
            db.upsert_snippet_in_tx(&mut tx, &make("row", "echo two", 700), &user_id, &lib_id)
                .await
                .unwrap(),
            UpsertOutcome::Written
        );
        tx.commit().await.unwrap();

        // A genuinely older push is still rejected.
        let mut tx = db.pool.begin().await.unwrap();
        assert_eq!(
            db.upsert_snippet_in_tx(&mut tx, &make("row", "echo older", 600), &user_id, &lib_id)
                .await
                .unwrap(),
            UpsertOutcome::Stale
        );
        tx.commit().await.unwrap();

        let (snippets, _) = db
            .get_snippets(&user_id, &lib_id, 0, 10, 0, false)
            .await
            .unwrap();
        let row = snippets
            .iter()
            .find(|s| s.id == "row")
            .expect("the updated snippet must be present");
        assert_eq!(row.command, "echo two");
        assert_eq!(row.updated_at, 700);
    }

    /// An existing database is upgraded in place: the backfill keeps every
    /// row at its current cursor position, so the rows a client pulls do not
    /// change across the migration.
    #[tokio::test]
    async fn test_migrate_snippet_visible_at_backfills_the_cursor() {
        let temp = tempfile::tempdir().unwrap();
        let db_path = temp.path().join("legacy.sqlite");
        let db = Database::connect(db_path.to_str().unwrap(), 1)
            .await
            .unwrap();
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        // Simulate a pre-migration database: the column does not exist.
        sqlx::query("DROP INDEX IF EXISTS idx_snippets_user_library_visible")
            .execute(db.pool())
            .await
            .unwrap();
        sqlx::query("ALTER TABLE snippets DROP COLUMN visible_at")
            .execute(db.pool())
            .await
            .unwrap();
        let snippet = Snippet {
            id: "legacy-1".to_string(),
            description: "legacy".to_string(),
            command: "echo legacy".to_string(),
            tags: vec![],
            created_at: 100,
            updated_at: 100,
            device_id: "device-1".to_string(),
            deleted: false,
            encrypted: false,
        };
        sqlx::query(
            "INSERT INTO snippets (id, user_id, library_id, description, command, tags, created_at, updated_at, device_id, deleted, encrypted)
             VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        )
        .bind(&snippet.id)
        .bind(&user_id)
        .bind(&lib_id)
        .bind(&snippet.description)
        .bind(&snippet.command)
        .bind("[]")
        .bind(snippet.created_at)
        .bind(snippet.updated_at)
        .bind(&snippet.device_id)
        .bind(0)
        .bind(0)
        .execute(db.pool())
        .await
        .unwrap();

        assert!(db.migrate_snippet_visible_at().await.unwrap());
        // A second run is a no-op.
        assert!(!db.migrate_snippet_visible_at().await.unwrap());

        assert_eq!(
            db.get_latest_timestamp(&user_id, &lib_id).await.unwrap(),
            100
        );
        let (snippets, total) = db
            .get_snippets(&user_id, &lib_id, 100, 10, 0, false)
            .await
            .unwrap();
        assert_eq!(total, 1);
        assert_eq!(snippets[0].id, "legacy-1");
        assert_eq!(snippets[0].updated_at, 100);
    }

    #[tokio::test]
    async fn test_delete_library_preserves_future_tombstone_timestamp() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let library_id = db.create_library(&user_id, "future-delete").await.unwrap();
        let future_timestamp = OffsetDateTime::now_utc().unix_timestamp() + 100;

        let snippet = Snippet {
            id: "future-snippet".to_string(),
            description: "desc".to_string(),
            command: "echo hi".to_string(),
            tags: vec![],
            created_at: 0,
            updated_at: future_timestamp,
            device_id: "device".to_string(),
            deleted: false,
            encrypted: false,
        };
        db.upsert_snippet(&snippet, &user_id, &library_id)
            .await
            .unwrap();

        db.delete_library(&user_id, &library_id).await.unwrap();

        let (snippets, total) = db
            .get_snippets(&user_id, &library_id, future_timestamp - 1, 10, 0, true)
            .await
            .unwrap();
        assert_eq!(total, 1);
        assert!(snippets[0].deleted);
        assert_eq!(snippets[0].updated_at, future_timestamp);
    }

    #[tokio::test]
    async fn test_upsert_updates_existing() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        let snippet = Snippet {
            id: "snip-1".to_string(),
            description: "Original".to_string(),
            command: "echo old".to_string(),
            tags: vec![],
            created_at: 100,
            updated_at: 100,
            device_id: "device-1".to_string(),
            deleted: false,
            encrypted: false,
        };
        db.upsert_snippet(&snippet, &user_id, &lib_id)
            .await
            .unwrap();

        // Update with newer timestamp
        let updated = Snippet {
            id: "snip-1".to_string(),
            description: "Updated".to_string(),
            command: "echo new".to_string(),
            tags: vec!["updated".to_string()],
            created_at: 100,
            updated_at: 200,
            device_id: "device-1".to_string(),
            deleted: false,
            encrypted: false,
        };
        db.upsert_snippet(&updated, &user_id, &lib_id)
            .await
            .unwrap();

        let (snippets, _) = db
            .get_snippets(&user_id, &lib_id, 0, 10, 0, false)
            .await
            .unwrap();
        assert_eq!(snippets.len(), 1);
        assert_eq!(snippets[0].description, "Updated");
        assert_eq!(snippets[0].command, "echo new");
    }

    #[tokio::test]
    async fn test_upsert_rejects_older_timestamp() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        // Insert snippet at updated_at: 200
        let snippet = Snippet {
            id: "snip-1".to_string(),
            description: "Newer".to_string(),
            command: "echo newer".to_string(),
            tags: vec![],
            created_at: 100,
            updated_at: 200,
            device_id: "d".to_string(),
            deleted: false,
            encrypted: false,
        };
        db.upsert_snippet(&snippet, &user_id, &lib_id)
            .await
            .unwrap();

        // Attempt to upsert same ID with older timestamp (should be rejected)
        let older = Snippet {
            id: "snip-1".to_string(),
            description: "Older".to_string(),
            command: "echo older".to_string(),
            tags: vec![],
            created_at: 100,
            updated_at: 100,
            device_id: "d".to_string(),
            deleted: false,
            encrypted: false,
        };
        db.upsert_snippet(&older, &user_id, &lib_id).await.unwrap();

        // Newer data should be preserved
        let (snippets, _) = db
            .get_snippets(&user_id, &lib_id, 0, 10, 0, false)
            .await
            .unwrap();
        assert_eq!(snippets.len(), 1);
        assert_eq!(snippets[0].description, "Newer");
        assert_eq!(snippets[0].command, "echo newer");
    }

    #[tokio::test]
    async fn test_get_latest_timestamp() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        // Empty library should return 0
        let ts = db.get_latest_timestamp(&user_id, &lib_id).await.unwrap();
        assert_eq!(ts, 0);

        let snippet = Snippet {
            id: "snip-1".to_string(),
            description: "Test".to_string(),
            command: "echo".to_string(),
            tags: vec![],
            created_at: 100,
            updated_at: 500,
            device_id: "d".to_string(),
            deleted: false,
            encrypted: false,
        };
        db.upsert_snippet(&snippet, &user_id, &lib_id)
            .await
            .unwrap();

        let ts = db.get_latest_timestamp(&user_id, &lib_id).await.unwrap();
        assert_eq!(ts, 500);
    }

    #[tokio::test]
    async fn test_delete_library() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.create_library(&user_id, "temp").await.unwrap();

        // Add a snippet
        let snippet = Snippet {
            id: "snip-1".to_string(),
            description: "Test".to_string(),
            command: "echo".to_string(),
            tags: vec![],
            created_at: 100,
            updated_at: 100,
            device_id: "d".to_string(),
            deleted: false,
            encrypted: false,
        };
        db.upsert_snippet(&snippet, &user_id, &lib_id)
            .await
            .unwrap();

        // Delete the library
        db.delete_library(&user_id, &lib_id).await.unwrap();

        // Should fail to delete again
        assert!(db.delete_library(&user_id, &lib_id).await.is_err());

        // Snippet should be marked deleted
        let (snippets, _) = db
            .get_snippets(&user_id, &lib_id, 0, 10, 0, false)
            .await
            .unwrap();
        assert_eq!(snippets.len(), 0);
    }

    #[tokio::test]
    async fn test_migrate_plaintext_api_keys() {
        let db = setup_db().await;
        let user_id = Uuid::new_v4().to_string();
        let now = OffsetDateTime::now_utc().unix_timestamp();

        // Insert a plaintext API key directly (simulating old schema)
        sqlx::query("INSERT INTO users (id, api_key, created_at, updated_at) VALUES (?, ?, ?, ?)")
            .bind(&user_id)
            .bind("plaintext-api-key")
            .bind(now)
            .bind(now)
            .execute(&db.pool)
            .await
            .unwrap();

        // Run migration
        let migrated = db.migrate_plaintext_api_keys().await.unwrap();
        assert_eq!(migrated, 1);

        // Verify the key is now hashed
        let (stored_hash,): (String,) = sqlx::query_as("SELECT api_key FROM users WHERE id = ?")
            .bind(&user_id)
            .fetch_one(&db.pool)
            .await
            .unwrap();

        assert!(stored_hash.starts_with("$argon2"));

        // Verify prefix was backfilled
        let (prefix,): (Option<String>,) =
            sqlx::query_as("SELECT api_key_prefix FROM users WHERE id = ?")
                .bind(&user_id)
                .fetch_one(&db.pool)
                .await
                .unwrap();
        assert!(
            prefix.is_some(),
            "API key prefix should be backfilled by migration"
        );

        // Verify the original key still works
        let found = db.get_user_by_api_key("plaintext-api-key").await.unwrap();
        assert_eq!(found, Some(user_id));

        // Running migration again should migrate 0
        let migrated_again = db.migrate_plaintext_api_keys().await.unwrap();
        assert_eq!(migrated_again, 0);
    }

    #[tokio::test]
    async fn test_api_key_prefix_lookup() {
        let db = setup_db().await;

        // Create multiple users
        let user1 = db.create_user("key-one-alpha").await.unwrap();
        let user2 = db.create_user("key-two-beta").await.unwrap();
        let user3 = db.create_user("key-three-gamma").await.unwrap();

        // Each user's key should resolve correctly
        assert_eq!(
            db.get_user_by_api_key("key-one-alpha").await.unwrap(),
            Some(user1)
        );
        assert_eq!(
            db.get_user_by_api_key("key-two-beta").await.unwrap(),
            Some(user2)
        );
        assert_eq!(
            db.get_user_by_api_key("key-three-gamma").await.unwrap(),
            Some(user3)
        );
        assert_eq!(db.get_user_by_api_key("nonexistent").await.unwrap(), None);

        // Verify all rows have prefixes
        let rows: Vec<(String,)> =
            sqlx::query_as("SELECT api_key_prefix FROM users WHERE api_key_prefix IS NOT NULL")
                .fetch_all(&db.pool)
                .await
                .unwrap();
        assert_eq!(rows.len(), 3);
    }

    #[tokio::test]
    async fn test_library_ownership_verification() {
        let db = setup_db().await;
        let user1_id = db.create_user("user1-api-key").await.unwrap();
        let user2_id = db.create_user("user2-api-key").await.unwrap();
        let lib_id = db.create_library(&user1_id, "private").await.unwrap();

        assert!(
            db.verify_library_ownership(&user1_id, &lib_id)
                .await
                .unwrap()
        );
        assert!(
            !db.verify_library_ownership(&user2_id, &lib_id)
                .await
                .unwrap()
        );

        let nonexistent_lib = Uuid::new_v4().to_string();
        assert!(
            !db.verify_library_ownership(&user1_id, &nonexistent_lib)
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn test_library_ownership_prevents_cross_user_access() {
        let db = setup_db().await;
        let user1_id = db.create_user("user1-key").await.unwrap();
        let user2_id = db.create_user("user2-key").await.unwrap();
        let user1_lib = db.create_library(&user1_id, "my-library").await.unwrap();

        let snippet = Snippet {
            id: "snip-1".to_string(),
            description: "User 1 secret".to_string(),
            command: "secret command".to_string(),
            tags: vec![],
            created_at: 100,
            updated_at: 100,
            device_id: "device-1".to_string(),
            deleted: false,
            encrypted: false,
        };

        db.upsert_snippet(&snippet, &user1_id, &user1_lib)
            .await
            .unwrap();

        let (snippets, _) = db
            .get_snippets(&user1_id, &user1_lib, 0, 10, 0, false)
            .await
            .unwrap();
        assert_eq!(snippets.len(), 1);

        let (snippets, _) = db
            .get_snippets(&user2_id, &user1_lib, 0, 10, 0, false)
            .await
            .unwrap();
        assert_eq!(snippets.len(), 0);
    }

    #[tokio::test]
    async fn test_deleted_library_not_owned() {
        let db = setup_db().await;
        let user_id = db.create_user("test-key").await.unwrap();
        let lib_id = db.create_library(&user_id, "temp-lib").await.unwrap();

        assert!(
            db.verify_library_ownership(&user_id, &lib_id)
                .await
                .unwrap()
        );

        db.delete_library(&user_id, &lib_id).await.unwrap();

        assert!(
            !db.verify_library_ownership(&user_id, &lib_id)
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn test_get_snippets_include_deleted() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        let snippet = Snippet {
            id: "snip-1".to_string(),
            description: "Test".to_string(),
            command: "echo".to_string(),
            tags: vec![],
            created_at: 100,
            updated_at: 100,
            device_id: "d".to_string(),
            deleted: false,
            encrypted: false,
        };
        db.upsert_snippet(&snippet, &user_id, &lib_id)
            .await
            .unwrap();

        // Soft-delete the snippet
        let deleted = Snippet {
            id: "snip-1".to_string(),
            description: "Test".to_string(),
            command: "echo".to_string(),
            tags: vec![],
            created_at: 100,
            updated_at: 200,
            device_id: "d".to_string(),
            deleted: true,
            encrypted: false,
        };
        db.upsert_snippet(&deleted, &user_id, &lib_id)
            .await
            .unwrap();

        // Without include_deleted: should not see deleted snippet
        let (snippets, _) = db
            .get_snippets(&user_id, &lib_id, 0, 10, 0, false)
            .await
            .unwrap();
        assert_eq!(snippets.len(), 0);

        // With include_deleted: should see deleted snippet
        let (snippets, _) = db
            .get_snippets(&user_id, &lib_id, 0, 10, 0, true)
            .await
            .unwrap();
        assert_eq!(snippets.len(), 1);
        assert!(snippets[0].deleted);
        assert_eq!(snippets[0].updated_at, 200);
    }

    // ---------------------------------------------------------------------
    // Write-sequence cursor
    // ---------------------------------------------------------------------

    fn sequence_snippet(id: &str, updated_at: i64) -> Snippet {
        Snippet {
            id: id.to_string(),
            description: "Test".to_string(),
            command: "echo".to_string(),
            tags: vec![],
            created_at: updated_at,
            updated_at,
            device_id: "d".to_string(),
            deleted: false,
            encrypted: false,
        }
    }

    async fn snippet_sequences(db: &Database, library_id: &str) -> Vec<(String, i64)> {
        sqlx::query_as("SELECT id, sequence FROM snippets WHERE library_id = ? ORDER BY sequence")
            .bind(library_id)
            .fetch_all(db.pool())
            .await
            .unwrap()
    }

    /// The sequence watermark is the highest sequence the library holds.
    #[tokio::test]
    async fn test_sequence_watermark_is_the_library_maximum() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        assert_eq!(db.get_latest_sequence(&user_id, &lib_id).await.unwrap(), 0);

        db.upsert_snippet(&sequence_snippet("a", 100), &user_id, &lib_id)
            .await
            .unwrap();
        db.upsert_snippet(&sequence_snippet("b", 200), &user_id, &lib_id)
            .await
            .unwrap();

        assert_eq!(db.get_latest_sequence(&user_id, &lib_id).await.unwrap(), 2);
        assert_eq!(snippet_sequences(&db, &lib_id).await.len(), 2);
    }

    /// Every write takes a sequence strictly greater than the library's
    /// current maximum, whatever timestamp the client pushed — including a
    /// push whose timestamp is older than the rows already stored.
    #[tokio::test]
    async fn test_write_assigns_a_sequence_above_the_library_maximum() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        db.upsert_snippet(&sequence_snippet("a", 300), &user_id, &lib_id)
            .await
            .unwrap();
        db.upsert_snippet(&sequence_snippet("b", 100), &user_id, &lib_id)
            .await
            .unwrap();
        db.upsert_snippet(&sequence_snippet("c", 200), &user_id, &lib_id)
            .await
            .unwrap();

        let rows = snippet_sequences(&db, &lib_id).await;
        let sequences: Vec<i64> = rows.iter().map(|(_, sequence)| *sequence).collect();
        assert_eq!(sequences, vec![1, 2, 3]);
        assert_eq!(db.get_latest_sequence(&user_id, &lib_id).await.unwrap(), 3);
    }

    /// Two concurrent pushes to one library take distinct sequence values.
    ///
    /// The counter is bumped inside the write transaction, so the two
    /// transactions serialize on it; the unique index on
    /// `(library_id, sequence)` is the enforcement that turns a shared
    /// value into a loud failure instead of a silent one.
    #[tokio::test]
    async fn test_concurrent_pushes_to_one_library_get_distinct_sequences() {
        // A file-backed database: the in-memory database shares one cache
        // across the pool, which serializes writes with a table lock
        // instead of the write lock a real deployment uses.
        let temp = tempfile::tempdir().unwrap();
        let db_path = temp.path().join("concurrent.sqlite");
        let db = Database::connect(db_path.to_str().unwrap(), 5)
            .await
            .unwrap();
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        let first_snippet = sequence_snippet("a", 100);
        let second_snippet = sequence_snippet("b", 200);
        let first = db.upsert_snippet(&first_snippet, &user_id, &lib_id);
        let second = db.upsert_snippet(&second_snippet, &user_id, &lib_id);
        let (first, second) = tokio::join!(first, second);
        first.unwrap();
        second.unwrap();

        let rows = snippet_sequences(&db, &lib_id).await;
        assert_eq!(rows.len(), 2);
        assert_ne!(
            rows[0].1, rows[1].1,
            "concurrent writes to one library must not share a sequence value"
        );
    }

    /// A rolled-back write consumes no sequence value: the counter bump
    /// shares the transaction with the row write.
    #[tokio::test]
    async fn test_rolled_back_write_does_not_consume_a_sequence_value() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        let mut tx = db.pool().begin().await.unwrap();
        db.upsert_snippet_in_tx(&mut tx, &sequence_snippet("a", 100), &user_id, &lib_id)
            .await
            .unwrap();
        tx.rollback().await.unwrap();

        assert_eq!(db.get_latest_sequence(&user_id, &lib_id).await.unwrap(), 0);
        assert!(snippet_sequences(&db, &lib_id).await.is_empty());

        // The next write takes the first sequence value.
        db.upsert_snippet(&sequence_snippet("b", 100), &user_id, &lib_id)
            .await
            .unwrap();
        assert_eq!(db.get_latest_sequence(&user_id, &lib_id).await.unwrap(), 1);
    }

    /// The unique index is the enforcement of the counter race: two rows
    /// that share a `(library_id, sequence)` are rejected loudly.
    #[tokio::test]
    async fn test_unique_index_rejects_a_shared_sequence_value() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        let insert = |id: &str, sequence: i64| {
            sqlx::query(
                "INSERT INTO snippets (id, user_id, library_id, description, command, tags, created_at, updated_at, device_id, deleted, encrypted, visible_at, sequence)
                 VALUES (?, ?, ?, 'd', 'echo', '[]', 1, 1, 'dev', 0, 0, 1, ?)",
            )
            .bind(id)
            .bind(&user_id)
            .bind(&lib_id)
            .bind(sequence)
        };

        insert("a", 1).execute(db.pool()).await.unwrap();
        let error = insert("b", 1).execute(db.pool()).await.unwrap_err();
        assert!(
            matches!(error, sqlx::Error::Database(_)),
            "a shared sequence value must be rejected by the unique index, got {error:?}"
        );
    }

    /// The migration backfills the sequence so that the backfilled ordering
    /// preserves the pre-migration `updated_at` ordering, and seeds the
    /// counter from the backfill so the next write cannot collide.
    #[tokio::test]
    async fn test_migrate_snippet_sequence_backfills_the_updated_at_ordering() {
        let temp = tempfile::tempdir().unwrap();
        let db_path = temp.path().join("legacy.sqlite");
        let db = Database::connect(db_path.to_str().unwrap(), 1)
            .await
            .unwrap();
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        // Simulate a pre-migration database: no sequence column, no indexes.
        sqlx::query("DROP INDEX IF EXISTS idx_snippets_library_sequence")
            .execute(db.pool())
            .await
            .unwrap();
        sqlx::query("DROP INDEX IF EXISTS idx_snippets_user_library_sequence")
            .execute(db.pool())
            .await
            .unwrap();
        sqlx::query("ALTER TABLE snippets DROP COLUMN sequence")
            .execute(db.pool())
            .await
            .unwrap();

        // Three legacy rows, inserted out of `updated_at` order.
        for (id, updated_at) in [("c", 300_i64), ("a", 100), ("b", 200)] {
            sqlx::query(
                "INSERT INTO snippets (id, user_id, library_id, description, command, tags, created_at, updated_at, device_id, deleted, encrypted)
                 VALUES (?, ?, ?, 'd', 'echo', '[]', ?, ?, 'dev', 0, 0)",
            )
            .bind(id)
            .bind(&user_id)
            .bind(&lib_id)
            .bind(updated_at)
            .bind(updated_at)
            .execute(db.pool())
            .await
            .unwrap();
        }

        assert!(db.migrate_snippet_sequence().await.unwrap());
        // A second run is a no-op.
        assert!(!db.migrate_snippet_sequence().await.unwrap());

        let rows: Vec<(String, i64, i64)> =
            sqlx::query_as("SELECT id, updated_at, sequence FROM snippets ORDER BY sequence")
                .fetch_all(db.pool())
                .await
                .unwrap();
        assert_eq!(rows.len(), 3);
        // The backfilled ordering preserves the pre-migration `updated_at`
        // ordering.
        assert_eq!(rows[0].0, "a");
        assert_eq!(rows[1].0, "b");
        assert_eq!(rows[2].0, "c");
        assert!(rows[0].2 < rows[1].2 && rows[1].2 < rows[2].2);

        // The counter is seeded from the backfill, so the next write takes a
        // fresh value instead of colliding with a backfilled row.
        assert_eq!(db.get_latest_sequence(&user_id, &lib_id).await.unwrap(), 3);
        db.upsert_snippet(&sequence_snippet("d", 400), &user_id, &lib_id)
            .await
            .unwrap();
        assert_eq!(db.get_latest_sequence(&user_id, &lib_id).await.unwrap(), 4);
    }

    /// A client that holds a sequence cursor receives exactly the rows
    /// written after that sequence.
    #[tokio::test]
    async fn test_sequence_cursor_selects_the_rows_written_after_it() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        db.upsert_snippet(&sequence_snippet("a", 100), &user_id, &lib_id)
            .await
            .unwrap();
        db.upsert_snippet(&sequence_snippet("b", 200), &user_id, &lib_id)
            .await
            .unwrap();
        db.upsert_snippet(&sequence_snippet("c", 300), &user_id, &lib_id)
            .await
            .unwrap();

        let page = db
            .get_snippets_page(&user_id, &lib_id, DeltaCursor::Sequence(2), 100, 0, true)
            .await
            .unwrap();
        assert_eq!(page.total, 2);
        let mut ids: Vec<&str> = page
            .snippets
            .iter()
            .map(|snippet| snippet.id.as_str())
            .collect();
        ids.sort_unstable();
        assert_eq!(ids, vec!["b", "c"]);

        // The cursor is inclusive: the boundary row is re-delivered, which
        // is safe because merges are idempotent by snippet identity.
        let page = db
            .get_snippets_page(&user_id, &lib_id, DeltaCursor::Sequence(3), 100, 0, true)
            .await
            .unwrap();
        assert_eq!(page.total, 1);
        assert_eq!(page.snippets[0].id, "c");
    }

    /// An old client that sends no sequence cursor receives the rows the
    /// timestamp filter would have selected.
    #[tokio::test]
    async fn test_timestamp_cursor_still_selects_the_timestamp_rows() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        db.upsert_snippet(&sequence_snippet("a", 100), &user_id, &lib_id)
            .await
            .unwrap();
        db.upsert_snippet(&sequence_snippet("b", 200), &user_id, &lib_id)
            .await
            .unwrap();

        let timestamp_page = db
            .get_snippets_page(&user_id, &lib_id, DeltaCursor::Timestamp(0), 100, 0, true)
            .await
            .unwrap();
        let sequence_page = db
            .get_snippets_page(&user_id, &lib_id, DeltaCursor::Sequence(0), 100, 0, true)
            .await
            .unwrap();

        assert_eq!(timestamp_page.total, 2);
        assert_eq!(timestamp_page.total, sequence_page.total);
        let mut timestamp_ids: Vec<&str> = timestamp_page
            .snippets
            .iter()
            .map(|snippet| snippet.id.as_str())
            .collect();
        let mut sequence_ids: Vec<&str> = sequence_page
            .snippets
            .iter()
            .map(|snippet| snippet.id.as_str())
            .collect();
        timestamp_ids.sort_unstable();
        sequence_ids.sort_unstable();
        assert_eq!(timestamp_ids, sequence_ids);
    }

    /// The delta filter stays scoped per user and per library: a sequence
    /// cursor from one library must not select another library's rows, and
    /// another user's rows are never selected.
    #[tokio::test]
    async fn test_sequence_cursor_is_scoped_to_its_library_and_user() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let lib_a = db.get_default_library(&user_id).await.unwrap();
        let lib_b = db.create_library(&user_id, "other").await.unwrap();

        let other_user = db.create_user("other-key").await.unwrap();
        let other_lib = db.get_default_library(&other_user).await.unwrap();

        db.upsert_snippet(&sequence_snippet("a", 100), &user_id, &lib_a)
            .await
            .unwrap();
        // Both libraries hold sequence value 1: the sequence space is
        // per library.
        db.upsert_snippet(&sequence_snippet("b", 100), &user_id, &lib_b)
            .await
            .unwrap();
        db.upsert_snippet(&sequence_snippet("c", 100), &other_user, &other_lib)
            .await
            .unwrap();

        let page = db
            .get_snippets_page(&user_id, &lib_a, DeltaCursor::Sequence(1), 100, 0, true)
            .await
            .unwrap();
        assert_eq!(page.total, 1);
        assert_eq!(page.snippets[0].id, "a");

        // Another user's library is never selected.
        let page = db
            .get_snippets_page(
                &other_user,
                &other_lib,
                DeltaCursor::Sequence(1),
                100,
                0,
                true,
            )
            .await
            .unwrap();
        assert_eq!(page.total, 1);
        assert_eq!(page.snippets[0].id, "c");
    }

    /// A tombstone takes a fresh write sequence, so a client on the sequence
    /// cursor sees the deletion.
    #[tokio::test]
    async fn test_delete_library_gives_the_tombstone_a_fresh_sequence() {
        let db = setup_db().await;
        let user_id = db.create_user("key").await.unwrap();
        let lib_id = db.get_default_library(&user_id).await.unwrap();

        db.upsert_snippet(&sequence_snippet("a", 100), &user_id, &lib_id)
            .await
            .unwrap();
        assert_eq!(db.get_latest_sequence(&user_id, &lib_id).await.unwrap(), 1);

        db.delete_library(&user_id, &lib_id).await.unwrap();

        assert_eq!(db.get_latest_sequence(&user_id, &lib_id).await.unwrap(), 2);
        let page = db
            .get_snippets_page(&user_id, &lib_id, DeltaCursor::Sequence(2), 100, 0, true)
            .await
            .unwrap();
        assert_eq!(page.total, 1);
        assert!(page.snippets[0].deleted);
    }
}