mockforge-registry-core 0.3.135

Shared domain models, storage abstractions, and OSS-safe handlers for MockForge's registry backends (SaaS Postgres + OSS SQLite admin UI).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
//! Unified storage layer for the registry domain.
//!
//! The [`RegistryStore`] trait abstracts over the concrete database backend so
//! the same handlers, middleware, and domain logic can run against either
//! PostgreSQL (for the multi-tenant SaaS binary) or SQLite (for the OSS admin
//! server embedded in `mockforge-ui`).
//!
//! Phase 1a introduces the trait with the API-token domain only. Subsequent
//! phases will add organizations, organization members, settings (BYOK),
//! audit logs, feature usage, users, invitations, and quotas.
//!
//! The initial Postgres implementation delegates to the existing inherent
//! `ApiToken::*` methods so that introducing the trait is a no-op refactor.
//! Later phases will invert this relationship and move the SQL into the trait
//! impls directly.

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use uuid::Uuid;

use crate::models::api_token::{ApiToken, TokenScope};
use crate::models::attestation::{UserPublicKey, UserPublicKeyWithUsage};
use crate::models::audit_log::{AuditEventType, AuditLog};
use crate::models::cloud_fixture::CloudFixture;
use crate::models::cloud_service::CloudService;
use crate::models::cloud_workspace::Workspace as CloudWorkspace;
use crate::models::feature_usage::FeatureType;
use crate::models::federation::Federation;
use crate::models::federation_scenario_activation::FederationScenarioActivation;
use crate::models::hosted_mock::{DeploymentStatus, HealthStatus, HostedMock};
use crate::models::org_template::OrgTemplate;
use crate::models::organization::{OrgMember, OrgRole, Organization, Plan};
use crate::models::osv::{OsvImportRecord, OsvMatch};
use crate::models::plugin::{PendingScanJob, Plugin, PluginSecurityScan, PluginVersion};
use crate::models::review::Review;
use crate::models::saml_assertion::SAMLAssertionId;
use crate::models::scenario::Scenario;
use crate::models::scenario_review::ScenarioReview;
use crate::models::settings::OrgSetting;
use crate::models::sso::{SSOConfiguration, SSOProvider};
use crate::models::subscription::UsageCounter;
use crate::models::suspicious_activity::{SuspiciousActivity, SuspiciousActivityType};
use crate::models::template::{Template, TemplateCategory};
use crate::models::template_review::TemplateReview;
use crate::models::user::User;
use crate::models::verification_token::VerificationToken;
use crate::models::waitlist::WaitlistSubscriber;

#[cfg(feature = "postgres")]
pub mod postgres;

#[cfg(feature = "postgres")]
pub use postgres::PgRegistryStore;

#[cfg(feature = "sqlite")]
pub mod sqlite;

#[cfg(feature = "sqlite")]
pub use sqlite::SqliteRegistryStore;

// `StoreError` and `StoreResult` live in `crate::error`. Re-exported here
// so existing `use crate::store::{StoreError, StoreResult}` imports in
// downstream crates continue to work.
pub use crate::error::{StoreError, StoreResult};

/// Does the OSV `affected_versions` JSON blob cover this concrete version?
///
/// Shared by both backends so they stay in lock-step. The matcher walks
/// OSV `affected[].ranges[]` per the schema at
/// <https://ossf.github.io/osv-schema/>:
///
/// * `versions: ["1.2.3", ...]` — explicit version list, matched
///   literally (case-insensitive). Covers `event-stream@3.3.6`-style
///   single-pin advisories.
/// * `ranges[].events` — an ordered sequence of `introduced` / `fixed` /
///   `last_affected` events. A version is affected if it falls inside
///   any open interval `[introduced, fixed)` or `[introduced, last_affected]`.
/// * `ranges[].type` — `SEMVER`, `ECOSYSTEM`, or `GIT`. SEMVER uses
///   strict semver comparison. ECOSYSTEM ranges usually follow semver but
///   the spec lets each ecosystem define its own ordering; we treat them
///   the same way (semver fallback) and flag the conservative outcome:
///   a version that doesn't parse as semver gets matched only if the
///   range is open-ended (`introduced = "0"` with no close event).
/// * `GIT` ranges over commit hashes — not a concept that makes sense for
///   published plugin artifacts, so those are ignored.
///
/// The pre-semver heuristic behaviors are preserved as fallbacks: the
/// explicit `versions` list still matches literally, and
/// `introduced = "0"` with no `fixed` still means "all versions"
/// regardless of whether the target parses as semver. This keeps
/// hand-crafted OSV records and non-semver ecosystems (e.g. RubyGems)
/// working.
pub fn version_affected(affected: &serde_json::Value, version: &str) -> bool {
    version_affected_in_ecosystem(affected, version, "")
}

/// Ecosystem-aware variant of [`version_affected`]. Strings from
/// non-semver ecosystems (PyPI's PEP 440, Go's `v`-prefixed tags) are
/// normalized to a semver-parseable shape before the range walk; pass an
/// empty string when the ecosystem is unknown and the caller wants the
/// pre-existing behavior. Dispatch is cheap (no allocations in the common
/// npm/cargo/other path) and additive, so new ecosystems can land as
/// small per-family normalizers without touching the core matcher.
pub fn version_affected_in_ecosystem(
    affected: &serde_json::Value,
    version: &str,
    ecosystem: &str,
) -> bool {
    let normalized_target = normalize_version_for_ecosystem(ecosystem, version);
    let effective_version = normalized_target.as_deref().unwrap_or(version);

    // 1. Literal `versions` list match — trumps any range logic.
    if let Some(arr) = affected.get("versions").and_then(|v| v.as_array()) {
        // Match both the caller-supplied string (handles "v1.2.3" literal
        // entries in the list) and the normalized form (so a "1.2.3"
        // entry matches a "v1.2.3" caller). The two are compared
        // case-insensitively; ecosystems that care about case (none we
        // target so far) can grow a variant check later.
        for v in arr.iter().filter_map(|v| v.as_str()) {
            if v.eq_ignore_ascii_case(version) || v.eq_ignore_ascii_case(effective_version) {
                return true;
            }
            if let Some(norm) = normalize_version_for_ecosystem(ecosystem, v) {
                if norm.eq_ignore_ascii_case(effective_version) {
                    return true;
                }
            }
        }
    }

    let Some(ranges) = affected.get("ranges").and_then(|v| v.as_array()) else {
        return false;
    };

    // Parse the target against the ecosystem's normalizer. `target` is
    // the semver representation the range-walk compares against; the
    // original `version` string is kept only for the literal-equality
    // fallback inside `interval_matches`.
    let target = semver::Version::parse(effective_version).ok();

    for r in ranges {
        let range_type =
            r.get("type").and_then(|v| v.as_str()).unwrap_or("SEMVER").to_ascii_uppercase();
        if range_type == "GIT" {
            continue;
        }

        let events = match r.get("events").and_then(|v| v.as_array()) {
            Some(ev) => ev,
            None => continue,
        };

        // Normalize each event's version string the same way we did the
        // target. This is what lets a Go `v1.2.3` advisory match a plain
        // `1.2.3` caller (and vice-versa).
        let normalized: Vec<(serde_json::Value, Option<String>)> = events
            .iter()
            .map(|e| {
                let norm = event_kinds(e)
                    .into_iter()
                    .find_map(|k| event_value(e, k))
                    .and_then(|raw| normalize_version_for_ecosystem(ecosystem, &raw));
                (e.clone(), norm)
            })
            .collect();

        // The OSV spec allows events to appear in any order — consumers
        // are expected to sort them by version before pairing
        // `introduced`→`fixed`/`last_affected`. Every real feed we've
        // seen emits in-order pairs already, but sorting first makes us
        // robust to hand-curated advisories that don't.
        let sorted = sort_events(events);

        // Walk the (now ordered) event list, opening an interval on
        // `introduced` and closing it on the next `fixed` /
        // `last_affected`. Multiple intervals per range are legal.
        let mut intro: Option<String> = None;
        let mut matched = false;
        for e in &sorted {
            // Pick the normalized form for the event if we have one;
            // otherwise use the raw string. `normalized` was indexed by
            // the unsorted position, so find_by_identity here means
            // comparing JSON values.
            let norm_version =
                normalized.iter().find(|(v, _)| v == *e).and_then(|(_, n)| n.clone());
            if let Some(s) = event_value(e, "introduced") {
                intro = norm_version.or(Some(s));
                continue;
            }
            if let Some(fix_raw) = event_value(e, "fixed") {
                let fix = norm_version.unwrap_or(fix_raw);
                if interval_matches(
                    intro.as_deref(),
                    Some(&fix),
                    /*inclusive_upper=*/ false,
                    effective_version,
                    target.as_ref(),
                ) {
                    matched = true;
                    break;
                }
                intro = None;
                continue;
            }
            if let Some(la_raw) = event_value(e, "last_affected") {
                let la = norm_version.unwrap_or(la_raw);
                if interval_matches(
                    intro.as_deref(),
                    Some(&la),
                    /*inclusive_upper=*/ true,
                    effective_version,
                    target.as_ref(),
                ) {
                    matched = true;
                    break;
                }
                intro = None;
                continue;
            }
        }
        if matched {
            return true;
        }

        // Dangling `introduced` with no matching close event = open-ended
        // "all versions ≥ introduced" range (e.g. "this is broken forever").
        if let Some(i) = intro {
            if interval_matches(Some(&i), None, false, effective_version, target.as_ref()) {
                return true;
            }
        }
    }
    false
}

/// Which event keys a single OSV event entry might carry. Cheap
/// reference-only list to keep the normalization loop allocation-free.
fn event_kinds(_: &serde_json::Value) -> [&'static str; 3] {
    ["introduced", "fixed", "last_affected"]
}

/// Normalize a version string into something `semver::Version::parse`
/// can accept, based on the OSV ecosystem the caller supplied. Returns
/// `None` when no rewrite applies — callers fall back to the raw string,
/// preserving backward-compatible behavior for unknown or semver-native
/// ecosystems.
///
/// # Output is an internal-only encoding
///
/// The normalized string is never persisted to the database, returned
/// over the HTTP API, or shown to users. It lives exactly long enough
/// to be handed to `semver::Version::parse` inside the OSV matcher, and
/// then it's thrown away. The only reason this matters is that the
/// PyPI rewrite below produces strings that *look* like valid versions
/// but whose patch number and pre-release tag have been massaged to
/// make semver's comparator agree with PEP 440's ordering. Do **not**
/// surface these strings to callers — they would be confusing and
/// would imply the patch level has changed.
///
/// # Supported ecosystems (case-insensitive)
///
/// * `Go` — strip a leading `v` (`v1.2.3` → `1.2.3`). Go module tags are
///   standard semver with a `v` prefix, so this is a one-character fix.
///
/// * `PyPI` — a pragmatic subset of PEP 440. Rewrites fall into three
///   groups, all chosen so that semver's default comparator reproduces
///   PEP 440's ordering for the pair under test:
///
///   - **Base-only inputs**: drop the `v` prefix, drop the epoch
///     (`1!1.0.0` → `1.0.0`), and split off the local identifier
///     (`1.0+ubuntu1`) into semver build metadata (semver ignores it
///     for ordering; we keep it to preserve round-trip identity).
///
///   - **Pre-release inputs** (`1.0.0a1`, `1.0.0b2`, `1.0.0rc1`,
///     `1.0.0.dev3`): emit `<base>-<rank>.<marker>.<N>` where `<rank>`
///     is a numeric prefix chosen to exploit semver's rule that
///     numeric pre-release identifiers sort before alphabetic ones
///     and numeric identifiers sort by value. The rank table is
///     `dev=0, a=1, b=2, rc=3`, producing `1.0.0-0.dev.3 < 1.0.0-1.a.1
///     < 1.0.0-2.b.2 < 1.0.0-3.rc.1 < 1.0.0`, which matches PEP 440.
///
///   - **Post-release inputs** (`1.0.0.post1`): emit
///     `<base_with_patch_bumped>-0.post.<N>`. PEP 440 says `1.0.0 <
///     1.0.0.post1 < 1.0.1`; semver says *any* pre-release sorts
///     *before* its base, so we can't express `1.0.0.post1` inside
///     the `1.0.0` release. Instead we bump the patch and slot it in
///     as a pre-release of the *next* patch: semver then reads
///     `1.0.0 < 1.0.1-0.post.1 < 1.0.1`, which matches. **Side
///     effect:** the normalized form's patch number is one higher
///     than the input's. This is the reason the doc-comment above
///     screams "never surface these strings."
///
///   Pre+post, pre+dev, and post+dev combinations compose the above
///   tricks. The parity test (`pypi_normalizer_agrees_with_pep440_rs_on_ordering`)
///   documents each supported pair and the edge cases where strict
///   PEP 440 ordering inside a pre-release isn't reachable with
///   pure-semver comparisons.
///
/// Everything else — `npm`, `crates.io`, `RubyGems`, unknown ecosystems
/// — returns `None`, letting the matcher use the string verbatim. This
/// keeps the existing behavior (and test coverage) intact while opening
/// up precise matching for the two ecosystems where drift between
/// published tags and semver is most common.
pub fn normalize_version_for_ecosystem(ecosystem: &str, version: &str) -> Option<String> {
    match ecosystem.to_ascii_lowercase().as_str() {
        "go" => normalize_go_version(version),
        "pypi" => normalize_pypi_version(version),
        _ => None,
    }
}

