mockforge-cli 0.3.118

CLI interface for MockForge
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
use std::path::PathBuf;

#[cfg(feature = "chaos")]
use mockforge_chaos::api::create_chaos_api_router;
#[cfg(feature = "chaos")]
use mockforge_chaos::config::ChaosConfig;
use mockforge_core::encryption::init_key_store;
use mockforge_core::ServerConfig;
use mockforge_observability::prometheus::{prometheus_router, MetricsRegistry};
use mockforge_openapi::OpenApiSpec;
use std::any::Any;
use std::net::SocketAddr;
use tokio::net::TcpListener;

/// Arguments for building server configuration and running the serve command.
///
/// This struct consolidates the 60+ parameters that `handle_serve` previously accepted
/// as individual function arguments, improving readability and maintainability.
#[derive(Debug)]
pub(crate) struct ServeArgs {
    pub(crate) config_path: Option<PathBuf>,
    pub(crate) profile: Option<String>,
    pub(crate) http_port: Option<u16>,
    pub(crate) ws_port: Option<u16>,
    pub(crate) grpc_port: Option<u16>,
    pub(crate) tcp_port: Option<u16>,
    pub(crate) admin: bool,
    pub(crate) admin_port: Option<u16>,
    pub(crate) metrics: bool,
    pub(crate) metrics_port: Option<u16>,
    pub(crate) tracing: bool,
    pub(crate) tracing_service_name: String,
    pub(crate) tracing_environment: String,
    pub(crate) jaeger_endpoint: String,
    pub(crate) tracing_sampling_rate: f64,
    pub(crate) recorder: bool,
    pub(crate) recorder_db: String,
    pub(crate) recorder_no_api: bool,
    pub(crate) recorder_api_port: Option<u16>,
    pub(crate) recorder_max_requests: i64,
    pub(crate) recorder_retention_days: i64,
    pub(crate) chaos: bool,
    pub(crate) chaos_scenario: Option<String>,
    pub(crate) chaos_latency_ms: Option<u64>,
    pub(crate) chaos_latency_range: Option<String>,
    pub(crate) chaos_latency_probability: f64,
    pub(crate) chaos_http_errors: Option<String>,
    pub(crate) chaos_http_error_probability: f64,
    pub(crate) chaos_rate_limit: Option<u32>,
    pub(crate) chaos_bandwidth_limit: Option<u64>,
    pub(crate) chaos_packet_loss: Option<f64>,
    pub(crate) spec: Vec<PathBuf>,
    pub(crate) spec_dir: Option<PathBuf>,
    pub(crate) merge_conflicts: String,
    pub(crate) api_versioning: String,
    pub(crate) base_path: Option<String>,
    pub(crate) tls_enabled: bool,
    pub(crate) tls_cert: Option<PathBuf>,
    pub(crate) tls_key: Option<PathBuf>,
    pub(crate) tls_ca: Option<PathBuf>,
    pub(crate) tls_min_version: String,
    pub(crate) mtls: String,
    pub(crate) ws_replay_file: Option<PathBuf>,
    pub(crate) graphql: Option<PathBuf>,
    pub(crate) graphql_port: Option<u16>,
    pub(crate) graphql_upstream: Option<String>,
    pub(crate) traffic_shaping: bool,
    pub(crate) bandwidth_limit: u64,
    pub(crate) burst_size: u64,
    pub(crate) ai_enabled: bool,
    pub(crate) rag_provider: Option<String>,
    pub(crate) rag_model: Option<String>,
    pub(crate) rag_api_key: Option<String>,
    pub(crate) network_profile: Option<String>,
    pub(crate) chaos_random: bool,
    /// Random chaos: error injection rate (0.0-1.0)
    pub(crate) chaos_random_error_rate: f64,
    /// Random chaos: delay injection rate (0.0-1.0)
    pub(crate) chaos_random_delay_rate: f64,
    /// Random chaos: minimum delay in milliseconds
    pub(crate) chaos_random_min_delay: u64,
    /// Random chaos: maximum delay in milliseconds
    pub(crate) chaos_random_max_delay: u64,
    pub(crate) reality_level: Option<u8>,
    pub(crate) dry_run: bool,
    #[allow(dead_code)]
    pub(crate) progress: bool,
    #[allow(dead_code)]
    pub(crate) verbose: bool,
    /// Skip auto-discovery of mockforge config files from the current working
    /// directory and its ancestors. Useful for embedded/SDK usage where the
    /// host project may happen to contain a `mockforge.yaml` that would
    /// otherwise be picked up and override explicit flags.
    pub(crate) no_config: bool,
}

impl Default for ServeArgs {
    fn default() -> Self {
        Self {
            config_path: None,
            profile: None,
            http_port: None,
            ws_port: None,
            grpc_port: None,
            tcp_port: None,
            admin: false,
            admin_port: None,
            metrics: false,
            metrics_port: None,
            tracing: false,
            tracing_service_name: "mockforge".to_string(),
            tracing_environment: "development".to_string(),
            jaeger_endpoint: String::new(),
            tracing_sampling_rate: 1.0,
            recorder: false,
            recorder_db: String::new(),
            recorder_no_api: false,
            recorder_api_port: None,
            recorder_max_requests: 0,
            recorder_retention_days: 0,
            chaos: false,
            chaos_scenario: None,
            chaos_latency_ms: None,
            chaos_latency_range: None,
            chaos_latency_probability: 0.0,
            chaos_http_errors: None,
            chaos_http_error_probability: 0.0,
            chaos_rate_limit: None,
            chaos_bandwidth_limit: None,
            chaos_packet_loss: None,
            spec: Vec::new(),
            spec_dir: None,
            merge_conflicts: "overwrite".to_string(),
            api_versioning: "none".to_string(),
            base_path: None,
            tls_enabled: false,
            tls_cert: None,
            tls_key: None,
            tls_ca: None,
            tls_min_version: "1.2".to_string(),
            mtls: "off".to_string(),
            ws_replay_file: None,
            graphql: None,
            graphql_port: None,
            graphql_upstream: None,
            traffic_shaping: false,
            bandwidth_limit: 0,
            burst_size: 0,
            ai_enabled: false,
            rag_provider: None,
            rag_model: None,
            rag_api_key: None,
            network_profile: None,
            chaos_random: false,
            chaos_random_error_rate: 0.0,
            chaos_random_delay_rate: 0.0,
            chaos_random_min_delay: 0,
            chaos_random_max_delay: 0,
            reality_level: None,
            dry_run: false,
            progress: false,
            verbose: false,
            no_config: false,
        }
    }
}

/// Build ServerConfig from CLI arguments, config file, and environment variables
/// Precedence: CLI args > Env vars > Profile > Config file > Defaults
pub(crate) async fn build_server_config_from_cli(serve_args: &ServeArgs) -> ServerConfig {
    use mockforge_core::apply_env_overrides;
    use mockforge_core::config::{
        discover_config_file_all_formats, load_config_auto, load_config_with_profile,
    };

    // Step 1: Load config from file if provided, otherwise try to auto-discover, otherwise use defaults
    let mut config = if let Some(path) = &serve_args.config_path {
        println!("📄 Loading configuration from: {}", path.display());

        // Try auto-format detection (supports .ts, .js, .yaml, .yml, .json)
        match load_config_auto(path).await {
            Ok(cfg) => {
                // Apply profile if specified
                if let Some(profile_name) = &serve_args.profile {
                    match load_config_with_profile(path, Some(profile_name)).await {
                        Ok(cfg_with_profile) => {
                            println!("✅ Applied profile: {}", profile_name);
                            cfg_with_profile
                        }
                        Err(e) => {
                            eprintln!("⚠️  Failed to apply profile '{}': {}", profile_name, e);
                            eprintln!("   Using base configuration without profile");
                            cfg
                        }
                    }
                } else {
                    cfg
                }
            }
            Err(e) => {
                eprintln!("⚠️  Failed to load config file: {}", e);
                eprintln!("   Using default configuration");
                ServerConfig::default()
            }
        }
    } else if serve_args.no_config {
        // Caller (e.g. @mockforge-dev/sdk) opted out of auto-discovery so
        // local mockforge.yaml files can't silently change behavior.
        ServerConfig::default()
    } else {
        // Try to auto-discover config file (now supports all formats)
        match discover_config_file_all_formats().await {
            Ok(discovered_path) => {
                println!("📄 Auto-discovered configuration from: {}", discovered_path.display());

                match load_config_auto(&discovered_path).await {
                    Ok(cfg) => {
                        // Apply profile if specified
                        if let Some(profile_name) = &serve_args.profile {
                            match load_config_with_profile(&discovered_path, Some(profile_name))
                                .await
                            {
                                Ok(cfg_with_profile) => {
                                    println!("✅ Applied profile: {}", profile_name);
                                    cfg_with_profile
                                }
                                Err(e) => {
                                    eprintln!(
                                        "⚠️  Failed to apply profile '{}': {}",
                                        profile_name, e
                                    );
                                    eprintln!("   Using base configuration without profile");
                                    cfg
                                }
                            }
                        } else {
                            cfg
                        }
                    }
                    Err(e) => {
                        eprintln!("⚠️  Failed to load auto-discovered config: {}", e);
                        ServerConfig::default()
                    }
                }
            }
            Err(_) => {
                // No config file found
                if serve_args.profile.is_some() {
                    eprintln!("⚠️  Profile specified but no config file found");
                    eprintln!("   Using default configuration");
                }
                ServerConfig::default()
            }
        }
    };

    // Step 2: Apply environment variable overrides
    config = apply_env_overrides(config);

    // Step 3: Apply CLI argument overrides (CLI takes highest precedence)

    // HTTP configuration
    if let Some(http_port) = serve_args.http_port {
        config.http.port = http_port;
    }
    // Handle spec files - use first spec for backward compatibility with config
    // Full multi-spec handling will be done in HTTP server integration
    if let Some(spec_path) = serve_args.spec.first() {
        config.http.openapi_spec = Some(spec_path.to_string_lossy().to_string());
    }

    // WebSocket configuration
    if let Some(ws_port) = serve_args.ws_port {
        config.websocket.port = ws_port;
    }
    if let Some(replay_path) = &serve_args.ws_replay_file {
        config.websocket.replay_file = Some(replay_path.to_string_lossy().to_string());
    }

    // GraphQL configuration
    if let Some(graphql_port) = serve_args.graphql_port {
        config.graphql.port = graphql_port;
    }
    if let Some(schema_path) = &serve_args.graphql {
        config.graphql.schema_path = Some(schema_path.to_string_lossy().to_string());
    }
    if let Some(upstream_url) = &serve_args.graphql_upstream {
        config.graphql.upstream_url = Some(upstream_url.clone());
    }

    // gRPC configuration
    if let Some(grpc_port) = serve_args.grpc_port {
        config.grpc.port = grpc_port;
    }

    // TCP configuration
    if let Some(tcp_port) = serve_args.tcp_port {
        config.tcp.port = tcp_port;
    }

    // Protocol-specific configurations are handled by their respective modules
    // MQTT, Kafka, and AMQP ports are configured through their individual modules

    // Admin configuration
    if serve_args.admin {
        config.admin.enabled = true;
    }
    if let Some(admin_port) = serve_args.admin_port {
        config.admin.port = admin_port;
    }

    // Prometheus metrics configuration
    if serve_args.metrics {
        config.observability.prometheus.enabled = true;
    }
    if let Some(metrics_port) = serve_args.metrics_port {
        config.observability.prometheus.port = metrics_port;
    }

    // OpenTelemetry tracing configuration
    if serve_args.tracing {
        config.observability.opentelemetry = Some(mockforge_core::config::OpenTelemetryConfig {
            enabled: true,
            service_name: serve_args.tracing_service_name.clone(),
            environment: serve_args.tracing_environment.clone(),
            jaeger_endpoint: serve_args.jaeger_endpoint.clone(),
            otlp_endpoint: None,
            protocol: "grpc".to_string(),
            sampling_rate: serve_args.tracing_sampling_rate,
        });
    }

    // API Flight Recorder configuration
    if serve_args.recorder {
        config.observability.recorder = Some(mockforge_core::config::RecorderConfig {
            enabled: true,
            database_path: serve_args.recorder_db.clone(),
            api_enabled: !serve_args.recorder_no_api,
            api_port: serve_args.recorder_api_port,
            max_requests: serve_args.recorder_max_requests,
            retention_days: serve_args.recorder_retention_days,
            record_http: true,
            record_grpc: true,
            record_websocket: true,
            record_graphql: true,
            record_proxy: true,
        });
    }

    // Chaos engineering configuration
    if serve_args.chaos {
        let mut chaos_config = mockforge_core::config::ChaosEngConfig {
            enabled: true,
            scenario: serve_args.chaos_scenario.clone(),
            latency: None,
            fault_injection: None,
            rate_limit: None,
            traffic_shaping: None,
        };

        // Configure latency injection
        if serve_args.chaos_latency_ms.is_some() || serve_args.chaos_latency_range.is_some() {
            let random_delay_range_ms = serve_args.chaos_latency_range.as_ref().and_then(|range| {
                let parts: Vec<&str> = range.split('-').collect();
                if parts.len() == 2 {
                    let min = parts[0].parse::<u64>().ok()?;
                    let max = parts[1].parse::<u64>().ok()?;
                    Some((min, max))
                } else {
                    None
                }
            });

            chaos_config.latency = Some(mockforge_core::config::LatencyInjectionConfig {
                enabled: true,
                fixed_delay_ms: serve_args.chaos_latency_ms,
                random_delay_range_ms,
                jitter_percent: 0.0,
                probability: serve_args.chaos_latency_probability,
            });
        }

        // Configure fault injection
        if serve_args.chaos_http_errors.is_some() {
            let http_errors = serve_args
                .chaos_http_errors
                .as_ref()
                .map(|errors| {
                    errors.split(',').filter_map(|s| s.trim().parse::<u16>().ok()).collect()
                })
                .unwrap_or_default();

            chaos_config.fault_injection = Some(mockforge_core::config::FaultConfig {
                enabled: true,
                http_errors,
                http_error_probability: serve_args.chaos_http_error_probability,
                connection_errors: false,
                connection_error_probability: 0.0,
                timeout_errors: false,
                timeout_ms: 30000,
                timeout_probability: 0.0,
            });
        }

        // Configure rate limiting
        if let Some(rps) = serve_args.chaos_rate_limit {
            chaos_config.rate_limit = Some(mockforge_core::config::RateLimitingConfig {
                enabled: true,
                requests_per_second: rps,
                burst_size: rps * 2,
                per_ip: false,
                per_endpoint: false,
            });
        }

        // Configure traffic shaping
        if serve_args.chaos_bandwidth_limit.is_some() || serve_args.chaos_packet_loss.is_some() {
            chaos_config.traffic_shaping = Some(mockforge_core::config::NetworkShapingConfig {
                enabled: true,
                bandwidth_limit_bps: serve_args.chaos_bandwidth_limit.unwrap_or(1_000_000),
                packet_loss_percent: serve_args.chaos_packet_loss.unwrap_or(0.0),
                max_connections: 100,
            });
        }

        config.observability.chaos = Some(chaos_config);
    }

    // Traffic shaping configuration (core feature)
    if serve_args.traffic_shaping {
        config.core.traffic_shaping_enabled = true;
        config.core.traffic_shaping.bandwidth.enabled = true;
        config.core.traffic_shaping.bandwidth.max_bytes_per_sec = serve_args.bandwidth_limit;
        config.core.traffic_shaping.bandwidth.burst_capacity_bytes = serve_args.burst_size;
    }

    // AI/RAG configuration
    if serve_args.ai_enabled {
        config.data.rag.enabled = true;
        if let Some(provider) = &serve_args.rag_provider {
            config.data.rag.provider = provider.clone();
        }
        if let Some(model) = &serve_args.rag_model {
            config.data.rag.model = Some(model.clone());
        }
        if let Some(api_key) = &serve_args.rag_api_key {
            config.data.rag.api_key = Some(api_key.clone());
        }
    }

    // Reality level configuration
    if let Some(level_value) = serve_args.reality_level {
        if let Some(level) = mockforge_core::RealityLevel::from_value(level_value) {
            config.reality.level = level;
            config.reality.enabled = true;
            println!("🎚️  Reality level set to {} ({})", level.value(), level.name());

            // Apply reality configuration to subsystems
            let reality_engine = mockforge_core::RealityEngine::with_level(level);
            reality_engine.apply_to_config(&mut config).await;
        } else {
            eprintln!(
                "⚠️  Invalid reality level: {}. Must be between 1 and 5. Using default.",
                level_value
            );
        }
    } else if config.reality.enabled {
        // Apply reality configuration from config file if enabled
        let level = config.reality.level;
        let reality_engine = mockforge_core::RealityEngine::with_level(level);
        reality_engine.apply_to_config(&mut config).await;
    }

    config
}

