greentic-deployer-dev 1.1.27501952916

Greentic deployer runtime for plan construction and deployment-pack dispatch
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
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
//! [`HttpEnvironmentStore`] — remote HTTP-backed implementation of
//! [`EnvironmentMutations`].
//!
//! Talks to a future `greentic-operator-store-server` (PR-4) over the
//! **A8 HTTP contract** specified in [`greentic_deploy_spec::remote`].
//! JSON over the wire, `reqwest::blocking::Client` for transport so the
//! sync `EnvironmentMutations` trait stays sync (no Tokio runtime needed
//! at call sites — see project memory `project_next_gen_deployment_phase_b`
//! for the `block_in_place` panic precedent that rules out async-in-sync).
//!
//! # Route table
//!
//! Every mutation maps to a single HTTP endpoint. The server (PR-4) mirrors
//! this table.
//!
//! | Trait method                   | Method | Path                                                      |
//! |-------------------------------|--------|-----------------------------------------------------------|
//! | `create_environment`          | POST   | `/environments`                                           |
//! | `update_environment`          | PATCH  | `/environments/{env_id}`                                  |
//! | `migrate_merge_bindings`      | POST   | `/environments/{env_id}/migrate-bindings`                 |
//! | `stage_revision`              | POST   | `/environments/{env_id}/revisions`                        |
//! | `warm_revision`               | POST   | `/environments/{env_id}/revisions/{rid}/warm`             |
//! | `drain_revision`              | POST   | `/environments/{env_id}/revisions/{rid}/drain`            |
//! | `archive_revision`            | POST   | `/environments/{env_id}/revisions/{rid}/archive`          |
//! | `add_bundle`                  | POST   | `/environments/{env_id}/bundles`                          |
//! | `update_bundle`               | PATCH  | `/environments/{env_id}/bundles/{deployment_id}`          |
//! | `remove_bundle`               | DELETE | `/environments/{env_id}/bundles/{deployment_id}`          |
//! | `add_pack_binding`            | POST   | `/environments/{env_id}/packs`                            |
//! | `update_pack_binding`         | PATCH  | `/environments/{env_id}/packs/{slot}`                     |
//! | `remove_pack_binding`         | DELETE | `/environments/{env_id}/packs/{slot}`                     |
//! | `rollback_pack_binding`       | POST   | `/environments/{env_id}/packs/{slot}/rollback`            |
//! | `add_extension_binding`       | POST   | `/environments/{env_id}/extensions`                       |
//! | `update_extension_binding`    | PATCH  | `/environments/{env_id}/extensions`                       |
//! | `remove_extension_binding`    | DELETE | `/environments/{env_id}/extensions`                       |
//! | `rollback_extension_binding`  | POST   | `/environments/{env_id}/extensions/rollback`              |
//! | `set_traffic_split`           | POST   | `/environments/{env_id}/traffic`                          |
//! | `rollback_traffic_split`      | POST   | `/environments/{env_id}/traffic/rollback`                 |
//! | `add_messaging_endpoint`      | POST   | `/environments/{env_id}/messaging`                        |
//! | `link_messaging_bundle`       | POST   | `/environments/{env_id}/messaging/{eid}/link`             |
//! | `unlink_messaging_bundle`     | POST   | `/environments/{env_id}/messaging/{eid}/unlink`           |
//! | `set_messaging_welcome_flow`  | POST   | `/environments/{env_id}/messaging/{eid}/welcome-flow`     |
//! | `remove_messaging_endpoint`   | DELETE | `/environments/{env_id}/messaging/{eid}`                  |
//! | `rotate_messaging_webhook_secret` | POST | `/environments/{env_id}/messaging/{eid}/rotate-secret` |
//! | `bootstrap_trust_root`        | POST   | `/environments/{env_id}/trust-root/bootstrap`             |
//! | `seed_trust_root_if_absent`   | POST   | `/environments/{env_id}/trust-root/seed`                  |
//! | `add_trusted_key`             | POST   | `/environments/{env_id}/trust-root/keys`                  |
//! | `remove_trusted_key`          | DELETE | `/environments/{env_id}/trust-root/keys/{key_id}`         |
//!
//! The backup/restore group (A8 #5, PR-4.4) is server-only — `LocalFsStore`
//! has no implementation, so these are inherent methods on
//! [`HttpEnvironmentStore`], not `EnvironmentMutations` verbs:
//!
//! | Inherent method               | Method | Path                                                      |
//! |-------------------------------|--------|-----------------------------------------------------------|
//! | `create_backup`               | POST   | `/environments/{env_id}/backups`                          |
//! | `list_backups`                | GET    | `/environments/{env_id}/backups`                          |
//! | `delete_backup`               | DELETE | `/environments/{env_id}/backups/{backup_id}`              |
//! | `restore`                     | POST   | `/environments/{env_id}/restore`                          |
//!
//! # Headers
//!
//! - `Content-Type: application/json` / `Accept: application/json` on every request.
//! - `Authorization: Bearer <token>` when [`AuthMethod::Bearer`].
//! - `Idempotency-Key: <ulid>` when the payload carries an [`IdempotencyKey`].
//!
//! # ETag / CAS
//!
//! Deferred to a follow-up (PR-3b-fu). Today the server is the source of
//! truth and we use last-write-wins. [`Precondition`] types from
//! `remote.rs` are ready but adding an optional precondition parameter to
//! the trait changes the trait — out of scope for this PR.
//!
//! # Error mapping
//!
//! Transport errors (connection refused, timeout, TLS handshake) map to
//! `StoreError::Conflict("transport: ...")`. A dedicated
//! `StoreError::Transport` variant would be cleaner but adding a new enum
//! variant cascades into every `match` site — follow-up.
//!
//! # Follow-ups
//!
//! - ETag/CAS at the wire layer (PR-3b-fu)
//! - `StoreError::Transport` variant
//! - `AuthMethod::Mtls` for production (mTLS)
//! - PR-3c wires dispatch between `LocalFsStore` and `HttpEnvironmentStore`

use greentic_deploy_spec::{
    AuditDecision, AuditEvent, AuditResult, BackupManifest, BindingGenerationOutcome,
    BundleDeployment, BundleId, CapabilitySlot, DeploymentId, EnvId, EnvPackBinding, Environment,
    EnvironmentHostConfig, ExtensionBinding, ExtensionBindingPayload, ExtensionKeyedPayload,
    IdempotencyKey, IdempotencyOutcome, MessagingBundleLinkPayload, MessagingEndpoint,
    MessagingEndpointId, PackBindingPayload, RemoteStoreError, RestoreOutcome, RestoreRequest,
    Revision, RevisionId, RollbackTrafficSplitPayload, RotateWebhookSecretPayload, StateEtag,
};
use percent_encoding::{AsciiSet, CONTROLS, utf8_percent_encode};
use reqwest::blocking::Client;
use serde::{Deserialize, Serialize};
use url::Url;

use super::mutations::{
    AddBundlePayload, AddMessagingEndpointPayload, AddTrustedKeyPayload, ApplyTrafficSplitOutcome,
    EnvironmentMutations, ExtensionKey, MigrateMergePayload, RemoveBundleOutcome,
    RevisionTransitionOutcome, RollbackTrafficSplitOutcome, SetMessagingWelcomeFlowPayload,
    SetTrafficSplitPayload, StageRevisionPayload, TrustRootAddOutcome, TrustRootRemoveOutcome,
    TrustRootSeed, UpdateBundlePayload, UpdateEnvironmentPayload, WarmRevisionPayload,
};
use super::store::StoreError;

// ---------------------------------------------------------------------------
// Auth
// ---------------------------------------------------------------------------

/// How the client authenticates to the remote store server.
#[derive(Debug, Clone)]
pub enum AuthMethod {
    /// No authentication (dev/loopback).
    None,
    /// Bearer token (Phase A).
    Bearer(String),
    // mTLS deferred — note in module doc but not implemented here.
}

// ---------------------------------------------------------------------------
// URL path-segment encoding (Fix 1: prevent path-traversal via dynamic IDs)
// ---------------------------------------------------------------------------

/// Characters that MUST be percent-encoded when interpolated into a URL path
/// segment. Covers RFC 3986 reserved + unsafe characters that `Url::join`
/// would otherwise interpret structurally (`/`, `?`, `#`, `..` via `.`).
const PATH_SEGMENT_ENCODE_SET: &AsciiSet = &CONTROLS
    .add(b' ')
    .add(b'"')
    .add(b'#')
    .add(b'<')
    .add(b'>')
    .add(b'?')
    .add(b'`')
    .add(b'{')
    .add(b'}')
    .add(b'/')
    .add(b'%')
    .add(b'.');

/// Percent-encode a dynamic identifier for safe interpolation into a URL path
/// segment. Normal alphanumeric identifiers pass through unchanged; characters
/// like `/`, `..`, `?`, `#` are escaped so they cannot alter the request path.
fn encode_segment(s: &str) -> String {
    utf8_percent_encode(s, PATH_SEGMENT_ENCODE_SET).to_string()
}

// ---------------------------------------------------------------------------
// Construction errors
// ---------------------------------------------------------------------------

/// Errors that can occur when constructing an [`HttpEnvironmentStore`].
#[derive(Debug, thiserror::Error)]
pub enum ConstructionError {
    /// Bearer auth over plaintext HTTP to a non-loopback host exposes the token.
    #[error(
        "bearer auth over http:// is only allowed to loopback hosts; \
         got `{0}` — use https:// or AuthMethod::None"
    )]
    InsecureTransport(String),
}

// ---------------------------------------------------------------------------
// HttpEnvironmentStore
// ---------------------------------------------------------------------------

/// Remote HTTP-backed implementation of [`EnvironmentMutations`].
///
/// See the module-level doc for the route table and design rationale.
#[derive(Debug, Clone)]
pub struct HttpEnvironmentStore {
    client: Client,
    base_url: Url,
    /// Pre-rendered `Authorization: Bearer <token>` value, built once at
    /// construction. `None` when [`AuthMethod::None`].
    auth_header_value: Option<String>,
}

/// Ensure `base_url`'s path ends with `/` so [`Url::join`] treats relative
/// paths as siblings of the base, not replacements of the last segment.
/// Idempotent — called once at construction.
fn normalize_base_url(mut base_url: Url) -> Url {
    if !base_url.path().ends_with('/') {
        let normalized = format!("{}/", base_url.path());
        base_url.set_path(&normalized);
    }
    base_url
}

/// Whether `url` points at a loopback address (`127.0.0.0/8`, `::1`, or
/// `localhost`). Used by the insecure-transport guard.
fn is_loopback(url: &Url) -> bool {
    match url.host_str() {
        Some("localhost") => true,
        Some(host) => host
            .parse::<std::net::IpAddr>()
            .map(|ip| ip.is_loopback())
            .unwrap_or(false),
        None => false,
    }
}

/// Validate that bearer auth is not used over plaintext HTTP to non-loopback.
fn validate_transport(url: &Url, auth: &AuthMethod) -> Result<(), ConstructionError> {
    if let AuthMethod::Bearer(_) = auth
        && url.scheme() == "http"
    {
        if is_loopback(url) {
            tracing::warn!(
                url = %url,
                "bearer auth over http:// to loopback — acceptable for dev, \
                 not for production"
            );
        } else {
            return Err(ConstructionError::InsecureTransport(
                url.host_str().unwrap_or("<none>").to_string(),
            ));
        }
    }
    Ok(())
}

impl HttpEnvironmentStore {
    /// Build with the default `reqwest::blocking::Client`.
    ///
    /// Returns [`ConstructionError::InsecureTransport`] if `auth` is
    /// [`AuthMethod::Bearer`] and `base_url` is `http://` to a non-loopback
    /// host.
    pub fn new(base_url: Url, auth: AuthMethod) -> Result<Self, ConstructionError> {
        Self::with_client(Client::new(), base_url, auth)
    }