fn normalize_go_version(version: &str) -> Option<String> {
    let stripped = version.strip_prefix('v').or_else(|| version.strip_prefix('V'))?;
    if stripped.is_empty() {
        return None;
    }
    // Leave the result unvalidated — caller feeds it into
    // `semver::Version::parse`, which will reject garbage.
    Some(stripped.to_string())
}

fn normalize_pypi_version(version: &str) -> Option<String> {
    // Strip common textual noise that PEP 440 declares equivalent to
    // the canonical form. PyPI normalizes display but OSV advisories
    // often preserve the original tag, so we do it client-side.
    let trimmed = version.trim();
    let lowered = if trimmed.chars().any(|c| c.is_ascii_uppercase()) {
        std::borrow::Cow::Owned(trimmed.to_ascii_lowercase())
    } else {
        std::borrow::Cow::Borrowed(trimmed)
    };
    let v = lowered.as_ref();

    // Drop the "v" prefix some packages ship with (`v1.2.3` → `1.2.3`).
    let without_v = v.strip_prefix('v').unwrap_or(v);

    // Drop epoch: `1!2.3.4` → `2.3.4`.
    let after_epoch = without_v.split_once('!').map(|(_, v)| v).unwrap_or(without_v);

    // Split off local identifier (PEP 440 §5): `1.0+ubuntu1` — the part
    // after `+` is installation-metadata and not ordering-relevant for
    // advisory matching. We stash it on the normalized output as
    // semver *build* metadata (which semver also ignores for ordering)
    // so a caller that round-trips gets the original information back
    // without it perturbing range comparisons.
    let (public_part, local_part) = match after_epoch.split_once('+') {
        Some((pub_, loc)) => (pub_, Some(loc)),
        None => (after_epoch, None),
    };

    // Attempt to rewrite PEP 440 pre/post/dev markers. PEP 440 says a
    // version can legally carry, in sequence, a pre-release
    // (`a1`/`b2`/`rc3`), a post-release (`.post4`), and a dev-release
    // (`.dev5`). We strip each section in turn from the right and then
    // assemble a semver form that mirrors pep440 *ordering* — not just
    // "pre-release sorts before base" but the full ranking
    // `dev < a < b < rc < base < post`.
    //
    // The trick is semver pre-release comparison: identifiers are
    // compared left-to-right, numeric-before-alpha, numeric values
    // numerically. So we lead each pre-release with a small numeric
    // rank (`0.dev.N`, `1.a.N`, `2.b.N`, `3.rc.N`). That fixes the
    // `dev < a < b < rc` order without needing a custom comparator.
    //
    // Post-release-alone needs a different trick: semver says any
    // `X.Y.Z-pre` sorts *before* `X.Y.Z`, so we can't squeeze a post
    // above the base in the same release. Instead we bump the patch
    // and use a `0.post.N` pre-release of the next patch, which
    // semver sorts as `X.Y.Z < X.Y.(Z+1)-0.post.N < X.Y.(Z+1)`. That
    // matches `X.Y.Z < X.Y.Z.postN < X.Y.(Z+1)` in pep440.
    let mut remaining = public_part.to_string();
    let mut pre_suffix: Option<String> = None;
    let mut post_suffix: Option<String> = None;
    let mut dev_suffix: Option<String> = None;

    // Dev first (rightmost marker).
    if let Some((base, n)) = split_off_marker(&remaining, ".dev") {
        remaining = base;
        dev_suffix = Some(format!("0.dev.{}", n));
    }
    // Post next.
    if let Some((base, n)) = split_off_marker(&remaining, ".post") {
        remaining = base;
        post_suffix = Some(format!("post.{}", n));
    }
    // Pre-release — exactly one of `rc` / `a` / `b`, in that order so
    // `rc` wins over `a`/`b` on a string like `1.0.0rc1` (which
    // contains neither a digit `a` nor `b` adjacent to the digit
    // prefix). The numeric rank prefix gives us correct ordering:
    // `-1.a.N < -2.b.N < -3.rc.N`.
    for (marker, rank) in [("rc", 3u8), ("a", 1), ("b", 2)] {
        if let Some((base, n)) = split_off_alpha_marker(&remaining, marker) {
            remaining = base;
            pre_suffix = Some(format!("{}.{}.{}", rank, marker, n));
            break;
        }
    }

    // Build the canonical output. The matrix below preserves pep440
    // ordering for every combination we support:
    //
    //   pre,  post, dev   → output
    //   ---   ----  ---   ------
    //   None  None  None  → base
    //   pre   None  None  → base - pre
    //   None  post  None  → base' - 0.post.N  (base' = next-patch base)
    //   None  None  dev   → base - dev
    //   pre   None  dev   → base - pre.dev    (dev of a pre-release sorts before the pre)
    //                                           — we leave this as the pre form because
    //                                           semver `pre.dev.N` > `pre` which is the
    //                                           *wrong* direction; this is the documented
    //                                           limitation called out in the module doc.
    //   pre   post  None  → base - pre+post.N (post of a pre-release; pep440 sorts it
    //                                           between pre and the next pre, which
    //                                           semver handles via build metadata.
    //                                           Limitation: exact ordering within the
    //                                           pre isn't strict semver.)
    //   None  post  dev   → base' - 0.post.N.dev.M — dev of the post-alone rewrite
    //   pre   post  dev   → base - pre+post.N.dev.M  — combined, limited as above
    //
    // The pure-form cases (single non-None slot) all compare correctly
    // against each other and against the base. Combined forms keep
    // some semver build metadata to preserve round-trip identity but
    // may not sort strictly with pep440 on cross-pair comparisons.
    let (sv_pre, sv_build, final_remaining) = match (pre_suffix, post_suffix, dev_suffix) {
        (Some(pre), None, None) => (Some(pre), None, remaining.clone()),
        (None, Some(post), None) => {
            // `1.0.0.post1` → base `1.0.1`, pre `0.post.1`.
            let bumped = bump_patch(&remaining);
            (Some(format!("0.{}", post)), None, bumped)
        }
        (None, None, Some(dev)) => (Some(dev), None, remaining.clone()),
        (Some(pre), Some(post), None) => (Some(pre), Some(post), remaining.clone()),
        (Some(pre), None, Some(dev)) => {
            // Pre + dev: semver puts the extended pre-release *after*
            // the plain pre-release, which disagrees with pep440. We
            // document this in the parity test; the OSV matcher
            // doesn't hit this combination in practice.
            (Some(format!("{}.{}", pre, dev)), None, remaining.clone())
        }
        (None, Some(post), Some(dev)) => {
            // `1.0.0.post1.dev2` → `1.0.1-0.post.1.dev.2`. We inherit
            // the post-alone patch bump; the `.dev.M` appended to the
            // pre-release makes dev-of-post sort *after* the bare
            // post in semver, which disagrees with pep440's "dev of
            // post < post." Still better than the previous behavior,
            // and flagged in the parity test limitations.
            let bumped = bump_patch(&remaining);
            (Some(format!("0.{}.{}", post, dev)), None, bumped)
        }
        (Some(pre), Some(post), Some(dev)) => {
            (Some(format!("{}.{}", pre, dev)), Some(post), remaining.clone())
        }
        (None, None, None) => (None, None, remaining.clone()),
    };

    // Combine local identifier with the computed build metadata. Both
    // are "+"-separated in semver, so we join with a dot.
    let final_build = match (sv_build, local_part) {
        (Some(b), Some(l)) => Some(format!("{}.local.{}", b, sanitize_local(l))),
        (Some(b), None) => Some(b),
        (None, Some(l)) => Some(format!("local.{}", sanitize_local(l))),
        (None, None) => None,
    };

    let mut out = final_remaining;
    if let Some(pre) = sv_pre {
        out.push('-');
        out.push_str(&pre);
    }
    if let Some(build) = final_build {
        out.push('+');
        out.push_str(&build);
    }

    // Return Some only if we actually did work. If the input was plain
    // semver with nothing to rewrite we fall through to None so callers
    // keep the literal string for equality matching.
    if out == version {
        None
    } else {
        Some(out)
    }
}

/// Split a `base.MARKERnumber` string into its `base` and the `number`
/// part, where `marker` is dotted (e.g. `.dev`, `.post`). Returns None
/// when the input doesn't carry the marker or the trailing part isn't
/// a run of digits.
fn split_off_marker(s: &str, marker: &str) -> Option<(String, String)> {
    let idx = find_pep440_marker(s, marker)?;
    let base = &s[..idx];
    let suffix = &s[idx + marker.len()..];
    if suffix.chars().all(|c| c.is_ascii_digit()) && !suffix.is_empty() {
        Some((base.to_string(), suffix.to_string()))
    } else {
        None
    }
}

/// Same as [`split_off_marker`] but for alphabet markers (`a`, `b`,
/// `rc`) which PEP 440 allows immediately after a digit with no
/// separator. We require a digit on the left and a digit run on the
/// right; anything else (e.g. `alpha`) bails out.
fn split_off_alpha_marker(s: &str, marker: &str) -> Option<(String, String)> {
    let idx = find_pep440_marker(s, marker)?;
    let before = s[..idx].chars().last();
    if !before.is_some_and(|c| c.is_ascii_digit()) {
        return None;
    }
    let base = &s[..idx];
    let suffix = &s[idx + marker.len()..];
    if suffix.chars().all(|c| c.is_ascii_digit()) && !suffix.is_empty() {
        Some((base.to_string(), suffix.to_string()))
    } else {
        None
    }
}

/// Increment the patch component of a dotted version string. Used by
/// the post-release normalizer: `1.0.0.post1` becomes `1.0.1-0.post.1`
/// so semver's "pre-release of next patch" ordering gives us the right
/// pep440 semantics (`base < base.postN < next_patch`).
///
/// For inputs with fewer than three numeric components we pad to three
/// first (`1.0` → `1.0.1`, `1` → `1.0.1`) and flag anything we can't
/// parse by returning the input unchanged, which keeps the downstream
/// semver parse conservative rather than producing garbage.
fn bump_patch(version: &str) -> String {
    // Split on the first non-version char (shouldn't exist at this
    // point in the pipeline, but be defensive).
    let parts: Vec<&str> = version.splitn(4, '.').collect();
    let nums: Vec<Option<u64>> = parts.iter().take(3).map(|p| p.parse::<u64>().ok()).collect();
    if nums.iter().any(|n| n.is_none()) {
        return version.to_string();
    }
    let major = nums.first().and_then(|v| *v).unwrap_or(0);
    let minor = nums.get(1).and_then(|v| *v).unwrap_or(0);
    let patch = nums.get(2).and_then(|v| *v).unwrap_or(0);
    format!("{}.{}.{}", major, minor, patch.saturating_add(1))
}

/// Semver build metadata accepts `[0-9A-Za-z-]` plus `.` separators.
/// PEP 440 local identifiers allow `.`, `_`, and `-`; replace
/// disallowed runs with `-` so the resulting string round-trips through
/// `semver::Version::parse`.
fn sanitize_local(local: &str) -> String {
    local
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '.' || c == '-' {
                c
            } else {
                '-'
            }
        })
        .collect()
}