pub(crate) fn ensure_ports_available(ports: &[(u16, &str)]) -> Result<(), String> {
    let mut unavailable_ports = Vec::new();

    for (port, name) in ports {
        match std::net::TcpListener::bind(("127.0.0.1", *port)) {
            Ok(_) => {}
            Err(err) => unavailable_ports.push((*port, *name, err)),
        }
    }

    if unavailable_ports.is_empty() {
        return Ok(());
    }

    let mut error_msg = String::from("One or more ports are already in use:\n\n");
    for (port, name, err) in &unavailable_ports {
        error_msg.push_str(&format!("{} port {}: {}\n", name, port, err));
    }
    error_msg.push_str("\nPossible solutions:\n");
    error_msg.push_str("  1. Stop the process using these ports\n");
    error_msg.push_str("  2. Use different ports with flags like --http-port, --ws-port, etc.\n");
    error_msg.push_str(
        "  3. Find the process using the port with: lsof -i :<port> or netstat -tulpn | grep <port>\n",
    );

    Err(error_msg)
}

/// Validate server configuration before starting
pub(crate) async fn validate_serve_config(
    config_path: &Option<PathBuf>,
    spec_paths: &[PathBuf],
    ports: &[(u16, &str)],
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    use std::fs;

    // Validate config file if provided
    if let Some(config) = config_path {
        if !config.exists() {
            return Err(format!(
                "Configuration file not found: {}\n\n\
                 Hint: Check that the path is correct and the file exists.",
                config.display()
            )
            .into());
        }

        // Try to read the file to ensure it's accessible
        if let Err(e) = fs::read_to_string(config) {
            return Err(format!(
                "Cannot read configuration file: {}\n\n\
                 Error: {}\n\
                 Hint: Check file permissions and ensure the file is readable.",
                config.display(),
                e
            )
            .into());
        }
    }

    // Validate spec files if provided
    for spec in spec_paths {
        if !spec.exists() {
            return Err(format!(
                "OpenAPI spec file not found: {}\n\n\
                 Hint: Check that the path is correct and the file exists.",
                spec.display()
            )
            .into());
        }

        // Try to read the file to ensure it's accessible
        if let Err(e) = fs::read_to_string(spec) {
            return Err(format!(
                "Cannot read OpenAPI spec file: {}\n\n\
                 Error: {}\n\
                 Hint: Check file permissions and ensure the file is readable.",
                spec.display(),
                e
            )
            .into());
        }
    }

    if let Err(err) = ensure_ports_available(ports) {
        return Err(err.into());
    }

    Ok(())
}

/// Initialize OpenTelemetry tracing with the given configuration
#[cfg(feature = "tracing")]
pub(crate) fn initialize_opentelemetry_tracing(
    otel_config: &mockforge_core::config::OpenTelemetryConfig,
    logging_config: &mockforge_observability::LoggingConfig,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    use mockforge_tracing::{init_tracer, TracingConfig};
    use tracing_opentelemetry::OpenTelemetryLayer;
    use tracing_subscriber::layer::SubscriberExt;
    use tracing_subscriber::util::SubscriberInitExt;
    use tracing_subscriber::EnvFilter;

    // Create tracing configuration from OpenTelemetry config
    let tracing_config = if let Some(ref otlp_endpoint) = otel_config.otlp_endpoint {
        TracingConfig::with_otlp(otel_config.service_name.clone(), otlp_endpoint.clone())
    } else {
        TracingConfig::with_jaeger(
            otel_config.service_name.clone(),
            otel_config.jaeger_endpoint.clone(),
        )
    }
    .with_sampling_rate(otel_config.sampling_rate)
    .with_environment(otel_config.environment.clone());

    // Initialize the tracer (this sets up the global tracer provider)
    // The global tracer provider is what the OpenTelemetry layer will use
    let _tracer = init_tracer(tracing_config)?;

    // Create OpenTelemetry layer that uses the global tracer provider
    // The layer() function automatically uses the global tracer provider set by init_tracer
    let otel_layer = OpenTelemetryLayer::default();

    // Parse log level
    let log_level = logging_config.level.clone();
    let env_filter = EnvFilter::try_from_default_env()
        .or_else(|_| EnvFilter::try_new(&log_level))
        .unwrap_or_else(|_| EnvFilter::new("info"));

    // Build the subscriber with OpenTelemetry layer
    // We need to reinitialize the subscriber to add the OpenTelemetry layer
    let registry = tracing_subscriber::registry().with(env_filter).with(otel_layer);

    // Add console layer based on config
    if logging_config.json_format {
        use tracing_subscriber::fmt;
        registry.with(fmt::layer().json()).init();
    } else {
        use tracing_subscriber::fmt;
        registry.with(fmt::layer()).init();
    }

    tracing::info!("OpenTelemetry tracing initialized successfully with layer integration");
    Ok(())
}