    /// Build with a caller-supplied client (custom timeouts, TLS config, etc.).
    ///
    /// Returns [`ConstructionError::InsecureTransport`] if `auth` is
    /// [`AuthMethod::Bearer`] and `base_url` is `http://` to a non-loopback
    /// host.
    pub fn with_client(
        client: Client,
        base_url: Url,
        auth: AuthMethod,
    ) -> Result<Self, ConstructionError> {
        validate_transport(&base_url, &auth)?;
        let auth_header_value = match auth {
            AuthMethod::Bearer(token) => Some(format!("Bearer {token}")),
            AuthMethod::None => None,
        };
        Ok(Self {
            client,
            base_url: normalize_base_url(base_url),
            auth_header_value,
        })
    }

    // -----------------------------------------------------------------------
    // Internal helpers
    // -----------------------------------------------------------------------

    /// Build a full URL by joining `path` onto `base_url`. The base is
    /// trailing-slash-normalized in the constructor so [`Url::join`] resolves
    /// `path` relative to the API root, not by replacing the last segment.
    fn url(&self, path: &str) -> Result<Url, StoreError> {
        self.base_url
            .join(path)
            .map_err(|e| StoreError::Conflict(format!("transport: invalid URL path `{path}`: {e}")))
    }

    /// Build the env-scoped request path: `environments/{env}{suffix}`. The
    /// returned string is suitable for [`Self::send_mutation`] et al; pass
    /// `""` for the env itself or `"/revisions"` (already-encoded) for
    /// sub-resources. For multi-segment paths (e.g. `/revisions/{rid}/warm`),
    /// callers still encode each dynamic segment themselves and inline the
    /// `format!` — the helper is only a win for the env-only and
    /// env+constant-suffix shapes.
    fn env_path(&self, env_id: &EnvId, suffix: &str) -> String {
        format!("environments/{}{suffix}", encode_segment(env_id.as_str()))
    }

    /// Send an HTTP request and parse the JSON response.
    ///
    /// - Sets `Content-Type` and `Accept` to `application/json`.
    /// - Adds `Authorization: Bearer` if configured.
    /// - Adds `Idempotency-Key` header when provided.
    /// - On success (2xx), deserializes the body as `R`.
    /// - On error, maps the HTTP status + body to [`StoreError`].
    fn send<P: Serialize, R: serde::de::DeserializeOwned>(
        &self,
        method: reqwest::Method,
        path: &str,
        idempotency_key: Option<&str>,
        body: Option<&P>,
    ) -> Result<R, StoreError> {
        let url = self.url(path)?;
        let mut builder = self
            .client
            .request(method, url)
            .header("Content-Type", "application/json")
            .header("Accept", "application/json");

        if let Some(value) = &self.auth_header_value {
            builder = builder.header("Authorization", value);
        }
        if let Some(key) = idempotency_key {
            builder = builder.header("Idempotency-Key", key);
        }
        if let Some(payload) = body {
            builder = builder.json(payload);
        }

        let response = builder
            .send()
            .map_err(|e| StoreError::Conflict(format!("transport: {e}")))?;

        let status = response.status();
        if status.is_success() {
            // After a 2xx the mutation may already be committed server-side.
            // Decode failures must not look retriable-with-a-fresh-key — wrap
            // them in `CommittedAfterSave`. Safe because `send_mutation` is
            // `send`'s only caller, so the 2xx path is exclusively mutations.
            if status == reqwest::StatusCode::NO_CONTENT {
                return serde_json::from_str("null").map_err(|e| {
                    committed_after_save(
                        format!("transport: cannot deserialize 204 body: {e}"),
                        idempotency_key,
                    )
                });
            }
            response.json::<R>().map_err(|e| {
                committed_after_save(
                    format!("transport: invalid response body: {e}"),
                    idempotency_key,
                )
            })
        } else {
            Err(map_error_response(status, response))
        }
    }

    /// Send a mutating request whose A8 response is a [`MutationEnvelope`]
    /// wrapping the domain result alongside ETag/generation/idempotency
    /// metadata. Enforces the A8 §4 audit invariant on the success envelope
    /// (see [`MutationEnvelope::validated`]) against `expected_env` — the
    /// env the request targeted — then returns only the domain `result`;
    /// PR-3b-fu will surface the remaining envelope metadata via a
    /// return-type extension.
    fn send_mutation<P: Serialize, R: serde::de::DeserializeOwned>(
        &self,
        expected_env: &EnvId,
        method: reqwest::Method,
        path: &str,
        idempotency_key: Option<&str>,
        body: Option<&P>,
    ) -> Result<R, StoreError> {
        let envelope: MutationEnvelope<R> = self.send(method, path, idempotency_key, body)?;
        envelope.validated(expected_env, idempotency_key)
    }

    /// [`send_mutation`](Self::send_mutation) variant with no request body.
    fn send_mutation_no_body<R: serde::de::DeserializeOwned>(
        &self,
        expected_env: &EnvId,
        method: reqwest::Method,
        path: &str,
        idempotency_key: Option<&str>,
    ) -> Result<R, StoreError> {
        self.send_mutation::<(), R>(expected_env, method, path, idempotency_key, None)
    }

    // -----------------------------------------------------------------------
    // Backup / restore (A8 #5, PR-4.4) — server-only operations
    // -----------------------------------------------------------------------
    //
    // `LocalFsStore` has no backup implementation (a local store is backed
    // up by copying the directory), so these live as inherent methods
    // rather than `EnvironmentMutations` verbs. The mutating three wear the
    // standard A8 envelope (audit validation applies unchanged); `list` is
    // a plain read.

    /// `POST /environments/{env_id}/backups` — snapshot the environment's
    /// stored state server-side; returns the contract's manifest.
    pub fn create_backup(
        &self,
        env_id: &EnvId,
        idempotency_key: &IdempotencyKey,
    ) -> Result<BackupManifest, StoreError> {
        self.send_mutation_no_body(
            env_id,
            reqwest::Method::POST,
            &self.env_path(env_id, "/backups"),
            Some(idempotency_key.as_str()),
        )
    }

    /// `GET /environments/{env_id}/backups` — list backup manifests,
    /// oldest first.
    pub fn list_backups(&self, env_id: &EnvId) -> Result<Vec<BackupManifest>, StoreError> {
        #[derive(Deserialize)]
        struct BackupsResponse {
            backups: Vec<BackupManifest>,
        }
        let response: BackupsResponse = self.send::<(), _>(
            reqwest::Method::GET,
            &self.env_path(env_id, "/backups"),
            None,
            None,
        )?;
        Ok(response.backups)
    }

    /// `DELETE /environments/{env_id}/backups/{backup_id}` — drop one
    /// backup (the server's per-env backup store is bounded; at the cap,
    /// `create_backup` refuses until old ones are deleted).
    pub fn delete_backup(
        &self,
        env_id: &EnvId,
        backup_id: &str,
        idempotency_key: &IdempotencyKey,
    ) -> Result<(), StoreError> {
        let path = format!(
            "{}/{}",
            self.env_path(env_id, "/backups"),
            encode_segment(backup_id)
        );
        let _ack: serde_json::Value = self.send_mutation_no_body(
            env_id,
            reqwest::Method::DELETE,
            &path,
            Some(idempotency_key.as_str()),
        )?;
        Ok(())
    }

    /// `POST /environments/{env_id}/restore` — restore the environment
    /// from a named backup. `request.precondition` MUST pin prior state
    /// (the server answers 428 otherwise; a stale pin is a 412). The
    /// returned outcome's `integrity` equals the backup's recorded digest,
    /// and [`RestoreOutcome::etag`] is the restored state's strong
    /// validator for the next CAS write.
    pub fn restore(
        &self,
        env_id: &EnvId,
        request: &RestoreRequest,
        idempotency_key: &IdempotencyKey,
    ) -> Result<RestoreOutcome, StoreError> {
        self.send_mutation(
            env_id,
            reqwest::Method::POST,
            &self.env_path(env_id, "/restore"),
            Some(idempotency_key.as_str()),
            Some(request),
        )
    }
}

/// PR-4.0 (F2): enforce the A8 §4 audit invariant on a success envelope.
///
/// The local path fails closed on audit via `cli::audit_and_record`; the
/// remote path skips local audit because the server owns the durable record
/// (A8 §4). That hand-off is only sound if the server actually returned the
/// record — so a 2xx envelope missing it, or carrying one that contradicts
/// the success (a `deny` decision, a non-`ok` result) or names a different
/// environment than the request targeted, is a contract violation the client
/// must reject rather than report as success.
///
/// `sent_idempotency_key`: when `Some(key)`, the audit record's
/// `idempotency_key` must match — otherwise a stale record from a
/// different request could satisfy the check. A8 §2 replays carry the
/// SAME key by definition, so replays pass. When `None` (no current
/// caller supplies one), the equality check is skipped.
fn validate_success_audit(
    audit: Option<&AuditEvent>,
    expected_env: &EnvId,
    sent_idempotency_key: Option<&str>,
) -> Result<(), StoreError> {
    let Some(audit) = audit else {
        return Err(StoreError::Conflict(
            "A8 contract violation: success response is missing the audit record (§4)".to_string(),
        ));
    };
    if let AuditDecision::Deny { policy, reason } = &audit.authorization {
        return Err(StoreError::Conflict(format!(
            "A8 contract violation: success response carries a deny audit decision \
             (policy `{policy}`: {reason}); a denial must be a 403"
        )));
    }
    match &audit.result {
        AuditResult::Ok => {}
        other => {
            return Err(StoreError::Conflict(format!(
                "A8 contract violation: success response carries a non-ok audit result \
                 ({other:?})"
            )));
        }
    }
    if audit.env_id != expected_env.as_str() {
        return Err(StoreError::Conflict(format!(
            "A8 contract violation: audit record names env `{}` but the request targeted \
             env `{expected_env}`",
            audit.env_id
        )));
    }
    // Bind the audit record to the request via the idempotency key: a
    // stale same-env record cannot carry the request's fresh ULID.
    if let Some(sent) = sent_idempotency_key {
        let audit_key = audit.idempotency_key.as_deref();
        if audit_key != Some(sent) {
            return Err(StoreError::Conflict(format!(
                "A8 contract violation: audit record idempotency key `{}` does not match \
                 the request's key `{sent}` — the record does not belong to this mutation",
                audit_key.unwrap_or("<missing>")
            )));
        }
    }
    Ok(())
}

/// Wrap a post-2xx failure in [`StoreError::CommittedAfterSave`] with the
/// shared replay guidance. After a 2xx status the mutation may already be
/// committed server-side, so the error must not look retriable — re-running
/// with a freshly minted idempotency key would double-apply, while the SAME
/// key replays (A8 §2).
fn committed_after_save(message: String, idempotency_key: Option<&str>) -> StoreError {
    let replay = match idempotency_key {
        Some(key) => format!(" — replay with Idempotency-Key `{key}` instead of re-applying"),
        None => {
            " — re-running with the SAME Idempotency-Key replays instead of re-applying".to_string()
        }
    };
    StoreError::CommittedAfterSave(Box::new(StoreError::Conflict(format!(
        "{message} — the server reported success (2xx), so the mutation may already be \
         committed{replay}"
    ))))
}

// ---------------------------------------------------------------------------
// A8 mutation-response envelope
// ---------------------------------------------------------------------------

/// The A8 success envelope for mutating calls. The server returns the domain
/// `result` alongside CAS/idempotency/audit metadata defined in
/// [`greentic_deploy_spec::remote::MutationResponse`].
///
/// PR-3b parses the full envelope so that a future return-type extension
/// (PR-3b-fu) can surface ETag/generation without re-parsing. Today the
/// trait methods return bare domain types, so callers see only `result` —
/// except `audit`, which [`validate_success_audit`] enforces on every
/// success (PR-4.0/F2). It stays `Option` so a missing record is rejected
/// with a precise contract-violation message instead of a generic serde
/// deserialize error.
#[derive(Debug, Deserialize)]
struct MutationEnvelope<T> {
    result: T,
    // Fields present per A8 but not yet surfaced to callers (PR-3b-fu).
    #[serde(default)]
    #[allow(dead_code)]
    etag: Option<StateEtag>,
    #[serde(default)]
    #[allow(dead_code)]
    generation: Option<u64>,
    #[serde(default)]
    #[allow(dead_code)]
    idempotency: Option<IdempotencyOutcome>,
    #[serde(default)]
    audit: Option<AuditEvent>,
}