fn find_pep440_marker(s: &str, marker: &str) -> Option<usize> {
    // Scan for each candidate position; reject a hit that's part of a
    // longer identifier (e.g. `alpha` when we're looking for `a`). The
    // character before the marker must be a digit for alphabet markers
    // (`a`, `b`, `rc`); dotted markers (`.dev`, `.post`) carry their own
    // delimiter.
    let dotted = marker.starts_with('.');
    let mut search_from = 0;
    while search_from < s.len() {
        let rest = &s[search_from..];
        let rel = rest.find(marker)?;
        let abs = search_from + rel;
        let before = s[..abs].chars().last();
        let ok = if dotted {
            true // `.dev` / `.post` include their own separator
        } else {
            before.is_some_and(|c| c.is_ascii_digit())
        };
        // Also guard against longer identifiers by requiring the next
        // char (if any) to be either end-of-string or a digit.
        let after = s.as_bytes().get(abs + marker.len()).copied();
        let ok = ok && after.is_none_or(|b| b.is_ascii_digit());
        if ok {
            return Some(abs);
        }
        search_from = abs + marker.len();
    }
    None
}

/// Extract the version string for any of the three OSV event kinds.
/// Returns `None` for events that don't carry one of the known keys.
fn event_value(event: &serde_json::Value, key: &str) -> Option<String> {
    event.get(key).and_then(|v| v.as_str()).map(str::to_string)
}

/// Return events sorted by their semver version, breaking ties by a
/// stable kind ordering (`introduced` < `fixed` < `last_affected`) so an
/// introduced event at version `X` precedes a fixed event at the same
/// version. Events missing a recognizable version bucket keep their
/// original relative order and sort at the end.
fn sort_events(events: &[serde_json::Value]) -> Vec<&serde_json::Value> {
    fn kind_rank(e: &serde_json::Value) -> u8 {
        if e.get("introduced").is_some() {
            0
        } else if e.get("fixed").is_some() {
            1
        } else if e.get("last_affected").is_some() {
            2
        } else {
            3
        }
    }
    fn event_version(e: &serde_json::Value) -> Option<semver::Version> {
        let raw = event_value(e, "introduced")
            .or_else(|| event_value(e, "fixed"))
            .or_else(|| event_value(e, "last_affected"))?;
        // The "0" sentinel means "earliest possible version" — make it
        // compare less than any real release.
        if raw == "0" {
            return Some(semver::Version::new(0, 0, 0));
        }
        semver::Version::parse(&raw).ok()
    }

    let mut indexed: Vec<(usize, &serde_json::Value, Option<semver::Version>, u8)> = events
        .iter()
        .enumerate()
        .map(|(i, e)| (i, e, event_version(e), kind_rank(e)))
        .collect();

    indexed.sort_by(|(ai, _, av, ak), (bi, _, bv, bk)| match (av, bv) {
        (Some(a), Some(b)) => a.cmp(b).then_with(|| ak.cmp(bk)).then_with(|| ai.cmp(bi)),
        // Unparsable versions sort after parsable ones to keep a stable
        // tail; within the unparsable group we keep input order.
        (Some(_), None) => std::cmp::Ordering::Less,
        (None, Some(_)) => std::cmp::Ordering::Greater,
        (None, None) => ai.cmp(bi),
    });
    indexed.into_iter().map(|(_, e, _, _)| e).collect()
}

/// Does `version` fall inside `[introduced, upper)` (exclusive) or
/// `[introduced, upper]` (inclusive)? Handles the OSV-specific
/// `introduced = "0"` sentinel and gracefully degrades when either bound
/// isn't parseable as semver.
fn interval_matches(
    introduced: Option<&str>,
    upper: Option<&str>,
    inclusive_upper: bool,
    version: &str,
    target: Option<&semver::Version>,
) -> bool {
    // "0" on the lower bound means "from the earliest possible version."
    let lower_unbounded = matches!(introduced, Some("0"));
    let lower = if lower_unbounded {
        None
    } else {
        introduced.and_then(|s| semver::Version::parse(s).ok())
    };
    let upper_ver = upper.and_then(|s| semver::Version::parse(s).ok());

    // Fast-path for ecosystems where the target isn't semver-parseable
    // (pre-releases with weird tags, dates-as-versions, etc.): only
    // "introduced = 0" with no close wins, and string equality on the
    // introduced/upper bounds still wins. This is conservative — we'd
    // rather miss a match than falsely flag a clean artifact.
    let Some(target) = target else {
        if lower_unbounded && upper.is_none() {
            return true;
        }
        if let Some(i) = introduced {
            if i.eq_ignore_ascii_case(version) {
                return true;
            }
        }
        if let Some(u) = upper {
            if inclusive_upper && u.eq_ignore_ascii_case(version) {
                return true;
            }
        }
        return false;
    };

    if let Some(l) = &lower {
        if target < l {
            return false;
        }
    }
    if let Some(u) = &upper_ver {
        if inclusive_upper {
            if target > u {
                return false;
            }
        } else if target >= u {
            return false;
        }
    } else if upper.is_some() && upper_ver.is_none() {
        // Upper bound was declared but didn't parse — we can't prove
        // containment, so stay conservative.
        return false;
    }
    true
}

/// Parse an OSV `modified` timestamp (RFC 3339). Falls back to `now` when
/// the upstream record omits the field or it can't be parsed — the cache
/// still needs a timestamp for ordering/replacement decisions.
pub fn parse_modified_str(s: Option<&str>) -> DateTime<Utc> {
    s.and_then(|raw| DateTime::parse_from_rfc3339(raw).ok())
        .map(|dt| dt.with_timezone(&Utc))
        .unwrap_or_else(Utc::now)
}