/// Start the MockForge server with the given configuration
///
/// This function is public so it can be called from other commands like deploy
pub async fn handle_serve(
    serve_args: ServeArgs,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    // Auto-discover config file if not provided
    let effective_config_path = if serve_args.config_path.is_some() {
        serve_args.config_path.clone()
    } else {
        // Try to discover config file
        if let Ok(current_dir) = std::env::current_dir() {
            let config_names = vec![
                "mockforge.yaml",
                "mockforge.yml",
                ".mockforge.yaml",
                ".mockforge.yml",
            ];

            // Check current directory
            let mut discovered = None;
            for name in &config_names {
                let path = current_dir.join(name);
                if path.exists() {
                    discovered = Some(path);
                    break;
                }
            }
            discovered
        } else {
            None
        }
    };

    // If no spec files provided, check MOCKFORGE_CONFIG env var (inline spec content)
    // or MOCKFORGE_OPENAPI_SPEC_URL env var (URL or local path to spec)
    let spec = if serve_args.spec.is_empty() {
        if let Ok(config_json) = std::env::var("MOCKFORGE_CONFIG") {
            // MOCKFORGE_CONFIG contains the OpenAPI spec as JSON
            let spec_dir = std::path::Path::new("/tmp/mockforge-specs");
            let _ = tokio::fs::create_dir_all(spec_dir).await;
            let spec_path = spec_dir.join("spec.json");
            match tokio::fs::write(&spec_path, config_json.as_bytes()).await {
                Ok(()) => {
                    tracing::info!("Loaded spec from MOCKFORGE_CONFIG env var");
                    vec![spec_path]
                }
                Err(e) => {
                    tracing::error!("Failed to write spec from MOCKFORGE_CONFIG: {}", e);
                    vec![]
                }
            }
        } else if let Ok(spec_url) = std::env::var("MOCKFORGE_OPENAPI_SPEC_URL") {
            if spec_url.starts_with("http://") || spec_url.starts_with("https://") {
                tracing::info!("Downloading spec from URL: {}", spec_url);
                match reqwest::get(&spec_url).await {
                    Ok(response) if response.status().is_success() => {
                        let spec_dir = std::path::Path::new("/tmp/mockforge-specs");
                        let _ = tokio::fs::create_dir_all(spec_dir).await;
                        let spec_path = spec_dir.join("spec.json");
                        match response.bytes().await {
                            Ok(bytes) => match tokio::fs::write(&spec_path, &bytes).await {
                                Ok(()) => {
                                    tracing::info!("Spec downloaded to {}", spec_path.display());
                                    vec![spec_path]
                                }
                                Err(e) => {
                                    tracing::error!("Failed to write spec file: {}", e);
                                    vec![]
                                }
                            },
                            Err(e) => {
                                tracing::error!("Failed to read spec response: {}", e);
                                vec![]
                            }
                        }
                    }
                    Ok(response) => {
                        tracing::error!("Failed to download spec: HTTP {}", response.status());
                        vec![]
                    }
                    Err(e) => {
                        tracing::error!("Failed to download spec: {}", e);
                        vec![]
                    }
                }
            } else {
                vec![PathBuf::from(spec_url)]
            }
        } else {
            vec![]
        }
    } else {
        serve_args.spec.clone()
    };

    // Rebuild serve_args with effective config path and resolved spec
    let serve_args = ServeArgs {
        config_path: effective_config_path.clone(),
        spec,
        reality_level: serve_args.reality_level.or_else(|| {
            // Check environment variable as fallback
            std::env::var("MOCKFORGE_REALITY_LEVEL").ok().and_then(|v| v.parse::<u8>().ok())
        }),
        ..serve_args
    };

    // Validate config and spec paths (skip port checks for now)
    validate_serve_config(&serve_args.config_path, &serve_args.spec, &[]).await?;

    // Merge configuration sources
    let mut config = build_server_config_from_cli(&serve_args).await;

    // Determine ports to validate using final configuration
    let mut final_ports = vec![
        (config.http.port, "HTTP"),
        (config.websocket.port, "WebSocket"),
        (config.grpc.port, "gRPC"),
    ];

    if config.admin.enabled {
        final_ports.push((config.admin.port, "Admin UI"));
    }

    if config.observability.prometheus.enabled {
        final_ports.push((config.observability.prometheus.port, "Metrics"));
    }

    // Skip port check for dry-run mode since we're not actually binding
    if !serve_args.dry_run {
        if let Err(port_error) = ensure_ports_available(&final_ports) {
            return Err(port_error.into());
        }
    }

    if serve_args.dry_run {
        println!("✅ Configuration validation passed!");
        if serve_args.config_path.is_some() {
            println!("✅ Configuration file is valid");
        }
        if !serve_args.spec.is_empty() {
            println!("✅ OpenAPI spec file(s) are valid");
        }
        if serve_args.spec_dir.is_some() {
            println!("✅ OpenAPI spec directory is valid");
        }
        println!("\n🎉 Dry run successful - no issues found!");
        return Ok(());
    }

    if !config.routes.is_empty() {
        println!("📄 Found {} routes in config", config.routes.len());
    } else {
        println!("📄 No routes found in config");
    }

    // Apply network profile if specified
    if let Some(profile_name) = serve_args.network_profile {
        use mockforge_chaos::core_network_profiles::NetworkProfileCatalog;
        let catalog = NetworkProfileCatalog::new();

        if let Some(profile) = catalog.get(&profile_name) {
            println!("📡 Applying network profile: {} - {}", profile.name, profile.description);
            let (latency_profile, traffic_shaping_config) = profile.apply();

            // Apply latency profile
            config.core.default_latency = latency_profile;
            config.core.latency_enabled = true;

            // Apply traffic shaping
            config.core.traffic_shaping = traffic_shaping_config;
            config.core.traffic_shaping_enabled = true;
        } else {
            eprintln!("⚠️  Warning: Unknown network profile '{}'. Use --list-network-profiles to see available profiles.", profile_name);
        }
    }

    // Enable random chaos mode if specified
    if serve_args.chaos_random {
        use mockforge_chaos::core_chaos_utilities::ChaosConfig;

        println!("🎲 Random chaos mode enabled");
        println!("   Error rate: {:.1}%", serve_args.chaos_random_error_rate * 100.0);
        println!("   Delay rate: {:.1}%", serve_args.chaos_random_delay_rate * 100.0);
        println!(
            "   Delay range: {}-{} ms",
            serve_args.chaos_random_min_delay, serve_args.chaos_random_max_delay
        );

        // Create and apply chaos config
        let chaos_config = ChaosConfig::new(
            serve_args.chaos_random_error_rate,
            serve_args.chaos_random_delay_rate,
        )
        .with_delay_range(serve_args.chaos_random_min_delay, serve_args.chaos_random_max_delay);

        config.core.chaos_random = Some(chaos_config);
    }

    // Re-initialize logging with configuration from config file
    // This allows JSON logging, file output, and OpenTelemetry integration
    let logging_config = mockforge_observability::LoggingConfig {
        level: config.logging.level.clone(),
        json_format: config.logging.json_format,
        file_path: config.logging.file_path.as_ref().map(|p| p.into()),
        max_file_size_mb: config.logging.max_file_size_mb,
        max_files: config.logging.max_files,
    };

    // If OpenTelemetry tracing is enabled, initialize with tracing layer
    if let Some(ref otel_config) = config.observability.opentelemetry {
        if otel_config.enabled {
            // Initialize OpenTelemetry tracer
            if let Err(e) = initialize_opentelemetry_tracing(otel_config, &logging_config) {
                tracing::warn!("Failed to initialize OpenTelemetry tracing: {}", e);
                // Fall back to standard logging
                if let Err(e) = mockforge_observability::init_logging(logging_config) {
                    eprintln!("Failed to initialize logging: {}", e);
                }
            }
        }
    }

    println!("🚀 Starting MockForge servers...");

    // Initialize the global request logger early, BEFORE any server tasks are spawned.
    // This ensures HTTP request logs are captured from the very first request,
    // not just after the admin UI router happens to initialize.
    mockforge_core::init_global_logger(1000);

    println!("📡 HTTP server on port {}", config.http.port);
    println!("🔌 WebSocket server on port {}", config.websocket.port);
    println!("⚡ gRPC server on port {}", config.grpc.port);
    if config.tcp.enabled {
        println!("🔌 TCP server on port {}", config.tcp.port);
    }

    if config.admin.enabled {
        println!("🎛️ Admin UI on port {}", config.admin.port);
    }

    if config.observability.prometheus.enabled {
        println!("📊 Metrics endpoint on port {}", config.observability.prometheus.port);
    }

    if let Some(ref tracing_config) = config.observability.opentelemetry {
        if tracing_config.enabled {
            println!("🔍 OpenTelemetry tracing enabled");
            println!("   Service: {}", tracing_config.service_name);
            println!("   Environment: {}", tracing_config.environment);
            println!("   Jaeger endpoint: {}", tracing_config.jaeger_endpoint);
        }
    }

    if let Some(ref recorder_config) = config.observability.recorder {
        if recorder_config.enabled {
            println!("📹 API Flight Recorder enabled");
            println!("   Database: {}", recorder_config.database_path);
            println!("   Max requests: {}", recorder_config.max_requests);
        }
    }

    if let Some(ref chaos_config) = config.observability.chaos {
        if chaos_config.enabled {
            println!("🌀 Chaos engineering enabled");
            if let Some(ref scenario) = chaos_config.scenario {
                println!("   Scenario: {}", scenario);
            }
        }
    }

    if config.data.rag.enabled {
        println!("🧠 AI features enabled");
        println!("   Provider: {}", config.data.rag.provider);
        if let Some(ref model) = config.data.rag.model {
            println!("   Model: {}", model);
        }
    }

    if config.core.traffic_shaping_enabled {
        println!("🚦 Traffic shaping enabled");
        println!(
            "   Bandwidth limit: {} bytes/sec",
            config.core.traffic_shaping.bandwidth.max_bytes_per_sec
        );
    }

    // Set AI environment variables if configured
    if let Some(ref api_key) = config.data.rag.api_key {
        std::env::set_var("MOCKFORGE_RAG_API_KEY", api_key);
    }
    std::env::set_var("MOCKFORGE_RAG_PROVIDER", &config.data.rag.provider);
    if let Some(ref model) = config.data.rag.model {
        std::env::set_var("MOCKFORGE_RAG_MODEL", model);
    }

    // Initialize key store at startup (lightweight operation, keep synchronous)
    init_key_store();

    // Initialize request capture manager lazily (defer until first use)
    // This is lightweight but can be deferred to improve startup time
    tokio::spawn(async {
        use mockforge_core::request_capture::init_global_capture_manager;
        init_global_capture_manager(1000); // Keep last 1000 requests
        tracing::info!(
            "Request capture manager initialized for contract diff analysis (lazy-loaded)"
        );
    });

    // Initialize SIEM emitter lazily (defer until first use to improve startup time)
    let siem_config = config.security.monitoring.siem.clone();
    if siem_config.enabled {
        use mockforge_core::security::init_global_siem_emitter;
        // Spawn async task to initialize SIEM emitter in background (non-blocking)
        tokio::spawn(async move {
            if let Err(e) = init_global_siem_emitter(siem_config.clone()).await {
                tracing::warn!("Failed to initialize SIEM emitter: {}", e);
            } else {
                tracing::info!(
                    "SIEM emitter initialized with {} destinations (lazy-loaded)",
                    siem_config.destinations.len()
                );
            }
        });
    }

    // Initialize access review system if enabled
    let _access_review_scheduler_handle = if config.security.monitoring.access_review.enabled {
        use mockforge_core::security::{
            access_review::AccessReviewEngine,
            access_review_notifications::{AccessReviewNotificationService, NotificationConfig},
            access_review_scheduler::AccessReviewScheduler,
            access_review_service::AccessReviewService,
            api_tokens::InMemoryApiTokenStorage,
            justification_storage::InMemoryJustificationStorage,
            mfa_tracking::InMemoryMfaStorage,
        };
        use std::sync::Arc;
        use tokio::sync::RwLock;

        // Create storage backends (in-memory for now, can be replaced with database-backed implementations)
        let _token_storage: Arc<dyn mockforge_core::security::ApiTokenStorage> =
            Arc::new(InMemoryApiTokenStorage::new());
        let _mfa_storage: Arc<dyn mockforge_core::security::MfaStorage> =
            Arc::new(InMemoryMfaStorage::new());
        let _justification_storage: Arc<dyn mockforge_core::security::JustificationStorage> =
            Arc::new(InMemoryJustificationStorage::new());

        // Create a simple user data provider (placeholder - would use CollabUserDataProvider if collab is enabled)
        // For now, we'll create a minimal implementation that can be extended
        struct SimpleUserDataProvider;
        #[async_trait::async_trait]
        impl mockforge_core::security::UserDataProvider for SimpleUserDataProvider {
            async fn get_all_users(
                &self,
            ) -> Result<Vec<mockforge_core::security::UserAccessInfo>, mockforge_core::Error>
            {
                // Return empty list - would be populated from actual user management system
                Ok(Vec::new())
            }
            async fn get_privileged_users(
                &self,
            ) -> Result<Vec<mockforge_core::security::PrivilegedAccessInfo>, mockforge_core::Error>
            {
                Ok(Vec::new())
            }
            async fn get_api_tokens(
                &self,
            ) -> Result<Vec<mockforge_core::security::ApiTokenInfo>, mockforge_core::Error>
            {
                Ok(Vec::new())
            }
            async fn get_user(
                &self,
                _user_id: uuid::Uuid,
            ) -> Result<Option<mockforge_core::security::UserAccessInfo>, mockforge_core::Error>
            {
                Ok(None)
            }
            async fn get_last_login(
                &self,
                _user_id: uuid::Uuid,
            ) -> Result<Option<chrono::DateTime<chrono::Utc>>, mockforge_core::Error> {
                Ok(None)
            }
            async fn revoke_user_access(
                &self,
                _user_id: uuid::Uuid,
                _reason: String,
            ) -> Result<(), mockforge_core::Error> {
                Ok(())
            }
            async fn update_user_permissions(
                &self,
                _user_id: uuid::Uuid,
                _roles: Vec<String>,
                _permissions: Vec<String>,
            ) -> Result<(), mockforge_core::Error> {
                Ok(())
            }
        }

        let user_provider = SimpleUserDataProvider;

        // Create access review engine and service
        let review_config = config.security.monitoring.access_review.clone();
        let review_config_for_scheduler = review_config.clone();
        let engine = AccessReviewEngine::new(review_config.clone());
        let review_service = AccessReviewService::new(engine, Box::new(user_provider));
        let review_service_arc = Arc::new(RwLock::new(review_service));

        // Create notification service
        let notification_config = NotificationConfig {
            enabled: review_config.notifications.enabled,
            channels: review_config
                .notifications
                .channels
                .iter()
                .map(|c| match c.as_str() {
                    "email" => mockforge_core::security::access_review_notifications::NotificationChannel::Email,
                    "slack" => mockforge_core::security::access_review_notifications::NotificationChannel::Slack,
                    "webhook" => mockforge_core::security::access_review_notifications::NotificationChannel::Webhook,
                    _ => mockforge_core::security::access_review_notifications::NotificationChannel::InApp,
                })
                .collect(),
            recipients: review_config.notifications.recipients,
            channel_config: std::collections::HashMap::new(),
        };
        let notification_service =
            Arc::new(AccessReviewNotificationService::new(notification_config));

        // Initialize global access review service for HTTP API
        use mockforge_core::security::init_global_access_review_service;
        if let Err(e) = init_global_access_review_service(review_service_arc.clone()).await {
            tracing::warn!("Failed to initialize global access review service: {}", e);
        } else {
            tracing::info!("Global access review service initialized");
        }

        // Create and start scheduler
        let scheduler = AccessReviewScheduler::with_notifications(
            review_service_arc,
            review_config_for_scheduler,
            Some(notification_service),
        );
        let handle = scheduler.start();

        tracing::info!("Access review scheduler started");
        Some(handle)
    } else {
        None
    };

    // Initialize privileged access manager if enabled
    let _privileged_access_manager = if config.security.monitoring.privileged_access.require_mfa {
        use mockforge_core::security::{
            justification_storage::InMemoryJustificationStorage, mfa_tracking::InMemoryMfaStorage,
            privileged_access::PrivilegedAccessManager,
        };
        use std::sync::Arc;

        let privileged_config = config.security.monitoring.privileged_access.clone();
        let mfa_storage: Arc<dyn mockforge_core::security::MfaStorage> =
            Arc::new(InMemoryMfaStorage::new());
        let justification_storage: Arc<dyn mockforge_core::security::JustificationStorage> =
            Arc::new(InMemoryJustificationStorage::new());

        let manager = PrivilegedAccessManager::new(
            privileged_config,
            Some(mfa_storage),
            Some(justification_storage),
        );

        // Start session cleanup task
        let manager_for_cleanup = Arc::new(RwLock::new(manager));
        let cleanup_manager = manager_for_cleanup.clone();
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(Duration::from_secs(300)); // Every 5 minutes
            loop {
                interval.tick().await;
                if let Err(e) = cleanup_manager.write().await.cleanup_expired_sessions().await {
                    tracing::warn!("Failed to cleanup expired privileged sessions: {}", e);
                }
            }
        });

        // Initialize global privileged access manager for HTTP API
        use mockforge_core::security::init_global_privileged_access_manager;
        if let Err(e) = init_global_privileged_access_manager(manager_for_cleanup.clone()).await {
            tracing::warn!("Failed to initialize global privileged access manager: {}", e);
        } else {
            tracing::info!("Global privileged access manager initialized");
        }

        tracing::info!("Privileged access manager initialized");
        Some(manager_for_cleanup)
    } else {
        None
    };

    // Initialize change management engine if enabled
    let _change_management_engine = if config.security.monitoring.change_management.enabled {
        use mockforge_core::security::change_management::ChangeManagementEngine;
        use std::sync::Arc;

        let change_config = config.security.monitoring.change_management.clone();
        let engine = ChangeManagementEngine::new(change_config);
        let engine_arc = Arc::new(RwLock::new(engine));

        // Initialize global change management engine for HTTP API
        use mockforge_core::security::init_global_change_management_engine;
        if let Err(e) = init_global_change_management_engine(engine_arc.clone()).await {
            tracing::warn!("Failed to initialize global change management engine: {}", e);
        } else {
            tracing::info!("Global change management engine initialized");
        }

        tracing::info!("Change management engine initialized");
        Some(engine_arc)
    } else {
        None
    };

    // Initialize compliance dashboard engine if enabled
    let _compliance_dashboard_engine = if config.security.monitoring.compliance_dashboard.enabled {
        use mockforge_core::security::compliance_dashboard::ComplianceDashboardEngine;
        use std::sync::Arc;

        let dashboard_config = config.security.monitoring.compliance_dashboard.clone();
        let engine = ComplianceDashboardEngine::new(dashboard_config);
        let engine_arc = Arc::new(RwLock::new(engine));

        // Initialize global compliance dashboard engine for HTTP API
        use mockforge_core::security::init_global_compliance_dashboard_engine;
        if let Err(e) = init_global_compliance_dashboard_engine(engine_arc.clone()).await {
            tracing::warn!("Failed to initialize global compliance dashboard engine: {}", e);
        } else {
            tracing::info!("Global compliance dashboard engine initialized");
        }

        tracing::info!("Compliance dashboard engine initialized");
        Some(engine_arc)
    } else {
        None
    };

    // Initialize risk assessment engine if enabled
    let _risk_assessment_engine = if config.security.monitoring.risk_assessment.enabled {
        use mockforge_core::security::risk_assessment::RiskAssessmentEngine;
        use std::sync::Arc;

        let risk_config = config.security.monitoring.risk_assessment.clone();
        let engine = RiskAssessmentEngine::new(risk_config);
        let engine_arc = Arc::new(RwLock::new(engine));

        // Initialize global risk assessment engine for HTTP API
        use mockforge_core::security::init_global_risk_assessment_engine;
        if let Err(e) = init_global_risk_assessment_engine(engine_arc.clone()).await {
            tracing::warn!("Failed to initialize global risk assessment engine: {}", e);
        } else {
            tracing::info!("Global risk assessment engine initialized");
        }

        tracing::info!("Risk assessment engine initialized");
        Some(engine_arc)
    } else {
        None
    };

    // Build HTTP router with OpenAPI spec, chain support, multi-tenant, and traffic shaping if enabled
    let multi_tenant_config = if config.multi_tenant.enabled {
        Some(config.multi_tenant.clone())
    } else {
        None
    };

    // Create SMTP registry if enabled
    #[cfg(feature = "smtp")]
    let smtp_registry = if config.smtp.enabled {
        use mockforge_smtp::SmtpSpecRegistry;
        use std::sync::Arc;

        let mut registry = SmtpSpecRegistry::new();

        if let Some(fixtures_dir) = &config.smtp.fixtures_dir {
            if fixtures_dir.exists() {
                if let Err(e) = registry.load_fixtures(fixtures_dir) {
                    eprintln!(
                        "⚠️  Warning: Failed to load SMTP fixtures from {:?}: {}",
                        fixtures_dir, e
                    );
                } else {
                    println!("   Loaded SMTP fixtures from {:?}", fixtures_dir);
                }
            } else {
                println!("   No SMTP fixtures directory found at {:?}", fixtures_dir);
            }
        }

        Some(Arc::new(registry) as Arc<dyn Any + Send + Sync>)
    } else {
        None
    };
    #[cfg(not(feature = "smtp"))]
    let smtp_registry = None::<Arc<dyn std::any::Any + Send + Sync>>;

    #[cfg(feature = "mqtt")]
    let mqtt_registry = if config.mqtt.enabled {
        use mockforge_mqtt::MqttSpecRegistry;
        use std::sync::Arc;

        let mut registry = MqttSpecRegistry::new();

        if let Some(fixtures_dir) = &config.mqtt.fixtures_dir {
            if fixtures_dir.exists() {
                if let Err(e) = registry.load_fixtures(fixtures_dir) {
                    eprintln!(
                        "⚠️  Warning: Failed to load MQTT fixtures from {:?}: {}",
                        fixtures_dir, e
                    );
                } else {
                    println!("   Loaded MQTT fixtures from {:?}", fixtures_dir);
                }
            } else {
                println!("   No MQTT fixtures directory found at {:?}", fixtures_dir);
            }
        }

        Some(Arc::new(registry))
    } else {
        None
    };

    #[cfg(feature = "mqtt")]
    let mqtt_broker = if let Some(ref registry_ref) = mqtt_registry {
        let mqtt_config = config.mqtt.clone();

        // Convert core MqttConfig to mockforge_mqtt::MqttConfig
        let broker_config = mockforge_mqtt::broker::MqttConfig {
            port: mqtt_config.port,
            host: mqtt_config.host.clone(),
            max_connections: mqtt_config.max_connections,
            max_packet_size: mqtt_config.max_packet_size,
            keep_alive_secs: mqtt_config.keep_alive_secs,
            version: mockforge_mqtt::broker::MqttVersion::default(),
            // TLS defaults (not yet exposed in core config)
            tls_enabled: false,
            tls_port: 8883,
            tls_cert_path: None,
            tls_key_path: None,
            tls_ca_path: None,
            tls_client_auth: false,
        };

        // MQTT registry is already Some, so we can safely clone it
        Some(Arc::new(mockforge_mqtt::MqttBroker::new(
            broker_config.clone(),
            registry_ref.clone(),
        )))
    } else {
        None
    };

    #[cfg(feature = "mqtt")]
    let mqtt_broker_for_http = mqtt_broker
        .as_ref()
        .map(|broker| Arc::clone(broker) as Arc<dyn Any + Send + Sync>);
    #[cfg(not(feature = "mqtt"))]
    let mqtt_broker_for_http = None::<Arc<dyn Any + Send + Sync>>;

    // Create health manager for Kubernetes-native health checks
    use mockforge_http::HealthManager;
    use std::sync::Arc;
    use std::time::Duration;

    let health_manager = Arc::new(HealthManager::with_init_timeout(Duration::from_secs(60)));
    let health_manager_for_router = health_manager.clone();

    // Initialize TimeTravelManager if configured
    use mockforge_core::TimeTravelManager;
    use mockforge_ui::time_travel_handlers;

    let time_travel_manager = {
        let time_travel_config = config.core.time_travel.clone();
        let manager = Arc::new(TimeTravelManager::new(time_travel_config));

        // Initialize the global time travel manager for UI handlers
        time_travel_handlers::init_time_travel_manager(manager.clone());

        if manager.clock().is_enabled() {
            println!("⏰ Time travel enabled");
            if let Some(virtual_time) = manager.clock().status().current_time {
                println!("   Virtual time: {}", virtual_time);
            }
            println!("   Scale factor: {}x", manager.clock().get_scale());
        }

        // Start cron scheduler background task
        let cron_scheduler = manager.cron_scheduler();
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(Duration::from_secs(1));
            loop {
                interval.tick().await;
                if let Err(e) = cron_scheduler.check_and_execute().await {
                    tracing::warn!("Error checking cron jobs: {}", e);
                }
            }
        });

        manager
    };

    // Initialize MutationRuleManager for time-based data mutations
    use mockforge_vbr::MutationRuleManager;
    let mutation_rule_manager = Arc::new(MutationRuleManager::new());
    time_travel_handlers::init_mutation_rule_manager(mutation_rule_manager.clone());

    // Initialize MockAI in parallel with router building to improve startup time
    // This allows MockAI initialization to happen concurrently with HTTP router setup
    let mockai = if config.mockai.enabled {
        use mockforge_core::intelligent_behavior::MockAI;
        use std::sync::Arc;
        use tokio::sync::RwLock;
        use tracing::{debug, info};

        let behavior_config = config.mockai.intelligent_behavior.clone();
        let spec_path = config.http.openapi_spec.clone();

        // Create MockAI with a default instance first (fast), then upgrade in background
        // This allows the server to start immediately while MockAI initializes
        let mockai_arc = Arc::new(RwLock::new(MockAI::new(behavior_config.clone())));
        let mockai_for_upgrade = mockai_arc.clone();
        let behavior_config_for_upgrade = behavior_config.clone();

        // Spawn task to upgrade MockAI with OpenAPI spec if available (non-blocking)
        tokio::spawn(async move {
            if let Some(ref spec_path) = spec_path {
                match OpenApiSpec::from_file(spec_path).await {
                    Ok(openapi_spec) => {
                        match MockAI::from_openapi(&openapi_spec, behavior_config_for_upgrade).await
                        {
                            Ok(instance) => {
                                *mockai_for_upgrade.write().await = instance;
                                info!("✅ MockAI upgraded with OpenAPI spec (background initialization)");
                            }
                            Err(e) => {
                                debug!(
                                    "MockAI not available (no OpenAI API key configured): {}",
                                    e
                                );
                                // Keep default instance
                            }
                        }
                    }
                    Err(e) => {
                        debug!("Failed to load OpenAPI spec for MockAI: {}", e);
                        // Keep default instance
                    }
                }
            }
        });

        Some(mockai_arc)
    } else {
        None
    };

    // Create ValidationOptions from config for template expansion
    use mockforge_openapi::openapi_routes::{ValidationMode, ValidationOptions};
    let request_mode = if let Some(ref validation) = config.http.validation {
        match validation.mode.as_str() {
            "off" | "disable" | "disabled" => ValidationMode::Disabled,
            "warn" | "warning" => ValidationMode::Warn,
            _ => ValidationMode::Enforce,
        }
    } else {
        ValidationMode::Enforce
    };

    let validation_options = ValidationOptions {
        request_mode,
        aggregate_errors: config.http.aggregate_validation_errors,
        validate_responses: config.http.validate_responses,
        overrides: std::collections::HashMap::new(),
        admin_skip_prefixes: vec!["/__mockforge".to_string(), "/health".to_string()],
        response_template_expand: config.http.response_template_expand,
        validation_status: config.http.validation_status,
    };

    // Process multiple specs if provided
    let final_spec_path = if !serve_args.spec.is_empty() || serve_args.spec_dir.is_some() {
        use mockforge_openapi::multi_spec::{
            group_specs_by_api_version, group_specs_by_openapi_version, load_specs_from_directory,
            load_specs_from_files, merge_specs, ConflictStrategy,
        };

        // Load specs
        let specs = if !serve_args.spec.is_empty() {
            load_specs_from_files(serve_args.spec.clone())
                .await
                .map_err(|e| format!("Failed to load spec files: {}", e))?
        } else if let Some(ref spec_dir) = serve_args.spec_dir {
            load_specs_from_directory(spec_dir)
                .await
                .map_err(|e| format!("Failed to load specs from directory: {}", e))?
        } else {
            Vec::new()
        };

        if specs.is_empty() {
            config.http.openapi_spec.clone()
        } else {
            // Determine conflict strategy
            let conflict_strategy = ConflictStrategy::from(serve_args.merge_conflicts.as_str());

            // Group by OpenAPI doc version first
            let openapi_groups = group_specs_by_openapi_version(specs);

            // Process each OpenAPI version group
            let mut merged_specs: Vec<(String, mockforge_openapi::spec::OpenApiSpec)> = Vec::new();
            for (_openapi_version, version_specs) in openapi_groups {
                // Apply API versioning grouping if enabled
                let api_versioning = serve_args.api_versioning.as_str();
                match api_versioning {
                    "info" | "path-prefix" => {
                        // Group by API version
                        let api_groups = group_specs_by_api_version(version_specs);
                        for (api_version, api_specs) in api_groups {
                            // Merge specs in this API version group
                            match merge_specs(api_specs, conflict_strategy) {
                                Ok(merged) => merged_specs.push((api_version, merged)),
                                Err(e) => {
                                    return Err(format!("Failed to merge specs: {}", e).into());
                                }
                            }
                        }
                    }
                    _ => {
                        // Merge all specs in this OpenAPI version group
                        match merge_specs(version_specs, conflict_strategy) {
                            Ok(merged) => merged_specs.push(("default".to_string(), merged)),
                            Err(e) => {
                                return Err(format!("Failed to merge specs: {}", e).into());
                            }
                        }
                    }
                }
            }

            // If we have multiple merged specs (different API versions), we need to handle them
            // For now, merge them all into one (or we could create separate routers with path prefixes)
            if merged_specs.len() == 1 {
                // Single merged spec - write to temp file
                let merged = &merged_specs[0].1;
                let raw_doc = merged
                    .raw_document
                    .as_ref()
                    .ok_or_else(|| "Merged spec has no raw document".to_string())?;
                let merged_json = serde_json::to_string_pretty(raw_doc)
                    .map_err(|e| format!("Failed to serialize merged spec: {}", e))?;

                // Use persistent temp file (won't be deleted automatically)
                let temp_dir = std::env::temp_dir();
                let temp_path =
                    temp_dir.join(format!("mockforge_merged_spec_{}.json", uuid::Uuid::new_v4()));
                std::fs::write(&temp_path, merged_json.as_bytes())
                    .map_err(|e| format!("Failed to write merged spec: {}", e))?;

                Some(temp_path.to_string_lossy().to_string())
            } else if merged_specs.is_empty() {
                config.http.openapi_spec.clone()
            } else if serve_args.api_versioning == "path-prefix" {
                let mut prefixed_specs: Vec<(PathBuf, mockforge_openapi::spec::OpenApiSpec)> =
                    Vec::new();

                for (api_version, spec) in merged_specs {
                    let version_suffix = api_version.trim().trim_start_matches('v');
                    let prefix = format!("/v{}", version_suffix);
                    let mut spec_json = spec.raw_document.clone().ok_or_else(|| {
                        format!("Merged spec for version '{}' has no raw document", api_version)
                    })?;

                    if let Some(paths_obj) =
                        spec_json.get_mut("paths").and_then(|p| p.as_object_mut())
                    {
                        let old_paths = std::mem::take(paths_obj);
                        let mut new_paths = serde_json::Map::new();
                        for (path, value) in old_paths {
                            let normalized_path = if path.starts_with('/') {
                                path
                            } else {
                                format!("/{}", path)
                            };
                            new_paths.insert(format!("{}{}", prefix, normalized_path), value);
                        }
                        *paths_obj = new_paths;
                    }

                    let prefixed_spec = mockforge_openapi::spec::OpenApiSpec::from_json(spec_json)
                        .map_err(|e| {
                            format!(
                                "Failed to build prefixed spec for API version '{}': {}",
                                api_version, e
                            )
                        })?;

                    prefixed_specs
                        .push((PathBuf::from(format!("api-{}", api_version)), prefixed_spec));
                }

                match merge_specs(prefixed_specs, conflict_strategy) {
                    Ok(final_merged) => {
                        let raw_doc = final_merged.raw_document.as_ref().ok_or_else(|| {
                            "Final merged prefixed spec has no raw document".to_string()
                        })?;
                        let merged_json = serde_json::to_string_pretty(raw_doc).map_err(|e| {
                            format!("Failed to serialize prefixed merged spec: {}", e)
                        })?;

                        let temp_dir = std::env::temp_dir();
                        let temp_path = temp_dir.join(format!(
                            "mockforge_merged_prefixed_spec_{}.json",
                            uuid::Uuid::new_v4()
                        ));
                        std::fs::write(&temp_path, merged_json.as_bytes())
                            .map_err(|e| format!("Failed to write prefixed merged spec: {}", e))?;

                        Some(temp_path.to_string_lossy().to_string())
                    }
                    Err(e) => {
                        return Err(format!(
                            "Failed to merge path-prefixed API version specs: {}",
                            e
                        )
                        .into());
                    }
                }
            } else {
                // Multiple merged specs - for now, merge them all
                let all_specs: Vec<_> =
                    merged_specs.into_iter().map(|(_, s)| (PathBuf::from("merged"), s)).collect();
                match merge_specs(all_specs, conflict_strategy) {
                    Ok(final_merged) => {
                        let raw_doc = final_merged
                            .raw_document
                            .as_ref()
                            .ok_or_else(|| "Final merged spec has no raw document".to_string())?;
                        let merged_json = serde_json::to_string_pretty(raw_doc)
                            .map_err(|e| format!("Failed to serialize final merged spec: {}", e))?;

                        // Use persistent temp file (won't be deleted automatically)
                        let temp_dir = std::env::temp_dir();
                        let temp_path = temp_dir
                            .join(format!("mockforge_merged_spec_{}.json", uuid::Uuid::new_v4()));
                        std::fs::write(&temp_path, merged_json.as_bytes())
                            .map_err(|e| format!("Failed to write merged spec: {}", e))?;

                        Some(temp_path.to_string_lossy().to_string())
                    }
                    Err(e) => {
                        return Err(
                            format!("Failed to merge multiple API version specs: {}", e).into()
                        );
                    }
                }
            }
        }
    } else {
        config.http.openapi_spec.clone()
    };

    // Apply --base-path override: prefix all paths in the OpenAPI spec
    let final_spec_path = if let Some(ref bp) = serve_args.base_path {
        if let Some(ref spec) = final_spec_path {
            let spec_content = tokio::fs::read_to_string(spec)
                .await
                .map_err(|e| format!("Failed to read spec for --base-path injection: {}", e))?;
            let mut spec_json: serde_json::Value =
                if spec.ends_with(".yaml") || spec.ends_with(".yml") {
                    serde_yaml::from_str(&spec_content)
                        .map_err(|e| format!("Failed to parse spec YAML: {}", e))?
                } else {
                    serde_json::from_str(&spec_content)
                        .map_err(|e| format!("Failed to parse spec JSON: {}", e))?
                };

            // Normalize base path: ensure leading slash, strip trailing slash
            let base = {
                let mut p = bp.clone();
                if !p.starts_with('/') {
                    p.insert(0, '/');
                }
                p.trim_end_matches('/').to_string()
            };

            // Rewrite the paths object: prefix every path key with the base path
            if let Some(paths_obj) = spec_json.get("paths").and_then(|v| v.as_object()).cloned() {
                let mut new_paths = serde_json::Map::new();
                for (path_key, path_value) in paths_obj {
                    let prefixed = format!("{}{}", base, path_key);
                    new_paths.insert(prefixed, path_value);
                }
                spec_json["paths"] = serde_json::Value::Object(new_paths);
            }

            let modified_json = serde_json::to_string_pretty(&spec_json)
                .map_err(|e| format!("Failed to serialize spec with base path: {}", e))?;
            let temp_dir = std::env::temp_dir();
            let temp_path =
                temp_dir.join(format!("mockforge_basepath_spec_{}.json", uuid::Uuid::new_v4()));
            std::fs::write(&temp_path, modified_json.as_bytes())
                .map_err(|e| format!("Failed to write base-path spec: {}", e))?;

            tracing::info!("Applied --base-path '{}': prefixed all spec paths with '{}'", bp, base);
            Some(temp_path.to_string_lossy().to_string())
        } else {
            final_spec_path
        }
    } else {
        final_spec_path
    };

    // Configure traffic shaping for HTTP middleware when enabled
    let traffic_shaping_enabled = config.core.traffic_shaping_enabled;
    let traffic_shaper = if traffic_shaping_enabled {
        Some(mockforge_core::TrafficShaper::new(config.core.traffic_shaping.clone()))
    } else {
        None
    };

    // Use standard router
    let mut http_app = mockforge_http::build_router_with_chains_and_multi_tenant(
        final_spec_path,
        Some(validation_options),
        None, // circling_config
        multi_tenant_config,
        Some(config.routes.clone()),
        config.http.cors.clone(),
        None, // ai_generator
        smtp_registry.as_ref().cloned(),
        mqtt_broker_for_http,
        traffic_shaper,                        // traffic_shaper
        traffic_shaping_enabled,               // traffic_shaping_enabled
        Some(health_manager_for_router),       // health_manager
        mockai.clone(),                        // mockai
        Some(config.deceptive_deploy.clone()), // deceptive_deploy_config
        None,                                  // proxy_config (ProxyConfig not in ServerConfig)
    )
    .await;

    // Integrate chaos engineering API router
    // Convert from ServerConfig's ChaosEngConfig to mockforge-chaos's ChaosConfig
    let chaos_config = if let Some(ref chaos_eng_config) = config.observability.chaos {
        // Convert ChaosEngConfig to ChaosConfig
        let chaos_cfg = ChaosConfig {
            enabled: chaos_eng_config.enabled,
            latency: chaos_eng_config.latency.as_ref().map(|l| {
                mockforge_chaos::config::LatencyConfig {
                    enabled: l.enabled,
                    fixed_delay_ms: l.fixed_delay_ms,
                    random_delay_range_ms: l.random_delay_range_ms,
                    jitter_percent: l.jitter_percent,
                    probability: l.probability,
                }
            }),
            fault_injection: chaos_eng_config.fault_injection.as_ref().map(|f| {
                mockforge_chaos::config::FaultInjectionConfig {
                    enabled: f.enabled,
                    http_errors: f.http_errors.clone(),
                    http_error_probability: f.http_error_probability,
                    connection_errors: f.connection_errors,
                    connection_error_probability: f.connection_error_probability,
                    timeout_errors: f.timeout_errors,
                    timeout_ms: f.timeout_ms,
                    timeout_probability: f.timeout_probability,
                    partial_responses: false,
                    partial_response_probability: 0.0,
                    payload_corruption: false,
                    payload_corruption_probability: 0.0,
                    corruption_type: mockforge_chaos::config::CorruptionType::None,
                    error_pattern: None,
                    mockai_enabled: false,
                }
            }),
            rate_limit: chaos_eng_config.rate_limit.as_ref().map(|r| {
                mockforge_chaos::config::RateLimitConfig {
                    enabled: r.enabled,
                    requests_per_second: r.requests_per_second,
                    burst_size: r.burst_size,
                    per_ip: r.per_ip,
                    per_endpoint: r.per_endpoint,
                }
            }),
            traffic_shaping: chaos_eng_config.traffic_shaping.as_ref().map(|t| {
                mockforge_chaos::config::TrafficShapingConfig {
                    enabled: t.enabled,
                    bandwidth_limit_bps: t.bandwidth_limit_bps,
                    packet_loss_percent: t.packet_loss_percent,
                    max_connections: 0,
                    connection_timeout_ms: 30000,
                }
            }),
            circuit_breaker: None,
            bulkhead: None,
        };
        chaos_cfg
    } else {
        // Default chaos config if not configured
        ChaosConfig::default()
    };

    // Create and merge chaos API router
    // Pass MockAI instance if available for dynamic error message generation
    // Note: Temporarily passing None to avoid type mismatch between different versions of MockAI
    if mockai.is_some() && chaos_config.enabled {
        tracing::warn!(
            "Chaos API is running without MockAI-assisted fault generation due temporary cross-crate type compatibility limits"
        );
    }
    let (chaos_router, chaos_config_arc, latency_tracker, chaos_api_state) =
        create_chaos_api_router(chaos_config.clone(), None);
    http_app = http_app.merge(chaos_router);
    println!("✅ Chaos Engineering API available at /api/chaos/*");

    // Store chaos_api_state for passing to admin server (Phase 3)
    let chaos_api_state_for_admin = chaos_api_state.clone();

    // Integrate chaos middleware if chaos is enabled
    if chaos_config.enabled {
        use axum::middleware::from_fn;
        use mockforge_chaos::middleware::{chaos_middleware_with_state, ChaosMiddleware};
        use std::sync::{Arc, OnceLock};

        // Create chaos middleware with shared config for hot-reload support
        // Pass the shared config Arc from chaos_api_state
        let chaos_middleware_instance =
            Arc::new(ChaosMiddleware::new(chaos_config_arc.clone(), latency_tracker));

        // Initialize middleware injectors from actual config (async, but we spawn it)
        let middleware_init = chaos_middleware_instance.clone();
        tokio::spawn(async move {
            middleware_init.init_from_config().await;
        });

        // Store the middleware in a static OnceLock to avoid Send issues with closures
        // This middleware will record latencies for the latency graph
        static CHAOS_MIDDLEWARE: OnceLock<Arc<ChaosMiddleware>> = OnceLock::new();
        let _ = CHAOS_MIDDLEWARE.set(chaos_middleware_instance.clone());

        // Use a closure that accesses the static - this is Send-safe because
        // the static is accessed inside the async block, not captured in the closure.
        // The RNG used by the middleware is thread-local and created fresh each time,
        // so it's safe even though the compiler can't prove it statically.
        // SAFETY: rand::rng() uses thread-local storage, so each thread gets its own RNG instance.
        // The RNG is created fresh on each call and never sent across threads, so this is Send-safe.
        // We use a wrapper to assert Send safety for the future.
        struct SendSafeWrapper<F>(F);
        unsafe impl<F> Send for SendSafeWrapper<F> {}
        impl<F: std::future::Future<Output = axum::response::Response>> std::future::Future
            for SendSafeWrapper<F>
        {
            type Output = axum::response::Response;
            fn poll(
                self: std::pin::Pin<&mut Self>,
                cx: &mut std::task::Context<'_>,
            ) -> std::task::Poll<Self::Output> {
                unsafe { std::pin::Pin::new_unchecked(&mut self.get_unchecked_mut().0).poll(cx) }
            }
        }

        http_app =
            http_app.layer(from_fn(|req: axum::extract::Request, next: axum::middleware::Next| {
                SendSafeWrapper(async move {
                    if let Some(state) = CHAOS_MIDDLEWARE.get() {
                        chaos_middleware_with_state(state.clone(), req, next).await
                    } else {
                        // Chaos middleware not initialized, pass through
                        next.run(req).await
                    }
                })
            }));
        println!("✅ Chaos middleware integrated - latency recording enabled");
    }

    // Note: OData URI rewrite is applied at the service level in serve_router_with_tls()

    println!(
        "✅ HTTP server configured with health checks at http://localhost:{}/health (live, ready, startup)",
        config.http.port
    );
    if !config.routes.is_empty() {
        println!("✅ Loaded {} custom routes", config.routes.len());
    }
    println!("✅ WebSocket server configured at ws://localhost:{}/ws", config.websocket.port);
    println!("✅ gRPC server configured at localhost:{}", config.grpc.port);
    if config.admin.enabled {
        println!("✅ Admin UI configured at http://localhost:{}", config.admin.port);
    }

    println!("💡 Press Ctrl+C to stop");

    // Create metrics registry (use global registry)
    let metrics_registry = Arc::new(MetricsRegistry::new());

    // Start system metrics collector if Prometheus is enabled
    if config.observability.prometheus.enabled {
        use mockforge_observability::{get_global_registry, SystemMetricsConfig};
        let system_metrics_config = SystemMetricsConfig {
            enabled: true,
            interval_seconds: 15,
        };
        mockforge_observability::system_metrics::start_with_config(
            get_global_registry(),
            system_metrics_config,
        );
        println!("📈 System metrics collector started (interval: 15s)");
    }

    // Create a cancellation token for graceful shutdown
    use tokio_util::sync::CancellationToken;
    let shutdown_token = CancellationToken::new();

    // Set up graceful shutdown integration with health manager
    let health_manager_for_shutdown = health_manager.clone();
    let shutdown_token_for_health = shutdown_token.clone();
    tokio::spawn(async move {
        shutdown_token_for_health.cancelled().await;
        health_manager_for_shutdown.trigger_shutdown().await;
    });

    // Start HTTP server
    let http_port = config.http.port;

    // Build TLS config: CLI flags take precedence over config file
    let mut http_tls_config = config.http.tls.clone();

    // Override with CLI flags if provided
    if serve_args.tls_enabled {
        http_tls_config = Some(mockforge_core::config::HttpTlsConfig {
            enabled: true,
            cert_file: serve_args
                .tls_cert
                .as_ref()
                .map(|p| p.to_string_lossy().to_string())
                .unwrap_or_else(|| {
                    http_tls_config.as_ref().map(|t| t.cert_file.clone()).unwrap_or_default()
                }),
            key_file: serve_args
                .tls_key
                .as_ref()
                .map(|p| p.to_string_lossy().to_string())
                .unwrap_or_else(|| {
                    http_tls_config.as_ref().map(|t| t.key_file.clone()).unwrap_or_default()
                }),
            ca_file: serve_args
                .tls_ca
                .as_ref()
                .map(|p| p.to_string_lossy().to_string())
                .or_else(|| http_tls_config.as_ref().and_then(|t| t.ca_file.clone())),
            min_version: serve_args.tls_min_version.clone(),
            cipher_suites: http_tls_config
                .as_ref()
                .map(|t| t.cipher_suites.clone())
                .unwrap_or_default(),
            require_client_cert: serve_args.mtls == "required",
            mtls_mode: serve_args.mtls.clone(),
        });
    } else if let Some(ref mut tls) = http_tls_config {
        // Update mtls_mode from CLI if provided, even if TLS wasn't enabled via CLI
        if serve_args.mtls != "off" {
            tls.mtls_mode = serve_args.mtls.clone();
            if serve_args.mtls == "required" {
                tls.require_client_cert = true;
            }
        }
    }

    let http_tls_config_final = http_tls_config.clone();
    let http_shutdown = shutdown_token.clone();
    let (http_bound_tx, http_bound_rx) = tokio::sync::oneshot::channel::<u16>();
    let http_tls_for_log = http_tls_config_final.clone();
    tokio::spawn(async move {
        if let Ok(actual_port) = http_bound_rx.await {
            match http_tls_for_log.as_ref() {
                Some(tls) if tls.enabled => {
                    println!("🔒 HTTPS server listening on https://localhost:{}", actual_port);
                }
                _ => {
                    println!("📡 HTTP server listening on http://localhost:{}", actual_port);
                }
            }
        }
    });
    let http_handle = tokio::spawn(async move {
        tokio::select! {
            result = mockforge_http::serve_router_with_tls_notify(http_port, http_app, http_tls_config_final, Some(http_bound_tx)) => {
                result.map_err(|e| format!("HTTP server error: {}", e))
            }
            _ = http_shutdown.cancelled() => {
                Ok(())
            }
        }
    });

    // Start WebSocket server
    let ws_handle: tokio::task::JoinHandle<Result<(), String>> = {
        #[cfg(feature = "ws")]
        {
            let ws_port = config.websocket.port;
            let ws_host = config.websocket.host.clone();
            let ws_shutdown = shutdown_token.clone();
            tokio::spawn(async move {
                println!("🔌 WebSocket server listening on ws://{}:{}", ws_host, ws_port);
                tokio::select! {
                    result = mockforge_ws::start_with_latency_and_host(ws_port, &ws_host, None) => {
                        result.map_err(|e| format!("WebSocket server error: {}", e))
                    }
                    _ = ws_shutdown.cancelled() => {
                        Ok(())
                    }
                }
            })
        }
        #[cfg(not(feature = "ws"))]
        {
            let shutdown = shutdown_token.clone();
            tokio::spawn(async move {
                shutdown.cancelled().await;
                Ok(())
            })
        }
    };

    // Start gRPC server (only if enabled and port is not 0)
    let grpc_handle: tokio::task::JoinHandle<Result<(), String>> = {
        #[cfg(feature = "grpc")]
        {
            let grpc_port = config.grpc.port;
            let grpc_enabled = config.grpc.enabled;
            let grpc_shutdown = shutdown_token.clone();
            if grpc_enabled && grpc_port != 0 {
                tokio::spawn(async move {
                    println!("⚡ gRPC server listening on localhost:{}", grpc_port);
                    tokio::select! {
                        result = mockforge_grpc::start(grpc_port) => {
                            result.map_err(|e| format!("gRPC server error: {}", e))
                        }
                        _ = grpc_shutdown.cancelled() => {
                            Ok(())
                        }
                    }
                })
            } else {
                tracing::debug!(
                    "gRPC server disabled (enabled: {}, port: {})",
                    grpc_enabled,
                    grpc_port
                );
                tokio::spawn(async move {
                    grpc_shutdown.cancelled().await;
                    Ok(())
                })
            }
        }
        #[cfg(not(feature = "grpc"))]
        {
            let shutdown = shutdown_token.clone();
            tokio::spawn(async move {
                shutdown.cancelled().await;
                Ok(())
            })
        }
    };

    #[cfg(feature = "smtp")]
    let _smtp_handle = if let Some(ref smtp_registry) = smtp_registry {
        let smtp_config = config.smtp.clone();
        let smtp_shutdown = shutdown_token.clone();

        // Convert core SmtpConfig to mockforge_smtp::SmtpConfig
        let server_config = mockforge_smtp::SmtpConfig {
            port: smtp_config.port,
            host: smtp_config.host.clone(),
            hostname: smtp_config.hostname.clone(),
            fixtures_dir: smtp_config.fixtures_dir.clone(),
            timeout_secs: smtp_config.timeout_secs,
            max_connections: smtp_config.max_connections,
            enable_mailbox: smtp_config.enable_mailbox,
            max_mailbox_messages: smtp_config.max_mailbox_messages,
            enable_starttls: smtp_config.enable_starttls,
            tls_cert_path: smtp_config.tls_cert_path.clone(),
            tls_key_path: smtp_config.tls_key_path.clone(),
        };

        // Downcast the registry with proper error handling
        let smtp_reg = match smtp_registry.clone().downcast::<mockforge_smtp::SmtpSpecRegistry>() {
            Ok(reg) => reg,
            Err(_) => {
                use crate::progress::{CliError, ExitCode};
                CliError::new(
                    "SMTP registry type mismatch - failed to downcast registry".to_string(),
                    ExitCode::ConfigurationError,
                )
                .with_suggestion(
                    "Ensure SMTP registry is properly configured and initialized".to_string(),
                )
                .display_and_exit();
            }
        };

        Some(tokio::spawn(async move {
            println!("📧 SMTP server listening on {}:{}", smtp_config.host, smtp_config.port);

            tokio::select! {
                result = async {
                    let server = mockforge_smtp::SmtpServer::new(server_config, smtp_reg)?;
                    server.start().await
                } => {
                    result.map_err(|e| format!("SMTP server error: {}", e))
                }
                _ = smtp_shutdown.cancelled() => {
                    println!("🛑 Shutting down SMTP server...");
                    Ok(())
                }
            }
        }))
    } else {
        None
    };

    #[cfg(feature = "mqtt")]
    let _mqtt_handle = if let Some(ref _mqtt_registry) = mqtt_registry {
        let mqtt_config = config.mqtt.clone();
        let mqtt_shutdown = shutdown_token.clone();

        // Convert core MqttConfig to mockforge_mqtt::MqttConfig
        let broker_config = mockforge_mqtt::broker::MqttConfig {
            port: mqtt_config.port,
            host: mqtt_config.host.clone(),
            max_connections: mqtt_config.max_connections,
            max_packet_size: mqtt_config.max_packet_size,
            keep_alive_secs: mqtt_config.keep_alive_secs,
            version: mockforge_mqtt::broker::MqttVersion::default(),
            // TLS defaults (not yet exposed in core config)
            tls_enabled: false,
            tls_port: 8883,
            tls_cert_path: None,
            tls_key_path: None,
            tls_ca_path: None,
            tls_client_auth: false,
        };

        Some(tokio::spawn(async move {
            use mockforge_mqtt::start_mqtt_server;

            println!("📡 MQTT broker listening on {}:{}", mqtt_config.host, mqtt_config.port);

            // Start the MQTT server
            tokio::select! {
                result = start_mqtt_server(broker_config) => {
                    result.map_err(|e| format!("MQTT server error: {:?}", e))
                }
                _ = mqtt_shutdown.cancelled() => {
                    println!("🛑 Shutting down MQTT broker...");
                    Ok(())
                }
            }
        }))
    } else {
        None
    };
    #[cfg(not(feature = "mqtt"))]
    let _mqtt_handle: Option<tokio::task::JoinHandle<Result<(), String>>> = None;

    // Auto-start tunnel if deceptive deploy is enabled with auto_tunnel
    let _tunnel_handle = if config.deceptive_deploy.enabled && config.deceptive_deploy.auto_tunnel {
        use mockforge_tunnel::{TunnelConfig, TunnelManager, TunnelProvider};

        use tokio::time::{sleep, Duration};

        let local_url = format!("http://localhost:{}", http_port);
        let deploy_config = config.deceptive_deploy.clone();
        let tunnel_shutdown = shutdown_token.clone();

        Some(tokio::spawn(async move {
            // Wait a bit for the server to be ready
            sleep(Duration::from_secs(2)).await;

            let provider = TunnelProvider::SelfHosted; // Default to self-hosted
            let mut tunnel_config = TunnelConfig::new(&local_url).with_provider(provider);

            // Use custom domain if specified
            if let Some(domain) = deploy_config.custom_domain {
                tunnel_config.custom_domain = Some(domain);
            }

            // Get tunnel server URL from environment or use default
            if let Ok(server_url) = std::env::var("MOCKFORGE_TUNNEL_SERVER_URL") {
                tunnel_config.server_url = Some(server_url);
            }

            // Get auth token from environment if available
            if let Ok(auth_token) = std::env::var("MOCKFORGE_TUNNEL_AUTH_TOKEN") {
                tunnel_config.auth_token = Some(auth_token);
            }

            match TunnelManager::new(&tunnel_config) {
                Ok(manager) => {
                    println!("🌐 Starting tunnel for deceptive deploy...");
                    match manager.create_tunnel(&tunnel_config).await {
                        Ok(status) => {
                            println!("✅ Tunnel created successfully!");
                            println!("   Public URL: {}", status.public_url);
                            println!("   Tunnel ID: {}", status.tunnel_id);
                            println!(
                                "💡 Your mock API is now accessible at: {}",
                                status.public_url
                            );

                            // Update deployment metadata with tunnel URL
                            let metadata_path = std::path::Path::new(".mockforge/deployment.json");
                            if metadata_path.exists() {
                                if let Ok(metadata_content) = std::fs::read_to_string(metadata_path)
                                {
                                    if let Ok(mut metadata) =
                                        serde_json::from_str::<serde_json::Value>(&metadata_content)
                                    {
                                        metadata["tunnel_url"] =
                                            serde_json::Value::String(status.public_url.clone());
                                        if let Ok(updated_json) =
                                            serde_json::to_string_pretty(&metadata)
                                        {
                                            if let Err(e) =
                                                std::fs::write(metadata_path, updated_json)
                                            {
                                                tracing::warn!("Failed to update deployment metadata with tunnel URL: {}", e);
                                            } else {
                                                tracing::info!("Updated deployment metadata with tunnel URL: {}", status.public_url);
                                            }
                                        }
                                    }
                                }
                            }

                            // Wait for shutdown signal
                            tokio::select! {
                                _ = tunnel_shutdown.cancelled() => {
                                    println!("🛑 Stopping tunnel...");
                                    if let Err(e) = manager.stop_tunnel().await {
                                        eprintln!("⚠️  Warning: Failed to stop tunnel: {}", e);
                                    }
                                    Ok::<(), anyhow::Error>(())
                                }
                            }
                        }
                        Err(e) => {
                            eprintln!("⚠️  Warning: Failed to create tunnel: {}", e);
                            eprintln!("💡 You can start a tunnel manually with: mockforge tunnel start --local-url {}", local_url);
                            Ok(())
                        }
                    }
                }
                Err(e) => {
                    eprintln!("⚠️  Warning: Failed to initialize tunnel manager: {}", e);
                    eprintln!("💡 You can start a tunnel manually with: mockforge tunnel start --local-url {}", local_url);
                    Ok(())
                }
            }
        }))
    } else {
        None
    };

    // Start Kafka broker (if enabled)
    #[cfg(feature = "kafka")]
    let _kafka_handle = if config.kafka.enabled {
        let kafka_config = config.kafka.clone();
        let kafka_shutdown = shutdown_token.clone();

        Some(tokio::spawn(async move {
            use mockforge_kafka::KafkaMockBroker;

            println!("📨 Kafka broker listening on {}:{}", kafka_config.host, kafka_config.port);

            // Create and start the Kafka broker
            match KafkaMockBroker::new(kafka_config.clone()).await {
                Ok(broker) => {
                    tokio::select! {
                        result = broker.start() => {
                            result.map_err(|e| format!("Kafka broker error: {:?}", e))
                        }
                        _ = kafka_shutdown.cancelled() => {
                            println!("🛑 Shutting down Kafka broker...");
                            Ok(())
                        }
                    }
                }
                Err(e) => Err(format!("Failed to initialize Kafka broker: {:?}", e)),
            }
        }))
    } else {
        None
    };
    #[cfg(not(feature = "kafka"))]
    let _kafka_handle: Option<tokio::task::JoinHandle<Result<(), String>>> = None;

    // Start AMQP broker (if enabled)
    #[cfg(feature = "amqp")]
    let _amqp_handle = if config.amqp.enabled {
        let amqp_config = config.amqp.clone();
        let amqp_shutdown = shutdown_token.clone();

        Some(tokio::spawn(async move {
            use mockforge_amqp::{AmqpBroker, AmqpSpecRegistry};
            use std::sync::Arc;

            println!("🐰 AMQP broker listening on {}:{}", amqp_config.host, amqp_config.port);

            // Create spec registry
            let spec_registry = Arc::new(
                AmqpSpecRegistry::new(amqp_config.clone())
                    .await
                    .map_err(|e| format!("Failed to create AMQP spec registry: {:?}", e))?,
            );

            // Load fixtures if configured
            if let Some(ref fixtures_dir) = amqp_config.fixtures_dir {
                if fixtures_dir.exists() {
                    println!("   Loading AMQP fixtures from {:?}", fixtures_dir);
                }
            }

            // Create and start the AMQP broker
            let broker = AmqpBroker::new(amqp_config.clone(), spec_registry);
            tokio::select! {
                result = broker.start() => {
                    result.map_err(|e| format!("AMQP broker error: {:?}", e))
                }
                _ = amqp_shutdown.cancelled() => {
                    println!("🛑 Shutting down AMQP broker...");
                    Ok(())
                }
            }
        }))
    } else {
        None
    };
    #[cfg(not(feature = "amqp"))]
    let _amqp_handle: Option<tokio::task::JoinHandle<Result<(), String>>> = None;

    // Start TCP server (if enabled)
    #[cfg(feature = "tcp")]
    let _tcp_handle = if config.tcp.enabled {
        use mockforge_tcp::{TcpConfig as TcpServerConfig, TcpServer, TcpSpecRegistry};
        use std::sync::Arc;

        let tcp_config = config.tcp.clone();
        let tcp_shutdown = shutdown_token.clone();

        // Convert core TcpConfig to mockforge_tcp::TcpConfig
        let server_config = TcpServerConfig {
            port: tcp_config.port,
            host: tcp_config.host.clone(),
            max_connections: tcp_config.max_connections,
            timeout_secs: tcp_config.timeout_secs,
            fixtures_dir: tcp_config.fixtures_dir.clone(),
            echo_mode: tcp_config.echo_mode,
            enable_tls: tcp_config.enable_tls,
            tls_cert_path: tcp_config.tls_cert_path.clone(),
            tls_key_path: tcp_config.tls_key_path.clone(),
            read_buffer_size: 8192, // Default buffer sizes
            write_buffer_size: 8192,
            delimiter: None, // Stream mode by default
        };

        Some(tokio::spawn(async move {
            let mut registry = TcpSpecRegistry::new();

            // Load fixtures if configured
            if let Some(ref fixtures_dir) = server_config.fixtures_dir {
                if fixtures_dir.exists() {
                    if let Err(e) = registry.load_fixtures(fixtures_dir) {
                        eprintln!(
                            "⚠️  Warning: Failed to load TCP fixtures from {:?}: {}",
                            fixtures_dir, e
                        );
                    } else {
                        println!("   Loaded TCP fixtures from {:?}", fixtures_dir);
                    }
                }
            }

            let registry_arc = Arc::new(registry);

            println!("🔌 TCP server listening on {}:{}", server_config.host, server_config.port);

            match TcpServer::new(server_config, registry_arc) {
                Ok(server) => {
                    tokio::select! {
                        result = server.start() => {
                            result.map_err(|e| format!("TCP server error: {}", e))
                        }
                        _ = tcp_shutdown.cancelled() => {
                            println!("🛑 Shutting down TCP server...");
                            Ok(())
                        }
                    }
                }
                Err(e) => Err(format!("Failed to initialize TCP server: {}", e)),
            }
        }))
    } else {
        None
    };
    #[cfg(not(feature = "tcp"))]
    let _tcp_handle: Option<tokio::task::JoinHandle<Result<(), String>>> = None;

    // Create latency injector if latency is enabled (for hot-reload support)
    use mockforge_foundation::latency::{FaultConfig, LatencyInjector};
    use tokio::sync::RwLock;

    let latency_injector_for_admin = if config.core.latency_enabled {
        let latency_profile = config.core.default_latency.clone();
        // Create a basic fault config (can be enhanced later)
        let fault_config = FaultConfig::default();
        Some(Arc::new(RwLock::new(LatencyInjector::new(latency_profile, fault_config))))
    } else {
        None
    };

    // Create recorder instance if configured
    let recorder_for_admin: Option<Arc<mockforge_recorder::Recorder>> =
        if let Some(ref recorder_config) = config.observability.recorder {
            if recorder_config.enabled {
                match mockforge_recorder::RecorderDatabase::new(&recorder_config.database_path)
                    .await
                {
                    Ok(db) => {
                        tracing::info!(
                            "Admin: recorder initialized from {}",
                            recorder_config.database_path
                        );
                        Some(Arc::new(mockforge_recorder::Recorder::new(db)))
                    }
                    Err(e) => {
                        tracing::warn!("Admin: failed to initialize recorder database: {}", e);
                        None
                    }
                }
            } else {
                None
            }
        } else {
            None
        };

    // Create VBR engine with in-memory backend (lightweight, no disk side-effects)
    let vbr_engine_for_admin: Option<Arc<mockforge_vbr::VbrEngine>> = {
        let vbr_config = mockforge_vbr::VbrConfig::new()
            .with_storage_backend(mockforge_vbr::StorageBackend::Memory);
        match mockforge_vbr::VbrEngine::new(vbr_config).await {
            Ok(engine) => {
                tracing::info!("Admin: VBR engine initialized (in-memory)");
                Some(Arc::new(engine))
            }
            Err(e) => {
                tracing::warn!("Admin: failed to initialize VBR engine: {}", e);
                None
            }
        }
    };

    // Create empty federation instance for admin dashboard
    let federation_for_admin: Option<Arc<mockforge_federation::Federation>> =
        Some(Arc::new(mockforge_federation::Federation::empty()));

    // Clone references for admin server
    let chaos_api_state_for_admin_clone = chaos_api_state_for_admin.clone();
    let latency_injector_for_admin_clone = latency_injector_for_admin.clone();
    let mockai_for_admin = mockai.clone();
    let continuum_config_for_admin = config.reality_continuum.clone();
    let time_travel_manager_for_admin = time_travel_manager.clone();

    // Start Admin UI server (if enabled)
    let admin_handle = if config.admin.enabled {
        let admin_port = config.admin.port;
        let http_port = config.http.port;
        let ws_port = config.websocket.port;
        let grpc_port = config.grpc.port;
        let prometheus_url = config.admin.prometheus_url.clone();
        let admin_shutdown = shutdown_token.clone();
        // Clone all host values before the async move closure
        let admin_host = config.admin.host.clone();
        let http_host = config.http.host.clone();
        let ws_host = config.websocket.host.clone();
        let grpc_host = config.grpc.host.clone();
        // Clone subsystem references for admin server
        let chaos_state = chaos_api_state_for_admin_clone.clone();
        let latency_injector = latency_injector_for_admin_clone.clone();
        let mockai_ref = mockai_for_admin.clone();
        let continuum_config = continuum_config_for_admin.clone();
        let time_travel_manager_clone = time_travel_manager_for_admin.clone();
        let recorder_clone = recorder_for_admin.clone();
        let federation_clone = federation_for_admin.clone();
        let vbr_engine_clone = vbr_engine_for_admin.clone();
        let (admin_bound_tx, admin_bound_rx) = tokio::sync::oneshot::channel::<u16>();
        let admin_host_for_log = admin_host.clone();
        tokio::spawn(async move {
            if let Ok(actual_port) = admin_bound_rx.await {
                println!("🎛️ Admin UI listening on http://{}:{}", admin_host_for_log, actual_port);
            }
        });
        Some(tokio::spawn(async move {
            // Parse addresses with proper error handling
            use crate::progress::parse_address;
            let addr = match parse_address(&format!("{}:{}", admin_host, admin_port), "admin UI") {
                Ok(addr) => addr,
                Err(e) => {
                    return Err(format!(
                        "Failed to bind Admin UI to {}:{}: {}",
                        admin_host, admin_port, e.message
                    ))
                }
            };

            let http_addr =
                match parse_address(&format!("{}:{}", http_host, http_port), "HTTP server") {
                    Ok(addr) => Some(addr),
                    Err(e) => {
                        return Err(format!(
                            "Failed to parse HTTP server address {}:{}: {}",
                            http_host, http_port, e.message
                        ))
                    }
                };
            let ws_addr =
                match parse_address(&format!("{}:{}", ws_host, ws_port), "WebSocket server") {
                    Ok(addr) => Some(addr),
                    Err(e) => {
                        return Err(format!(
                            "Failed to parse WebSocket server address {}:{}: {}",
                            ws_host, ws_port, e.message
                        ))
                    }
                };
            let grpc_addr =
                match parse_address(&format!("{}:{}", grpc_host, grpc_port), "gRPC server") {
                    Ok(addr) => Some(addr),
                    Err(e) => {
                        return Err(format!(
                            "Failed to parse gRPC server address {}:{}: {}",
                            grpc_host, grpc_port, e.message
                        ))
                    }
                };

            // Initialize continuum engine from config
            let continuum_config = Some(continuum_config);
            let virtual_clock_for_continuum = Some(time_travel_manager_clone.clock());

            tokio::select! {
                result = mockforge_ui::start_admin_server_notify(
                    addr,
                    http_addr,
                    ws_addr,
                    grpc_addr,
                    None, // graphql_server_addr
                    true, // api_enabled
                    prometheus_url,
                    Some(chaos_state),
                    latency_injector,
                    mockai_ref,
                    continuum_config,
                    virtual_clock_for_continuum,
                    recorder_clone,
                    federation_clone,
                    vbr_engine_clone,
                    Some(admin_bound_tx),
                ) => {
                    result.map_err(|e| format!("Admin UI server error: {}", e))
                }
                _ = admin_shutdown.cancelled() => {
                    Ok(())
                }
            }
        }))
    } else {
        None
    };

    // Start Prometheus metrics server (if enabled)
    let metrics_handle = if config.observability.prometheus.enabled {
        let metrics_port = config.observability.prometheus.port;
        let metrics_registry = metrics_registry.clone();
        let metrics_shutdown = shutdown_token.clone();
        Some(tokio::spawn(async move {
            println!(
                "📊 Prometheus metrics server listening on http://0.0.0.0:{}/metrics",
                metrics_port
            );
            let app = prometheus_router(metrics_registry);
            let addr = SocketAddr::from(([0, 0, 0, 0], metrics_port));
            let listener = TcpListener::bind(addr)
                .await
                .map_err(|e| format!("Failed to bind metrics server to {}: {}", addr, e))?;
            tokio::select! {
                result = axum::serve(listener, app) => {
                    result.map_err(|e| format!("Metrics server error: {}", e))
                }
                _ = metrics_shutdown.cancelled() => {
                    Ok(())
                }
            }
        }))
    } else {
        None
    };

    // Give servers a moment to start, then mark service as ready
    tokio::time::sleep(Duration::from_millis(500)).await;
    health_manager.set_ready().await;
    tracing::info!("Service marked as ready - all servers initialized");

    // Wait for all servers or shutdown signal, handling errors properly
    let result = tokio::select! {
        result = http_handle => {
            match result {
                Ok(Ok(())) => {
                    println!("📡 HTTP server stopped gracefully");
                    None
                }
                Ok(Err(e)) => {
                    eprintln!("{}", e);
                    Some(e)
                }
                Err(e) => {
                    let error = format!("HTTP server task panicked: {}", e);
                    eprintln!("{}", error);
                    Some(error)
                }
            }
        }
        result = ws_handle => {
            match result {
                Ok(Ok(())) => {
                    println!("🔌 WebSocket server stopped gracefully");
                    None
                }
                Ok(Err(e)) => {
                    eprintln!("{}", e);
                    Some(e)
                }
                Err(e) => {
                    let error = format!("WebSocket server task panicked: {}", e);
                    eprintln!("{}", error);
                    Some(error)
                }
            }
        }
        result = grpc_handle => {
            match result {
                Ok(Ok(())) => {
                    println!("⚡ gRPC server stopped gracefully");
                    None
                }
                Ok(Err(e)) => {
                    eprintln!("{}", e);
                    Some(e)
                }
                Err(e) => {
                    let error = format!("gRPC server task panicked: {}", e);
                    eprintln!("{}", error);
                    Some(error)
                }
            }
        }
        result = async {
            if let Some(handle) = admin_handle {
                Some(handle.await)
            } else {
                std::future::pending::<Option<Result<Result<(), String>, tokio::task::JoinError>>>().await
            }
        } => {
            match result {
                Some(Ok(Ok(()))) => {
                    println!("🎛️ Admin UI stopped gracefully");
                    None
                }
                Some(Ok(Err(e))) => {
                    eprintln!("{}", e);
                    Some(e)
                }
                Some(Err(e)) => {
                    let error = format!("Admin UI task panicked: {}", e);
                    eprintln!("{}", error);
                    Some(error)
                }
                None => None
            }
        }
        result = async {
            if let Some(handle) = metrics_handle {
                Some(handle.await)
            } else {
                std::future::pending::<Option<Result<Result<(), String>, tokio::task::JoinError>>>().await
            }
        } => {
            match result {
                Some(Ok(Ok(()))) => {
                    println!("📊 Metrics server stopped gracefully");
                    None
                }
                Some(Ok(Err(e))) => {
                    eprintln!("{}", e);
                    Some(e)
                }
                Some(Err(e)) => {
                    let error = format!("Metrics server task panicked: {}", e);
                    eprintln!("{}", error);
                    Some(error)
                }
                None => None
            }
        }
        _ = tokio::signal::ctrl_c() => {
            println!("🛑 Received shutdown signal");
            // Trigger health manager shutdown
            health_manager.trigger_shutdown().await;
            None
        }
    };

    // Trigger shutdown for all remaining tasks
    println!("👋 Shutting down remaining servers...");
    shutdown_token.cancel();

    // Give tasks a moment to shut down gracefully
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Return error if any server failed
    if let Some(error) = result {
        Err(error.into())
    } else {
        Ok(())
    }
}