impl<T> MutationEnvelope<T> {
    /// Enforce the A8 §4 audit invariant (see [`validate_success_audit`])
    /// and return the domain `result`. Consuming the envelope here couples
    /// the invariant to the type: any future path that deserializes a
    /// `MutationEnvelope` must go through `validated` to reach the result,
    /// so the check cannot be silently skipped. Violations are wrapped in
    /// [`StoreError::CommittedAfterSave`] — the envelope only exists after
    /// a 2xx, so the mutation may already be committed server-side.
    fn validated(
        self,
        expected_env: &EnvId,
        sent_idempotency_key: Option<&str>,
    ) -> Result<T, StoreError> {
        validate_success_audit(self.audit.as_ref(), expected_env, sent_idempotency_key)
            .map_err(|inner| committed_after_save(inner.to_string(), sent_idempotency_key))?;
        Ok(self.result)
    }
}

/// Mint a one-shot idempotency key for methods whose trait signature does
/// not carry one. Delegates to [`crate::cli::mint_idempotency_key`] so the
/// ULID-generation strategy stays in one place. Returns the inner string for
/// header-value use; each call produces a unique ULID so retries by the
/// caller are safe.
fn mint_idempotency_key() -> String {
    crate::cli::mint_idempotency_key().as_str().to_string()
}

/// Map an error HTTP response to [`StoreError`].
///
/// Tries to parse the body as [`RemoteStoreError`] (the A8 error contract);
/// falls back to a generic `StoreError::Conflict` with the status code and
/// raw body text.
fn map_error_response(
    status: reqwest::StatusCode,
    response: reqwest::blocking::Response,
) -> StoreError {
    let body_text = response.text().unwrap_or_default();

    // Try to parse as the A8 contract error shape.
    if let Ok(remote_err) = serde_json::from_str::<RemoteStoreError>(&body_text) {
        let actual = status.as_u16();
        let expected = remote_err.http_status();
        // A8 defines `Unauthorized` as 403; accept 401 (unauthenticated) as
        // well since both express a denial. All other kinds must match exactly.
        let consistent = actual == expected
            || (actual == 401 && matches!(remote_err, RemoteStoreError::Unauthorized { .. }));
        if consistent {
            return map_remote_error(remote_err);
        }
        return StoreError::Conflict(format!(
            "A8 contract violation: HTTP status {actual} contradicts the A8 error body \
             (kind expects {expected}): {remote_err}"
        ));
    }

    // Fallback: map by status code with raw body.
    match status.as_u16() {
        404 => StoreError::NotFound(EnvId::try_from("unknown").unwrap_or_else(|_| {
            // EnvId::try_from should not fail for "unknown" but guard anyway.
            unreachable!("EnvId::try_from(\"unknown\") must succeed")
        })),
        409 => StoreError::Conflict(body_text),
        400 | 422 => StoreError::InvalidArgument(body_text),
        // PR-4.0 (F4): a 401/403 whose body is not the A8 error shape (e.g.
        // from a proxy/LB in front of the store) is still a denial — keep
        // the `unauthorized` noun rather than degrading to `conflict`.
        401 | 403 => StoreError::Unauthorized {
            policy: GATEWAY_DENIAL_POLICY.to_string(),
            reason: body_text,
        },
        501 => StoreError::NotYetImplemented(body_text),
        _ => StoreError::Conflict(format!("server ({status}): {body_text}")),
    }
}

/// `policy` value for denials that did not come through the A8 error shape
/// (non-A8 401/403 bodies, e.g. a proxy/LB in front of the store). Named so
/// downstream consumers matching on `policy` have one stable value.
const GATEWAY_DENIAL_POLICY: &str = "remote";

/// Map a parsed [`RemoteStoreError`] to [`StoreError`]. Takes the error by
/// value so the owned `String` fields move instead of cloning.
fn map_remote_error(err: RemoteStoreError) -> StoreError {
    match err {
        RemoteStoreError::NotFound => StoreError::NotFound(
            EnvId::try_from("unknown")
                .unwrap_or_else(|_| unreachable!("EnvId::try_from(\"unknown\") must succeed")),
        ),
        RemoteStoreError::PreconditionFailed(conflict) => {
            StoreError::Conflict(format!("precondition failed: {conflict:?}"))
        }
        RemoteStoreError::PreconditionRequired { detail } => {
            StoreError::Conflict(format!("precondition required: {detail}"))
        }
        RemoteStoreError::IdempotencyConflict { reason } => {
            StoreError::Conflict(format!("idempotency conflict: {reason}"))
        }
        RemoteStoreError::Unauthorized { policy, reason } => {
            StoreError::Unauthorized { policy, reason }
        }
        // Same noun the local impl uses for create-on-existing — the CLI
        // mapper downcasts `Conflict` uniformly across backends.
        RemoteStoreError::AlreadyExists { detail } => StoreError::Conflict(detail),
        RemoteStoreError::Conflict { detail } => StoreError::Conflict(detail),
        RemoteStoreError::DependentNotFound { detail } => StoreError::DependentNotFound(detail),
        // Reconstruct the local store's typed health-gate failure so CLI
        // callers (committed-on-error handling, gate-failed telemetry emit)
        // behave identically against a remote store. The server persisted
        // the `Failed` lifecycle before responding — committed, like local.
        RemoteStoreError::HealthGateFailed {
            revision_id,
            failed_checks,
            message,
        } => StoreError::Lifecycle(Box::new(
            crate::environment::LifecycleError::HealthGateFailed {
                revision_id,
                failed_checks,
                message,
            },
        )),
        RemoteStoreError::InvalidRequest { detail } => StoreError::InvalidArgument(detail),
        RemoteStoreError::IntegrityMismatch { expected, actual } => StoreError::InvalidArgument(
            format!("integrity mismatch: expected {expected}, computed {actual}"),
        ),
        RemoteStoreError::NotYetImplemented { detail } => StoreError::NotYetImplemented(detail),
        RemoteStoreError::Internal { message } => {
            StoreError::Conflict(format!("server: {message}"))
        }
    }
}

// ---------------------------------------------------------------------------
// Wire types — request payloads sent to the server.
//
// The trait's payload structs (`StageRevisionPayload`, etc.) don't derive
// `Serialize` (they are deployer-internal). We define thin wire DTOs here
// that do, and convert at the call boundary.
// ---------------------------------------------------------------------------

// Env-lifecycle wire shapes (`CreateEnvironmentPayload`,
// `UpdateEnvironmentPayload`, `MigrateMergePayload`, `MergeReport`) moved to
// `greentic_deploy_spec::engine` in PR-4.2a; the revision verb group's
// (`StageRevisionPayload`, `WarmRevisionPayload`,
// `RevisionTransitionOutcome`) followed in PR-4.2b — the payload structs
// now carry serde derives in the exact wire encoding this module
// established, so the client serializes them directly and the
// operator-store-server deserializes the same types. Remaining verb groups
// migrate as their routes land.

// Bundle wire shapes (`AddBundlePayload`, `UpdateBundlePayload`,
// `RemoveBundleOutcome`) are the shared `greentic_deploy_spec::engine`
// types since PR-4.2g — the client serializes the same structs the server
// deserializes.

// Binding wire shapes (`PackBindingPayload`, `ExtensionBindingPayload`,
// `ExtensionKeyedPayload`, `BindingGenerationOutcome`) are the shared
// `greentic_deploy_spec::engine` types since PR-4.2d — the client
// serializes the same structs the server deserializes.

// Traffic wire shapes (`SetTrafficSplitPayload`,
// `RollbackTrafficSplitPayload`, `ApplyTrafficSplitOutcome`,
// `RollbackTrafficSplitOutcome`) are the shared
// `greentic_deploy_spec::engine` types since PR-4.2c — the client
// serializes the same structs the server deserializes.

// Messaging wire shapes (`AddMessagingEndpointPayload`,
// `MessagingBundleLinkPayload`, `SetMessagingWelcomeFlowPayload`,
// `RotateWebhookSecretPayload`) are the shared
// `greentic_deploy_spec::engine` types since PR-4.2h — the client
// serializes the same structs the server deserializes.

// Trust-root wire shapes (`AddTrustedKeyPayload`, `TrustRootSeed`,
// `TrustRootAddOutcome`, `TrustRootRemoveOutcome`) are the shared
// `greentic_deploy_spec::engine` types since PR-4.2f — the client
// serializes/deserializes the same structs the server does.

// ---------------------------------------------------------------------------
// EnvironmentMutations impl
// ---------------------------------------------------------------------------

impl EnvironmentMutations for HttpEnvironmentStore {
    fn create_environment(
        &self,
        env_id: &EnvId,
        name: String,
        host_config: EnvironmentHostConfig,
    ) -> Result<Environment, StoreError> {
        let idem_key = mint_idempotency_key();
        let req = greentic_deploy_spec::CreateEnvironmentPayload {
            env_id: env_id.clone(),
            name,
            host_config,
        };
        self.send_mutation(
            env_id,
            reqwest::Method::POST,
            "environments",
            Some(&idem_key),
            Some(&req),
        )
    }

    fn update_environment(
        &self,
        env_id: &EnvId,
        patch: UpdateEnvironmentPayload,
    ) -> Result<Environment, StoreError> {
        let idem_key = mint_idempotency_key();
        self.send_mutation(
            env_id,
            reqwest::Method::PATCH,
            &self.env_path(env_id, ""),
            Some(&idem_key),
            Some(&patch),
        )
    }

    fn migrate_merge_bindings(
        &self,
        target_env_id: &EnvId,
        payload: MigrateMergePayload,
    ) -> Result<(Vec<String>, Vec<String>), StoreError> {
        let idem_key = mint_idempotency_key();
        let resp: greentic_deploy_spec::MergeReport = self.send_mutation(
            target_env_id,
            reqwest::Method::POST,
            &format!(
                "environments/{}/migrate-bindings",
                encode_segment(target_env_id.as_str())
            ),
            Some(&idem_key),
            Some(&payload),
        )?;
        Ok((resp.merged_slots, resp.merged_extensions))
    }