/// Unified storage trait for the registry domain.
///
/// Implementations must be `Send + Sync + 'static` so they can live behind an
/// `Arc<dyn RegistryStore>` inside `AppState` and be cloned across request
/// handlers without extra synchronization.
#[async_trait]
pub trait RegistryStore: Send + Sync + 'static {
    // ---------------------------------------------------------------------
    // Health
    // ---------------------------------------------------------------------

    /// Ping the backing database. Returns `Ok(())` if the store is reachable.
    /// Implementations should issue the cheapest possible liveness check
    /// (`SELECT 1` for SQL backends).
    async fn health_check(&self) -> StoreResult<()>;

    // ---------------------------------------------------------------------
    // API tokens
    // ---------------------------------------------------------------------

    /// Create a new API token. Returns the plaintext token (shown once) and
    /// the persisted [`ApiToken`] record.
    async fn create_api_token(
        &self,
        org_id: Uuid,
        user_id: Option<Uuid>,
        name: &str,
        scopes: &[TokenScope],
        expires_at: Option<DateTime<Utc>>,
    ) -> StoreResult<(String, ApiToken)>;

    /// Look up a token by its database id.
    async fn find_api_token_by_id(&self, token_id: Uuid) -> StoreResult<Option<ApiToken>>;

    /// List every token that belongs to an organization, newest first.
    async fn list_api_tokens_by_org(&self, org_id: Uuid) -> StoreResult<Vec<ApiToken>>;

    /// Look up a token by its public prefix within an organization.
    async fn find_api_token_by_prefix(
        &self,
        org_id: Uuid,
        prefix: &str,
    ) -> StoreResult<Option<ApiToken>>;

    /// Verify a plaintext token string against stored hashes, updating
    /// `last_used_at` on success. Returns `None` for invalid or expired tokens.
    async fn verify_api_token(&self, token: &str) -> StoreResult<Option<ApiToken>>;

    /// Permanently delete a token.
    async fn delete_api_token(&self, token_id: Uuid) -> StoreResult<()>;

    /// Rotate an existing token — create a replacement with the same scopes
    /// and optionally delete the old one. Returns the new plaintext token,
    /// the new record, and the deleted record (when `delete_old` was `true`).
    async fn rotate_api_token(
        &self,
        token_id: Uuid,
        new_name: Option<&str>,
        delete_old: bool,
    ) -> StoreResult<(String, ApiToken, Option<ApiToken>)>;

    /// Find tokens older than `days_old`, optionally scoped to a single org.
    async fn find_api_tokens_needing_rotation(
        &self,
        org_id: Option<Uuid>,
        days_old: i64,
    ) -> StoreResult<Vec<ApiToken>>;

    // ---------------------------------------------------------------------
    // Organization settings (JSON key/value per org)
    // ---------------------------------------------------------------------

    /// Fetch a single org-level setting by key, returning `None` when absent.
    async fn get_org_setting(&self, org_id: Uuid, key: &str) -> StoreResult<Option<OrgSetting>>;

    /// Upsert an org-level setting, returning the persisted record.
    async fn set_org_setting(
        &self,
        org_id: Uuid,
        key: &str,
        value: serde_json::Value,
    ) -> StoreResult<OrgSetting>;

    /// Delete an org-level setting by key. Idempotent.
    async fn delete_org_setting(&self, org_id: Uuid, key: &str) -> StoreResult<()>;

    // ---------------------------------------------------------------------
    // Organizations
    // ---------------------------------------------------------------------

    /// Create a new organization and auto-create the owner membership.
    async fn create_organization(
        &self,
        name: &str,
        slug: &str,
        owner_id: Uuid,
        plan: Plan,
    ) -> StoreResult<Organization>;

    /// Look up an organization by id.
    async fn find_organization_by_id(&self, org_id: Uuid) -> StoreResult<Option<Organization>>;

    /// Look up an organization by slug.
    async fn find_organization_by_slug(&self, slug: &str) -> StoreResult<Option<Organization>>;

    /// List all organizations a user belongs to (as owner or member).
    async fn list_organizations_by_user(&self, user_id: Uuid) -> StoreResult<Vec<Organization>>;

    /// Update an organization's display name.
    async fn update_organization_name(&self, org_id: Uuid, name: &str) -> StoreResult<()>;

    /// Update an organization's slug.
    async fn update_organization_slug(&self, org_id: Uuid, slug: &str) -> StoreResult<()>;

    /// Update an organization's plan (and refresh limits).
    async fn update_organization_plan(&self, org_id: Uuid, plan: Plan) -> StoreResult<()>;

    /// Check whether an organization has an active or trialing subscription.
    async fn organization_has_active_subscription(&self, org_id: Uuid) -> StoreResult<bool>;

    /// Permanently delete an organization (cascades to related rows).
    async fn delete_organization(&self, org_id: Uuid) -> StoreResult<()>;

    // ---------------------------------------------------------------------
    // Organization members
    // ---------------------------------------------------------------------

    /// Add a user to an organization with the given role.
    async fn create_org_member(
        &self,
        org_id: Uuid,
        user_id: Uuid,
        role: OrgRole,
    ) -> StoreResult<OrgMember>;

    /// Look up a specific (org, user) membership.
    async fn find_org_member(&self, org_id: Uuid, user_id: Uuid) -> StoreResult<Option<OrgMember>>;

    /// List every member of an organization, oldest first.
    async fn list_org_members(&self, org_id: Uuid) -> StoreResult<Vec<OrgMember>>;

    /// Update a member's role.
    async fn update_org_member_role(
        &self,
        org_id: Uuid,
        user_id: Uuid,
        role: OrgRole,
    ) -> StoreResult<()>;

    /// Remove a member from an organization.
    async fn delete_org_member(&self, org_id: Uuid, user_id: Uuid) -> StoreResult<()>;

    // ---------------------------------------------------------------------
    // Audit logs
    // ---------------------------------------------------------------------

    /// Best-effort audit event recording. Failures are logged and swallowed
    /// so they never block the caller's primary operation.
    #[allow(clippy::too_many_arguments)]
    async fn record_audit_event(
        &self,
        org_id: Uuid,
        user_id: Option<Uuid>,
        event_type: AuditEventType,
        description: String,
        metadata: Option<serde_json::Value>,
        ip_address: Option<&str>,
        user_agent: Option<&str>,
    );

    /// List audit logs for an organization with optional filters.
    ///
    /// `event_types` is an OR filter: pass an empty slice to return logs of every type.
    async fn list_audit_logs(
        &self,
        org_id: Uuid,
        limit: Option<i64>,
        offset: Option<i64>,
        event_types: &[AuditEventType],
    ) -> StoreResult<Vec<AuditLog>>;

    /// Count audit logs matching the filter (for pagination).
    async fn count_audit_logs(
        &self,
        org_id: Uuid,
        event_types: &[AuditEventType],
    ) -> StoreResult<i64>;

    // ---------------------------------------------------------------------
    // Feature usage
    // ---------------------------------------------------------------------

    /// Record a feature-usage event. Failures are logged and swallowed.
    async fn record_feature_usage(
        &self,
        org_id: Uuid,
        user_id: Option<Uuid>,
        feature: FeatureType,
        metadata: Option<serde_json::Value>,
    );

    /// Count how many times an org used a feature over the last `days` days.
    async fn count_feature_usage_by_org(
        &self,
        org_id: Uuid,
        feature: FeatureType,
        days: i64,
    ) -> StoreResult<i64>;

    // ---------------------------------------------------------------------
    // Suspicious activity — record_suspicious_activity is below after Users
    // ---------------------------------------------------------------------

    // ---------------------------------------------------------------------
    // Users
    // ---------------------------------------------------------------------

    /// Create a new user with an already-hashed password.
    async fn create_user(
        &self,
        username: &str,
        email: &str,
        password_hash: &str,
    ) -> StoreResult<User>;

    /// Look up a user by id.
    async fn find_user_by_id(&self, user_id: Uuid) -> StoreResult<Option<User>>;

    /// Look up a user by email.
    async fn find_user_by_email(&self, email: &str) -> StoreResult<Option<User>>;

    /// Look up a user by username.
    async fn find_user_by_username(&self, username: &str) -> StoreResult<Option<User>>;

    /// Batch lookup by id to avoid N+1 queries.
    async fn find_users_by_ids(&self, ids: &[Uuid]) -> StoreResult<Vec<User>>;

    /// Set the persistent API token on a user record.
    async fn set_user_api_token(&self, user_id: Uuid, token: &str) -> StoreResult<()>;

    /// Enable TOTP 2FA for a user with the given secret and hashed backup codes.
    async fn enable_user_2fa(
        &self,
        user_id: Uuid,
        secret: &str,
        backup_codes: &[String],
    ) -> StoreResult<()>;

    /// Disable 2FA and clear stored secret + backup codes.
    async fn disable_user_2fa(&self, user_id: Uuid) -> StoreResult<()>;

    /// Refresh the 2FA verified timestamp (e.g. after a successful TOTP challenge).
    async fn update_user_2fa_verified(&self, user_id: Uuid) -> StoreResult<()>;

    /// Remove a consumed backup code by index.
    async fn remove_user_backup_code(&self, user_id: Uuid, code_index: usize) -> StoreResult<()>;

    /// Look up a user by their GitHub account id.
    async fn find_user_by_github_id(&self, github_id: &str) -> StoreResult<Option<User>>;

    /// Look up a user by their Google account id.
    async fn find_user_by_google_id(&self, google_id: &str) -> StoreResult<Option<User>>;

    /// Link an existing user to a GitHub account (sets github_id, auth_provider, avatar_url).
    async fn link_user_github_account(
        &self,
        user_id: Uuid,
        github_id: &str,
        avatar_url: Option<&str>,
    ) -> StoreResult<()>;

    /// Link an existing user to a Google account (sets google_id, auth_provider, avatar_url).
    async fn link_user_google_account(
        &self,
        user_id: Uuid,
        google_id: &str,
        avatar_url: Option<&str>,
    ) -> StoreResult<()>;

    /// Create a new verified user from an OAuth provider (random password hash).
    #[allow(clippy::too_many_arguments)]
    async fn create_oauth_user(
        &self,
        username: &str,
        email: &str,
        password_hash: &str,
        auth_provider: &str,
        github_id: Option<&str>,
        google_id: Option<&str>,
        avatar_url: Option<&str>,
    ) -> StoreResult<User>;

    /// Fetch or create a user's personal/default organization.
    async fn get_or_create_personal_org(
        &self,
        user_id: Uuid,
        username: &str,
    ) -> StoreResult<Organization>;

    /// Replace a user's password hash (no-op on verification).
    async fn update_user_password_hash(
        &self,
        user_id: Uuid,
        password_hash: &str,
    ) -> StoreResult<()>;

    /// Mark a user's email as verified.
    async fn mark_user_verified(&self, user_id: Uuid) -> StoreResult<()>;

    /// Update a user's username and/or email. Pass `None` to leave a field unchanged.
    /// Returns the updated user.
    async fn update_user_profile(
        &self,
        user_id: Uuid,
        username: Option<&str>,
        email: Option<&str>,
    ) -> StoreResult<User>;

    /// Update a user's notification-preference flags.
    async fn update_user_notification_prefs(
        &self,
        user_id: Uuid,
        email_notifications: bool,
        security_alerts: bool,
    ) -> StoreResult<()>;

    /// Replace a user's UI preferences JSON blob.
    async fn update_user_preferences(
        &self,
        user_id: Uuid,
        preferences: &serde_json::Value,
    ) -> StoreResult<()>;

    // ---------------------------------------------------------------------
    // Verification / password-reset tokens
    // ---------------------------------------------------------------------

    /// Create a new verification token for a user (24h default expiry).
    async fn create_verification_token(&self, user_id: Uuid) -> StoreResult<VerificationToken>;

    /// Shorten a verification token's expiry to `hours` from now.
    /// Used by password-reset to override the default 24h window.
    async fn set_verification_token_expiry_hours(
        &self,
        token_id: Uuid,
        hours: i64,
    ) -> StoreResult<()>;

    /// Look up a verification token by its plaintext token string.
    async fn find_verification_token_by_token(
        &self,
        token: &str,
    ) -> StoreResult<Option<VerificationToken>>;

    /// Mark a verification token as consumed.
    async fn mark_verification_token_used(&self, token_id: Uuid) -> StoreResult<()>;

    #[allow(clippy::too_many_arguments)]
    async fn record_suspicious_activity(
        &self,
        org_id: Option<Uuid>,
        user_id: Option<Uuid>,
        activity_type: SuspiciousActivityType,
        severity: &str,
        description: String,
        metadata: Option<serde_json::Value>,
        ip_address: Option<&str>,
        user_agent: Option<&str>,
    );

    // ---------------------------------------------------------------------
    // Federations
    // ---------------------------------------------------------------------

    async fn create_federation(
        &self,
        org_id: Uuid,
        created_by: Uuid,
        name: &str,
        description: &str,
        services: &serde_json::Value,
    ) -> StoreResult<Federation>;

    async fn find_federation_by_id(&self, id: Uuid) -> StoreResult<Option<Federation>>;

    async fn list_federations_by_org(&self, org_id: Uuid) -> StoreResult<Vec<Federation>>;

    async fn update_federation(
        &self,
        id: Uuid,
        name: Option<&str>,
        description: Option<&str>,
        services: Option<&serde_json::Value>,
    ) -> StoreResult<Option<Federation>>;

    async fn delete_federation(&self, id: Uuid) -> StoreResult<()>;

    // ---------------------------------------------------------------------
    // Federation scenario activations
    // ---------------------------------------------------------------------

    /// Record a new active scenario for a federation. The caller is
    /// responsible for ensuring no other scenario is currently active — the
    /// database enforces this invariant with a partial unique index.
    #[allow(clippy::too_many_arguments)]
    async fn create_federation_scenario_activation(
        &self,
        federation_id: Uuid,
        scenario_id: Option<Uuid>,
        scenario_name: &str,
        manifest_snapshot: &serde_json::Value,
        service_overrides: &serde_json::Value,
        per_service_state: &serde_json::Value,
        activated_by: Uuid,
    ) -> StoreResult<FederationScenarioActivation>;

    /// Return the currently active scenario activation for a federation, if
    /// any. Deactivated/failed rows are excluded.
    async fn find_active_federation_scenario_activation(
        &self,
        federation_id: Uuid,
    ) -> StoreResult<Option<FederationScenarioActivation>>;

    /// Mark an activation as deactivated, setting `deactivated_at = now()`.
    async fn deactivate_federation_scenario_activation(
        &self,
        id: Uuid,
    ) -> StoreResult<Option<FederationScenarioActivation>>;

    /// Overwrite the `per_service_state` JSON for an activation — the runtime
    /// uses this to flip services from `pending` → `applied` as they observe
    /// the overrides.
    async fn update_federation_scenario_per_service_state(
        &self,
        id: Uuid,
        per_service_state: &serde_json::Value,
    ) -> StoreResult<Option<FederationScenarioActivation>>;

    /// Active activations whose federation's services list the given
    /// workspace. This backs the runtime-side polling endpoint.
    async fn find_active_federation_scenarios_for_workspace(
        &self,
        workspace_id: Uuid,
    ) -> StoreResult<Vec<FederationScenarioActivation>>;

    /// List unresolved suspicious activities with optional filters.
    async fn list_unresolved_suspicious_activities(
        &self,
        org_id: Option<Uuid>,
        user_id: Option<Uuid>,
        severity: Option<&str>,
        limit: Option<i64>,
    ) -> StoreResult<Vec<SuspiciousActivity>>;

    /// Count unresolved suspicious activities for an org.
    async fn count_unresolved_suspicious_activities(&self, org_id: Uuid) -> StoreResult<i64>;

    /// Mark a suspicious activity as resolved by the given user, scoped to
    /// `org_id`. The org constraint is enforced inside the UPDATE so callers
    /// from one organization cannot resolve activities belonging to another.
    /// Returns [`StoreError::NotFound`] when no matching activity exists.
    async fn resolve_suspicious_activity(
        &self,
        org_id: Uuid,
        activity_id: Uuid,
        resolved_by: Uuid,
    ) -> StoreResult<()>;

    // ---------------------------------------------------------------------
    // Cloud workspaces
    // ---------------------------------------------------------------------

    async fn create_cloud_workspace(
        &self,
        org_id: Uuid,
        created_by: Uuid,
        name: &str,
        description: &str,
    ) -> StoreResult<CloudWorkspace>;

    async fn find_cloud_workspace_by_id(&self, id: Uuid) -> StoreResult<Option<CloudWorkspace>>;

    async fn list_cloud_workspaces_by_org(&self, org_id: Uuid) -> StoreResult<Vec<CloudWorkspace>>;

    async fn update_cloud_workspace(
        &self,
        id: Uuid,
        name: Option<&str>,
        description: Option<&str>,
        is_active: Option<bool>,
        settings: Option<&serde_json::Value>,
    ) -> StoreResult<Option<CloudWorkspace>>;

    async fn delete_cloud_workspace(&self, id: Uuid) -> StoreResult<()>;

    // ---------------------------------------------------------------------
    // Cloud services
    // ---------------------------------------------------------------------

    #[allow(clippy::too_many_arguments)]
    async fn create_cloud_service(
        &self,
        org_id: Uuid,
        workspace_id: Option<Uuid>,
        created_by: Uuid,
        name: &str,
        description: &str,
        base_url: &str,
    ) -> StoreResult<CloudService>;

    async fn find_cloud_service_by_id(&self, id: Uuid) -> StoreResult<Option<CloudService>>;

    async fn list_cloud_services_by_org(&self, org_id: Uuid) -> StoreResult<Vec<CloudService>>;

    async fn list_cloud_services_by_workspace(
        &self,
        org_id: Uuid,
        workspace_id: Uuid,
    ) -> StoreResult<Vec<CloudService>>;

    /// Update a cloud service. The `workspace_id` parameter is tri-state:
    /// `None` = leave the column untouched, `Some(None)` = unassign (write
    /// SQL NULL), `Some(Some(id))` = assign to workspace `id`.
    #[allow(clippy::too_many_arguments)]
    async fn update_cloud_service(
        &self,
        id: Uuid,
        name: Option<&str>,
        description: Option<&str>,
        base_url: Option<&str>,
        enabled: Option<bool>,
        tags: Option<&serde_json::Value>,
        routes: Option<&serde_json::Value>,
        workspace_id: Option<Option<Uuid>>,
    ) -> StoreResult<Option<CloudService>>;

    async fn delete_cloud_service(&self, id: Uuid) -> StoreResult<()>;

    // ---------------------------------------------------------------------
    // Cloud fixtures
    // ---------------------------------------------------------------------

    #[allow(clippy::too_many_arguments)]
    async fn create_cloud_fixture(
        &self,
        org_id: Uuid,
        created_by: Uuid,
        name: &str,
        description: &str,
        path: &str,
        method: &str,
        content: Option<&serde_json::Value>,
        protocol: Option<&str>,
        tags: Option<&serde_json::Value>,
        workspace_id: Option<Uuid>,
        route_path: Option<&str>,
    ) -> StoreResult<CloudFixture>;

    async fn find_cloud_fixture_by_id(&self, id: Uuid) -> StoreResult<Option<CloudFixture>>;

    /// List fixtures in an organization. When `workspace_id` is `Some`, only
    /// fixtures assigned to that workspace are returned; when `None`, every
    /// fixture in the org is returned.
    async fn list_cloud_fixtures_by_org(
        &self,
        org_id: Uuid,
        workspace_id: Option<Uuid>,
    ) -> StoreResult<Vec<CloudFixture>>;

    /// Update mutable fields on a cloud fixture. `workspace_id` is tri-state:
    /// `None` leaves it untouched; `Some(None)` clears it; `Some(Some(id))`
    /// assigns/changes it.
    #[allow(clippy::too_many_arguments)]
    async fn update_cloud_fixture(
        &self,
        id: Uuid,
        name: Option<&str>,
        description: Option<&str>,
        path: Option<&str>,
        method: Option<&str>,
        content: Option<&serde_json::Value>,
        protocol: Option<&str>,
        tags: Option<&serde_json::Value>,
        route_path: Option<&str>,
        workspace_id: Option<Option<Uuid>>,
    ) -> StoreResult<Option<CloudFixture>>;

    async fn delete_cloud_fixture(&self, id: Uuid) -> StoreResult<()>;

    /// Delete many fixtures atomically, scoped to an org. Returns the IDs
    /// that were actually deleted.
    async fn delete_cloud_fixtures_bulk(
        &self,
        org_id: Uuid,
        ids: &[Uuid],
    ) -> StoreResult<Vec<Uuid>>;

    // ---------------------------------------------------------------------
    // Hosted mocks (deployments)
    // ---------------------------------------------------------------------

    #[allow(clippy::too_many_arguments)]
    async fn create_hosted_mock(
        &self,
        org_id: Uuid,
        project_id: Option<Uuid>,
        name: &str,
        slug: &str,
        description: Option<&str>,
        config_json: serde_json::Value,
        openapi_spec_url: Option<&str>,
        region: Option<&str>,
    ) -> StoreResult<HostedMock>;

    async fn find_hosted_mock_by_id(&self, id: Uuid) -> StoreResult<Option<HostedMock>>;

    async fn find_hosted_mock_by_slug(
        &self,
        org_id: Uuid,
        slug: &str,
    ) -> StoreResult<Option<HostedMock>>;

    async fn list_hosted_mocks_by_org(&self, org_id: Uuid) -> StoreResult<Vec<HostedMock>>;

    async fn update_hosted_mock_status(
        &self,
        id: Uuid,
        status: DeploymentStatus,
        error_message: Option<&str>,
    ) -> StoreResult<()>;

    async fn update_hosted_mock_urls(
        &self,
        id: Uuid,
        deployment_url: Option<&str>,
        internal_url: Option<&str>,
    ) -> StoreResult<()>;

    async fn update_hosted_mock_health(
        &self,
        id: Uuid,
        health_status: HealthStatus,
        health_check_url: Option<&str>,
    ) -> StoreResult<()>;

    async fn delete_hosted_mock(&self, id: Uuid) -> StoreResult<()>;

    // ---------------------------------------------------------------------
    // Waitlist
    // ---------------------------------------------------------------------

    async fn subscribe_waitlist(
        &self,
        email: &str,
        source: &str,
    ) -> StoreResult<WaitlistSubscriber>;

    async fn unsubscribe_waitlist_by_token(&self, token: Uuid) -> StoreResult<bool>;

    // ---------------------------------------------------------------------
    // Usage counters
    // ---------------------------------------------------------------------

    async fn get_or_create_current_usage_counter(&self, org_id: Uuid) -> StoreResult<UsageCounter>;

    async fn list_usage_counters_by_org(&self, org_id: Uuid) -> StoreResult<Vec<UsageCounter>>;

    // ---------------------------------------------------------------------
    // SSO configuration
    // ---------------------------------------------------------------------

    async fn find_sso_config_by_org(&self, org_id: Uuid) -> StoreResult<Option<SSOConfiguration>>;

    #[allow(clippy::too_many_arguments)]
    async fn upsert_sso_config(
        &self,
        org_id: Uuid,
        provider: SSOProvider,
        saml_entity_id: Option<&str>,
        saml_sso_url: Option<&str>,
        saml_slo_url: Option<&str>,
        saml_x509_cert: Option<&str>,
        saml_name_id_format: Option<&str>,
        attribute_mapping: Option<serde_json::Value>,
        require_signed_assertions: bool,
        require_signed_responses: bool,
        allow_unsolicited_responses: bool,
    ) -> StoreResult<SSOConfiguration>;

    async fn enable_sso_config(&self, org_id: Uuid) -> StoreResult<()>;
    async fn disable_sso_config(&self, org_id: Uuid) -> StoreResult<()>;
    async fn delete_sso_config(&self, org_id: Uuid) -> StoreResult<()>;

    // ---------------------------------------------------------------------
    // SAML replay prevention
    // ---------------------------------------------------------------------

    async fn is_saml_assertion_used(&self, assertion_id: &str, org_id: Uuid) -> StoreResult<bool>;

    #[allow(clippy::too_many_arguments)]
    async fn record_saml_assertion_used(
        &self,
        assertion_id: &str,
        org_id: Uuid,
        user_id: Option<Uuid>,
        name_id: Option<&str>,
        issued_at: DateTime<Utc>,
        expires_at: DateTime<Utc>,
    ) -> StoreResult<SAMLAssertionId>;

    // ---------------------------------------------------------------------
    // Organization templates
    // ---------------------------------------------------------------------

    #[allow(clippy::too_many_arguments)]
    async fn create_org_template(
        &self,
        org_id: Uuid,
        name: &str,
        description: Option<&str>,
        blueprint_config: Option<serde_json::Value>,
        security_baseline: Option<serde_json::Value>,
        created_by: Uuid,
        is_default: bool,
    ) -> StoreResult<OrgTemplate>;

    async fn find_org_template_by_id(&self, id: Uuid) -> StoreResult<Option<OrgTemplate>>;

    async fn list_org_templates_by_org(&self, org_id: Uuid) -> StoreResult<Vec<OrgTemplate>>;

    async fn update_org_template(
        &self,
        template: &OrgTemplate,
        name: Option<&str>,
        description: Option<&str>,
        blueprint_config: Option<serde_json::Value>,
        security_baseline: Option<serde_json::Value>,
        is_default: Option<bool>,
    ) -> StoreResult<OrgTemplate>;

    async fn delete_org_template(&self, id: Uuid) -> StoreResult<()>;

    // ---------------------------------------------------------------------
    // Marketplace templates
    // ---------------------------------------------------------------------

    #[allow(clippy::too_many_arguments)]
    async fn create_template(
        &self,
        org_id: Option<Uuid>,
        name: &str,
        slug: &str,
        description: &str,
        author_id: Uuid,
        version: &str,
        category: TemplateCategory,
        content_json: serde_json::Value,
    ) -> StoreResult<Template>;

    async fn find_template_by_name_version(
        &self,
        name: &str,
        version: &str,
    ) -> StoreResult<Option<Template>>;

    async fn list_templates_by_org(&self, org_id: Uuid) -> StoreResult<Vec<Template>>;

    async fn search_templates(
        &self,
        query: Option<&str>,
        category: Option<&str>,
        tags: &[String],
        org_id: Option<Uuid>,
        limit: i64,
        offset: i64,
    ) -> StoreResult<Vec<Template>>;

    async fn count_search_templates(
        &self,
        query: Option<&str>,
        category: Option<&str>,
        tags: &[String],
        org_id: Option<Uuid>,
    ) -> StoreResult<i64>;

    // ---------------------------------------------------------------------
    // Marketplace scenarios
    // ---------------------------------------------------------------------

    #[allow(clippy::too_many_arguments)]
    async fn create_scenario(
        &self,
        org_id: Option<Uuid>,
        name: &str,
        slug: &str,
        description: &str,
        author_id: Uuid,
        current_version: &str,
        category: &str,
        license: &str,
        manifest_json: serde_json::Value,
    ) -> StoreResult<Scenario>;

    async fn find_scenario_by_name(&self, name: &str) -> StoreResult<Option<Scenario>>;

    /// Look up a scenario by UUID, regardless of org scoping. Callers that
    /// care about org access control must check `scenario.org_id` themselves.
    async fn find_scenario_by_id(&self, id: Uuid) -> StoreResult<Option<Scenario>>;

    async fn list_scenarios_by_org(&self, org_id: Uuid) -> StoreResult<Vec<Scenario>>;

    #[allow(clippy::too_many_arguments)]
    async fn search_scenarios(
        &self,
        query: Option<&str>,
        category: Option<&str>,
        tags: &[String],
        org_id: Option<Uuid>,
        sort: &str,
        limit: i64,
        offset: i64,
    ) -> StoreResult<Vec<Scenario>>;

    async fn count_search_scenarios(
        &self,
        query: Option<&str>,
        category: Option<&str>,
        tags: &[String],
        org_id: Option<Uuid>,
    ) -> StoreResult<i64>;

    // ---------------------------------------------------------------------
    // Marketplace plugins
    // ---------------------------------------------------------------------

    #[allow(clippy::too_many_arguments)]
    async fn search_plugins(
        &self,
        query: Option<&str>,
        category: Option<&str>,
        language: Option<&str>,
        tags: &[String],
        sort_by: &str,
        limit: i64,
        offset: i64,
    ) -> StoreResult<Vec<Plugin>>;

    async fn count_search_plugins(
        &self,
        query: Option<&str>,
        category: Option<&str>,
        language: Option<&str>,
        tags: &[String],
    ) -> StoreResult<i64>;

    async fn find_plugin_by_name(&self, name: &str) -> StoreResult<Option<Plugin>>;

    async fn get_plugin_tags(&self, plugin_id: Uuid) -> StoreResult<Vec<String>>;

    #[allow(clippy::too_many_arguments)]
    async fn create_plugin(
        &self,
        name: &str,
        description: &str,
        version: &str,
        category: &str,
        license: &str,
        repository: Option<&str>,
        homepage: Option<&str>,
        author_id: Uuid,
        language: &str,
    ) -> StoreResult<Plugin>;

    async fn list_plugin_versions(&self, plugin_id: Uuid) -> StoreResult<Vec<PluginVersion>>;

    async fn find_plugin_version(
        &self,
        plugin_id: Uuid,
        version: &str,
    ) -> StoreResult<Option<PluginVersion>>;

    #[allow(clippy::too_many_arguments)]
    async fn create_plugin_version(
        &self,
        plugin_id: Uuid,
        version: &str,
        download_url: &str,
        checksum: &str,
        file_size: i64,
        min_mockforge_version: Option<&str>,
        sbom_json: Option<&serde_json::Value>,
    ) -> StoreResult<PluginVersion>;

    /// Fetch the SBOM (CycloneDX JSON or similar) stored alongside a
    /// plugin version at publish time. Returns `None` when the version
    /// predates SBOM support or the publisher didn't include one.
    async fn get_plugin_version_sbom(
        &self,
        plugin_version_id: Uuid,
    ) -> StoreResult<Option<serde_json::Value>>;

    async fn yank_plugin_version(&self, version_id: Uuid) -> StoreResult<()>;

    async fn get_plugin_version_dependencies(
        &self,
        version_id: Uuid,
    ) -> StoreResult<std::collections::HashMap<String, String>>;

    async fn add_plugin_version_dependency(
        &self,
        version_id: Uuid,
        plugin_name: &str,
        version_req: &str,
    ) -> StoreResult<()>;

    // --- Plugin security scans ---

    /// Upsert a security scan result for a specific plugin version. Each
    /// version has at most one row (latest scan wins).
    async fn upsert_plugin_security_scan(
        &self,
        plugin_version_id: Uuid,
        status: &str,
        score: i16,
        findings: &serde_json::Value,
        scanner_version: Option<&str>,
    ) -> StoreResult<()>;

    /// Fetch the latest security scan for a plugin's current version.
    async fn latest_security_scan_for_plugin(
        &self,
        plugin_id: Uuid,
    ) -> StoreResult<Option<PluginSecurityScan>>;

    /// List up to `limit` pending security scans with enough context
    /// (plugin name + version + declared file size) for a worker to
    /// re-download the artifact and run checks.
    async fn list_pending_security_scans(&self, limit: i64) -> StoreResult<Vec<PendingScanJob>>;

    // --- OSV vulnerability cache ---

    /// Find cached OSV advisories that affect this specific
    /// `(ecosystem, name, version)`. Returns an empty vec when the cache
    /// has no hit — the caller should treat that as "clean", not "error".
    async fn find_osv_matches(
        &self,
        ecosystem: &str,
        package_name: &str,
        version: &str,
    ) -> StoreResult<Vec<OsvMatch>>;

    /// Import a single OSV advisory record. Idempotent — the
    /// `(advisory_id, ecosystem, package_name)` uniqueness constraint
    /// means repeat imports just refresh `modified_at` and
    /// `affected_versions`. Returns the number of `(ecosystem, package)`
    /// rows the import landed into (an advisory can cover several
    /// packages; each lands as its own row).
    async fn upsert_osv_advisory(&self, record: &OsvImportRecord) -> StoreResult<usize>;

    /// Count rows in the OSV cache. Used by the scanner to decide whether
    /// to fall back to the hardcoded seed list — an empty cache means the
    /// sync worker hasn't run yet, so a fresh install should still surface
    /// findings against a built-in baseline rather than returning silence.
    async fn count_osv_advisories(&self) -> StoreResult<i64>;

    // --- Publisher attestation keys ---

    /// List every non-revoked public key registered against a user. The
    /// attestation verifier tries each one at publish time.
    async fn list_user_public_keys(&self, user_id: Uuid) -> StoreResult<Vec<UserPublicKey>>;

    /// List a user's keys for the user-facing settings page. Unlike
    /// [`Self::list_user_public_keys`] (which is the verifier hot path
    /// and only ever returns active keys), this variant:
    ///
    /// * optionally includes revoked keys when `include_revoked` is true,
    ///   so the UI can render a revocation history;
    /// * joins `plugin_versions.sbom_signed_key_id` so each row carries
    ///   the count of versions it has verified — a single round-trip
    ///   instead of N+1 per-key counts.
    async fn list_user_public_keys_with_usage(
        &self,
        user_id: Uuid,
        include_revoked: bool,
    ) -> StoreResult<Vec<UserPublicKeyWithUsage>>;

    /// Register a new public key on a user's account. The caller has
    /// already validated that `public_key_b64` decodes to the right
    /// length for the algorithm. `org_id` is `Some` when the key is
    /// scoped to an organization (caller must be an Owner/Admin of that
    /// org — handler-level check). Returns the persisted record.
    async fn create_user_public_key(
        &self,
        user_id: Uuid,
        algorithm: &str,
        public_key_b64: &str,
        label: &str,
        org_id: Option<Uuid>,
    ) -> StoreResult<UserPublicKey>;

    /// Soft-revoke a key (sets `revoked_at`). Revoked keys are skipped
    /// by the attestation verifier but stay in the table so historical
    /// signatures stay traceable. Returns `true` if a row was actually
    /// updated, so the handler can distinguish "not yours" (or "already
    /// revoked") from "done."
    async fn revoke_user_public_key(&self, user_id: Uuid, key_id: Uuid) -> StoreResult<bool>;

    /// Soft-revoke a key on behalf of an org Owner/Admin. The handler is
    /// responsible for verifying the caller's role; this method only
    /// guards on the key actually being scoped to the supplied org.
    /// Returns `true` if a row updated.
    async fn revoke_org_public_key(&self, org_id: Uuid, key_id: Uuid) -> StoreResult<bool>;

    /// Look up a single key by id, regardless of owner. Used by the
    /// revoke handler to resolve an org-scoped revoke when the caller
    /// isn't the key's owner but is an org admin.
    async fn find_user_public_key_by_id(&self, key_id: Uuid) -> StoreResult<Option<UserPublicKey>>;

    /// List keys tagged with `org_id`. Mirrors
    /// [`Self::list_user_public_keys_with_usage`] for the org listing
    /// endpoint — joins usage and respects `include_revoked`.
    async fn list_org_public_keys_with_usage(
        &self,
        org_id: Uuid,
        include_revoked: bool,
    ) -> StoreResult<Vec<UserPublicKeyWithUsage>>;

    /// Pull every active key the verifier should try when `author_id`
    /// publishes — i.e. the author's own keys plus any keys tagged with
    /// orgs the author is a member of. Single round-trip.
    async fn list_keys_for_publisher(&self, author_id: Uuid) -> StoreResult<Vec<UserPublicKey>>;

    /// Atomic publisher-key rotation: register a new key with the same
    /// org tag as `old_key_id` and revoke the old one in one
    /// transaction. Returns the new key. Callers (handler) audit-log
    /// the rotation as a single event.
    async fn rotate_user_public_key(
        &self,
        user_id: Uuid,
        old_key_id: Uuid,
        algorithm: &str,
        new_public_key_b64: &str,
        new_label: &str,
    ) -> StoreResult<UserPublicKey>;

    /// Record a verified SBOM attestation against a plugin version. No-op
    /// when `key_id` is `None` (publisher didn't submit a signature).
    async fn record_plugin_version_attestation(
        &self,
        plugin_version_id: Uuid,
        key_id: Option<Uuid>,
    ) -> StoreResult<()>;

    /// Fetch a stored attestation pointer for the scanner worker. Returns
    /// the verifying key id + signed-at timestamp, or `None` when the
    /// version wasn't signed.
    async fn get_plugin_version_attestation(
        &self,
        plugin_version_id: Uuid,
    ) -> StoreResult<Option<(Uuid, DateTime<Utc>)>>;

    // --- Plugin reviews ---

    async fn get_plugin_reviews(
        &self,
        plugin_id: Uuid,
        limit: i64,
        offset: i64,
    ) -> StoreResult<Vec<Review>>;

    async fn count_plugin_reviews(&self, plugin_id: Uuid) -> StoreResult<i64>;

    async fn create_plugin_review(
        &self,
        plugin_id: Uuid,
        user_id: Uuid,
        version: &str,
        rating: i16,
        title: Option<&str>,
        comment: &str,
    ) -> StoreResult<Review>;

    /// Returns (average_rating, total_reviews) for a plugin.
    async fn get_plugin_review_stats(&self, plugin_id: Uuid) -> StoreResult<(f64, i64)>;

    /// Returns map of rating -> count for a plugin.
    async fn get_plugin_review_distribution(
        &self,
        plugin_id: Uuid,
    ) -> StoreResult<std::collections::HashMap<i16, i64>>;

    async fn find_existing_plugin_review(
        &self,
        plugin_id: Uuid,
        user_id: Uuid,
    ) -> StoreResult<Option<Uuid>>;

    async fn update_plugin_rating_stats(
        &self,
        plugin_id: Uuid,
        avg: f64,
        count: i32,
    ) -> StoreResult<()>;

    async fn increment_plugin_review_vote(
        &self,
        plugin_id: Uuid,
        review_id: Uuid,
        helpful: bool,
    ) -> StoreResult<()>;

    /// Increment both `plugins.downloads_total` and
    /// `plugin_versions.downloads`. Called by the tracked-download endpoint
    /// before redirecting to the artifact URL.
    async fn increment_plugin_download(
        &self,
        plugin_id: Uuid,
        plugin_version_id: Uuid,
    ) -> StoreResult<()>;

    /// Set / clear the takedown columns. `reason` is preserved on the row
    /// so admins can audit moderation history.
    async fn take_down_plugin(&self, plugin_id: Uuid, reason: Option<&str>) -> StoreResult<()>;

    async fn restore_plugin(&self, plugin_id: Uuid) -> StoreResult<()>;

    /// All currently taken-down plugins, newest takedown first. Powers
    /// the admin moderation page; the public search excludes these so
    /// this is the only programmatic way to enumerate them.
    async fn list_taken_down_plugins(&self) -> StoreResult<Vec<Plugin>>;

    /// Look up a single review by id, scoped to the plugin path param so the
    /// author-response endpoint can return 404 (not 403) when the path/body
    /// disagree.
    async fn find_review_in_plugin(
        &self,
        plugin_id: Uuid,
        review_id: Uuid,
    ) -> StoreResult<Option<Review>>;

    /// Set or clear the author response on a review. None clears both
    /// columns so authors can retract a response.
    async fn set_review_author_response(
        &self,
        review_id: Uuid,
        text: Option<&str>,
    ) -> StoreResult<()>;

    /// Lookup (id, username) for a user.
    async fn get_user_public_info(&self, user_id: Uuid) -> StoreResult<Option<(String, String)>>;

    // --- Template reviews ---

    async fn get_template_reviews(
        &self,
        template_id: Uuid,
        limit: i64,
        offset: i64,
    ) -> StoreResult<Vec<TemplateReview>>;

    async fn count_template_reviews(&self, template_id: Uuid) -> StoreResult<i64>;

    async fn create_template_review(
        &self,
        template_id: Uuid,
        reviewer_id: Uuid,
        rating: i32,
        title: Option<&str>,
        comment: &str,
    ) -> StoreResult<TemplateReview>;

    async fn update_template_review_stats(&self, template_id: Uuid) -> StoreResult<()>;

    async fn find_existing_template_review(
        &self,
        template_id: Uuid,
        reviewer_id: Uuid,
    ) -> StoreResult<Option<Uuid>>;

    // --- Template stars ---

    /// Toggle a template star for a user.
    /// Returns `(now_starred, new_count)`.
    async fn toggle_template_star(
        &self,
        template_id: Uuid,
        user_id: Uuid,
    ) -> StoreResult<(bool, i64)>;

    /// Whether `user_id` has starred `template_id`.
    async fn is_template_starred_by(&self, template_id: Uuid, user_id: Uuid) -> StoreResult<bool>;

    /// Live star count for a single template.
    async fn count_template_stars(&self, template_id: Uuid) -> StoreResult<i64>;

    /// Batch-fetch star counts for many templates in a single query.
    /// Templates with zero stars are absent from the map — callers default to 0.
    async fn count_template_stars_batch(
        &self,
        template_ids: &[Uuid],
    ) -> StoreResult<std::collections::HashMap<Uuid, i64>>;

    // --- Scenario reviews ---

    async fn get_scenario_reviews(
        &self,
        scenario_id: Uuid,
        limit: i64,
        offset: i64,
    ) -> StoreResult<Vec<ScenarioReview>>;

    async fn count_scenario_reviews(&self, scenario_id: Uuid) -> StoreResult<i64>;

    async fn create_scenario_review(
        &self,
        scenario_id: Uuid,
        reviewer_id: Uuid,
        rating: i32,
        title: Option<&str>,
        comment: &str,
    ) -> StoreResult<ScenarioReview>;

    async fn update_scenario_review_stats(&self, scenario_id: Uuid) -> StoreResult<()>;

    async fn find_existing_scenario_review(
        &self,
        scenario_id: Uuid,
        reviewer_id: Uuid,
    ) -> StoreResult<Option<Uuid>>;

    /// Fire-and-forget increment of a review's helpful counter. Mirrors the
    /// plugin equivalent — no per-user vote tracking; the UI is responsible
    /// for not letting a user click "helpful" twice in a row.
    async fn increment_scenario_review_helpful_count(
        &self,
        scenario_id: Uuid,
        review_id: Uuid,
    ) -> StoreResult<()>;

    // --- Scenario stars ---

    /// Toggle a scenario star for a user.
    /// Returns `(now_starred, new_count)`.
    async fn toggle_scenario_star(
        &self,
        scenario_id: Uuid,
        user_id: Uuid,
    ) -> StoreResult<(bool, i64)>;

    /// Whether `user_id` has starred `scenario_id`.
    async fn is_scenario_starred_by(&self, scenario_id: Uuid, user_id: Uuid) -> StoreResult<bool>;

    /// Live star count for a single scenario.
    async fn count_scenario_stars(&self, scenario_id: Uuid) -> StoreResult<i64>;

    /// Batch-fetch star counts for many scenarios in a single query.
    /// Scenarios with zero stars are absent from the map — callers default to 0.
    async fn count_scenario_stars_batch(
        &self,
        scenario_ids: &[Uuid],
    ) -> StoreResult<std::collections::HashMap<Uuid, i64>>;

    // --- Scenario version yank ---

    /// Mark a scenario version as yanked. The version row stays in the DB so
    /// download URLs continue to resolve for users who pinned to it, but
    /// search/list paths filter out yanked versions.
    async fn yank_scenario_version(&self, version_id: Uuid) -> StoreResult<()>;

    // --- Admin analytics snapshots ---

    /// Fetch a single aggregated snapshot covering every metric surfaced by
    /// the admin analytics dashboard. Encapsulates ~40 raw SQL queries so
    /// handlers stay thin and SQLite implementations can specialize.
    async fn get_admin_analytics_snapshot(&self) -> StoreResult<AdminAnalyticsSnapshot>;

    /// Fetch conversion funnel counts for the given textual Postgres interval
    /// (e.g. "7 days", "30 days"). SQLite implementations may parse this.
    async fn get_conversion_funnel_snapshot(
        &self,
        interval: &str,
    ) -> StoreResult<ConversionFunnelSnapshot>;

    // --- GDPR data export and deletion ---

    async fn list_user_settings_raw(&self, user_id: Uuid) -> StoreResult<Vec<UserSettingRow>>;

    async fn list_user_api_tokens(&self, user_id: Uuid) -> StoreResult<Vec<ApiToken>>;

    async fn get_org_membership_role(
        &self,
        org_id: Uuid,
        user_id: Uuid,
    ) -> StoreResult<Option<String>>;

    async fn list_org_settings_raw(&self, org_id: Uuid) -> StoreResult<Vec<OrgSettingRow>>;

    async fn list_org_projects_raw(&self, org_id: Uuid) -> StoreResult<Vec<ProjectRow>>;

    async fn list_org_subscriptions_raw(&self, org_id: Uuid) -> StoreResult<Vec<SubscriptionRow>>;

    async fn list_org_hosted_mocks_raw(&self, org_id: Uuid) -> StoreResult<Vec<HostedMock>>;

    /// Transactionally erase a user's personal data (GDPR right to erasure),
    /// transferring solo-owned orgs with other members to the next admin and
    /// cascade-deleting orgs with no remaining members. Returns the number of
    /// owned organizations affected (for audit logging).
    async fn delete_user_data_cascade(&self, user_id: Uuid) -> StoreResult<usize>;
}

/// Aggregated admin analytics data, corresponding to a single dashboard load.
#[derive(Debug, Clone)]
pub struct AdminAnalyticsSnapshot {
    pub total_users: i64,
    pub verified_users: i64,
    pub auth_providers: Vec<(Option<String>, i64)>,
    pub new_users_7d: i64,
    pub new_users_30d: i64,
    pub total_orgs: i64,
    pub plan_distribution: Vec<(String, i64)>,
    pub active_subs: i64,
    pub trial_orgs: i64,
    pub total_requests: Option<i64>,
    pub total_storage: Option<i64>,
    pub total_ai_tokens: Option<i64>,
    pub top_orgs: Vec<(Uuid, String, String, i64, i64)>,
    pub hosted_mocks_count: i64,
    pub hosted_mocks_orgs: i64,
    pub hosted_mocks_30d: i64,
    pub plugins_count: i64,
    pub plugins_orgs: i64,
    pub plugins_30d: i64,
    pub templates_count: i64,
    pub templates_orgs: i64,
    pub templates_30d: i64,
    pub scenarios_count: i64,
    pub scenarios_orgs: i64,
    pub scenarios_30d: i64,
    pub api_tokens_count: i64,
    pub api_tokens_orgs: i64,
    pub api_tokens_30d: i64,
    pub user_growth_30d: Vec<(chrono::NaiveDate, i64)>,
    pub org_growth_30d: Vec<(chrono::NaiveDate, i64)>,
    pub logins_24h: i64,
    pub logins_7d: i64,
    pub api_requests_24h: i64,
    pub api_requests_7d: i64,
}