    fn stage_revision(
        &self,
        env_id: &EnvId,
        payload: StageRevisionPayload,
        idempotency_key: IdempotencyKey,
    ) -> Result<Revision, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        self.send_mutation(
            env_id,
            reqwest::Method::POST,
            &self.env_path(env_id, "/revisions"),
            Some(&idem_key),
            Some(&payload),
        )
    }

    fn warm_revision(
        &self,
        env_id: &EnvId,
        payload: WarmRevisionPayload,
        idempotency_key: IdempotencyKey,
    ) -> Result<RevisionTransitionOutcome, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        let rid = payload.revision_id;
        self.send_mutation(
            env_id,
            reqwest::Method::POST,
            &format!(
                "environments/{}/revisions/{}/warm",
                encode_segment(env_id.as_str()),
                encode_segment(&rid.to_string()),
            ),
            Some(&idem_key),
            Some(&payload),
        )
    }

    fn drain_revision(
        &self,
        env_id: &EnvId,
        revision_id: RevisionId,
        idempotency_key: IdempotencyKey,
    ) -> Result<RevisionTransitionOutcome, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        self.send_mutation_no_body(
            env_id,
            reqwest::Method::POST,
            &format!(
                "environments/{}/revisions/{}/drain",
                encode_segment(env_id.as_str()),
                encode_segment(&revision_id.to_string()),
            ),
            Some(&idem_key),
        )
    }

    fn archive_revision(
        &self,
        env_id: &EnvId,
        revision_id: RevisionId,
        idempotency_key: IdempotencyKey,
    ) -> Result<RevisionTransitionOutcome, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        self.send_mutation_no_body(
            env_id,
            reqwest::Method::POST,
            &format!(
                "environments/{}/revisions/{}/archive",
                encode_segment(env_id.as_str()),
                encode_segment(&revision_id.to_string()),
            ),
            Some(&idem_key),
        )
    }

    fn add_bundle(
        &self,
        env_id: &EnvId,
        payload: AddBundlePayload,
        idempotency_key: IdempotencyKey,
    ) -> Result<BundleDeployment, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        self.send_mutation(
            env_id,
            reqwest::Method::POST,
            &self.env_path(env_id, "/bundles"),
            Some(&idem_key),
            Some(&payload),
        )
    }

    fn update_bundle(
        &self,
        env_id: &EnvId,
        payload: UpdateBundlePayload,
        idempotency_key: IdempotencyKey,
    ) -> Result<BundleDeployment, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        let did = payload.deployment_id.to_string();
        self.send_mutation(
            env_id,
            reqwest::Method::PATCH,
            &format!(
                "environments/{}/bundles/{}",
                encode_segment(env_id.as_str()),
                encode_segment(&did),
            ),
            Some(&idem_key),
            Some(&payload),
        )
    }

    fn remove_bundle(
        &self,
        env_id: &EnvId,
        deployment_id: DeploymentId,
        idempotency_key: IdempotencyKey,
    ) -> Result<RemoveBundleOutcome, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        self.send_mutation_no_body(
            env_id,
            reqwest::Method::DELETE,
            &format!(
                "environments/{}/bundles/{}",
                encode_segment(env_id.as_str()),
                encode_segment(&deployment_id.to_string()),
            ),
            Some(&idem_key),
        )
    }

    fn add_pack_binding(
        &self,
        env_id: &EnvId,
        binding: EnvPackBinding,
        idempotency_key: IdempotencyKey,
    ) -> Result<EnvPackBinding, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        let req = PackBindingPayload { binding };
        self.send_mutation(
            env_id,
            reqwest::Method::POST,
            &self.env_path(env_id, "/packs"),
            Some(&idem_key),
            Some(&req),
        )
    }

    fn update_pack_binding(
        &self,
        env_id: &EnvId,
        slot: CapabilitySlot,
        binding: EnvPackBinding,
        idempotency_key: IdempotencyKey,
    ) -> Result<(EnvPackBinding, u64), StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        let req = PackBindingPayload { binding };
        let resp: BindingGenerationOutcome<EnvPackBinding> = self.send_mutation(
            env_id,
            reqwest::Method::PATCH,
            &format!(
                "environments/{}/packs/{}",
                encode_segment(env_id.as_str()),
                encode_segment(&slot.to_string()),
            ),
            Some(&idem_key),
            Some(&req),
        )?;
        Ok((resp.binding, resp.generation))
    }

    fn remove_pack_binding(
        &self,
        env_id: &EnvId,
        slot: CapabilitySlot,
        idempotency_key: IdempotencyKey,
    ) -> Result<(EnvPackBinding, u64), StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        let resp: BindingGenerationOutcome<EnvPackBinding> = self.send_mutation_no_body(
            env_id,
            reqwest::Method::DELETE,
            &format!(
                "environments/{}/packs/{}",
                encode_segment(env_id.as_str()),
                encode_segment(&slot.to_string()),
            ),
            Some(&idem_key),
        )?;
        Ok((resp.binding, resp.generation))
    }

    fn rollback_pack_binding(
        &self,
        env_id: &EnvId,
        slot: CapabilitySlot,
        idempotency_key: IdempotencyKey,
    ) -> Result<(EnvPackBinding, u64), StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        let resp: BindingGenerationOutcome<EnvPackBinding> = self.send_mutation_no_body(
            env_id,
            reqwest::Method::POST,
            &format!(
                "environments/{}/packs/{}/rollback",
                encode_segment(env_id.as_str()),
                encode_segment(&slot.to_string()),
            ),
            Some(&idem_key),
        )?;
        Ok((resp.binding, resp.generation))
    }

    fn add_extension_binding(
        &self,
        env_id: &EnvId,
        binding: ExtensionBinding,
        idempotency_key: IdempotencyKey,
    ) -> Result<ExtensionBinding, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        let req = ExtensionBindingPayload { binding };
        self.send_mutation(
            env_id,
            reqwest::Method::POST,
            &format!(
                "environments/{}/extensions",
                encode_segment(env_id.as_str())
            ),
            Some(&idem_key),
            Some(&req),
        )
    }

    fn update_extension_binding(
        &self,
        env_id: &EnvId,
        key: ExtensionKey,
        binding: ExtensionBinding,
        idempotency_key: IdempotencyKey,
    ) -> Result<(ExtensionBinding, u64), StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        let req = ExtensionKeyedPayload {
            key,
            binding: Some(binding),
        };
        let resp: BindingGenerationOutcome<ExtensionBinding> = self.send_mutation(
            env_id,
            reqwest::Method::PATCH,
            &format!(
                "environments/{}/extensions",
                encode_segment(env_id.as_str())
            ),
            Some(&idem_key),
            Some(&req),
        )?;
        Ok((resp.binding, resp.generation))
    }

    fn remove_extension_binding(
        &self,
        env_id: &EnvId,
        key: ExtensionKey,
        idempotency_key: IdempotencyKey,
    ) -> Result<(ExtensionBinding, u64), StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        let req = ExtensionKeyedPayload { key, binding: None };
        let resp: BindingGenerationOutcome<ExtensionBinding> = self.send_mutation(
            env_id,
            reqwest::Method::DELETE,
            &format!(
                "environments/{}/extensions",
                encode_segment(env_id.as_str())
            ),
            Some(&idem_key),
            Some(&req),
        )?;
        Ok((resp.binding, resp.generation))
    }

    fn rollback_extension_binding(
        &self,
        env_id: &EnvId,
        key: ExtensionKey,
        idempotency_key: IdempotencyKey,
    ) -> Result<(ExtensionBinding, u64), StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        let req = ExtensionKeyedPayload { key, binding: None };
        let resp: BindingGenerationOutcome<ExtensionBinding> = self.send_mutation(
            env_id,
            reqwest::Method::POST,
            &format!(
                "environments/{}/extensions/rollback",
                encode_segment(env_id.as_str())
            ),
            Some(&idem_key),
            Some(&req),
        )?;
        Ok((resp.binding, resp.generation))
    }

    fn set_traffic_split(
        &self,
        env_id: &EnvId,
        payload: SetTrafficSplitPayload,
        idempotency_key: IdempotencyKey,
    ) -> Result<ApplyTrafficSplitOutcome, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        self.send_mutation(
            env_id,
            reqwest::Method::POST,
            &self.env_path(env_id, "/traffic"),
            Some(&idem_key),
            Some(&payload),
        )
    }

    fn rollback_traffic_split(
        &self,
        env_id: &EnvId,
        deployment_id: DeploymentId,
        idempotency_key: IdempotencyKey,
    ) -> Result<RollbackTrafficSplitOutcome, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        let req = RollbackTrafficSplitPayload { deployment_id };
        self.send_mutation(
            env_id,
            reqwest::Method::POST,
            &format!(
                "environments/{}/traffic/rollback",
                encode_segment(env_id.as_str())
            ),
            Some(&idem_key),
            Some(&req),
        )
    }

    fn add_messaging_endpoint(
        &self,
        env_id: &EnvId,
        payload: AddMessagingEndpointPayload,
        idempotency_key: IdempotencyKey,
    ) -> Result<MessagingEndpoint, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        self.send_mutation(
            env_id,
            reqwest::Method::POST,
            &self.env_path(env_id, "/messaging"),
            Some(&idem_key),
            Some(&payload),
        )
    }

    fn link_messaging_bundle(
        &self,
        env_id: &EnvId,
        endpoint_id: MessagingEndpointId,
        bundle_id: BundleId,
        updated_by: String,
        idempotency_key: IdempotencyKey,
    ) -> Result<MessagingEndpoint, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        let req = MessagingBundleLinkPayload {
            bundle_id,
            updated_by,
        };
        self.send_mutation(
            env_id,
            reqwest::Method::POST,
            &format!(
                "environments/{}/messaging/{}/link",
                encode_segment(env_id.as_str()),
                encode_segment(&endpoint_id.to_string()),
            ),
            Some(&idem_key),
            Some(&req),
        )
    }

    fn unlink_messaging_bundle(
        &self,
        env_id: &EnvId,
        endpoint_id: MessagingEndpointId,
        bundle_id: BundleId,
        updated_by: String,
        idempotency_key: IdempotencyKey,
    ) -> Result<MessagingEndpoint, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        let req = MessagingBundleLinkPayload {
            bundle_id,
            updated_by,
        };
        self.send_mutation(
            env_id,
            reqwest::Method::POST,
            &format!(
                "environments/{}/messaging/{}/unlink",
                encode_segment(env_id.as_str()),
                encode_segment(&endpoint_id.to_string()),
            ),
            Some(&idem_key),
            Some(&req),
        )
    }

    fn set_messaging_welcome_flow(
        &self,
        env_id: &EnvId,
        payload: SetMessagingWelcomeFlowPayload,
        idempotency_key: IdempotencyKey,
    ) -> Result<MessagingEndpoint, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        let eid = payload.endpoint_id.to_string();
        self.send_mutation(
            env_id,
            reqwest::Method::POST,
            &format!(
                "environments/{}/messaging/{}/welcome-flow",
                encode_segment(env_id.as_str()),
                encode_segment(&eid),
            ),
            Some(&idem_key),
            Some(&payload),
        )
    }

    fn remove_messaging_endpoint(
        &self,
        env_id: &EnvId,
        endpoint_id: MessagingEndpointId,
    ) -> Result<MessagingEndpointId, StoreError> {
        let idem_key = mint_idempotency_key();
        self.send_mutation_no_body(
            env_id,
            reqwest::Method::DELETE,
            &format!(
                "environments/{}/messaging/{}",
                encode_segment(env_id.as_str()),
                encode_segment(&endpoint_id.to_string()),
            ),
            Some(&idem_key),
        )
    }

    fn rotate_messaging_webhook_secret(
        &self,
        env_id: &EnvId,
        endpoint_id: MessagingEndpointId,
        updated_by: String,
        idempotency_key: IdempotencyKey,
    ) -> Result<MessagingEndpoint, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        let req = RotateWebhookSecretPayload { updated_by };
        self.send_mutation(
            env_id,
            reqwest::Method::POST,
            &format!(
                "environments/{}/messaging/{}/rotate-secret",
                encode_segment(env_id.as_str()),
                encode_segment(&endpoint_id.to_string()),
            ),
            Some(&idem_key),
            Some(&req),
        )
    }

    fn bootstrap_trust_root(&self, env_id: &EnvId) -> Result<TrustRootSeed, StoreError> {
        let idem_key = mint_idempotency_key();
        self.send_mutation_no_body(
            env_id,
            reqwest::Method::POST,
            &self.env_path(env_id, "/trust-root/bootstrap"),
            Some(&idem_key),
        )
    }

    fn seed_trust_root_if_absent(
        &self,
        env_id: &EnvId,
    ) -> Result<Option<TrustRootSeed>, StoreError> {
        let idem_key = mint_idempotency_key();
        self.send_mutation_no_body(
            env_id,
            reqwest::Method::POST,
            &self.env_path(env_id, "/trust-root/seed"),
            Some(&idem_key),
        )
    }

    fn add_trusted_key(
        &self,
        env_id: &EnvId,
        key_id: String,
        public_key_pem: String,
        idempotency_key: IdempotencyKey,
    ) -> Result<TrustRootAddOutcome, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        let req = AddTrustedKeyPayload {
            key_id,
            public_key_pem,
        };
        self.send_mutation(
            env_id,
            reqwest::Method::POST,
            &self.env_path(env_id, "/trust-root/keys"),
            Some(&idem_key),
            Some(&req),
        )
    }

    fn remove_trusted_key(
        &self,
        env_id: &EnvId,
        key_id: String,
        idempotency_key: IdempotencyKey,
    ) -> Result<TrustRootRemoveOutcome, StoreError> {
        let idem_key = idempotency_key.as_str().to_string();
        self.send_mutation_no_body(
            env_id,
            reqwest::Method::DELETE,
            &format!(
                "environments/{}/trust-root/keys/{}",
                encode_segment(env_id.as_str()),
                encode_segment(&key_id),
            ),
            Some(&idem_key),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use greentic_deploy_spec::{PackId, RevisionLifecycle};
    use std::io::{BufRead, BufReader, Write};
    use std::net::{SocketAddr, TcpListener};
    use std::path::PathBuf;
    use std::sync::Arc;

    /// Minimal mock server: binds an ephemeral port, accepts one request,
    /// validates it with `check`, and responds with the given status + body.
    struct MockServer {
        addr: SocketAddr,
        _handle: std::thread::JoinHandle<()>,
    }

    type CheckFn = Arc<dyn Fn(&str, &str, &[u8]) + Send + Sync>;

    /// A mock that serves multiple sequential requests.
    fn start_mock(responses: Vec<(u16, &str)>, check: Option<CheckFn>) -> MockServer {
        let responses: Vec<(u16, String)> = responses
            .into_iter()
            .map(|(s, b)| (s, b.to_string()))
            .collect();
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();
        let handle = std::thread::spawn(move || {
            for (status, body) in responses {
                let (stream, _) = listener.accept().unwrap();
                // Use a single BufReader for both headers and body so buffered
                // bytes are not lost between the header scan and the body read.
                let mut reader = BufReader::new(stream);
                let mut lines: Vec<String> = Vec::new();
                loop {
                    let mut line = String::new();
                    reader.read_line(&mut line).unwrap();
                    let trimmed = line.trim_end_matches('\n').trim_end_matches('\r');
                    if trimmed.is_empty() {
                        break;
                    }
                    lines.push(trimmed.to_string());
                }
                // Read body if Content-Length present (same buffered reader).
                let content_length: usize = lines
                    .iter()
                    .find(|l| l.to_lowercase().starts_with("content-length:"))
                    .and_then(|l| l.split(':').nth(1))
                    .and_then(|v| v.trim().parse().ok())
                    .unwrap_or(0);
                let mut req_body = vec![0u8; content_length];
                if content_length > 0 {
                    std::io::Read::read_exact(&mut reader, &mut req_body).unwrap();
                }

                if let Some(ref check_fn) = check {
                    let request_line = lines.first().map(|s| s.as_str()).unwrap_or("");
                    let headers = lines[1..].join("\r\n");
                    check_fn(request_line, &headers, &req_body);
                }

                // Extract the Idempotency-Key header value from the request
                // and substitute `{{IDEMPOTENCY_KEY}}` in the response body
                // so happy-path tests echo the real sent key back in the
                // audit record (FIX 1 correlation).
                let body = if body.contains("{{IDEMPOTENCY_KEY}}") {
                    let idem_val = lines
                        .iter()
                        .find(|l| l.to_lowercase().starts_with("idempotency-key:"))
                        .and_then(|l| l.split_once(':').map(|(_, v)| v.trim().to_string()))
                        .unwrap_or_default();
                    body.replace("{{IDEMPOTENCY_KEY}}", &idem_val)
                } else {
                    body
                };

                let status_text = match status {
                    200 => "OK",
                    201 => "Created",
                    204 => "No Content",
                    400 => "Bad Request",
                    401 => "Unauthorized",
                    403 => "Forbidden",
                    404 => "Not Found",
                    409 => "Conflict",
                    422 => "Unprocessable Entity",
                    500 => "Internal Server Error",
                    501 => "Not Implemented",
                    _ => "Unknown",
                };
                // Recompute Content-Length after substitution.
                let response = format!(
                    "HTTP/1.1 {status} {status_text}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
                    body.len()
                );
                let stream_ref = reader.get_mut();
                stream_ref.write_all(response.as_bytes()).unwrap();
                stream_ref.flush().unwrap();
            }
        });
        MockServer {
            addr,
            _handle: handle,
        }
    }

    fn mock_store(addr: SocketAddr, auth: AuthMethod) -> HttpEnvironmentStore {
        HttpEnvironmentStore::new(Url::parse(&format!("http://{addr}")).unwrap(), auth).unwrap()
    }

    /// One-shot helper for the common no-auth single-response test shape:
    /// start a mock that returns `(status, body)` once and build a store
    /// pointed at it. Callers must hold the returned `MockServer` alive
    /// (binding it; dropping closes the listener mid-test).
    fn happy_store(status: u16, body: &str) -> (MockServer, HttpEnvironmentStore) {
        let mock = start_mock(vec![(status, body)], None);
        let store = mock_store(mock.addr, AuthMethod::None);
        (mock, store)
    }

    /// Wrap a domain-type JSON value in the A8 `MutationEnvelope` shape.
    fn wrap_mutation(domain: serde_json::Value) -> String {
        serde_json::json!({
            "result": domain,
            "etag": "sha256:test",
            "generation": 1,
            "idempotency": {"idempotency": "applied"},
            "audit": {
                "schema": "greentic.audit-event.v1",
                "event_id": "01TEST000000000000000000AA",
                "ts": "2026-06-09T12:00:00Z",
                "actor": {"kind": "operator"},
                "env_id": "local",
                "noun": "test",
                "verb": "test",
                "target": null,
                "authorization": {"decision": "allow", "policy": "local-only", "reason": "test"},
                "result": {"outcome": "ok"},
                "idempotency_key": "{{IDEMPOTENCY_KEY}}"
            }
        })
        .to_string()
    }

    fn env_id() -> EnvId {
        EnvId::try_from("local").unwrap()
    }

    fn idem() -> IdempotencyKey {
        IdempotencyKey::new("01JABC000000000000000000ZZ").unwrap()
    }

    /// A minimal valid `Environment` domain body for envelope-shape tests.
    fn sample_env_domain() -> serde_json::Value {
        serde_json::json!({
            "schema": "greentic.environment.v1",
            "environment_id": "local",
            "name": "test",
            "host_config": {"env_id": "local"},
            "packs": [],
            "bundles": [],
            "revisions": [],
            "traffic_splits": [],
            "messaging_endpoints": [],
            "extensions": [],
            "revocation": {},
            "retention": {},
            "health": {}
        })
    }

    /// [`wrap_mutation`] variant with the envelope mutated by `tweak` after
    /// assembly — for the PR-4.0/F2 audit-invariant negative tests.
    fn wrap_mutation_tweaked(
        domain: serde_json::Value,
        tweak: impl FnOnce(&mut serde_json::Value),
    ) -> String {
        let mut envelope: serde_json::Value = serde_json::from_str(&wrap_mutation(domain)).unwrap();
        tweak(&mut envelope);
        envelope.to_string()
    }

    // -----------------------------------------------------------------------
    // PR-4.0 (F2): A8 §4 audit invariant on success envelopes
    // -----------------------------------------------------------------------

    /// Helper: unwrap a `CommittedAfterSave` wrapper and return the inner
    /// `Conflict` message string. Panics if the shape doesn't match.
    fn unwrap_committed_conflict(result: Result<Environment, StoreError>) -> String {
        match result {
            Err(StoreError::CommittedAfterSave(inner)) => match *inner {
                StoreError::Conflict(msg) => msg,
                other => panic!("expected inner Conflict, got {other:?}"),
            },
            other => panic!("expected CommittedAfterSave, got {other:?}"),
        }
    }

    /// Shared skeleton for the audit-invariant negative tests: serve a 200
    /// envelope mutated by `tweak`, run a mutation, and assert the
    /// `CommittedAfterSave`-wrapped violation message contains
    /// `expected_substr`.
    fn assert_audit_violation(tweak: impl FnOnce(&mut serde_json::Value), expected_substr: &str) {
        let body = wrap_mutation_tweaked(sample_env_domain(), tweak);
        let (_mock, store) = happy_store(200, &body);
        let result = store.update_environment(&env_id(), UpdateEnvironmentPayload::default());
        let msg = unwrap_committed_conflict(result);
        assert!(
            msg.contains(expected_substr),
            "expected `{expected_substr}` violation, got: {msg}"
        );
    }

    #[test]
    fn success_without_audit_is_rejected() {
        assert_audit_violation(
            |env| {
                env.as_object_mut().unwrap().remove("audit");
            },
            "missing the audit record",
        );
    }

    #[test]
    fn success_with_deny_audit_is_rejected() {
        assert_audit_violation(
            |env| {
                env["audit"]["authorization"] = serde_json::json!({
                    "decision": "deny", "policy": "rbac-v1", "reason": "nope"
                });
            },
            "deny audit decision",
        );
    }

    #[test]
    fn success_with_non_ok_audit_result_is_rejected() {
        assert_audit_violation(
            |env| {
                env["audit"]["result"] = serde_json::json!({
                    "outcome": "error", "kind": "store", "message": "boom"
                });
            },
            "non-ok audit result",
        );
    }

    #[test]
    fn success_with_mismatched_audit_env_is_rejected() {
        assert_audit_violation(
            |env| {
                env["audit"]["env_id"] = serde_json::json!("other-env");
            },
            "names env `other-env`",
        );
    }

    // FIX 1: audit idempotency key correlation tests

    #[test]
    fn success_with_wrong_audit_idempotency_key_is_rejected() {
        assert_audit_violation(
            |env| {
                // Set audit key to a fixed wrong value (not the placeholder).
                env["audit"]["idempotency_key"] = serde_json::json!("01WRONG00000000000000000XX");
            },
            "does not belong",
        );
    }

    #[test]
    fn success_with_missing_audit_idempotency_key_is_rejected() {
        assert_audit_violation(
            |env| {
                env["audit"]
                    .as_object_mut()
                    .unwrap()
                    .remove("idempotency_key");
            },
            "does not belong",
        );
    }

    // FIX 2: post-2xx failures wrapped in CommittedAfterSave

    #[test]
    fn success_2xx_with_garbage_body_maps_to_committed_after_save() {
        // A 200 response with a non-JSON body: the mutation may already be
        // committed, so the error must be CommittedAfterSave.
        let (_mock, store) = happy_store(200, "this is not json");
        let result = store.update_environment(&env_id(), UpdateEnvironmentPayload::default());
        let msg = unwrap_committed_conflict(result);
        assert!(
            msg.contains("already be committed"),
            "expected committed guidance, got: {msg}"
        );
    }

    // FIX 3: A8 status/kind agreement tests

    #[test]
    fn error_500_with_unauthorized_body_is_contract_violation() {
        // HTTP 500 but the A8 body claims `unauthorized` (expects 403) — don't
        // trust either side.
        let err_body = serde_json::json!({
            "kind": "unauthorized",
            "policy": "rbac-v1",
            "reason": "nope"
        });
        let (_mock, store) = happy_store(500, &err_body.to_string());
        let result = store.update_environment(&env_id(), UpdateEnvironmentPayload::default());
        match result {
            Err(StoreError::Conflict(msg)) => {
                assert!(
                    msg.contains("contradicts"),
                    "expected contract violation, got: {msg}"
                );
            }
            other => panic!("expected Conflict(contradicts...), got {other:?}"),
        }
    }

    #[test]
    fn error_401_with_unauthorized_body_maps_to_unauthorized() {
        // HTTP 401 with an A8 `unauthorized`-kind body: the 401 allowance
        // means we trust the body.
        let err_body = serde_json::json!({
            "kind": "unauthorized",
            "policy": "rbac-v1",
            "reason": "expired token"
        });
        let (_mock, store) = happy_store(401, &err_body.to_string());
        let result = store.update_environment(&env_id(), UpdateEnvironmentPayload::default());
        match result {
            Err(StoreError::Unauthorized { policy, reason }) => {
                assert_eq!(policy, "rbac-v1");
                assert_eq!(reason, "expired token");
            }
            other => panic!("expected Unauthorized, got {other:?}"),
        }
    }

    // -----------------------------------------------------------------------
    // Environment lifecycle
    // -----------------------------------------------------------------------

    #[test]
    fn create_environment_happy_path() {
        let domain = serde_json::json!({
            "schema": "greentic.environment.v1",
            "environment_id": "local",
            "name": "test",
            "host_config": {"env_id": "local"},
            "packs": [],
            "bundles": [],
            "revisions": [],
            "traffic_splits": [],
            "messaging_endpoints": [],
            "extensions": [],
            "revocation": {},
            "retention": {},
            "health": {}
        });
        let body = wrap_mutation(domain);
        let (_mock, store) = happy_store(201, &body);
        let result = store.create_environment(
            &env_id(),
            "test".to_string(),
            EnvironmentHostConfig {
                env_id: env_id(),
                region: None,
                tenant_org_id: None,
                listen_addr: None,
                public_base_url: None,
            },
        );
        assert!(result.is_ok());
        assert_eq!(result.unwrap().name, "test");
    }

    #[test]
    fn create_environment_conflict_returns_conflict() {
        let err_body = serde_json::json!({
            "kind": "idempotency-conflict",
            "reason": "environment already exists"
        });
        let (_mock, store) = happy_store(409, &err_body.to_string());
        let result = store.create_environment(
            &env_id(),
            "test".to_string(),
            EnvironmentHostConfig {
                env_id: env_id(),
                region: None,
                tenant_org_id: None,
                listen_addr: None,
                public_base_url: None,
            },
        );
        assert!(matches!(result, Err(StoreError::Conflict(_))));
    }

    #[test]
    fn update_environment_happy_path() {
        let domain = serde_json::json!({
            "schema": "greentic.environment.v1",
            "environment_id": "local",
            "name": "updated",
            "host_config": {"env_id": "local"},
            "packs": [],
            "bundles": [],
            "revisions": [],
            "traffic_splits": [],
            "messaging_endpoints": [],
            "extensions": [],
            "revocation": {},
            "retention": {},
            "health": {}
        });
        let body = wrap_mutation(domain);
        let (_mock, store) = happy_store(200, &body);
        let result = store.update_environment(
            &env_id(),
            UpdateEnvironmentPayload {
                name: Some("updated".to_string()),
                ..Default::default()
            },
        );
        assert!(result.is_ok());
        assert_eq!(result.unwrap().name, "updated");
    }

    #[test]
    fn update_environment_not_found() {
        let err_body = serde_json::json!({"kind": "not-found"});
        let (_mock, store) = happy_store(404, &err_body.to_string());
        let result = store.update_environment(&env_id(), UpdateEnvironmentPayload::default());
        assert!(matches!(result, Err(StoreError::NotFound(_))));
    }

    // -----------------------------------------------------------------------
    // Migration
    // -----------------------------------------------------------------------

    #[test]
    fn migrate_merge_bindings_happy_path() {
        let domain = serde_json::json!({
            "merged_slots": ["messaging"],
            "merged_extensions": ["capability/memory/long-term"]
        });
        let body = wrap_mutation(domain);
        let (_mock, store) = happy_store(200, &body);
        let result = store.migrate_merge_bindings(
            &env_id(),
            MigrateMergePayload {
                packs: Vec::new(),
                extensions: Vec::new(),
                seed_if_missing: None,
            },
        );
        assert!(result.is_ok());
        let (slots, exts) = result.unwrap();
        assert_eq!(slots, vec!["messaging"]);
        assert_eq!(exts, vec!["capability/memory/long-term"]);
    }

    // -----------------------------------------------------------------------
    // Revision lifecycle
    // -----------------------------------------------------------------------

    fn sample_revision_response() -> String {
        wrap_mutation(serde_json::json!({
            "schema": "greentic.revision.v1",
            "revision_id": "01JTKW5B4W4Q5Y1CQW93F7S5VH",
            "env_id": "local",
            "bundle_id": "fast2flow",
            "deployment_id": "01JTKW5B4W4Q5Y1CQW93F7S5VH",
            "sequence": 1,
            "created_at": "2026-06-09T12:00:00Z",
            "bundle_digest": "sha256:00",
            "pack_list": [],
            "pack_list_lock_ref": "",
            "pack_config_refs": [],
            "config_digest": "sha256:00",
            "signature_sidecar_ref": "rev.sig",
            "lifecycle": "staged",
            "staged_at": "2026-06-09T12:00:00Z",
            "drain_seconds": 30,
            "abort_metrics": []
        }))
    }

    #[test]
    fn stage_revision_happy_path() {
        let body = sample_revision_response();
        let (_mock, store) = happy_store(201, &body);
        let result = store.stage_revision(
            &env_id(),
            StageRevisionPayload {
                revision_id: RevisionId::new(),
                deployment_id: DeploymentId::new(),
                bundle_digest: "sha256:00".to_string(),
                pack_list: Vec::new(),
                pack_list_lock_ref: PathBuf::new(),
                pack_config_refs: Vec::new(),
                config_digest: "sha256:00".to_string(),
                signature_sidecar_ref: PathBuf::from("rev.sig"),
                drain_seconds: 30,
            },
            idem(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn stage_revision_422_returns_invalid_argument() {
        let err_body = serde_json::json!({
            "kind": "integrity-mismatch",
            "expected": "abc",
            "actual": "def"
        });
        let (_mock, store) = happy_store(422, &err_body.to_string());
        let result = store.stage_revision(
            &env_id(),
            StageRevisionPayload {
                revision_id: RevisionId::new(),
                deployment_id: DeploymentId::new(),
                bundle_digest: "sha256:00".to_string(),
                pack_list: Vec::new(),
                pack_list_lock_ref: PathBuf::new(),
                pack_config_refs: Vec::new(),
                config_digest: "sha256:00".to_string(),
                signature_sidecar_ref: PathBuf::from("rev.sig"),
                drain_seconds: 30,
            },
            idem(),
        );
        assert!(matches!(result, Err(StoreError::InvalidArgument(_))));
    }

    fn sample_transition_response(lifecycle: &str) -> String {
        wrap_mutation(serde_json::json!({
            "revision": {
                "schema": "greentic.revision.v1",
                "revision_id": "01JTKW5B4W4Q5Y1CQW93F7S5VH",
                "env_id": "local",
                "bundle_id": "fast2flow",
                "deployment_id": "01JTKW5B4W4Q5Y1CQW93F7S5VH",
                "sequence": 1,
                "created_at": "2026-06-09T12:00:00Z",
                "bundle_digest": "sha256:00",
                "pack_list": [],
                "pack_list_lock_ref": "",
                "pack_config_refs": [],
                "config_digest": "sha256:00",
                "signature_sidecar_ref": "rev.sig",
                "lifecycle": lifecycle,
                "staged_at": "2026-06-09T12:00:00Z",
                "drain_seconds": 30,
                "abort_metrics": []
            },
            "environment": {
                "schema": "greentic.environment.v1",
                "environment_id": "local",
                "name": "test",
                "host_config": {"env_id": "local"},
                "packs": [],
                "bundles": [],
                "revisions": [],
                "traffic_splits": [],
                "messaging_endpoints": [],
                "extensions": [],
                "revocation": {},
                "retention": {},
                "health": {}
            },
            "starting_lifecycle": "staged"
        }))
    }

    #[test]
    fn warm_revision_happy_path() {
        let body = sample_transition_response("ready");
        let (_mock, store) = happy_store(200, &body);
        let result = store.warm_revision(
            &env_id(),
            WarmRevisionPayload {
                revision_id: RevisionId::new(),
                health_gate: Ok(()),
                expected_lifecycle: RevisionLifecycle::Staged,
            },
            idem(),
        );
        assert!(result.is_ok());
        let outcome = result.unwrap();
        assert_eq!(outcome.revision.lifecycle, RevisionLifecycle::Ready);
        assert_eq!(outcome.starting_lifecycle, RevisionLifecycle::Staged);
    }

    #[test]
    fn drain_revision_happy_path() {
        let body = sample_transition_response("draining");
        let (_mock, store) = happy_store(200, &body);
        let result = store.drain_revision(&env_id(), RevisionId::new(), idem());
        assert!(result.is_ok());
    }

    #[test]
    fn archive_revision_happy_path() {
        let body = sample_transition_response("archived");
        let (_mock, store) = happy_store(200, &body);
        let result = store.archive_revision(&env_id(), RevisionId::new(), idem());
        assert!(result.is_ok());
    }

    // -----------------------------------------------------------------------
    // Bundle CRUD
    // -----------------------------------------------------------------------

    fn sample_bundle_deployment() -> String {
        wrap_mutation(serde_json::json!({
            "schema": "greentic.bundle-deployment.v1",
            "deployment_id": "01JTKW5B4W4Q5Y1CQW93F7S5VH",
            "env_id": "local",
            "bundle_id": "fast2flow",
            "customer_id": "local-dev",
            "status": "active",
            "current_revisions": [],
            "route_binding": {
                "hosts": ["fast2flow.local"],
                "path_prefixes": [],
                "tenant_selector": {"tenant": "default", "team": "default"}
            },
            "revenue_share": [{"party_id": "greentic", "basis_points": 10000}],
            "revenue_policy_ref": "revenue.json",
            "created_at": "2026-06-09T12:00:00Z",
            "authorization_ref": "auth.json",
            "config_overrides": {}
        }))
    }

    #[test]
    fn add_bundle_happy_path() {
        let body = sample_bundle_deployment();
        let (_mock, store) = happy_store(201, &body);
        let result = store.add_bundle(
            &env_id(),
            AddBundlePayload {
                bundle_id: BundleId::new("fast2flow"),
                customer_id: greentic_deploy_spec::CustomerId::new("local-dev"),
                revenue_share: Vec::new(),
                route_binding: None,
                authorization_ref: None,
                config_overrides: std::collections::BTreeMap::new(),
            },
            idem(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn update_bundle_happy_path() {
        let body = sample_bundle_deployment();
        let (_mock, store) = happy_store(200, &body);
        let result = store.update_bundle(
            &env_id(),
            UpdateBundlePayload {
                deployment_id: DeploymentId::new(),
                status: Some(greentic_deploy_spec::BundleDeploymentStatus::Active),
                route_binding: None,
                revenue_share: None,
                config_overrides: None,
            },
            idem(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn remove_bundle_happy_path() {
        let domain = serde_json::json!({
            "deployment": {
                "schema": "greentic.bundle-deployment.v1",
                "deployment_id": "01JTKW5B4W4Q5Y1CQW93F7S5VH",
                "env_id": "local",
                "bundle_id": "fast2flow",
                "customer_id": "local-dev",
                "status": "active",
                "current_revisions": [],
                "route_binding": {
                    "hosts": ["fast2flow.local"],
                    "path_prefixes": [],
                    "tenant_selector": {"tenant": "default", "team": "default"}
                },
                "revenue_share": [{"party_id": "greentic", "basis_points": 10000}],
                "revenue_policy_ref": "revenue.json",
                "created_at": "2026-06-09T12:00:00Z",
                "authorization_ref": "auth.json",
                "config_overrides": {}
            },
            "pruned_revision_ids": []
        });
        let body = wrap_mutation(domain);
        let (_mock, store) = happy_store(200, &body);
        let result = store.remove_bundle(&env_id(), DeploymentId::new(), idem());
        assert!(result.is_ok());
        assert!(result.unwrap().pruned_revision_ids.is_empty());
    }

    // -----------------------------------------------------------------------
    // Pack binding CRUD
    // -----------------------------------------------------------------------

    fn sample_pack_binding_json() -> String {
        serde_json::json!({
            "slot": "messaging",
            "kind": "greentic.messaging@0.5.0",
            "pack_ref": "greentic-messaging",
            "generation": 1
        })
        .to_string()
    }

    fn sample_pack_binding() -> String {
        wrap_mutation(serde_json::from_str(&sample_pack_binding_json()).unwrap())
    }

    fn sample_binding_generation_response(binding_json: &str, generation: u64) -> String {
        let domain: serde_json::Value = serde_json::from_str(&format!(
            r#"{{"binding": {binding_json}, "generation": {generation}}}"#
        ))
        .unwrap();
        wrap_mutation(domain)
    }

    #[test]
    fn add_pack_binding_happy_path() {
        let body = sample_pack_binding();
        let (_mock, store) = happy_store(201, &body);
        let binding: EnvPackBinding = serde_json::from_str(&sample_pack_binding_json()).unwrap();
        let result = store.add_pack_binding(&env_id(), binding, idem());
        assert!(result.is_ok());
    }

    #[test]
    fn update_pack_binding_happy_path() {
        let binding_json = sample_pack_binding_json();
        let body = sample_binding_generation_response(&binding_json, 2);
        let (_mock, store) = happy_store(200, &body);
        let binding: EnvPackBinding = serde_json::from_str(&binding_json).unwrap();
        let result =
            store.update_pack_binding(&env_id(), CapabilitySlot::Messaging, binding, idem());
        assert!(result.is_ok());
        let (_, generation) = result.unwrap();
        assert_eq!(generation, 2);
    }

    #[test]
    fn remove_pack_binding_happy_path() {
        let binding_json = sample_pack_binding_json();
        let body = sample_binding_generation_response(&binding_json, 3);
        let (_mock, store) = happy_store(200, &body);
        let result = store.remove_pack_binding(&env_id(), CapabilitySlot::Messaging, idem());
        assert!(result.is_ok());
    }

    #[test]
    fn rollback_pack_binding_happy_path() {
        let binding_json = sample_pack_binding_json();
        let body = sample_binding_generation_response(&binding_json, 4);
        let (_mock, store) = happy_store(200, &body);
        let result = store.rollback_pack_binding(&env_id(), CapabilitySlot::Messaging, idem());
        assert!(result.is_ok());
    }

    // -----------------------------------------------------------------------
    // Extension binding CRUD
    // -----------------------------------------------------------------------

    fn sample_extension_binding_json() -> String {
        serde_json::json!({
            "kind": "greentic.memory-chronicle@0.1.0",
            "pack_ref": "greentic-chronicle",
            "generation": 1
        })
        .to_string()
    }

    fn sample_extension_binding() -> String {
        wrap_mutation(serde_json::from_str(&sample_extension_binding_json()).unwrap())
    }

    #[test]
    fn add_extension_binding_happy_path() {
        let body = sample_extension_binding();
        let (_mock, store) = happy_store(201, &body);
        let binding: ExtensionBinding =
            serde_json::from_str(&sample_extension_binding_json()).unwrap();
        let result = store.add_extension_binding(&env_id(), binding, idem());
        assert!(result.is_ok());
    }

    #[test]
    fn update_extension_binding_happy_path() {
        let ext_json = sample_extension_binding_json();
        let body = sample_binding_generation_response(&ext_json, 2);
        let (_mock, store) = happy_store(200, &body);
        let binding: ExtensionBinding = serde_json::from_str(&ext_json).unwrap();
        let key = ExtensionKey::new("capability/memory/long-term", None);
        let result = store.update_extension_binding(&env_id(), key, binding, idem());
        assert!(result.is_ok());
        let (_, generation) = result.unwrap();
        assert_eq!(generation, 2);
    }

    #[test]
    fn remove_extension_binding_happy_path() {
        let ext_json = sample_extension_binding_json();
        let body = sample_binding_generation_response(&ext_json, 3);
        let (_mock, store) = happy_store(200, &body);
        let key = ExtensionKey::new("capability/memory/long-term", None);
        let result = store.remove_extension_binding(&env_id(), key, idem());
        assert!(result.is_ok());
    }

    #[test]
    fn rollback_extension_binding_happy_path() {
        let ext_json = sample_extension_binding_json();
        let body = sample_binding_generation_response(&ext_json, 4);
        let (_mock, store) = happy_store(200, &body);
        let key = ExtensionKey::new("capability/memory/long-term", None);
        let result = store.rollback_extension_binding(&env_id(), key, idem());
        assert!(result.is_ok());
    }

    // -----------------------------------------------------------------------
    // Traffic
    // -----------------------------------------------------------------------

    fn sample_traffic_split() -> serde_json::Value {
        serde_json::json!({
            "schema": "greentic.traffic-split.v1",
            "env_id": "local",
            "deployment_id": "01JTKW5B4W4Q5Y1CQW93F7S5VH",
            "bundle_id": "fast2flow",
            "generation": 2,
            "entries": [],
            "updated_at": "2026-06-09T12:00:00Z",
            "updated_by": "tester",
            "idempotency_key": "01JABC000000000000000000ZZ",
            "authorization_ref": "auth.json"
        })
    }

    #[test]
    fn set_traffic_split_happy_path() {
        let domain = serde_json::json!({
            "split": sample_traffic_split(),
            "previous_generation": 1,
            "new_generation": 2,
            "environment": sample_env_domain()
        });
        let body = wrap_mutation(domain);
        let (_mock, store) = happy_store(200, &body);
        let result = store.set_traffic_split(
            &env_id(),
            SetTrafficSplitPayload {
                deployment_id: DeploymentId::new(),
                entries: Vec::new(),
                updated_by: "tester".to_string(),
                authorization_ref: None,
            },
            idem(),
        );
        assert!(result.is_ok());
        let outcome = result.unwrap();
        assert_eq!(outcome.previous_generation, Some(1));
        assert_eq!(outcome.new_generation, Some(2));
        assert_eq!(outcome.environment.environment_id, env_id());
    }

    #[test]
    fn rollback_traffic_split_happy_path() {
        let domain = serde_json::json!({
            "restored": sample_traffic_split(),
            "previous_generation": 2,
            "new_generation": 3,
            "environment": sample_env_domain()
        });
        let body = wrap_mutation(domain);
        let (_mock, store) = happy_store(200, &body);
        let result = store.rollback_traffic_split(&env_id(), DeploymentId::new(), idem());
        assert!(result.is_ok());
    }

    // -----------------------------------------------------------------------
    // Messaging endpoints
    // -----------------------------------------------------------------------

    fn sample_messaging_endpoint() -> String {
        wrap_mutation(serde_json::json!({
            "schema": "greentic.messaging-endpoint.v1",
            "env_id": "local",
            "endpoint_id": "01JTKW5B4W4Q5Y1CQW93F7S5VH",
            "provider_id": "tg-bot",
            "provider_type": "telegram",
            "display_name": "Telegram Bot",
            "secret_refs": [],
            "linked_bundles": [],
            "generation": 0,
            "created_at": "2026-06-09T12:00:00Z",
            "updated_at": "2026-06-09T12:00:00Z",
            "updated_by": "tester"
        }))
    }

    #[test]
    fn add_messaging_endpoint_happy_path() {
        let body = sample_messaging_endpoint();
        let (_mock, store) = happy_store(201, &body);
        let result = store.add_messaging_endpoint(
            &env_id(),
            AddMessagingEndpointPayload {
                provider_id: "tg-bot".to_string(),
                provider_type: "telegram".to_string(),
                display_name: "Telegram Bot".to_string(),
                secret_refs: Vec::new(),
                updated_by: "tester".to_string(),
            },
            idem(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn link_messaging_bundle_happy_path() {
        let body = sample_messaging_endpoint();
        let (_mock, store) = happy_store(200, &body);
        let result = store.link_messaging_bundle(
            &env_id(),
            MessagingEndpointId::new(),
            BundleId::new("fast2flow"),
            "tester".to_string(),
            idem(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn unlink_messaging_bundle_happy_path() {
        let body = sample_messaging_endpoint();
        let (_mock, store) = happy_store(200, &body);
        let result = store.unlink_messaging_bundle(
            &env_id(),
            MessagingEndpointId::new(),
            BundleId::new("fast2flow"),
            "tester".to_string(),
            idem(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn set_messaging_welcome_flow_happy_path() {
        let body = sample_messaging_endpoint();
        let (_mock, store) = happy_store(200, &body);
        let result = store.set_messaging_welcome_flow(
            &env_id(),
            SetMessagingWelcomeFlowPayload {
                endpoint_id: MessagingEndpointId::new(),
                bundle_id: BundleId::new("fast2flow"),
                pack_id: PackId::new("greentic-messaging"),
                flow_id: "welcome".to_string(),
                updated_by: "tester".to_string(),
            },
            idem(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn remove_messaging_endpoint_happy_path() {
        let eid = MessagingEndpointId::new();
        let body = wrap_mutation(serde_json::json!(eid.to_string()));
        let (_mock, store) = happy_store(200, &body);
        let result = store.remove_messaging_endpoint(&env_id(), eid);
        assert!(result.is_ok());
    }

    #[test]
    fn rotate_messaging_webhook_secret_happy_path() {
        let body = sample_messaging_endpoint();
        let (_mock, store) = happy_store(200, &body);
        let result = store.rotate_messaging_webhook_secret(
            &env_id(),
            MessagingEndpointId::new(),
            "tester".to_string(),
            idem(),
        );
        assert!(result.is_ok());
    }

    // -----------------------------------------------------------------------
    // Trust root
    // -----------------------------------------------------------------------

    fn sample_trust_root_seed() -> String {
        wrap_mutation(serde_json::json!({
            "key_id": "op-key-1",
            "public_key_pem": "-----BEGIN PUBLIC KEY-----\nMFkw...\n-----END PUBLIC KEY-----",
            "trusted_key_count": 1
        }))
    }

    #[test]
    fn bootstrap_trust_root_happy_path() {
        let body = sample_trust_root_seed();
        let (_mock, store) = happy_store(201, &body);
        let result = store.bootstrap_trust_root(&env_id());
        assert!(result.is_ok());
        let seed = result.unwrap();
        assert_eq!(seed.key_id, "op-key-1");
        assert_eq!(seed.trusted_key_count, 1);
    }

    #[test]
    fn seed_trust_root_if_absent_when_seeded() {
        let body = sample_trust_root_seed();
        let (_mock, store) = happy_store(200, &body);
        let result = store.seed_trust_root_if_absent(&env_id());
        assert!(result.is_ok());
        assert!(result.unwrap().is_some());
    }

    #[test]
    fn seed_trust_root_if_absent_when_already_exists() {
        let body = wrap_mutation(serde_json::Value::Null);
        let (_mock, store) = happy_store(200, &body);
        let result = store.seed_trust_root_if_absent(&env_id());
        assert!(result.is_ok());
        assert!(result.unwrap().is_none());
    }

    #[test]
    fn add_trusted_key_happy_path() {
        let domain = serde_json::json!({
            "added_key_id": "external-key-1",
            "trusted_key_count": 2
        });
        let body = wrap_mutation(domain);
        let (_mock, store) = happy_store(201, &body);
        let result = store.add_trusted_key(
            &env_id(),
            "external-key-1".to_string(),
            "PEM-DATA".to_string(),
            idem(),
        );
        assert!(result.is_ok());
        let outcome = result.unwrap();
        assert_eq!(outcome.added_key_id, "external-key-1");
        assert_eq!(outcome.trusted_key_count, 2);
    }

    #[test]
    fn remove_trusted_key_happy_path() {
        let domain = serde_json::json!({
            "removed_key_id": "external-key-1",
            "removed_public_key_pem": "PEM-DATA",
            "trusted_key_count": 1
        });
        let body = wrap_mutation(domain);
        let (_mock, store) = happy_store(200, &body);
        let result = store.remove_trusted_key(&env_id(), "external-key-1".to_string(), idem());
        assert!(result.is_ok());
        let outcome = result.unwrap();
        assert_eq!(outcome.removed_key_id, "external-key-1");
        assert_eq!(outcome.removed_public_key_pem, Some("PEM-DATA".to_string()));
    }

    // -----------------------------------------------------------------------
    // Auth + header tests
    // -----------------------------------------------------------------------

    #[test]
    fn bearer_auth_sends_authorization_header() {
        let body = sample_trust_root_seed();
        let check = Arc::new(|_req_line: &str, headers: &str, _body: &[u8]| {
            assert!(
                headers.contains("Authorization: Bearer my-secret-token"),
                "expected Bearer header in: {headers}"
            );
        });
        let mock = start_mock(vec![(200, &body)], Some(check));
        let store = mock_store(mock.addr, AuthMethod::Bearer("my-secret-token".to_string()));
        let _ = store.bootstrap_trust_root(&env_id());
    }

    #[test]
    fn idempotency_key_header_is_sent() {
        let body = sample_revision_response();
        let check = Arc::new(|_req_line: &str, headers: &str, _body: &[u8]| {
            assert!(
                headers.contains("Idempotency-Key: 01JABC000000000000000000ZZ"),
                "expected Idempotency-Key header in: {headers}"
            );
        });
        let mock = start_mock(vec![(201, &body)], Some(check));
        let store = mock_store(mock.addr, AuthMethod::None);
        let _ = store.stage_revision(
            &env_id(),
            StageRevisionPayload {
                revision_id: RevisionId::new(),
                deployment_id: DeploymentId::new(),
                bundle_digest: "sha256:00".to_string(),
                pack_list: Vec::new(),
                pack_list_lock_ref: PathBuf::new(),
                pack_config_refs: Vec::new(),
                config_digest: "sha256:00".to_string(),
                signature_sidecar_ref: PathBuf::from("rev.sig"),
                drain_seconds: 30,
            },
            idem(),
        );
    }

    // -----------------------------------------------------------------------
    // Error mapping tests
    // -----------------------------------------------------------------------

    #[test]
    fn error_404_maps_to_not_found() {
        let err_body = serde_json::json!({"kind": "not-found"});
        let (_mock, store) = happy_store(404, &err_body.to_string());
        let result = store.update_environment(&env_id(), UpdateEnvironmentPayload::default());
        assert!(matches!(result, Err(StoreError::NotFound(_))));
    }

    #[test]
    fn error_409_maps_to_conflict() {
        let err_body = serde_json::json!({
            "kind": "idempotency-conflict",
            "reason": "key reused"
        });
        let (_mock, store) = happy_store(409, &err_body.to_string());
        let result = store.update_environment(&env_id(), UpdateEnvironmentPayload::default());
        assert!(matches!(result, Err(StoreError::Conflict(_))));
    }

    #[test]
    fn error_500_maps_to_conflict_server() {
        let err_body = serde_json::json!({
            "kind": "internal",
            "message": "disk full"
        });
        let (_mock, store) = happy_store(500, &err_body.to_string());
        let result = store.update_environment(&env_id(), UpdateEnvironmentPayload::default());
        match result {
            Err(StoreError::Conflict(msg)) => {
                assert!(
                    msg.contains("server:"),
                    "expected 'server:' prefix, got: {msg}"
                );
            }
            other => panic!("expected Conflict, got {other:?}"),
        }
    }

    #[test]
    fn error_403_maps_to_unauthorized() {
        // PR-4.0 (F4): an A8 `unauthorized` body keeps its typed noun —
        // previously it was flattened into `StoreError::Conflict`, so RBAC
        // denials rendered as `error.kind: conflict` in the CLI envelope.
        let err_body = serde_json::json!({
            "kind": "unauthorized",
            "policy": "rbac-v1",
            "reason": "insufficient permissions"
        });
        let (_mock, store) = happy_store(403, &err_body.to_string());
        let result = store.update_environment(&env_id(), UpdateEnvironmentPayload::default());
        match result {
            Err(StoreError::Unauthorized { policy, reason }) => {
                assert_eq!(policy, "rbac-v1");
                assert_eq!(reason, "insufficient permissions");
            }
            other => panic!("expected Unauthorized, got {other:?}"),
        }
    }

    #[test]
    fn error_403_without_a8_body_maps_to_unauthorized() {
        // A denial from a proxy/LB in front of the store (non-A8 body) is
        // still a denial — the fallback status mapping keeps the noun.
        let (_mock, store) = happy_store(403, "access denied by gateway");
        let result = store.update_environment(&env_id(), UpdateEnvironmentPayload::default());
        match result {
            Err(StoreError::Unauthorized { policy, reason }) => {
                assert_eq!(policy, "remote");
                assert_eq!(reason, "access denied by gateway");
            }
            other => panic!("expected Unauthorized, got {other:?}"),
        }
    }

    #[test]
    fn error_501_maps_to_not_yet_implemented() {
        let err_body = serde_json::json!({
            "kind": "not-yet-implemented",
            "detail": "backup/restore lands in PR-4"
        });
        let (_mock, store) = happy_store(501, &err_body.to_string());
        let result = store.update_environment(&env_id(), UpdateEnvironmentPayload::default());
        match result {
            Err(StoreError::NotYetImplemented(detail)) => {
                assert_eq!(detail, "backup/restore lands in PR-4");
            }
            other => panic!("expected NotYetImplemented, got {other:?}"),
        }
    }

    #[test]
    fn transport_error_maps_to_conflict() {
        // Connect to a port that is definitely not listening.
        let store =
            HttpEnvironmentStore::new(Url::parse("http://127.0.0.1:1").unwrap(), AuthMethod::None)
                .unwrap();
        let result = store.update_environment(&env_id(), UpdateEnvironmentPayload::default());
        match result {
            Err(StoreError::Conflict(msg)) => {
                assert!(
                    msg.starts_with("transport:"),
                    "expected 'transport:' prefix, got: {msg}"
                );
            }
            other => panic!("expected Conflict(transport:...), got {other:?}"),
        }
    }

    // -----------------------------------------------------------------------
    // Fix 1: percent-encoding of dynamic path segments
    // -----------------------------------------------------------------------

    #[test]
    fn encode_segment_passes_through_alphanumeric() {
        assert_eq!(encode_segment("local"), "local");
        assert_eq!(
            encode_segment("01JTKW5B4W4Q5Y1CQW93F7S5VH"),
            "01JTKW5B4W4Q5Y1CQW93F7S5VH"
        );
    }

    #[test]
    fn encode_segment_escapes_slash() {
        let encoded = encode_segment("foo/bar");
        assert!(
            !encoded.contains('/'),
            "slash must be escaped, got: {encoded}"
        );
        assert!(encoded.contains("%2F"));
    }

    #[test]
    fn encode_segment_escapes_dot_dot() {
        let encoded = encode_segment("..");
        assert!(
            !encoded.contains(".."),
            "dots must be escaped, got: {encoded}"
        );
        assert!(encoded.contains("%2E"));
    }

    #[test]
    fn encode_segment_escapes_query() {
        let encoded = encode_segment("id?foo=bar");
        assert!(
            !encoded.contains('?'),
            "query marker must be escaped, got: {encoded}"
        );
    }

    #[test]
    fn encode_segment_escapes_fragment() {
        let encoded = encode_segment("id#frag");
        assert!(
            !encoded.contains('#'),
            "fragment marker must be escaped, got: {encoded}"
        );
    }

    #[test]
    fn key_id_with_slash_reaches_escaped_path() {
        let body = wrap_mutation(serde_json::json!({
            "removed_key_id": "a/b",
            "removed_public_key_pem": "PEM",
            "trusted_key_count": 0
        }));
        let check = Arc::new(|req_line: &str, _headers: &str, _body: &[u8]| {
            assert!(
                req_line.contains("a%2Fb"),
                "expected escaped slash in path, got: {req_line}"
            );
        });
        let mock = start_mock(vec![(200, &body)], Some(check));
        let store = mock_store(mock.addr, AuthMethod::None);
        let _ = store.remove_trusted_key(&env_id(), "a/b".to_string(), idem());
    }

    // -----------------------------------------------------------------------
    // Fix 2: MutationEnvelope parsing
    // -----------------------------------------------------------------------

    #[test]
    fn mutation_envelope_with_extra_metadata_is_accepted() {
        // The envelope carries etag/generation/audit alongside the result.
        // Verify that the client parses the envelope correctly.
        let domain = serde_json::json!({
            "schema": "greentic.environment.v1",
            "environment_id": "local",
            "name": "test-envelope",
            "host_config": {"env_id": "local"},
            "packs": [],
            "bundles": [],
            "revisions": [],
            "traffic_splits": [],
            "messaging_endpoints": [],
            "extensions": [],
            "revocation": {},
            "retention": {},
            "health": {}
        });
        let body = wrap_mutation(domain);
        let (_mock, store) = happy_store(201, &body);
        let result = store.create_environment(
            &env_id(),
            "test-envelope".to_string(),
            EnvironmentHostConfig {
                env_id: env_id(),
                region: None,
                tenant_org_id: None,
                listen_addr: None,
                public_base_url: None,
            },
        );
        assert!(result.is_ok());
        assert_eq!(result.unwrap().name, "test-envelope");
    }

    // -----------------------------------------------------------------------
    // Fix 4: idempotency-key minting for methods without payload key
    // -----------------------------------------------------------------------

    #[test]
    fn bootstrap_trust_root_sends_idempotency_key() {
        let body = sample_trust_root_seed();
        let check = Arc::new(|_req_line: &str, headers: &str, _body: &[u8]| {
            assert!(
                headers.contains("Idempotency-Key:"),
                "expected Idempotency-Key header in: {headers}"
            );
            // Extract the key value and verify it's non-empty.
            let key = headers
                .lines()
                .find(|l| l.starts_with("Idempotency-Key:"))
                .and_then(|l| l.split(':').nth(1))
                .map(|v| v.trim())
                .unwrap_or("");
            assert!(!key.is_empty(), "Idempotency-Key must be non-empty");
        });
        let mock = start_mock(vec![(201, &body)], Some(check));
        let store = mock_store(mock.addr, AuthMethod::None);
        let _ = store.bootstrap_trust_root(&env_id());
    }

    #[test]
    fn create_environment_sends_idempotency_key() {
        let domain = serde_json::json!({
            "schema": "greentic.environment.v1",
            "environment_id": "local",
            "name": "test",
            "host_config": {"env_id": "local"},
            "packs": [],
            "bundles": [],
            "revisions": [],
            "traffic_splits": [],
            "messaging_endpoints": [],
            "extensions": [],
            "revocation": {},
            "retention": {},
            "health": {}
        });
        let body = wrap_mutation(domain);
        let check = Arc::new(|_req_line: &str, headers: &str, _body: &[u8]| {
            assert!(
                headers.contains("Idempotency-Key:"),
                "expected Idempotency-Key header for create_environment: {headers}"
            );
        });
        let mock = start_mock(vec![(201, &body)], Some(check));
        let store = mock_store(mock.addr, AuthMethod::None);
        let _ = store.create_environment(
            &env_id(),
            "test".to_string(),
            EnvironmentHostConfig {
                env_id: env_id(),
                region: None,
                tenant_org_id: None,
                listen_addr: None,
                public_base_url: None,
            },
        );
    }

    #[test]
    fn minted_keys_are_unique_per_call() {
        let key1 = mint_idempotency_key();
        let key2 = mint_idempotency_key();
        assert_ne!(key1, key2, "minted keys must differ per call");
    }

    // -----------------------------------------------------------------------
    // Fix 5: insecure-transport guard
    // -----------------------------------------------------------------------

    #[test]
    fn bearer_over_http_non_loopback_is_rejected() {
        let result = HttpEnvironmentStore::new(
            Url::parse("http://192.0.2.1:8080").unwrap(),
            AuthMethod::Bearer("token".to_string()),
        );
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            matches!(err, ConstructionError::InsecureTransport(_)),
            "expected InsecureTransport, got: {err:?}"
        );
    }

    #[test]
    fn bearer_over_http_loopback_is_allowed() {
        let result = HttpEnvironmentStore::new(
            Url::parse("http://127.0.0.1:8080").unwrap(),
            AuthMethod::Bearer("token".to_string()),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn bearer_over_https_is_allowed() {
        let result = HttpEnvironmentStore::new(
            Url::parse("https://example.com").unwrap(),
            AuthMethod::Bearer("token".to_string()),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn no_auth_over_http_is_allowed() {
        let result = HttpEnvironmentStore::new(
            Url::parse("http://192.0.2.1:8080").unwrap(),
            AuthMethod::None,
        );
        assert!(result.is_ok());
    }

    // -----------------------------------------------------------------------
    // Object-safety compile guard (same as mutations.rs)
    // -----------------------------------------------------------------------

    #[allow(dead_code)]
    fn _http_store_is_trait_object(store: &HttpEnvironmentStore) {
        let _dyn: &dyn EnvironmentMutations = store;
    }
}