/// Aggregated conversion funnel counts for admin dashboards.
#[derive(Debug, Clone)]
pub struct ConversionFunnelSnapshot {
    pub signups: i64,
    pub verified: i64,
    pub logged_in: i64,
    pub org_created: i64,
    pub feature_users: i64,
    pub checkout_initiated: i64,
    pub paid_subscribers: i64,
    pub time_to_convert_days: Option<f64>,
}

/// Raw user_settings row used by GDPR export.
#[derive(Debug, Clone)]
pub struct UserSettingRow {
    pub key: String,
    pub value: serde_json::Value,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

/// Raw org_settings row used by GDPR export.
#[derive(Debug, Clone)]
pub struct OrgSettingRow {
    pub key: String,
    pub value: serde_json::Value,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

/// Raw projects row used by GDPR export.
#[derive(Debug, Clone)]
pub struct ProjectRow {
    pub id: Uuid,
    pub name: String,
    pub visibility: String,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

/// Raw subscriptions row used by GDPR export.
#[derive(Debug, Clone)]
pub struct SubscriptionRow {
    pub id: Uuid,
    pub plan: String,
    pub status: String,
    pub current_period_end: DateTime<Utc>,
    pub created_at: DateTime<Utc>,
}

#[cfg(test)]
mod osv_matcher_tests {
    use super::{normalize_version_for_ecosystem, version_affected, version_affected_in_ecosystem};
    use serde_json::json;

    #[test]
    fn literal_versions_list_matches() {
        let affected = json!({
            "versions": ["1.2.3", "1.2.4"],
            "ranges": []
        });
        assert!(version_affected(&affected, "1.2.3"));
        assert!(version_affected(&affected, "1.2.4"));
        assert!(!version_affected(&affected, "1.2.5"));
    }

    #[test]
    fn introduced_zero_all_versions() {
        let affected = json!({
            "versions": [],
            "ranges": [{
                "type": "SEMVER",
                "events": [{"introduced": "0"}]
            }]
        });
        assert!(version_affected(&affected, "0.0.1"));
        assert!(version_affected(&affected, "999.0.0"));
    }

    #[test]
    fn introduced_fixed_exclusive_upper() {
        // Advisory says fixed in 1.5.0 — anything from 1.2.0 up to
        // (but not including) 1.5.0 is vulnerable.
        let affected = json!({
            "versions": [],
            "ranges": [{
                "type": "SEMVER",
                "events": [
                    {"introduced": "1.2.0"},
                    {"fixed": "1.5.0"}
                ]
            }]
        });
        assert!(!version_affected(&affected, "1.1.9"));
        assert!(version_affected(&affected, "1.2.0"));
        assert!(version_affected(&affected, "1.3.0"));
        assert!(version_affected(&affected, "1.4.99"));
        assert!(!version_affected(&affected, "1.5.0"), "fixed is exclusive");
        assert!(!version_affected(&affected, "2.0.0"));
    }

    #[test]
    fn last_affected_inclusive_upper() {
        let affected = json!({
            "ranges": [{
                "type": "SEMVER",
                "events": [
                    {"introduced": "1.0.0"},
                    {"last_affected": "1.2.3"}
                ]
            }]
        });
        assert!(version_affected(&affected, "1.0.0"));
        assert!(version_affected(&affected, "1.2.3"), "last_affected is inclusive");
        assert!(!version_affected(&affected, "1.2.4"));
    }

    #[test]
    fn multiple_intervals_per_range() {
        // Real-world shape: a regression introduced in 1.0, fixed in
        // 1.1, reintroduced in 2.0, fixed in 2.1.
        let affected = json!({
            "ranges": [{
                "type": "SEMVER",
                "events": [
                    {"introduced": "1.0.0"},
                    {"fixed": "1.1.0"},
                    {"introduced": "2.0.0"},
                    {"fixed": "2.1.0"}
                ]
            }]
        });
        assert!(version_affected(&affected, "1.0.5"));
        assert!(!version_affected(&affected, "1.5.0"), "between fixes");
        assert!(version_affected(&affected, "2.0.0"));
        assert!(!version_affected(&affected, "2.1.0"));
    }

    #[test]
    fn git_ranges_ignored() {
        let affected = json!({
            "ranges": [{
                "type": "GIT",
                "events": [{"introduced": "0"}]
            }]
        });
        // GIT ranges don't apply to published plugin versions.
        assert!(!version_affected(&affected, "1.2.3"));
    }

    #[test]
    fn go_ecosystem_strips_v_prefix() {
        // Go advisories tag versions as `v1.2.3`. The SEMVER range type
        // assumes plain semver, so without normalization nothing matches.
        let affected = serde_json::json!({
            "ranges": [{
                "type": "SEMVER",
                "events": [{"introduced": "v1.2.0"}, {"fixed": "v1.5.0"}]
            }]
        });
        // Without ecosystem hint → no match (the literal `v1.2.0` fails
        // to parse as semver in the interval walker).
        assert!(!version_affected(&affected, "1.3.0"));
        // With the Go ecosystem hint, both event versions and the target
        // get normalized and match.
        assert!(version_affected_in_ecosystem(&affected, "1.3.0", "Go"));
        assert!(version_affected_in_ecosystem(&affected, "v1.3.0", "Go"));
        assert!(!version_affected_in_ecosystem(&affected, "1.5.0", "Go"), "fixed exclusive");
        assert!(!version_affected_in_ecosystem(&affected, "1.1.9", "Go"));
    }

    #[test]
    fn pypi_prerelease_tags_normalize() {
        // Pre-release markers carry a numeric rank prefix so
        // `dev < a < b < rc` falls out of semver's identifier
        // comparison (numeric-before-alpha, then numeric by value).
        assert_eq!(
            normalize_version_for_ecosystem("PyPI", "1.0.0a1").as_deref(),
            Some("1.0.0-1.a.1")
        );
        assert_eq!(
            normalize_version_for_ecosystem("PyPI", "1.2.3rc2").as_deref(),
            Some("1.2.3-3.rc.2")
        );
        assert_eq!(
            normalize_version_for_ecosystem("PyPI", "2.0.0.dev7").as_deref(),
            Some("2.0.0-0.dev.7")
        );
        // Post-release-alone bumps the patch so semver puts it between
        // the base and the next patch: `3.1.4 < 3.1.5-0.post.1 < 3.1.5`.
        assert_eq!(
            normalize_version_for_ecosystem("PyPI", "3.1.4.post1").as_deref(),
            Some("3.1.5-0.post.1")
        );
    }

    #[test]
    fn pypi_epoch_gets_stripped() {
        assert_eq!(normalize_version_for_ecosystem("PyPI", "2!1.0.0").as_deref(), Some("1.0.0"),);
    }

    /// PEP 440 conformance table. Rows are `(input, expected_output)`
    /// where `None` means "leave literal" (already plain semver or
    /// unrecognized shape). Covers the shapes that actually show up in
    /// PyPI OSV advisories: epoch, release-only, pre-release, post-
    /// release, dev, local version, "v"-prefix, mixed-case alpha
    /// markers, and compound pre+post / pre+dev / post+dev combos.
    #[test]
    fn pypi_conformance_table() {
        let cases: &[(&str, Option<&str>)] = &[
            // Plain semver — no rewrite.
            ("1.2.3", None),
            ("0.0.1", None),
            // Epoch stripping.
            ("1!2.3.4", Some("2.3.4")),
            ("10!0.1.0", Some("0.1.0")),
            // "v" prefix is common in ported Go/npm advisories.
            ("v1.2.3", Some("1.2.3")),
            ("V1.2.3", Some("1.2.3")),
            // Pre-releases, all three markers — each gets a numeric
            // rank prefix so semver's identifier comparison reproduces
            // pep440's `dev < a < b < rc` order.
            ("1.0.0a1", Some("1.0.0-1.a.1")),
            ("1.0.0b2", Some("1.0.0-2.b.2")),
            ("1.2.3rc4", Some("1.2.3-3.rc.4")),
            // Case-insensitive marker matching.
            ("1.0.0RC1", Some("1.0.0-3.rc.1")),
            ("1.0.0A1", Some("1.0.0-1.a.1")),
            // Dev and post releases.
            // `.dev` is a pre-release (earlier than the base version)
            // with rank 0. `.post` bumps the patch so semver sorts it
            // between the base and the next patch — matching pep440
            // (`X.Y.Z < X.Y.Z.postN < X.Y.(Z+1)`).
            ("2.0.0.dev7", Some("2.0.0-0.dev.7")),
            ("3.1.4.post1", Some("3.1.5-0.post.1")),
            // Local identifiers become build metadata.
            ("1.0.0+ubuntu1", Some("1.0.0+local.ubuntu1")),
            ("1.0.0+deb.9", Some("1.0.0+local.deb.9")),
            // Illegal characters in a local id get sanitized to hyphens
            // so the result stays a valid semver build-metadata string.
            ("1.0.0+has_underscore", Some("1.0.0+local.has-underscore")),
            // Compound suffixes: pre-release + local.
            ("1.0.0a1+ubuntu", Some("1.0.0-1.a.1+local.ubuntu")),
            // Pre-release + post. Pre-release slot holds the pre +
            // rank; build slot holds the post.
            ("1.0.0rc1.post2", Some("1.0.0-3.rc.1+post.2")),
            // Pre-release + dev: pre-release slot accumulates both.
            // Documented limitation — this sorts incorrectly vs plain
            // pre (`1.0.0b2.dev3 > 1.0.0b2` in semver, the opposite of
            // pep440), but OSV advisories don't use this combination.
            ("1.0.0b2.dev3", Some("1.0.0-2.b.2.0.dev.3")),
            // Post + dev: patch bumped (as with plain post) and dev
            // appended into the pre-release slot.
            ("1.0.0.post1.dev2", Some("1.0.1-0.post.1.0.dev.2")),
            // Epoch + pre-release + local.
            ("2!1.0.0a1+ubuntu", Some("1.0.0-1.a.1+local.ubuntu")),
            // Words that contain `a`/`b`/`rc` as substrings must NOT
            // trigger the marker path.
            ("1.0.0-alpha", None),
            ("1.2.3-beta", None),
        ];

        for (input, expected) in cases {
            let got = normalize_version_for_ecosystem("PyPI", input);
            assert_eq!(
                got.as_deref(),
                *expected,
                "normalize_version_for_ecosystem(\"PyPI\", {:?}) mismatch — got {:?}, expected {:?}",
                input,
                got,
                expected,
            );
        }
    }

    /// Cross-check our PyPI normalizer against the canonical
    /// `pep440_rs` implementation. For every `(a, b)` pair in the
    /// fixture table, `pep440_rs::Version::cmp(a, b)` must agree with
    /// the ordering our normalizer produces. The point isn't to
    /// replicate every edge case of PEP 440 — it's to make sure our
    /// rewrite preserves the *ordering* invariants the OSV matcher
    /// relies on across the shapes that actually ship in advisories.
    #[test]
    fn pypi_normalizer_agrees_with_pep440_rs_on_ordering() {
        use pep440_rs::Version as PyVer;
        use std::cmp::Ordering;

        // The pairs below exercise the complete pep440 order
        // `dev < a < b < rc < base < post`. Our normalizer now handles
        // this by:
        //
        // * prefixing each pre-release kind with a numeric rank
        //   (`0.dev`, `1.a`, `2.b`, `3.rc`), which semver compares
        //   numerically before alphanumerically — giving the correct
        //   left-to-right order;
        // * bumping the patch for post-releases so
        //   `X.Y.Z.postN` → `X.Y.(Z+1)-0.post.N`, which semver sorts
        //   strictly between `X.Y.Z` and `X.Y.(Z+1)` exactly matching
        //   pep440's definition.
        //
        // Combined forms (pre + post, pre + dev, post + dev) are
        // exercised in the conformance table for structural
        // correctness but intentionally not here; the simplification
        // notes at the top of `normalize_pypi_version` explain why
        // those can drift from pep440 ordering.
        let pairs: &[(&str, &str)] = &[
            // Plain releases.
            ("1.0.0", "1.0.1"),
            ("1.0.0", "1.1.0"),
            ("0.9.9", "1.0.0"),
            // Pre-releases sort before the base.
            ("1.0.0a1", "1.0.0"),
            ("1.0.0b2", "1.0.0rc1"),
            ("1.0.0rc1", "1.0.0"),
            // `dev < a < b < rc` — the reason we added numeric rank prefixes.
            ("1.0.0.dev1", "1.0.0a1"),
            ("1.0.0a1", "1.0.0b1"),
            ("1.0.0b1", "1.0.0rc1"),
            // Dev sorts before the base it pre-releases.
            ("1.0.0.dev1", "1.0.0"),
            // Post sorts after the base but before the next patch.
            ("1.0.0", "1.0.0.post1"),
            ("1.0.0.post1", "1.0.0.post2"),
            ("1.0.0.post1", "1.0.1"),
            // "v" prefix should round-trip equal to the bare version.
            ("v1.0.0", "1.0.1"),
        ];

        // Local-identifier pairs (`1.0.0` vs `1.0.0+ubuntu`) are
        // checked separately because semver *ignores* build metadata
        // for ordering while pep440 sorts the local version higher.
        // OSV advisories never name a local version so this gap is
        // irrelevant for matching, but we sanity-check the public
        // release agrees instead.
        let public_release_pairs: &[(&str, &str)] =
            &[("1.0.0", "1.0.0+ubuntu"), ("1.0.0+deb", "1.0.0+ubuntu")];
        for (a_raw, b_raw) in public_release_pairs {
            let a = a_raw.split('+').next().unwrap();
            let b = b_raw.split('+').next().unwrap();
            let py_a: PyVer = a.parse().unwrap();
            let py_b: PyVer = b.parse().unwrap();
            assert_eq!(
                py_a.cmp(&py_b),
                Ordering::Equal,
                "public releases of {:?} / {:?} should compare equal",
                a_raw,
                b_raw,
            );
        }

        for (a_raw, b_raw) in pairs {
            let py_a: PyVer =
                a_raw.parse().unwrap_or_else(|e| panic!("pep440_rs reject {}: {}", a_raw, e));
            let py_b: PyVer =
                b_raw.parse().unwrap_or_else(|e| panic!("pep440_rs reject {}: {}", b_raw, e));
            let oracle = py_a.cmp(&py_b);

            // Our side: normalize, drop the build metadata so semver's
            // ordering matches pep440_rs's "public release is the
            // sort key for local/post" semantics.
            let norm_a =
                normalize_version_for_ecosystem("PyPI", a_raw).unwrap_or_else(|| a_raw.to_string());
            let norm_b =
                normalize_version_for_ecosystem("PyPI", b_raw).unwrap_or_else(|| b_raw.to_string());
            let strip_meta = |s: &str| s.split('+').next().unwrap_or(s).to_string();
            let (sa, sb) = (strip_meta(&norm_a), strip_meta(&norm_b));
            let ours_a = semver::Version::parse(&sa)
                .unwrap_or_else(|e| panic!("{} → {} not semver: {}", a_raw, sa, e));
            let ours_b = semver::Version::parse(&sb)
                .unwrap_or_else(|e| panic!("{} → {} not semver: {}", b_raw, sb, e));
            let ours = ours_a.cmp(&ours_b);

            // For pairs pep440 considers equal (e.g. `1.0.0` vs
            // `1.0.0+local`), semver strips of build metadata also
            // sorts equal. Otherwise the pair must be strictly less.
            if oracle == Ordering::Equal {
                assert_eq!(
                    ours,
                    Ordering::Equal,
                    "oracle equal but ours not equal for ({:?}, {:?})",
                    a_raw,
                    b_raw,
                );
            } else {
                assert_eq!(
                    ours, oracle,
                    "ordering mismatch for ({:?}, {:?}): pep440_rs {:?} ours {:?} via ({:?}, {:?})",
                    a_raw, b_raw, oracle, ours, sa, sb,
                );
            }
        }
    }

    #[test]
    fn pypi_local_identifiers_are_semver_parsable() {
        // Every rewritten output must be accepted by semver::Version::parse —
        // if it isn't, the downstream matcher silently falls into the
        // "unparsable target → conservative" path and we'd lose the
        // precision the normalizer exists to add.
        let inputs = &[
            "1.0.0+ubuntu1",
            "1.0.0+deb.9",
            "1.0.0+has_underscore",
            "1.0.0a1+ubuntu",
            "2!1.0.0a1+ubuntu",
            "1.0.0rc1.post2",
            "1.0.0b2.dev3",
            "1.0.0.post1.dev2",
        ];
        for input in inputs {
            let normalized = normalize_version_for_ecosystem("PyPI", input)
                .unwrap_or_else(|| panic!("{} did not normalize", input));
            semver::Version::parse(&normalized).unwrap_or_else(|e| {
                panic!("normalized form of {} ({}) failed semver parse: {}", input, normalized, e)
            });
        }
    }

    #[test]
    fn pypi_interval_match_with_prerelease() {
        // CVE says "vulnerable up to 1.0.0 inclusive," including pre-releases.
        let affected = serde_json::json!({
            "ranges": [{
                "type": "ECOSYSTEM",
                "events": [{"introduced": "0"}, {"last_affected": "1.0.0"}]
            }]
        });
        assert!(version_affected_in_ecosystem(&affected, "0.9.0", "PyPI"));
        assert!(version_affected_in_ecosystem(&affected, "1.0.0a1", "PyPI"));
        assert!(version_affected_in_ecosystem(&affected, "1.0.0", "PyPI"));
        assert!(!version_affected_in_ecosystem(&affected, "1.0.1", "PyPI"));
    }

    #[test]
    fn unknown_ecosystem_falls_through_unchanged() {
        assert_eq!(normalize_version_for_ecosystem("RubyGems", "1.2.3"), None);
        assert_eq!(normalize_version_for_ecosystem("", "1.2.3"), None);
        // Behavior must match the pre-existing `version_affected` path
        // exactly so the existing tests (cargo, npm) keep passing.
        let affected = serde_json::json!({
            "ranges": [{
                "type": "SEMVER",
                "events": [{"introduced": "1.0.0"}, {"fixed": "2.0.0"}]
            }]
        });
        assert_eq!(
            version_affected(&affected, "1.5.0"),
            version_affected_in_ecosystem(&affected, "1.5.0", "RubyGems"),
        );
    }

    #[test]
    fn events_are_sorted_before_pairing() {
        // Same advisory as `multiple_intervals_per_range`, but the events
        // are supplied out of order. The matcher must sort them first,
        // otherwise a naive walk would pair "introduced 1.0 → fixed 2.1"
        // (one giant interval) and falsely match 1.5 which is between
        // the two real fixes.
        let affected = serde_json::json!({
            "ranges": [{
                "type": "SEMVER",
                "events": [
                    {"fixed": "2.1.0"},
                    {"introduced": "1.0.0"},
                    {"fixed": "1.1.0"},
                    {"introduced": "2.0.0"}
                ]
            }]
        });
        assert!(version_affected(&affected, "1.0.5"));
        assert!(!version_affected(&affected, "1.5.0"), "sorting pairs (1.0,1.1) not (1.0,2.1)");
        assert!(version_affected(&affected, "2.0.0"));
        assert!(!version_affected(&affected, "2.1.0"));
    }

    #[test]
    fn non_semver_target_conservative_fallback() {
        // Target version isn't valid semver ("2024-01-15"). The matcher
        // must not panic and should only match literal equality or
        // fully-open intervals.
        let literal = json!({
            "versions": ["2024-01-15"]
        });
        assert!(version_affected(&literal, "2024-01-15"));

        let open = json!({
            "ranges": [{"type": "SEMVER", "events": [{"introduced": "0"}]}]
        });
        assert!(version_affected(&open, "2024-01-15"));

        let bounded = json!({
            "ranges": [{
                "type": "SEMVER",
                "events": [{"introduced": "1.0.0"}, {"fixed": "2.0.0"}]
            }]
        });
        // Can't semver-compare "2024-01-15"; stay conservative.
        assert!(!version_affected(&bounded, "2024-01-15"));
    }
}