rustapi-core 0.1.450

The core engine of the RustAPI framework. Provides the hyper-based HTTP server, router, extraction logic, and foundational traits.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
//! Router implementation using radix tree (matchit)
//!
//! This module provides HTTP routing functionality for RustAPI. Routes are
//! registered using path patterns and HTTP method handlers.
//!
//! # Path Patterns
//!
//! Routes support dynamic path parameters using `{param}` syntax:
//!
//! - `/users` - Static path
//! - `/users/{id}` - Single parameter
//! - `/users/{user_id}/posts/{post_id}` - Multiple parameters
//!
//! # Example
//!
//! ```rust,ignore
//! use rustapi_core::{Router, get, post, put, delete};
//!
//! async fn list_users() -> &'static str { "List users" }
//! async fn get_user() -> &'static str { "Get user" }
//! async fn create_user() -> &'static str { "Create user" }
//! async fn update_user() -> &'static str { "Update user" }
//! async fn delete_user() -> &'static str { "Delete user" }
//!
//! let router = Router::new()
//!     .route("/users", get(list_users).post(create_user))
//!     .route("/users/{id}", get(get_user).put(update_user).delete(delete_user));
//! ```
//!
//! # Method Chaining
//!
//! Multiple HTTP methods can be registered for the same path using method chaining:
//!
//! ```rust,ignore
//! .route("/users", get(list).post(create))
//! .route("/users/{id}", get(show).put(update).delete(destroy))
//! ```
//!
//! # Route Conflict Detection
//!
//! The router detects conflicting routes at registration time and provides
//! helpful error messages with resolution guidance.

use crate::handler::{into_boxed_handler, BoxedHandler, Handler};
use crate::path_params::PathParams;
use crate::typed_path::TypedPath;
use http::{Extensions, Method};
use matchit::Router as MatchitRouter;
use rustapi_openapi::Operation;
use std::collections::HashMap;
use std::sync::Arc;

/// Information about a registered route for conflict detection
#[derive(Debug, Clone)]
pub struct RouteInfo {
    /// The original path pattern (e.g., "/users/{id}")
    pub path: String,
    /// The HTTP methods registered for this path
    pub methods: Vec<Method>,
}

/// Error returned when a route conflict is detected
#[derive(Debug, Clone)]
pub struct RouteConflictError {
    /// The path that was being registered
    pub new_path: String,
    /// The HTTP method that conflicts
    pub method: Option<Method>,
    /// The existing path that conflicts
    pub existing_path: String,
    /// Detailed error message from the underlying router
    pub details: String,
}

impl std::fmt::Display for RouteConflictError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(
            f,
            "\n╭─────────────────────────────────────────────────────────────╮"
        )?;
        writeln!(
            f,
            "│                    ROUTE CONFLICT DETECTED                   │"
        )?;
        writeln!(
            f,
            "╰─────────────────────────────────────────────────────────────╯"
        )?;
        writeln!(f)?;
        writeln!(f, "  Conflicting routes:")?;
        writeln!(f, "    → Existing: {}", self.existing_path)?;
        writeln!(f, "    → New:      {}", self.new_path)?;
        writeln!(f)?;
        if let Some(ref method) = self.method {
            writeln!(f, "  HTTP Method: {}", method)?;
            writeln!(f)?;
        }
        writeln!(f, "  Details: {}", self.details)?;
        writeln!(f)?;
        writeln!(f, "  How to resolve:")?;
        writeln!(f, "    1. Use different path patterns for each route")?;
        writeln!(
            f,
            "    2. If paths must be similar, ensure parameter names differ"
        )?;
        writeln!(
            f,
            "    3. Consider using different HTTP methods if appropriate"
        )?;
        writeln!(f)?;
        writeln!(f, "  Example:")?;
        writeln!(f, "    Instead of:")?;
        writeln!(f, "      .route(\"/users/{{id}}\", get(handler1))")?;
        writeln!(f, "      .route(\"/users/{{user_id}}\", get(handler2))")?;
        writeln!(f)?;
        writeln!(f, "    Use:")?;
        writeln!(f, "      .route(\"/users/{{id}}\", get(handler1))")?;
        writeln!(f, "      .route(\"/users/{{id}}/profile\", get(handler2))")?;
        Ok(())
    }
}

impl std::error::Error for RouteConflictError {}

/// HTTP method router for a single path
pub struct MethodRouter {
    handlers: HashMap<Method, BoxedHandler>,
    pub(crate) operations: HashMap<Method, Operation>,
    pub(crate) component_registrars: Vec<fn(&mut rustapi_openapi::OpenApiSpec)>,
}

impl Clone for MethodRouter {
    fn clone(&self) -> Self {
        Self {
            handlers: self.handlers.clone(),
            operations: self.operations.clone(),
            component_registrars: self.component_registrars.clone(),
        }
    }
}

impl MethodRouter {
    /// Create a new empty method router
    pub fn new() -> Self {
        Self {
            handlers: HashMap::new(),
            operations: HashMap::new(),
            component_registrars: Vec::new(),
        }
    }

    /// Add a handler for a specific method
    fn on(
        mut self,
        method: Method,
        handler: BoxedHandler,
        operation: Operation,
        component_registrar: fn(&mut rustapi_openapi::OpenApiSpec),
    ) -> Self {
        self.handlers.insert(method.clone(), handler);
        self.operations.insert(method, operation);
        self.component_registrars.push(component_registrar);
        self
    }

    /// Get handler for a method
    pub(crate) fn get_handler(&self, method: &Method) -> Option<&BoxedHandler> {
        self.handlers.get(method)
    }

    /// Get allowed methods for 405 response
    pub(crate) fn allowed_methods(&self) -> Vec<Method> {
        self.handlers.keys().cloned().collect()
    }

    /// Create from pre-boxed handlers (internal use)
    pub(crate) fn from_boxed(handlers: HashMap<Method, BoxedHandler>) -> Self {
        Self {
            handlers,
            operations: HashMap::new(), // Operations lost when using raw boxed handlers for now
            component_registrars: Vec::new(),
        }
    }

    /// Insert a pre-boxed handler and its OpenAPI operation (internal use).
    ///
    /// Panics if the same method is inserted twice for the same path.
    pub(crate) fn insert_boxed_with_operation(
        &mut self,
        method: Method,
        handler: BoxedHandler,
        operation: Operation,
        component_registrar: fn(&mut rustapi_openapi::OpenApiSpec),
    ) {
        if self.handlers.contains_key(&method) {
            panic!(
                "Duplicate handler for method {} on the same path",
                method.as_str()
            );
        }

        self.handlers.insert(method.clone(), handler);
        self.operations.insert(method, operation);
        self.component_registrars.push(component_registrar);
    }

    /// Add a GET handler
    pub fn get<H, T>(self, handler: H) -> Self
    where
        H: Handler<T>,
        T: 'static,
    {
        let mut op = Operation::new();
        H::update_operation(&mut op);
        self.on(
            Method::GET,
            into_boxed_handler(handler),
            op,
            <H as Handler<T>>::register_components,
        )
    }

    /// Add a POST handler
    pub fn post<H, T>(self, handler: H) -> Self
    where
        H: Handler<T>,
        T: 'static,
    {
        let mut op = Operation::new();
        H::update_operation(&mut op);
        self.on(
            Method::POST,
            into_boxed_handler(handler),
            op,
            <H as Handler<T>>::register_components,
        )
    }

    /// Add a PUT handler
    pub fn put<H, T>(self, handler: H) -> Self
    where
        H: Handler<T>,
        T: 'static,
    {
        let mut op = Operation::new();
        H::update_operation(&mut op);
        self.on(
            Method::PUT,
            into_boxed_handler(handler),
            op,
            <H as Handler<T>>::register_components,
        )
    }

    /// Add a PATCH handler
    pub fn patch<H, T>(self, handler: H) -> Self
    where
        H: Handler<T>,
        T: 'static,
    {
        let mut op = Operation::new();
        H::update_operation(&mut op);
        self.on(
            Method::PATCH,
            into_boxed_handler(handler),
            op,
            <H as Handler<T>>::register_components,
        )
    }

    /// Add a DELETE handler
    pub fn delete<H, T>(self, handler: H) -> Self
    where
        H: Handler<T>,
        T: 'static,
    {
        let mut op = Operation::new();
        H::update_operation(&mut op);
        self.on(
            Method::DELETE,
            into_boxed_handler(handler),
            op,
            <H as Handler<T>>::register_components,
        )
    }
}

impl Default for MethodRouter {
    fn default() -> Self {
        Self::new()
    }
}

/// Create a GET route handler
pub fn get<H, T>(handler: H) -> MethodRouter
where
    H: Handler<T>,
    T: 'static,
{
    let mut op = Operation::new();
    H::update_operation(&mut op);
    MethodRouter::new().on(
        Method::GET,
        into_boxed_handler(handler),
        op,
        <H as Handler<T>>::register_components,
    )
}

/// Create a POST route handler
pub fn post<H, T>(handler: H) -> MethodRouter
where
    H: Handler<T>,
    T: 'static,
{
    let mut op = Operation::new();
    H::update_operation(&mut op);
    MethodRouter::new().on(
        Method::POST,
        into_boxed_handler(handler),
        op,
        <H as Handler<T>>::register_components,
    )
}

/// Create a PUT route handler
pub fn put<H, T>(handler: H) -> MethodRouter
where
    H: Handler<T>,
    T: 'static,
{
    let mut op = Operation::new();
    H::update_operation(&mut op);
    MethodRouter::new().on(
        Method::PUT,
        into_boxed_handler(handler),
        op,
        <H as Handler<T>>::register_components,
    )
}

/// Create a PATCH route handler
pub fn patch<H, T>(handler: H) -> MethodRouter
where
    H: Handler<T>,
    T: 'static,
{
    let mut op = Operation::new();
    H::update_operation(&mut op);
    MethodRouter::new().on(
        Method::PATCH,
        into_boxed_handler(handler),
        op,
        <H as Handler<T>>::register_components,
    )
}

/// Create a DELETE route handler
pub fn delete<H, T>(handler: H) -> MethodRouter
where
    H: Handler<T>,
    T: 'static,
{
    let mut op = Operation::new();
    H::update_operation(&mut op);
    MethodRouter::new().on(
        Method::DELETE,
        into_boxed_handler(handler),
        op,
        <H as Handler<T>>::register_components,
    )
}

/// Main router
pub struct Router {
    inner: MatchitRouter<MethodRouter>,
    state: Arc<Extensions>,
    /// Track registered routes for conflict detection
    registered_routes: HashMap<String, RouteInfo>,
    /// Store MethodRouters for nesting support (keyed by matchit path)
    method_routers: HashMap<String, MethodRouter>,
    /// Track state type IDs for merging (type name -> whether it's set)
    /// This is a workaround since Extensions doesn't support iteration
    state_type_ids: Vec<std::any::TypeId>,
}

impl Router {
    /// Create a new router
    pub fn new() -> Self {
        Self {
            inner: MatchitRouter::new(),
            state: Arc::new(Extensions::new()),
            registered_routes: HashMap::new(),
            method_routers: HashMap::new(),
            state_type_ids: Vec::new(),
        }
    }

    /// Add a typed route using a TypedPath
    pub fn typed<P: TypedPath>(self, method_router: MethodRouter) -> Self {
        self.route(P::PATH, method_router)
    }

    /// Add a route
    pub fn route(mut self, path: &str, method_router: MethodRouter) -> Self {
        // Convert {param} style to :param for matchit
        let matchit_path = convert_path_params(path);

        // Get the methods being registered
        let methods: Vec<Method> = method_router.handlers.keys().cloned().collect();

        // Store a clone of the MethodRouter for nesting support
        self.method_routers
            .insert(matchit_path.clone(), method_router.clone());

        match self.inner.insert(matchit_path.clone(), method_router) {
            Ok(_) => {
                // Track the registered route
                self.registered_routes.insert(
                    matchit_path.clone(),
                    RouteInfo {
                        path: path.to_string(),
                        methods,
                    },
                );
            }
            Err(e) => {
                // Remove the method_router we just added since registration failed
                self.method_routers.remove(&matchit_path);

                // Find the existing conflicting route
                let existing_path = self
                    .find_conflicting_route(&matchit_path)
                    .map(|info| info.path.clone())
                    .unwrap_or_else(|| "<unknown>".to_string());

                let conflict_error = RouteConflictError {
                    new_path: path.to_string(),
                    method: methods.first().cloned(),
                    existing_path,
                    details: e.to_string(),
                };

                panic!("{}", conflict_error);
            }
        }
        self
    }

    /// Find a conflicting route by checking registered routes
    fn find_conflicting_route(&self, matchit_path: &str) -> Option<&RouteInfo> {
        // Try to find an exact match first
        if let Some(info) = self.registered_routes.get(matchit_path) {
            return Some(info);
        }

        // Try to find a route that would conflict (same structure but different param names)
        let normalized_new = normalize_path_for_comparison(matchit_path);

        for (registered_path, info) in &self.registered_routes {
            let normalized_existing = normalize_path_for_comparison(registered_path);
            if normalized_new == normalized_existing {
                return Some(info);
            }
        }

        None
    }

    /// Add application state
    pub fn state<S: Clone + Send + Sync + 'static>(mut self, state: S) -> Self {
        let type_id = std::any::TypeId::of::<S>();
        let extensions = Arc::make_mut(&mut self.state);
        extensions.insert(state);
        if !self.state_type_ids.contains(&type_id) {
            self.state_type_ids.push(type_id);
        }
        self
    }

    /// Check if state of a given type exists
    pub fn has_state<S: 'static>(&self) -> bool {
        self.state_type_ids.contains(&std::any::TypeId::of::<S>())
    }

    /// Get state type IDs (for testing and debugging)
    pub fn state_type_ids(&self) -> &[std::any::TypeId] {
        &self.state_type_ids
    }

    /// Nest another router under a prefix
    ///
    /// All routes from the nested router will be registered with the prefix
    /// prepended to their paths. State from the nested router is merged into
    /// the parent router (parent state takes precedence for type conflicts).
    ///
    /// # State Merging
    ///
    /// When nesting routers with state:
    /// - If the parent router has state of type T, it is preserved (parent wins)
    /// - If only the nested router has state of type T, it is added to the parent
    /// - State type tracking is merged to enable proper conflict detection
    ///
    /// Note: Due to limitations of `http::Extensions`, automatic state merging
    /// requires using the `merge_state` method for specific types.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use rustapi_core::{Router, get};
    ///
    /// async fn list_users() -> &'static str { "List users" }
    /// async fn get_user() -> &'static str { "Get user" }
    ///
    /// let users_router = Router::new()
    ///     .route("/", get(list_users))
    ///     .route("/{id}", get(get_user));
    ///
    /// let app = Router::new()
    ///     .nest("/api/users", users_router);
    ///
    /// // Routes are now:
    /// // GET /api/users/
    /// // GET /api/users/{id}
    /// ```
    ///
    /// # Nesting with State
    ///
    /// The `nest` method automatically tracks state types from the nested router to prevent
    /// conflicts, but it does NOT automatically merge the state values instance by instance.
    /// You should distinctively add state to the parent, or use `merge_state` if you want
    /// to pull a specific state object from the child.
    ///
    /// ```rust,ignore
    /// use rustapi_core::Router;
    /// use std::sync::Arc;
    ///
    /// #[derive(Clone)]
    /// struct Database { /* ... */ }
    ///
    /// let db = Database { /* ... */ };
    ///
    /// // Option 1: Add state to the parent (Recommended)
    /// let api = Router::new()
    ///     .nest("/v1", Router::new()
    ///         .route("/users", get(list_users))) // Needs Database
    ///     .state(db);
    ///
    /// // Option 2: Define specific state in sub-router and merge explicitly
    /// let sub_router = Router::new()
    ///     .state(Database { /* ... */ })
    ///     .route("/items", get(list_items));
    ///
    /// let app = Router::new()
    ///     .merge_state::<Database>(&sub_router) // Pulls Database from sub_router
    ///     .nest("/api", sub_router);
    /// ```
    pub fn nest(mut self, prefix: &str, router: Router) -> Self {
        // 1. Normalize the prefix
        let normalized_prefix = normalize_prefix(prefix);

        // 2. Merge state type IDs from nested router
        // Parent state takes precedence - we only track types, actual values
        // are handled by merge_state calls or by the user adding state to parent
        for type_id in &router.state_type_ids {
            if !self.state_type_ids.contains(type_id) {
                self.state_type_ids.push(*type_id);
            }
        }

        // 3. Collect routes from the nested router before consuming it
        // We need to iterate over registered_routes and get the corresponding MethodRouters
        let nested_routes: Vec<(String, RouteInfo, MethodRouter)> = router
            .registered_routes
            .into_iter()
            .filter_map(|(matchit_path, route_info)| {
                router
                    .method_routers
                    .get(&matchit_path)
                    .map(|mr| (matchit_path, route_info, mr.clone()))
            })
            .collect();

        // 4. Register each nested route with the prefix
        for (matchit_path, route_info, method_router) in nested_routes {
            // Build the prefixed path
            // The matchit_path already has the :param format
            // The route_info.path has the {param} format
            let prefixed_matchit_path = if matchit_path == "/" {
                normalized_prefix.clone()
            } else {
                format!("{}{}", normalized_prefix, matchit_path)
            };

            let prefixed_display_path = if route_info.path == "/" {
                normalized_prefix.clone()
            } else {
                format!("{}{}", normalized_prefix, route_info.path)
            };

            // Store the MethodRouter for future nesting
            self.method_routers
                .insert(prefixed_matchit_path.clone(), method_router.clone());

            // Try to insert into the matchit router
            match self
                .inner
                .insert(prefixed_matchit_path.clone(), method_router)
            {
                Ok(_) => {
                    // Track the registered route
                    self.registered_routes.insert(
                        prefixed_matchit_path,
                        RouteInfo {
                            path: prefixed_display_path,
                            methods: route_info.methods,
                        },
                    );
                }
                Err(e) => {
                    // Remove the method_router we just added since registration failed
                    self.method_routers.remove(&prefixed_matchit_path);

                    // Find the existing conflicting route
                    let existing_path = self
                        .find_conflicting_route(&prefixed_matchit_path)
                        .map(|info| info.path.clone())
                        .unwrap_or_else(|| "<unknown>".to_string());

                    let conflict_error = RouteConflictError {
                        new_path: prefixed_display_path,
                        method: route_info.methods.first().cloned(),
                        existing_path,
                        details: e.to_string(),
                    };

                    panic!("{}", conflict_error);
                }
            }
        }

        self
    }

    /// Merge state from another router into this one
    ///
    /// This method allows explicit state merging when nesting routers.
    /// Parent state takes precedence - if the parent already has state of type S,
    /// the nested state is ignored.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// #[derive(Clone)]
    /// struct DbPool(String);
    ///
    /// let nested = Router::new().state(DbPool("nested".to_string()));
    /// let parent = Router::new()
    ///     .merge_state::<DbPool>(&nested); // Adds DbPool from nested
    /// ```
    pub fn merge_state<S: Clone + Send + Sync + 'static>(mut self, other: &Router) -> Self {
        let type_id = std::any::TypeId::of::<S>();

        // Parent wins - only merge if parent doesn't have this state type
        if !self.state_type_ids.contains(&type_id) {
            // Try to get the state from the other router
            if let Some(state) = other.state.get::<S>() {
                let extensions = Arc::make_mut(&mut self.state);
                extensions.insert(state.clone());
                self.state_type_ids.push(type_id);
            }
        }

        self
    }

    /// Match a request and return the handler + params
    pub fn match_route(&self, path: &str, method: &Method) -> RouteMatch<'_> {
        match self.inner.at(path) {
            Ok(matched) => {
                let method_router = matched.value;

                if let Some(handler) = method_router.get_handler(method) {
                    // Use stack-optimized PathParams (avoids heap allocation for ≤4 params)
                    let params: PathParams = matched
                        .params
                        .iter()
                        .map(|(k, v)| (k.to_string(), v.to_string()))
                        .collect();

                    RouteMatch::Found { handler, params }
                } else {
                    RouteMatch::MethodNotAllowed {
                        allowed: method_router.allowed_methods(),
                    }
                }
            }
            Err(_) => RouteMatch::NotFound,
        }
    }

    /// Get shared state
    pub fn state_ref(&self) -> Arc<Extensions> {
        self.state.clone()
    }

    /// Get registered routes (for testing and debugging)
    pub fn registered_routes(&self) -> &HashMap<String, RouteInfo> {
        &self.registered_routes
    }

    /// Get method routers (for OpenAPI integration during nesting)
    pub fn method_routers(&self) -> &HashMap<String, MethodRouter> {
        &self.method_routers
    }
}

impl Default for Router {
    fn default() -> Self {
        Self::new()
    }
}

/// Result of route matching
pub enum RouteMatch<'a> {
    Found {
        handler: &'a BoxedHandler,
        params: PathParams,
    },
    NotFound,
    MethodNotAllowed {
        allowed: Vec<Method>,
    },
}

/// Convert {param} style to :param for matchit
fn convert_path_params(path: &str) -> String {
    let mut result = String::with_capacity(path.len());

    for ch in path.chars() {
        match ch {
            '{' => {
                result.push(':');
            }
            '}' => {
                // Skip closing brace
            }
            _ => {
                result.push(ch);
            }
        }
    }

    result
}

/// Normalize a path for conflict comparison by replacing parameter names with a placeholder
fn normalize_path_for_comparison(path: &str) -> String {
    let mut result = String::with_capacity(path.len());
    let mut in_param = false;

    for ch in path.chars() {
        match ch {
            ':' => {
                in_param = true;
                result.push_str(":_");
            }
            '/' => {
                in_param = false;
                result.push('/');
            }
            _ if in_param => {
                // Skip parameter name characters
            }
            _ => {
                result.push(ch);
            }
        }
    }

    result
}

/// Normalize a prefix for router nesting.
///
/// Ensures the prefix:
/// - Starts with exactly one leading slash
/// - Has no trailing slash (unless it's just "/")
/// - Has no double slashes
///
/// # Examples
///
/// ```ignore
/// assert_eq!(normalize_prefix("api"), "/api");
/// assert_eq!(normalize_prefix("/api"), "/api");
/// assert_eq!(normalize_prefix("/api/"), "/api");
/// assert_eq!(normalize_prefix("//api//"), "/api");
/// assert_eq!(normalize_prefix(""), "/");
/// ```
pub(crate) fn normalize_prefix(prefix: &str) -> String {
    // Handle empty string
    if prefix.is_empty() {
        return "/".to_string();
    }

    // Split by slashes and filter out empty segments (handles multiple slashes)
    let segments: Vec<&str> = prefix.split('/').filter(|s| !s.is_empty()).collect();

    // If no segments after filtering, return root
    if segments.is_empty() {
        return "/".to_string();
    }

    // Build the normalized prefix with leading slash
    let mut result = String::with_capacity(prefix.len() + 1);
    for segment in segments {
        result.push('/');
        result.push_str(segment);
    }

    result
}

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

    #[test]
    fn test_convert_path_params() {
        assert_eq!(convert_path_params("/users/{id}"), "/users/:id");
        assert_eq!(
            convert_path_params("/users/{user_id}/posts/{post_id}"),
            "/users/:user_id/posts/:post_id"
        );
        assert_eq!(convert_path_params("/static/path"), "/static/path");
    }

    #[test]
    fn test_normalize_path_for_comparison() {
        assert_eq!(normalize_path_for_comparison("/users/:id"), "/users/:_");
        assert_eq!(
            normalize_path_for_comparison("/users/:user_id"),
            "/users/:_"
        );
        assert_eq!(
            normalize_path_for_comparison("/users/:id/posts/:post_id"),
            "/users/:_/posts/:_"
        );
        assert_eq!(
            normalize_path_for_comparison("/static/path"),
            "/static/path"
        );
    }

    #[test]
    fn test_normalize_prefix() {
        // Basic cases
        assert_eq!(normalize_prefix("api"), "/api");
        assert_eq!(normalize_prefix("/api"), "/api");
        assert_eq!(normalize_prefix("/api/"), "/api");
        assert_eq!(normalize_prefix("api/"), "/api");

        // Multiple segments
        assert_eq!(normalize_prefix("api/v1"), "/api/v1");
        assert_eq!(normalize_prefix("/api/v1"), "/api/v1");
        assert_eq!(normalize_prefix("/api/v1/"), "/api/v1");

        // Edge cases: empty and root
        assert_eq!(normalize_prefix(""), "/");
        assert_eq!(normalize_prefix("/"), "/");

        // Multiple slashes
        assert_eq!(normalize_prefix("//api"), "/api");
        assert_eq!(normalize_prefix("api//v1"), "/api/v1");
        assert_eq!(normalize_prefix("//api//v1//"), "/api/v1");
        assert_eq!(normalize_prefix("///"), "/");
    }

    #[test]
    #[should_panic(expected = "ROUTE CONFLICT DETECTED")]
    fn test_route_conflict_detection() {
        async fn handler1() -> &'static str {
            "handler1"
        }
        async fn handler2() -> &'static str {
            "handler2"
        }

        let _router = Router::new()
            .route("/users/{id}", get(handler1))
            .route("/users/{user_id}", get(handler2)); // This should panic
    }

    #[test]
    fn test_no_conflict_different_paths() {
        async fn handler1() -> &'static str {
            "handler1"
        }
        async fn handler2() -> &'static str {
            "handler2"
        }

        let router = Router::new()
            .route("/users/{id}", get(handler1))
            .route("/users/{id}/profile", get(handler2));

        assert_eq!(router.registered_routes().len(), 2);
    }

    #[test]
    fn test_route_info_tracking() {
        async fn handler() -> &'static str {
            "handler"
        }

        let router = Router::new().route("/users/{id}", get(handler));

        let routes = router.registered_routes();
        assert_eq!(routes.len(), 1);

        let info = routes.get("/users/:id").unwrap();
        assert_eq!(info.path, "/users/{id}");
        assert_eq!(info.methods.len(), 1);
        assert_eq!(info.methods[0], Method::GET);
    }

    #[test]
    fn test_basic_router_nesting() {
        async fn list_users() -> &'static str {
            "list users"
        }
        async fn get_user() -> &'static str {
            "get user"
        }

        let users_router = Router::new()
            .route("/", get(list_users))
            .route("/{id}", get(get_user));

        let app = Router::new().nest("/api/users", users_router);

        let routes = app.registered_routes();
        assert_eq!(routes.len(), 2);

        // Check that routes are registered with prefix
        assert!(routes.contains_key("/api/users"));
        assert!(routes.contains_key("/api/users/:id"));

        // Check display paths
        let list_info = routes.get("/api/users").unwrap();
        assert_eq!(list_info.path, "/api/users");

        let get_info = routes.get("/api/users/:id").unwrap();
        assert_eq!(get_info.path, "/api/users/{id}");
    }

    #[test]
    fn test_nested_route_matching() {
        async fn handler() -> &'static str {
            "handler"
        }

        let users_router = Router::new().route("/{id}", get(handler));

        let app = Router::new().nest("/api/users", users_router);

        // Test that the route can be matched
        match app.match_route("/api/users/123", &Method::GET) {
            RouteMatch::Found { params, .. } => {
                assert_eq!(params.get("id"), Some(&"123".to_string()));
            }
            _ => panic!("Route should be found"),
        }
    }

    #[test]
    fn test_nested_route_matching_multiple_params() {
        async fn handler() -> &'static str {
            "handler"
        }

        let posts_router = Router::new().route("/{user_id}/posts/{post_id}", get(handler));

        let app = Router::new().nest("/api", posts_router);

        // Test that multiple parameters are correctly extracted
        match app.match_route("/api/42/posts/100", &Method::GET) {
            RouteMatch::Found { params, .. } => {
                assert_eq!(params.get("user_id"), Some(&"42".to_string()));
                assert_eq!(params.get("post_id"), Some(&"100".to_string()));
            }
            _ => panic!("Route should be found"),
        }
    }

    #[test]
    fn test_nested_route_matching_static_path() {
        async fn handler() -> &'static str {
            "handler"
        }

        let health_router = Router::new().route("/health", get(handler));

        let app = Router::new().nest("/api/v1", health_router);

        // Test that static paths are correctly matched
        match app.match_route("/api/v1/health", &Method::GET) {
            RouteMatch::Found { params, .. } => {
                assert!(params.is_empty(), "Static path should have no params");
            }
            _ => panic!("Route should be found"),
        }
    }

    #[test]
    fn test_nested_route_not_found() {
        async fn handler() -> &'static str {
            "handler"
        }

        let users_router = Router::new().route("/users", get(handler));

        let app = Router::new().nest("/api", users_router);

        // Test that non-existent paths return NotFound
        match app.match_route("/api/posts", &Method::GET) {
            RouteMatch::NotFound => {
                // Expected
            }
            _ => panic!("Route should not be found"),
        }

        // Test that wrong prefix returns NotFound
        match app.match_route("/v2/users", &Method::GET) {
            RouteMatch::NotFound => {
                // Expected
            }
            _ => panic!("Route with wrong prefix should not be found"),
        }
    }

    #[test]
    fn test_nested_route_method_not_allowed() {
        async fn handler() -> &'static str {
            "handler"
        }

        let users_router = Router::new().route("/users", get(handler));

        let app = Router::new().nest("/api", users_router);

        // Test that wrong method returns MethodNotAllowed
        match app.match_route("/api/users", &Method::POST) {
            RouteMatch::MethodNotAllowed { allowed } => {
                assert!(allowed.contains(&Method::GET));
                assert!(!allowed.contains(&Method::POST));
            }
            _ => panic!("Should return MethodNotAllowed"),
        }
    }

    #[test]
    fn test_nested_route_multiple_methods() {
        async fn get_handler() -> &'static str {
            "get"
        }
        async fn post_handler() -> &'static str {
            "post"
        }

        // Create a method router with both GET and POST
        let get_router = get(get_handler);
        let post_router = post(post_handler);
        let mut combined = MethodRouter::new();
        for (method, handler) in get_router.handlers {
            combined.handlers.insert(method, handler);
        }
        for (method, handler) in post_router.handlers {
            combined.handlers.insert(method, handler);
        }

        let users_router = Router::new().route("/users", combined);
        let app = Router::new().nest("/api", users_router);

        // Both GET and POST should work
        match app.match_route("/api/users", &Method::GET) {
            RouteMatch::Found { .. } => {}
            _ => panic!("GET should be found"),
        }

        match app.match_route("/api/users", &Method::POST) {
            RouteMatch::Found { .. } => {}
            _ => panic!("POST should be found"),
        }

        // DELETE should return MethodNotAllowed with GET and POST in allowed
        match app.match_route("/api/users", &Method::DELETE) {
            RouteMatch::MethodNotAllowed { allowed } => {
                assert!(allowed.contains(&Method::GET));
                assert!(allowed.contains(&Method::POST));
            }
            _ => panic!("DELETE should return MethodNotAllowed"),
        }
    }

    #[test]
    fn test_nested_router_prefix_normalization() {
        async fn handler() -> &'static str {
            "handler"
        }

        // Test various prefix formats
        let router1 = Router::new().route("/test", get(handler));
        let app1 = Router::new().nest("api", router1);
        assert!(app1.registered_routes().contains_key("/api/test"));

        let router2 = Router::new().route("/test", get(handler));
        let app2 = Router::new().nest("/api/", router2);
        assert!(app2.registered_routes().contains_key("/api/test"));

        let router3 = Router::new().route("/test", get(handler));
        let app3 = Router::new().nest("//api//", router3);
        assert!(app3.registered_routes().contains_key("/api/test"));
    }

    #[test]
    fn test_state_tracking() {
        #[derive(Clone)]
        struct MyState(#[allow(dead_code)] String);

        let router = Router::new().state(MyState("test".to_string()));

        assert!(router.has_state::<MyState>());
        assert!(!router.has_state::<String>());
    }

    #[test]
    fn test_state_merge_nested_only() {
        #[derive(Clone, PartialEq, Debug)]
        struct NestedState(String);

        async fn handler() -> &'static str {
            "handler"
        }

        // Create a router with state to use as source for merging
        let state_source = Router::new().state(NestedState("nested".to_string()));

        let nested = Router::new().route("/test", get(handler));

        let parent = Router::new()
            .nest("/api", nested)
            .merge_state::<NestedState>(&state_source);

        // Parent should now have the nested state
        assert!(parent.has_state::<NestedState>());

        // Verify the state value
        let state = parent.state.get::<NestedState>().unwrap();
        assert_eq!(state.0, "nested");
    }

    #[test]
    fn test_state_merge_parent_wins() {
        #[derive(Clone, PartialEq, Debug)]
        struct SharedState(String);

        async fn handler() -> &'static str {
            "handler"
        }

        // Create a router with state to use as source for merging
        let state_source = Router::new().state(SharedState("nested".to_string()));

        let nested = Router::new().route("/test", get(handler));

        let parent = Router::new()
            .state(SharedState("parent".to_string()))
            .nest("/api", nested)
            .merge_state::<SharedState>(&state_source);

        // Parent should still have its own state (parent wins)
        assert!(parent.has_state::<SharedState>());

        // Verify the state value is from parent
        let state = parent.state.get::<SharedState>().unwrap();
        assert_eq!(state.0, "parent");
    }

    #[test]
    fn test_state_type_ids_merged_on_nest() {
        #[derive(Clone)]
        struct NestedState(#[allow(dead_code)] String);

        async fn handler() -> &'static str {
            "handler"
        }

        let nested = Router::new()
            .route("/test", get(handler))
            .state(NestedState("nested".to_string()));

        let parent = Router::new().nest("/api", nested);

        // Parent should track the nested state type ID
        assert!(parent
            .state_type_ids()
            .contains(&std::any::TypeId::of::<NestedState>()));
    }

    #[test]
    #[should_panic(expected = "ROUTE CONFLICT DETECTED")]
    fn test_nested_route_conflict_with_existing_route() {
        async fn handler1() -> &'static str {
            "handler1"
        }
        async fn handler2() -> &'static str {
            "handler2"
        }

        // Create a parent router with an existing route
        let parent = Router::new().route("/api/users/{id}", get(handler1));

        // Create a nested router with a conflicting route
        let nested = Router::new().route("/{user_id}", get(handler2));

        // This should panic because /api/users/{id} conflicts with /api/users/{user_id}
        let _app = parent.nest("/api/users", nested);
    }

    #[test]
    #[should_panic(expected = "ROUTE CONFLICT DETECTED")]
    fn test_nested_route_conflict_same_path_different_param_names() {
        async fn handler1() -> &'static str {
            "handler1"
        }
        async fn handler2() -> &'static str {
            "handler2"
        }

        // Create two nested routers with same path structure but different param names
        let nested1 = Router::new().route("/{id}", get(handler1));
        let nested2 = Router::new().route("/{user_id}", get(handler2));

        // Nest both under the same prefix - should conflict
        let _app = Router::new()
            .nest("/api/users", nested1)
            .nest("/api/users", nested2);
    }

    #[test]
    fn test_nested_route_conflict_error_contains_both_paths() {
        use std::panic::{catch_unwind, AssertUnwindSafe};

        async fn handler1() -> &'static str {
            "handler1"
        }
        async fn handler2() -> &'static str {
            "handler2"
        }

        let result = catch_unwind(AssertUnwindSafe(|| {
            let parent = Router::new().route("/api/users/{id}", get(handler1));
            let nested = Router::new().route("/{user_id}", get(handler2));
            let _app = parent.nest("/api/users", nested);
        }));

        assert!(result.is_err(), "Should have panicked due to conflict");

        if let Err(panic_info) = result {
            if let Some(msg) = panic_info.downcast_ref::<String>() {
                assert!(
                    msg.contains("ROUTE CONFLICT DETECTED"),
                    "Error should contain 'ROUTE CONFLICT DETECTED'"
                );
                assert!(
                    msg.contains("Existing:") && msg.contains("New:"),
                    "Error should contain both 'Existing:' and 'New:' labels"
                );
                assert!(
                    msg.contains("How to resolve:"),
                    "Error should contain resolution guidance"
                );
            }
        }
    }

    #[test]
    fn test_nested_routes_no_conflict_different_prefixes() {
        async fn handler1() -> &'static str {
            "handler1"
        }
        async fn handler2() -> &'static str {
            "handler2"
        }

        // Create two nested routers with same internal paths but different prefixes
        let nested1 = Router::new().route("/{id}", get(handler1));
        let nested2 = Router::new().route("/{id}", get(handler2));

        // Nest under different prefixes - should NOT conflict
        let app = Router::new()
            .nest("/api/users", nested1)
            .nest("/api/posts", nested2);

        assert_eq!(app.registered_routes().len(), 2);
        assert!(app.registered_routes().contains_key("/api/users/:id"));
        assert!(app.registered_routes().contains_key("/api/posts/:id"));
    }

    // **Feature: router-nesting, Property 4: Multiple Router Composition**
    // Tests for nesting multiple routers under different prefixes
    // **Validates: Requirements 1.5**

    #[test]
    fn test_multiple_router_composition_all_routes_registered() {
        async fn users_list() -> &'static str {
            "users list"
        }
        async fn users_get() -> &'static str {
            "users get"
        }
        async fn posts_list() -> &'static str {
            "posts list"
        }
        async fn posts_get() -> &'static str {
            "posts get"
        }
        async fn comments_list() -> &'static str {
            "comments list"
        }

        // Create multiple sub-routers with different routes
        let users_router = Router::new()
            .route("/", get(users_list))
            .route("/{id}", get(users_get));

        let posts_router = Router::new()
            .route("/", get(posts_list))
            .route("/{id}", get(posts_get));

        let comments_router = Router::new().route("/", get(comments_list));

        // Nest all routers under different prefixes
        let app = Router::new()
            .nest("/api/users", users_router)
            .nest("/api/posts", posts_router)
            .nest("/api/comments", comments_router);

        // Verify all routes are registered (2 + 2 + 1 = 5 routes)
        let routes = app.registered_routes();
        assert_eq!(routes.len(), 5, "Should have 5 routes registered");

        // Verify users routes
        assert!(
            routes.contains_key("/api/users"),
            "Should have /api/users route"
        );
        assert!(
            routes.contains_key("/api/users/:id"),
            "Should have /api/users/:id route"
        );

        // Verify posts routes
        assert!(
            routes.contains_key("/api/posts"),
            "Should have /api/posts route"
        );
        assert!(
            routes.contains_key("/api/posts/:id"),
            "Should have /api/posts/:id route"
        );

        // Verify comments routes
        assert!(
            routes.contains_key("/api/comments"),
            "Should have /api/comments route"
        );
    }

    #[test]
    fn test_multiple_router_composition_no_interference() {
        async fn users_handler() -> &'static str {
            "users"
        }
        async fn posts_handler() -> &'static str {
            "posts"
        }
        async fn admin_handler() -> &'static str {
            "admin"
        }

        // Create routers with same internal structure but different prefixes
        let users_router = Router::new()
            .route("/list", get(users_handler))
            .route("/{id}", get(users_handler));

        let posts_router = Router::new()
            .route("/list", get(posts_handler))
            .route("/{id}", get(posts_handler));

        let admin_router = Router::new()
            .route("/list", get(admin_handler))
            .route("/{id}", get(admin_handler));

        // Nest all routers
        let app = Router::new()
            .nest("/api/v1/users", users_router)
            .nest("/api/v1/posts", posts_router)
            .nest("/admin", admin_router);

        // Verify all routes are registered (2 + 2 + 2 = 6 routes)
        let routes = app.registered_routes();
        assert_eq!(routes.len(), 6, "Should have 6 routes registered");

        // Verify each prefix group has its routes
        assert!(routes.contains_key("/api/v1/users/list"));
        assert!(routes.contains_key("/api/v1/users/:id"));
        assert!(routes.contains_key("/api/v1/posts/list"));
        assert!(routes.contains_key("/api/v1/posts/:id"));
        assert!(routes.contains_key("/admin/list"));
        assert!(routes.contains_key("/admin/:id"));

        // Verify routes are matchable and don't interfere with each other
        match app.match_route("/api/v1/users/list", &Method::GET) {
            RouteMatch::Found { params, .. } => {
                assert!(params.is_empty(), "Static path should have no params");
            }
            _ => panic!("Should find /api/v1/users/list"),
        }

        match app.match_route("/api/v1/posts/123", &Method::GET) {
            RouteMatch::Found { params, .. } => {
                assert_eq!(params.get("id"), Some(&"123".to_string()));
            }
            _ => panic!("Should find /api/v1/posts/123"),
        }

        match app.match_route("/admin/456", &Method::GET) {
            RouteMatch::Found { params, .. } => {
                assert_eq!(params.get("id"), Some(&"456".to_string()));
            }
            _ => panic!("Should find /admin/456"),
        }
    }

    #[test]
    fn test_multiple_router_composition_with_multiple_methods() {
        async fn get_handler() -> &'static str {
            "get"
        }
        async fn post_handler() -> &'static str {
            "post"
        }
        async fn put_handler() -> &'static str {
            "put"
        }

        // Create routers with multiple HTTP methods
        // Combine GET and POST for users root
        let get_router = get(get_handler);
        let post_router = post(post_handler);
        let mut users_root_combined = MethodRouter::new();
        for (method, handler) in get_router.handlers {
            users_root_combined.handlers.insert(method, handler);
        }
        for (method, handler) in post_router.handlers {
            users_root_combined.handlers.insert(method, handler);
        }

        // Combine GET and PUT for users/{id}
        let get_router2 = get(get_handler);
        let put_router = put(put_handler);
        let mut users_id_combined = MethodRouter::new();
        for (method, handler) in get_router2.handlers {
            users_id_combined.handlers.insert(method, handler);
        }
        for (method, handler) in put_router.handlers {
            users_id_combined.handlers.insert(method, handler);
        }

        let users_router = Router::new()
            .route("/", users_root_combined)
            .route("/{id}", users_id_combined);

        // Combine GET and POST for posts root
        let get_router3 = get(get_handler);
        let post_router2 = post(post_handler);
        let mut posts_root_combined = MethodRouter::new();
        for (method, handler) in get_router3.handlers {
            posts_root_combined.handlers.insert(method, handler);
        }
        for (method, handler) in post_router2.handlers {
            posts_root_combined.handlers.insert(method, handler);
        }

        let posts_router = Router::new().route("/", posts_root_combined);

        // Nest routers
        let app = Router::new()
            .nest("/users", users_router)
            .nest("/posts", posts_router);

        // Verify routes are registered
        let routes = app.registered_routes();
        assert_eq!(routes.len(), 3, "Should have 3 routes registered");

        // Verify methods are preserved for users routes
        let users_root = routes.get("/users").unwrap();
        assert!(users_root.methods.contains(&Method::GET));
        assert!(users_root.methods.contains(&Method::POST));

        let users_id = routes.get("/users/:id").unwrap();
        assert!(users_id.methods.contains(&Method::GET));
        assert!(users_id.methods.contains(&Method::PUT));

        // Verify methods are preserved for posts routes
        let posts_root = routes.get("/posts").unwrap();
        assert!(posts_root.methods.contains(&Method::GET));
        assert!(posts_root.methods.contains(&Method::POST));

        // Verify route matching works for all methods
        match app.match_route("/users", &Method::GET) {
            RouteMatch::Found { .. } => {}
            _ => panic!("GET /users should be found"),
        }
        match app.match_route("/users", &Method::POST) {
            RouteMatch::Found { .. } => {}
            _ => panic!("POST /users should be found"),
        }
        match app.match_route("/users/123", &Method::PUT) {
            RouteMatch::Found { .. } => {}
            _ => panic!("PUT /users/123 should be found"),
        }
    }

    #[test]
    fn test_multiple_router_composition_deep_nesting() {
        async fn handler() -> &'static str {
            "handler"
        }

        // Create nested routers at different depth levels
        let deep_router = Router::new().route("/action", get(handler));

        let mid_router = Router::new().route("/info", get(handler));

        let shallow_router = Router::new().route("/status", get(handler));

        // Nest at different depths
        let app = Router::new()
            .nest("/api/v1/resources/items", deep_router)
            .nest("/api/v1/resources", mid_router)
            .nest("/api", shallow_router);

        // Verify all routes are registered
        let routes = app.registered_routes();
        assert_eq!(routes.len(), 3, "Should have 3 routes registered");

        assert!(routes.contains_key("/api/v1/resources/items/action"));
        assert!(routes.contains_key("/api/v1/resources/info"));
        assert!(routes.contains_key("/api/status"));

        // Verify all routes are matchable
        match app.match_route("/api/v1/resources/items/action", &Method::GET) {
            RouteMatch::Found { .. } => {}
            _ => panic!("Should find deep route"),
        }
        match app.match_route("/api/v1/resources/info", &Method::GET) {
            RouteMatch::Found { .. } => {}
            _ => panic!("Should find mid route"),
        }
        match app.match_route("/api/status", &Method::GET) {
            RouteMatch::Found { .. } => {}
            _ => panic!("Should find shallow route"),
        }
    }
}

#[cfg(test)]
mod property_tests {
    use super::*;
    use proptest::prelude::*;
    use std::panic::{catch_unwind, AssertUnwindSafe};

    // **Feature: router-nesting, Property 2: Prefix Normalization**
    //
    // For any prefix string (with or without leading/trailing slashes), the normalized
    // prefix should start with exactly one slash and have no trailing slash, and all
    // nested routes should have properly formed paths without double slashes.
    //
    // **Validates: Requirements 1.2, 1.3**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Property: Normalized prefix always starts with exactly one slash
        ///
        /// For any input prefix, the normalized result should always start with
        /// exactly one leading slash.
        #[test]
        fn prop_normalized_prefix_starts_with_single_slash(
            // Generate prefix with optional leading slashes
            leading_slashes in prop::collection::vec(Just('/'), 0..5),
            segments in prop::collection::vec("[a-z][a-z0-9]{0,5}", 0..4),
            trailing_slashes in prop::collection::vec(Just('/'), 0..5),
        ) {
            // Build the input prefix
            let mut prefix = String::new();
            for _ in &leading_slashes {
                prefix.push('/');
            }
            for (i, segment) in segments.iter().enumerate() {
                if i > 0 {
                    prefix.push('/');
                }
                prefix.push_str(segment);
            }
            for _ in &trailing_slashes {
                prefix.push('/');
            }

            let normalized = normalize_prefix(&prefix);

            // Property 1: Always starts with exactly one slash
            prop_assert!(
                normalized.starts_with('/'),
                "Normalized prefix '{}' should start with '/', input was '{}'",
                normalized, prefix
            );

            // Property 2: No double slashes at the start
            prop_assert!(
                !normalized.starts_with("//"),
                "Normalized prefix '{}' should not start with '//', input was '{}'",
                normalized, prefix
            );
        }

        /// Property: Normalized prefix has no trailing slash (unless root)
        ///
        /// For any input prefix with non-empty segments, the normalized result
        /// should have no trailing slash.
        #[test]
        fn prop_normalized_prefix_no_trailing_slash(
            segments in prop::collection::vec("[a-z][a-z0-9]{0,5}", 1..4),
            trailing_slashes in prop::collection::vec(Just('/'), 0..5),
        ) {
            // Build the input prefix with segments
            let mut prefix = String::from("/");
            for (i, segment) in segments.iter().enumerate() {
                if i > 0 {
                    prefix.push('/');
                }
                prefix.push_str(segment);
            }
            for _ in &trailing_slashes {
                prefix.push('/');
            }

            let normalized = normalize_prefix(&prefix);

            // Property: No trailing slash when there are segments
            prop_assert!(
                !normalized.ends_with('/'),
                "Normalized prefix '{}' should not end with '/', input was '{}'",
                normalized, prefix
            );
        }

        /// Property: Normalized prefix has no double slashes
        ///
        /// For any input prefix, the normalized result should never contain
        /// consecutive slashes.
        #[test]
        fn prop_normalized_prefix_no_double_slashes(
            // Generate prefix with random slashes between segments
            segments in prop::collection::vec("[a-z][a-z0-9]{0,5}", 1..4),
            extra_slashes in prop::collection::vec(0..4usize, 1..4),
        ) {
            // Build the input prefix with extra slashes between segments
            let mut prefix = String::from("/");
            for (i, segment) in segments.iter().enumerate() {
                if i > 0 {
                    // Add extra slashes between segments
                    let num_slashes = extra_slashes.get(i).copied().unwrap_or(1);
                    for _ in 0..=num_slashes {
                        prefix.push('/');
                    }
                }
                prefix.push_str(segment);
            }

            let normalized = normalize_prefix(&prefix);

            // Property: No consecutive slashes
            prop_assert!(
                !normalized.contains("//"),
                "Normalized prefix '{}' should not contain '//', input was '{}'",
                normalized, prefix
            );
        }

        /// Property: Prefix normalization preserves segment content
        ///
        /// For any input prefix, all non-empty segments should be preserved
        /// in the normalized output in the same order.
        #[test]
        fn prop_normalized_prefix_preserves_segments(
            segments in prop::collection::vec("[a-z][a-z0-9]{1,5}", 1..4),
        ) {
            // Build the input prefix
            let prefix = format!("/{}", segments.join("/"));

            let normalized = normalize_prefix(&prefix);

            // Extract segments from normalized prefix
            let normalized_segments: Vec<&str> = normalized
                .split('/')
                .filter(|s| !s.is_empty())
                .collect();

            prop_assert_eq!(
                segments.len(),
                normalized_segments.len(),
                "Segment count should be preserved"
            );

            for (original, normalized_seg) in segments.iter().zip(normalized_segments.iter()) {
                prop_assert_eq!(
                    original, normalized_seg,
                    "Segment content should be preserved"
                );
            }
        }

        /// Property: Empty or slash-only input normalizes to root
        ///
        /// For any input that contains only slashes or is empty, the normalized
        /// result should be exactly "/".
        #[test]
        fn prop_empty_or_slashes_normalize_to_root(
            num_slashes in 0..10usize,
        ) {
            let prefix = "/".repeat(num_slashes);

            let normalized = normalize_prefix(&prefix);

            prop_assert_eq!(
                normalized, "/",
                "Empty or slash-only prefix '{}' should normalize to '/'",
                prefix
            );
        }
    }

    // **Feature: router-nesting, Property 3: HTTP Method Preservation**
    //
    // For any router with routes having multiple HTTP methods, cloning the MethodRouter
    // should preserve all method handlers for each route.
    //
    // **Validates: Requirements 1.4**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Property: Cloning a MethodRouter preserves all HTTP method handlers
        ///
        /// For any combination of HTTP methods registered on a MethodRouter,
        /// cloning should preserve all handlers and their associated methods.
        #[test]
        fn prop_method_router_clone_preserves_methods(
            // Generate a random subset of HTTP methods to register
            use_get in any::<bool>(),
            use_post in any::<bool>(),
            use_put in any::<bool>(),
            use_patch in any::<bool>(),
            use_delete in any::<bool>(),
        ) {
            // Ensure at least one method is selected
            prop_assume!(use_get || use_post || use_put || use_patch || use_delete);

            // Build a MethodRouter with the selected methods
            let mut method_router = MethodRouter::new();
            let mut expected_methods: Vec<Method> = Vec::new();

            async fn handler() -> &'static str { "handler" }

            if use_get {
                method_router = get(handler);
                expected_methods.push(Method::GET);
            }

            if use_post {
                let post_router = post(handler);
                for (method, handler) in post_router.handlers {
                    method_router.handlers.insert(method.clone(), handler);
                    if !expected_methods.contains(&method) {
                        expected_methods.push(method);
                    }
                }
            }

            if use_put {
                let put_router = put(handler);
                for (method, handler) in put_router.handlers {
                    method_router.handlers.insert(method.clone(), handler);
                    if !expected_methods.contains(&method) {
                        expected_methods.push(method);
                    }
                }
            }

            if use_patch {
                let patch_router = patch(handler);
                for (method, handler) in patch_router.handlers {
                    method_router.handlers.insert(method.clone(), handler);
                    if !expected_methods.contains(&method) {
                        expected_methods.push(method);
                    }
                }
            }

            if use_delete {
                let delete_router = delete(handler);
                for (method, handler) in delete_router.handlers {
                    method_router.handlers.insert(method.clone(), handler);
                    if !expected_methods.contains(&method) {
                        expected_methods.push(method);
                    }
                }
            }

            // Clone the MethodRouter
            let cloned_router = method_router.clone();

            // Verify all methods are preserved in the clone
            let original_methods = method_router.allowed_methods();
            let cloned_methods = cloned_router.allowed_methods();

            prop_assert_eq!(
                original_methods.len(),
                cloned_methods.len(),
                "Cloned router should have same number of methods"
            );

            for method in &expected_methods {
                prop_assert!(
                    cloned_router.get_handler(method).is_some(),
                    "Cloned router should have handler for method {:?}",
                    method
                );
            }

            // Verify handlers are accessible (not null/invalid)
            for method in &cloned_methods {
                prop_assert!(
                    cloned_router.get_handler(method).is_some(),
                    "Handler for {:?} should be accessible after clone",
                    method
                );
            }
        }
    }

    // **Feature: router-nesting, Property 1: Route Registration with Prefix**
    //
    // For any router with routes and any valid prefix, nesting the router should
    // result in all routes being registered with the prefix prepended to their
    // original paths.
    //
    // **Validates: Requirements 1.1**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Property: All nested routes are registered with prefix prepended
        ///
        /// For any router with routes and any valid prefix, nesting should result
        /// in all routes being registered with the prefix prepended.
        #[test]
        fn prop_nested_routes_have_prefix(
            // Generate prefix segments
            prefix_segments in prop::collection::vec("[a-z][a-z0-9]{0,5}", 1..3),
            // Generate route path segments
            route_segments in prop::collection::vec("[a-z][a-z0-9]{0,5}", 1..3),
            has_param in any::<bool>(),
        ) {
            async fn handler() -> &'static str { "handler" }

            // Build the prefix
            let prefix = format!("/{}", prefix_segments.join("/"));

            // Build the route path
            let mut route_path = format!("/{}", route_segments.join("/"));
            if has_param {
                route_path.push_str("/{id}");
            }

            // Create nested router and nest it
            let nested_router = Router::new().route(&route_path, get(handler));
            let app = Router::new().nest(&prefix, nested_router);

            // Build expected prefixed path (matchit format)
            let expected_matchit_path = if has_param {
                format!("{}/{}/:id", prefix, route_segments.join("/"))
            } else {
                format!("{}/{}", prefix, route_segments.join("/"))
            };

            let routes = app.registered_routes();

            // Property: The prefixed route should exist
            prop_assert!(
                routes.contains_key(&expected_matchit_path),
                "Expected route '{}' not found. Available routes: {:?}",
                expected_matchit_path,
                routes.keys().collect::<Vec<_>>()
            );

            // Property: The route info should have the correct display path
            let route_info = routes.get(&expected_matchit_path).unwrap();
            let expected_display_path = format!("{}{}", prefix, route_path);
            prop_assert_eq!(
                &route_info.path, &expected_display_path,
                "Display path should be prefix + original path"
            );
        }

        /// Property: Number of routes is preserved after nesting
        ///
        /// For any router with N routes, nesting should result in exactly N routes
        /// being registered in the parent router (assuming no conflicts).
        #[test]
        fn prop_route_count_preserved_after_nesting(
            // Generate number of routes (1-3 to keep test fast)
            num_routes in 1..4usize,
            prefix_segments in prop::collection::vec("[a-z][a-z0-9]{0,5}", 1..3),
        ) {
            async fn handler() -> &'static str { "handler" }

            let prefix = format!("/{}", prefix_segments.join("/"));

            // Create nested router with multiple routes
            let mut nested_router = Router::new();
            for i in 0..num_routes {
                let path = format!("/route{}", i);
                nested_router = nested_router.route(&path, get(handler));
            }

            let app = Router::new().nest(&prefix, nested_router);

            prop_assert_eq!(
                app.registered_routes().len(),
                num_routes,
                "Number of routes should be preserved after nesting"
            );
        }

        /// Property: Nested routes are matchable
        ///
        /// For any nested route, a request to the prefixed path should match.
        #[test]
        fn prop_nested_routes_are_matchable(
            prefix_segments in prop::collection::vec("[a-z][a-z0-9]{1,5}", 1..3),
            route_segments in prop::collection::vec("[a-z][a-z0-9]{1,5}", 1..3),
        ) {
            async fn handler() -> &'static str { "handler" }

            let prefix = format!("/{}", prefix_segments.join("/"));
            let route_path = format!("/{}", route_segments.join("/"));

            let nested_router = Router::new().route(&route_path, get(handler));
            let app = Router::new().nest(&prefix, nested_router);

            // Build the full path to match
            let full_path = format!("{}{}", prefix, route_path);

            // Property: The route should be matchable
            match app.match_route(&full_path, &Method::GET) {
                RouteMatch::Found { .. } => {
                    // Success - route was found
                }
                RouteMatch::NotFound => {
                    prop_assert!(false, "Route '{}' should be found but got NotFound", full_path);
                }
                RouteMatch::MethodNotAllowed { .. } => {
                    prop_assert!(false, "Route '{}' should be found but got MethodNotAllowed", full_path);
                }
            }
        }
    }

    // **Feature: router-nesting, Property 9: State Merging**
    //
    // For any nested router with state, that state should be accessible via the
    // State extractor in handlers after nesting (assuming no type conflict with parent).
    //
    // **Validates: Requirements 3.1, 3.3**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Property: State type IDs are merged from nested router
        ///
        /// For any nested router with state, the parent router should track
        /// the state type IDs after nesting.
        #[test]
        fn prop_state_type_ids_merged(
            prefix_segments in prop::collection::vec("[a-z][a-z0-9]{1,5}", 1..3),
            has_nested_state in any::<bool>(),
        ) {
            #[derive(Clone)]
            struct TestState(#[allow(dead_code)] i32);

            async fn handler() -> &'static str { "handler" }

            let prefix = format!("/{}", prefix_segments.join("/"));

            let mut nested = Router::new().route("/test", get(handler));
            if has_nested_state {
                nested = nested.state(TestState(42));
            }

            let parent = Router::new().nest(&prefix, nested);

            // Property: If nested had state, parent should track the type ID
            if has_nested_state {
                prop_assert!(
                    parent.state_type_ids().contains(&std::any::TypeId::of::<TestState>()),
                    "Parent should track nested state type ID"
                );
            }
        }

        /// Property: State merging adds nested state to parent
        ///
        /// For any nested router with state that the parent doesn't have,
        /// merge_state should add that state to the parent.
        #[test]
        fn prop_merge_state_adds_nested_state(
            state_value in any::<i32>(),
        ) {
            #[derive(Clone, PartialEq, Debug)]
            struct UniqueState(i32);

            // Create a source router with state
            let source = Router::new().state(UniqueState(state_value));

            // Create a parent without this state type
            let parent = Router::new().merge_state::<UniqueState>(&source);

            // Property: Parent should now have the state
            prop_assert!(
                parent.has_state::<UniqueState>(),
                "Parent should have state after merge"
            );

            // Property: State value should match
            let merged_state = parent.state.get::<UniqueState>().unwrap();
            prop_assert_eq!(
                merged_state.0, state_value,
                "Merged state value should match source"
            );
        }
    }

    // **Feature: router-nesting, Property 10: State Precedence**
    //
    // For any parent and nested router both having state of the same type,
    // the parent's state value should be preserved after nesting.
    //
    // **Validates: Requirements 3.2**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Property: Parent state takes precedence over nested state
        ///
        /// For any parent and nested router both having state of the same type,
        /// the parent's state value should be preserved after merge_state.
        #[test]
        fn prop_parent_state_takes_precedence(
            parent_value in any::<i32>(),
            nested_value in any::<i32>(),
        ) {
            // Ensure values are different to make the test meaningful
            prop_assume!(parent_value != nested_value);

            #[derive(Clone, PartialEq, Debug)]
            struct SharedState(i32);

            // Create source router with nested state
            let source = Router::new().state(SharedState(nested_value));

            // Create parent with its own state
            let parent = Router::new()
                .state(SharedState(parent_value))
                .merge_state::<SharedState>(&source);

            // Property: Parent should still have state
            prop_assert!(
                parent.has_state::<SharedState>(),
                "Parent should have state"
            );

            // Property: Parent's state value should be preserved (parent wins)
            let final_state = parent.state.get::<SharedState>().unwrap();
            prop_assert_eq!(
                final_state.0, parent_value,
                "Parent state value should be preserved, not overwritten by nested"
            );
        }

        /// Property: State precedence is consistent regardless of merge order
        ///
        /// For any parent with state, merging from a source with the same type
        /// should always preserve the parent's value.
        #[test]
        fn prop_state_precedence_consistent(
            parent_value in any::<i32>(),
            source1_value in any::<i32>(),
            source2_value in any::<i32>(),
        ) {
            #[derive(Clone, PartialEq, Debug)]
            struct ConsistentState(i32);

            // Create multiple source routers
            let source1 = Router::new().state(ConsistentState(source1_value));
            let source2 = Router::new().state(ConsistentState(source2_value));

            // Create parent with its own state and merge from multiple sources
            let parent = Router::new()
                .state(ConsistentState(parent_value))
                .merge_state::<ConsistentState>(&source1)
                .merge_state::<ConsistentState>(&source2);

            // Property: Parent's original state should be preserved
            let final_state = parent.state.get::<ConsistentState>().unwrap();
            prop_assert_eq!(
                final_state.0, parent_value,
                "Parent state should be preserved after multiple merges"
            );
        }
    }

    // **Feature: phase4-ergonomics-v1, Property 1: Route Conflict Detection**
    //
    // For any two routes with the same path and HTTP method registered on the same
    // RustApi instance, the system should detect the conflict and report an error
    // at startup time.
    //
    // **Validates: Requirements 1.2**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Property: Routes with identical path structure but different parameter names conflict
        ///
        /// For any valid path with parameters, registering two routes with the same
        /// structure but different parameter names should be detected as a conflict.
        #[test]
        fn prop_same_structure_different_param_names_conflict(
            // Generate valid path segments
            segments in prop::collection::vec("[a-z][a-z0-9]{0,5}", 1..4),
            // Generate two different parameter names
            param1 in "[a-z][a-z0-9]{0,5}",
            param2 in "[a-z][a-z0-9]{0,5}",
        ) {
            // Ensure param names are different
            prop_assume!(param1 != param2);

            // Build two paths with same structure but different param names
            let mut path1 = String::from("/");
            let mut path2 = String::from("/");

            for segment in &segments {
                path1.push_str(segment);
                path1.push('/');
                path2.push_str(segment);
                path2.push('/');
            }

            path1.push('{');
            path1.push_str(&param1);
            path1.push('}');

            path2.push('{');
            path2.push_str(&param2);
            path2.push('}');

            // Try to register both routes - should panic
            let result = catch_unwind(AssertUnwindSafe(|| {
                async fn handler1() -> &'static str { "handler1" }
                async fn handler2() -> &'static str { "handler2" }

                let _router = Router::new()
                    .route(&path1, get(handler1))
                    .route(&path2, get(handler2));
            }));

            prop_assert!(
                result.is_err(),
                "Routes '{}' and '{}' should conflict but didn't",
                path1, path2
            );
        }

        /// Property: Routes with different path structures don't conflict
        ///
        /// For any two paths with different structures (different number of segments
        /// or different static segments), they should not conflict.
        #[test]
        fn prop_different_structures_no_conflict(
            // Generate different path segments for two routes
            segments1 in prop::collection::vec("[a-z][a-z0-9]{0,5}", 1..3),
            segments2 in prop::collection::vec("[a-z][a-z0-9]{0,5}", 1..3),
            // Optional parameter at the end
            has_param1 in any::<bool>(),
            has_param2 in any::<bool>(),
        ) {
            // Build two paths
            let mut path1 = String::from("/");
            let mut path2 = String::from("/");

            for segment in &segments1 {
                path1.push_str(segment);
                path1.push('/');
            }
            path1.pop(); // Remove trailing slash

            for segment in &segments2 {
                path2.push_str(segment);
                path2.push('/');
            }
            path2.pop(); // Remove trailing slash

            if has_param1 {
                path1.push_str("/{id}");
            }

            if has_param2 {
                path2.push_str("/{id}");
            }

            // Normalize paths for comparison
            let norm1 = normalize_path_for_comparison(&convert_path_params(&path1));
            let norm2 = normalize_path_for_comparison(&convert_path_params(&path2));

            // Only test if paths are actually different
            prop_assume!(norm1 != norm2);

            // Try to register both routes - should succeed
            let result = catch_unwind(AssertUnwindSafe(|| {
                async fn handler1() -> &'static str { "handler1" }
                async fn handler2() -> &'static str { "handler2" }

                let router = Router::new()
                    .route(&path1, get(handler1))
                    .route(&path2, get(handler2));

                router.registered_routes().len()
            }));

            prop_assert!(
                result.is_ok(),
                "Routes '{}' and '{}' should not conflict but did",
                path1, path2
            );

            if let Ok(count) = result {
                prop_assert_eq!(count, 2, "Should have registered 2 routes");
            }
        }

        /// Property: Conflict error message contains both route paths
        ///
        /// When a conflict is detected, the error message should include both
        /// the existing route path and the new conflicting route path.
        #[test]
        fn prop_conflict_error_contains_both_paths(
            // Generate a valid path segment
            segment in "[a-z][a-z0-9]{1,5}",
            param1 in "[a-z][a-z0-9]{1,5}",
            param2 in "[a-z][a-z0-9]{1,5}",
        ) {
            prop_assume!(param1 != param2);

            let path1 = format!("/{}/{{{}}}", segment, param1);
            let path2 = format!("/{}/{{{}}}", segment, param2);

            let result = catch_unwind(AssertUnwindSafe(|| {
                async fn handler1() -> &'static str { "handler1" }
                async fn handler2() -> &'static str { "handler2" }

                let _router = Router::new()
                    .route(&path1, get(handler1))
                    .route(&path2, get(handler2));
            }));

            prop_assert!(result.is_err(), "Should have panicked due to conflict");

            // Check that the panic message contains useful information
            if let Err(panic_info) = result {
                if let Some(msg) = panic_info.downcast_ref::<String>() {
                    prop_assert!(
                        msg.contains("ROUTE CONFLICT DETECTED"),
                        "Error should contain 'ROUTE CONFLICT DETECTED', got: {}",
                        msg
                    );
                    prop_assert!(
                        msg.contains("Existing:") && msg.contains("New:"),
                        "Error should contain both 'Existing:' and 'New:' labels, got: {}",
                        msg
                    );
                    prop_assert!(
                        msg.contains("How to resolve:"),
                        "Error should contain resolution guidance, got: {}",
                        msg
                    );
                }
            }
        }

        /// Property: Exact duplicate paths conflict
        ///
        /// Registering the exact same path twice should always be detected as a conflict.
        #[test]
        fn prop_exact_duplicate_paths_conflict(
            // Generate valid path segments
            segments in prop::collection::vec("[a-z][a-z0-9]{0,5}", 1..4),
            has_param in any::<bool>(),
        ) {
            // Build a path
            let mut path = String::from("/");

            for segment in &segments {
                path.push_str(segment);
                path.push('/');
            }
            path.pop(); // Remove trailing slash

            if has_param {
                path.push_str("/{id}");
            }

            // Try to register the same path twice - should panic
            let result = catch_unwind(AssertUnwindSafe(|| {
                async fn handler1() -> &'static str { "handler1" }
                async fn handler2() -> &'static str { "handler2" }

                let _router = Router::new()
                    .route(&path, get(handler1))
                    .route(&path, get(handler2));
            }));

            prop_assert!(
                result.is_err(),
                "Registering path '{}' twice should conflict but didn't",
                path
            );
        }
    }

    // **Feature: router-nesting, Property 5: Nested Route Matching**
    //
    // For any nested route and a request with a matching path and method,
    // the router should return the correct handler.
    //
    // **Validates: Requirements 2.1**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Property: Nested routes with path parameters are correctly matched
        ///
        /// For any nested route with path parameters, a request to the prefixed path
        /// with valid parameter values should match and return Found.
        #[test]
        fn prop_nested_route_with_params_matches(
            prefix_segments in prop::collection::vec("[a-z][a-z0-9]{1,5}", 1..3),
            route_segments in prop::collection::vec("[a-z][a-z0-9]{1,5}", 0..2),
            param_value in "[a-z0-9]{1,10}",
        ) {
            async fn handler() -> &'static str { "handler" }

            let prefix = format!("/{}", prefix_segments.join("/"));
            let route_path = if route_segments.is_empty() {
                "/{id}".to_string()
            } else {
                format!("/{}/{{id}}", route_segments.join("/"))
            };

            let nested_router = Router::new().route(&route_path, get(handler));
            let app = Router::new().nest(&prefix, nested_router);

            // Build the full path to match with actual parameter value
            let full_path = if route_segments.is_empty() {
                format!("{}/{}", prefix, param_value)
            } else {
                format!("{}/{}/{}", prefix, route_segments.join("/"), param_value)
            };

            // Property: The route should be matched
            match app.match_route(&full_path, &Method::GET) {
                RouteMatch::Found { params, .. } => {
                    // Verify the parameter was extracted
                    prop_assert!(
                        params.contains_key("id"),
                        "Should have 'id' parameter, got: {:?}",
                        params
                    );
                    prop_assert_eq!(
                        params.get("id").unwrap(),
                        &param_value,
                        "Parameter value should match"
                    );
                }
                RouteMatch::NotFound => {
                    prop_assert!(false, "Route '{}' should be found but got NotFound", full_path);
                }
                RouteMatch::MethodNotAllowed { .. } => {
                    prop_assert!(false, "Route '{}' should be found but got MethodNotAllowed", full_path);
                }
            }
        }

        /// Property: Nested routes match correct HTTP method
        ///
        /// For any nested route registered with a specific HTTP method, only requests
        /// with that method should return Found.
        #[test]
        fn prop_nested_route_matches_correct_method(
            prefix_segments in prop::collection::vec("[a-z][a-z0-9]{1,5}", 1..2),
            route_segments in prop::collection::vec("[a-z][a-z0-9]{1,5}", 1..2),
            use_get in any::<bool>(),
        ) {
            async fn handler() -> &'static str { "handler" }

            let prefix = format!("/{}", prefix_segments.join("/"));
            let route_path = format!("/{}", route_segments.join("/"));

            // Register with either GET or POST
            let method_router = if use_get { get(handler) } else { post(handler) };
            let nested_router = Router::new().route(&route_path, method_router);
            let app = Router::new().nest(&prefix, nested_router);

            let full_path = format!("{}{}", prefix, route_path);
            let registered_method = if use_get { Method::GET } else { Method::POST };
            let other_method = if use_get { Method::POST } else { Method::GET };

            // Property: Registered method should match
            match app.match_route(&full_path, &registered_method) {
                RouteMatch::Found { .. } => {
                    // Success
                }
                other => {
                    prop_assert!(false, "Route should be found for registered method, got: {:?}",
                        match other {
                            RouteMatch::NotFound => "NotFound",
                            RouteMatch::MethodNotAllowed { .. } => "MethodNotAllowed",
                            _ => "Found",
                        }
                    );
                }
            }

            // Property: Other method should return MethodNotAllowed
            match app.match_route(&full_path, &other_method) {
                RouteMatch::MethodNotAllowed { allowed } => {
                    prop_assert!(
                        allowed.contains(&registered_method),
                        "Allowed methods should contain {:?}",
                        registered_method
                    );
                }
                other => {
                    prop_assert!(false, "Route should return MethodNotAllowed for other method, got: {:?}",
                        match other {
                            RouteMatch::NotFound => "NotFound",
                            RouteMatch::Found { .. } => "Found",
                            _ => "MethodNotAllowed",
                        }
                    );
                }
            }
        }
    }

    // **Feature: router-nesting, Property 6: Path Parameter Extraction**
    //
    // For any nested route with path parameters and a matching request,
    // the extracted parameters should have the correct names and values.
    //
    // **Validates: Requirements 2.2**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Property: Single path parameter is correctly extracted from nested route
        ///
        /// For any nested route with a single path parameter, the parameter name
        /// and value should be correctly extracted.
        #[test]
        fn prop_single_param_extraction(
            prefix in "[a-z][a-z0-9]{1,5}",
            param_name in "[a-z][a-z0-9]{1,5}",
            param_value in "[a-z0-9]{1,10}",
        ) {
            async fn handler() -> &'static str { "handler" }

            let prefix = format!("/{}", prefix);
            let route_path = format!("/{{{}}}", param_name);

            let nested_router = Router::new().route(&route_path, get(handler));
            let app = Router::new().nest(&prefix, nested_router);

            let full_path = format!("{}/{}", prefix, param_value);

            match app.match_route(&full_path, &Method::GET) {
                RouteMatch::Found { params, .. } => {
                    prop_assert!(
                        params.contains_key(&param_name),
                        "Should have '{}' parameter, got: {:?}",
                        param_name, params
                    );
                    prop_assert_eq!(
                        params.get(&param_name).unwrap(),
                        &param_value,
                        "Parameter '{}' value should be '{}'",
                        param_name, param_value
                    );
                }
                _ => {
                    prop_assert!(false, "Route should be found");
                }
            }
        }

        /// Property: Multiple path parameters are correctly extracted from nested route
        ///
        /// For any nested route with multiple path parameters, all parameters
        /// should be correctly extracted with their names and values.
        #[test]
        fn prop_multiple_params_extraction(
            prefix in "[a-z][a-z0-9]{1,5}",
            param1_name in "[a-z]{1,5}",
            param1_value in "[a-z0-9]{1,10}",
            param2_name in "[a-z]{1,5}",
            param2_value in "[a-z0-9]{1,10}",
        ) {
            // Ensure param names are different
            prop_assume!(param1_name != param2_name);

            async fn handler() -> &'static str { "handler" }

            let prefix = format!("/{}", prefix);
            let route_path = format!("/{{{}}}/items/{{{}}}", param1_name, param2_name);

            let nested_router = Router::new().route(&route_path, get(handler));
            let app = Router::new().nest(&prefix, nested_router);

            let full_path = format!("{}/{}/items/{}", prefix, param1_value, param2_value);

            match app.match_route(&full_path, &Method::GET) {
                RouteMatch::Found { params, .. } => {
                    // Check first parameter
                    prop_assert!(
                        params.contains_key(&param1_name),
                        "Should have '{}' parameter, got: {:?}",
                        param1_name, params
                    );
                    prop_assert_eq!(
                        params.get(&param1_name).unwrap(),
                        &param1_value,
                        "Parameter '{}' value should be '{}'",
                        param1_name, param1_value
                    );

                    // Check second parameter
                    prop_assert!(
                        params.contains_key(&param2_name),
                        "Should have '{}' parameter, got: {:?}",
                        param2_name, params
                    );
                    prop_assert_eq!(
                        params.get(&param2_name).unwrap(),
                        &param2_value,
                        "Parameter '{}' value should be '{}'",
                        param2_name, param2_value
                    );
                }
                _ => {
                    prop_assert!(false, "Route should be found");
                }
            }
        }

        /// Property: Path parameters preserve special characters in values
        ///
        /// For any nested route with path parameters, parameter values containing
        /// URL-safe special characters should be preserved correctly.
        #[test]
        fn prop_param_value_preservation(
            prefix in "[a-z]{1,5}",
            // Generate values with alphanumeric and some special chars
            param_value in "[a-zA-Z0-9_-]{1,15}",
        ) {
            async fn handler() -> &'static str { "handler" }

            let prefix = format!("/{}", prefix);
            let route_path = "/{id}".to_string();

            let nested_router = Router::new().route(&route_path, get(handler));
            let app = Router::new().nest(&prefix, nested_router);

            let full_path = format!("{}/{}", prefix, param_value);

            match app.match_route(&full_path, &Method::GET) {
                RouteMatch::Found { params, .. } => {
                    prop_assert_eq!(
                        params.get("id").unwrap(),
                        &param_value,
                        "Parameter value should be preserved exactly"
                    );
                }
                _ => {
                    prop_assert!(false, "Route should be found");
                }
            }
        }
    }

    // **Feature: router-nesting, Property 7: Not Found Response**
    //
    // For any request path that doesn't match any registered route (nested or otherwise),
    // the router should return NotFound.
    //
    // **Validates: Requirements 2.3**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Property: Unregistered paths return NotFound
        ///
        /// For any path that doesn't match any registered route, the router
        /// should return NotFound.
        #[test]
        fn prop_unregistered_path_returns_not_found(
            prefix in "[a-z][a-z0-9]{1,5}",
            route_segment in "[a-z][a-z0-9]{1,5}",
            unregistered_segment in "[a-z][a-z0-9]{6,10}",
        ) {
            // Ensure segments are different
            prop_assume!(route_segment != unregistered_segment);

            async fn handler() -> &'static str { "handler" }

            let prefix = format!("/{}", prefix);
            let route_path = format!("/{}", route_segment);

            let nested_router = Router::new().route(&route_path, get(handler));
            let app = Router::new().nest(&prefix, nested_router);

            // Try to match an unregistered path
            let unregistered_path = format!("{}/{}", prefix, unregistered_segment);

            match app.match_route(&unregistered_path, &Method::GET) {
                RouteMatch::NotFound => {
                    // Success - this is expected
                }
                RouteMatch::Found { .. } => {
                    prop_assert!(false, "Path '{}' should not be found", unregistered_path);
                }
                RouteMatch::MethodNotAllowed { .. } => {
                    prop_assert!(false, "Path '{}' should return NotFound, not MethodNotAllowed", unregistered_path);
                }
            }
        }

        /// Property: Wrong prefix returns NotFound
        ///
        /// For any nested route, a request with a different prefix should return NotFound.
        #[test]
        fn prop_wrong_prefix_returns_not_found(
            prefix1 in "[a-z][a-z0-9]{1,5}",
            prefix2 in "[a-z][a-z0-9]{6,10}",
            route_segment in "[a-z][a-z0-9]{1,5}",
        ) {
            // Ensure prefixes are different
            prop_assume!(prefix1 != prefix2);

            async fn handler() -> &'static str { "handler" }

            let prefix = format!("/{}", prefix1);
            let route_path = format!("/{}", route_segment);

            let nested_router = Router::new().route(&route_path, get(handler));
            let app = Router::new().nest(&prefix, nested_router);

            // Try to match with wrong prefix
            let wrong_prefix_path = format!("/{}/{}", prefix2, route_segment);

            match app.match_route(&wrong_prefix_path, &Method::GET) {
                RouteMatch::NotFound => {
                    // Success - this is expected
                }
                _ => {
                    prop_assert!(false, "Path '{}' with wrong prefix should return NotFound", wrong_prefix_path);
                }
            }
        }

        /// Property: Partial path match returns NotFound
        ///
        /// For any nested route with multiple segments, a request matching only
        /// part of the path should return NotFound.
        #[test]
        fn prop_partial_path_returns_not_found(
            prefix in "[a-z][a-z0-9]{1,5}",
            segment1 in "[a-z][a-z0-9]{1,5}",
            segment2 in "[a-z][a-z0-9]{1,5}",
        ) {
            async fn handler() -> &'static str { "handler" }

            let prefix = format!("/{}", prefix);
            let route_path = format!("/{}/{}", segment1, segment2);

            let nested_router = Router::new().route(&route_path, get(handler));
            let app = Router::new().nest(&prefix, nested_router);

            // Try to match only the first segment (partial path)
            let partial_path = format!("{}/{}", prefix, segment1);

            match app.match_route(&partial_path, &Method::GET) {
                RouteMatch::NotFound => {
                    // Success - partial path should not match
                }
                _ => {
                    prop_assert!(false, "Partial path '{}' should return NotFound", partial_path);
                }
            }
        }
    }

    // **Feature: router-nesting, Property 8: Method Not Allowed Response**
    //
    // For any request to a valid path but with an unregistered HTTP method,
    // the router should return MethodNotAllowed with the list of allowed methods.
    //
    // **Validates: Requirements 2.4**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Property: Unregistered method returns MethodNotAllowed with allowed methods
        ///
        /// For any nested route registered with specific methods, a request with
        /// an unregistered method should return MethodNotAllowed with the correct
        /// list of allowed methods.
        #[test]
        fn prop_unregistered_method_returns_method_not_allowed(
            prefix in "[a-z][a-z0-9]{1,5}",
            route_segment in "[a-z][a-z0-9]{1,5}",
        ) {
            async fn handler() -> &'static str { "handler" }

            let prefix = format!("/{}", prefix);
            let route_path = format!("/{}", route_segment);

            // Register only GET
            let nested_router = Router::new().route(&route_path, get(handler));
            let app = Router::new().nest(&prefix, nested_router);

            let full_path = format!("{}{}", prefix, route_path);

            // Try POST on a GET-only route
            match app.match_route(&full_path, &Method::POST) {
                RouteMatch::MethodNotAllowed { allowed } => {
                    prop_assert!(
                        allowed.contains(&Method::GET),
                        "Allowed methods should contain GET, got: {:?}",
                        allowed
                    );
                    prop_assert!(
                        !allowed.contains(&Method::POST),
                        "Allowed methods should not contain POST"
                    );
                }
                RouteMatch::Found { .. } => {
                    prop_assert!(false, "POST should not be found on GET-only route");
                }
                RouteMatch::NotFound => {
                    prop_assert!(false, "Path exists, should return MethodNotAllowed not NotFound");
                }
            }
        }

        /// Property: Multiple registered methods are all returned in allowed list
        ///
        /// For any nested route registered with multiple methods, the MethodNotAllowed
        /// response should include all registered methods.
        #[test]
        fn prop_multiple_methods_in_allowed_list(
            prefix in "[a-z][a-z0-9]{1,5}",
            route_segment in "[a-z][a-z0-9]{1,5}",
            use_get in any::<bool>(),
            use_post in any::<bool>(),
            use_put in any::<bool>(),
        ) {
            // Ensure at least one method is registered
            prop_assume!(use_get || use_post || use_put);

            async fn handler() -> &'static str { "handler" }

            let prefix = format!("/{}", prefix);
            let route_path = format!("/{}", route_segment);

            // Build method router with selected methods
            let mut method_router = MethodRouter::new();
            let mut expected_methods: Vec<Method> = Vec::new();

            if use_get {
                let get_router = get(handler);
                for (method, h) in get_router.handlers {
                    method_router.handlers.insert(method.clone(), h);
                    expected_methods.push(method);
                }
            }
            if use_post {
                let post_router = post(handler);
                for (method, h) in post_router.handlers {
                    method_router.handlers.insert(method.clone(), h);
                    expected_methods.push(method);
                }
            }
            if use_put {
                let put_router = put(handler);
                for (method, h) in put_router.handlers {
                    method_router.handlers.insert(method.clone(), h);
                    expected_methods.push(method);
                }
            }

            let nested_router = Router::new().route(&route_path, method_router);
            let app = Router::new().nest(&prefix, nested_router);

            let full_path = format!("{}{}", prefix, route_path);

            // Try DELETE (which we never register)
            match app.match_route(&full_path, &Method::DELETE) {
                RouteMatch::MethodNotAllowed { allowed } => {
                    // All registered methods should be in allowed list
                    for method in &expected_methods {
                        prop_assert!(
                            allowed.contains(method),
                            "Allowed methods should contain {:?}, got: {:?}",
                            method, allowed
                        );
                    }
                    // DELETE should not be in allowed list
                    prop_assert!(
                        !allowed.contains(&Method::DELETE),
                        "Allowed methods should not contain DELETE"
                    );
                }
                RouteMatch::Found { .. } => {
                    prop_assert!(false, "DELETE should not be found");
                }
                RouteMatch::NotFound => {
                    prop_assert!(false, "Path exists, should return MethodNotAllowed not NotFound");
                }
            }
        }
    }

    // **Feature: router-nesting, Property 12: Conflict Detection**
    //
    // For any nested route that conflicts with an existing route (same path structure),
    // the router should detect and report the conflict with both route paths.
    //
    // **Validates: Requirements 5.1, 5.3**

    // **Feature: router-nesting, Property 4: Multiple Router Composition**
    //
    // For any set of routers with non-overlapping route structures nested under
    // different prefixes, all routes should be registered without conflicts.
    //
    // **Validates: Requirements 1.5**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Property: Multiple routers nested under different prefixes register all routes
        ///
        /// For any set of routers with routes nested under different prefixes,
        /// all routes should be registered and the total count should equal the
        /// sum of routes from all nested routers.
        #[test]
        fn prop_multiple_routers_all_routes_registered(
            // Generate 2-3 different prefixes
            prefix1_segments in prop::collection::vec("[a-z][a-z0-9]{1,5}", 1..3),
            prefix2_segments in prop::collection::vec("[a-z][a-z0-9]{1,5}", 1..3),
            // Generate route counts for each router (1-3 routes each)
            num_routes1 in 1..4usize,
            num_routes2 in 1..4usize,
        ) {
            // Build prefixes
            let prefix1 = format!("/{}", prefix1_segments.join("/"));
            let prefix2 = format!("/{}", prefix2_segments.join("/"));

            // Ensure prefixes are different
            prop_assume!(prefix1 != prefix2);

            async fn handler() -> &'static str { "handler" }

            // Create first router with routes
            let mut router1 = Router::new();
            for i in 0..num_routes1 {
                let path = format!("/route1_{}", i);
                router1 = router1.route(&path, get(handler));
            }

            // Create second router with routes
            let mut router2 = Router::new();
            for i in 0..num_routes2 {
                let path = format!("/route2_{}", i);
                router2 = router2.route(&path, get(handler));
            }

            // Nest both routers
            let app = Router::new()
                .nest(&prefix1, router1)
                .nest(&prefix2, router2);

            let routes = app.registered_routes();

            // Property: Total route count should equal sum of all nested routes
            let expected_count = num_routes1 + num_routes2;
            prop_assert_eq!(
                routes.len(),
                expected_count,
                "Should have {} routes ({}+{}), got {}",
                expected_count, num_routes1, num_routes2, routes.len()
            );

            // Property: All routes from router1 should be registered with prefix1
            for i in 0..num_routes1 {
                let expected_path = format!("{}/route1_{}", prefix1, i);
                let matchit_path = convert_path_params(&expected_path);
                prop_assert!(
                    routes.contains_key(&matchit_path),
                    "Route '{}' should be registered",
                    expected_path
                );
            }

            // Property: All routes from router2 should be registered with prefix2
            for i in 0..num_routes2 {
                let expected_path = format!("{}/route2_{}", prefix2, i);
                let matchit_path = convert_path_params(&expected_path);
                prop_assert!(
                    routes.contains_key(&matchit_path),
                    "Route '{}' should be registered",
                    expected_path
                );
            }
        }

        /// Property: Multiple routers with same internal routes don't interfere
        ///
        /// For any set of routers with identical internal route structures nested
        /// under different prefixes, all routes should be independently matchable.
        #[test]
        fn prop_multiple_routers_no_interference(
            prefix1 in "[a-z][a-z0-9]{1,5}",
            prefix2 in "[a-z][a-z0-9]{1,5}",
            route_segment in "[a-z][a-z0-9]{1,5}",
            param_value1 in "[a-z0-9]{1,10}",
            param_value2 in "[a-z0-9]{1,10}",
        ) {
            // Ensure prefixes are different
            prop_assume!(prefix1 != prefix2);

            let prefix1 = format!("/{}", prefix1);
            let prefix2 = format!("/{}", prefix2);

            async fn handler() -> &'static str { "handler" }

            // Create two routers with identical internal structure
            let router1 = Router::new()
                .route(&format!("/{}", route_segment), get(handler))
                .route("/{id}", get(handler));

            let router2 = Router::new()
                .route(&format!("/{}", route_segment), get(handler))
                .route("/{id}", get(handler));

            // Nest both routers
            let app = Router::new()
                .nest(&prefix1, router1)
                .nest(&prefix2, router2);

            // Property: Routes under prefix1 should be matchable
            let path1_static = format!("{}/{}", prefix1, route_segment);
            match app.match_route(&path1_static, &Method::GET) {
                RouteMatch::Found { params, .. } => {
                    prop_assert!(params.is_empty(), "Static path should have no params");
                }
                _ => {
                    prop_assert!(false, "Route '{}' should be found", path1_static);
                }
            }

            let path1_param = format!("{}/{}", prefix1, param_value1);
            match app.match_route(&path1_param, &Method::GET) {
                RouteMatch::Found { params, .. } => {
                    prop_assert_eq!(
                        params.get("id"),
                        Some(&param_value1.to_string()),
                        "Parameter should be extracted correctly"
                    );
                }
                _ => {
                    prop_assert!(false, "Route '{}' should be found", path1_param);
                }
            }

            // Property: Routes under prefix2 should be matchable independently
            let path2_static = format!("{}/{}", prefix2, route_segment);
            match app.match_route(&path2_static, &Method::GET) {
                RouteMatch::Found { params, .. } => {
                    prop_assert!(params.is_empty(), "Static path should have no params");
                }
                _ => {
                    prop_assert!(false, "Route '{}' should be found", path2_static);
                }
            }

            let path2_param = format!("{}/{}", prefix2, param_value2);
            match app.match_route(&path2_param, &Method::GET) {
                RouteMatch::Found { params, .. } => {
                    prop_assert_eq!(
                        params.get("id"),
                        Some(&param_value2.to_string()),
                        "Parameter should be extracted correctly"
                    );
                }
                _ => {
                    prop_assert!(false, "Route '{}' should be found", path2_param);
                }
            }
        }

        /// Property: Multiple routers preserve HTTP methods independently
        ///
        /// For any set of routers with different HTTP methods nested under different
        /// prefixes, each route should preserve its own set of allowed methods.
        #[test]
        fn prop_multiple_routers_preserve_methods(
            prefix1 in "[a-z][a-z0-9]{1,5}",
            prefix2 in "[a-z][a-z0-9]{1,5}",
            route_segment in "[a-z][a-z0-9]{1,5}",
            router1_use_get in any::<bool>(),
            router1_use_post in any::<bool>(),
            router2_use_get in any::<bool>(),
            router2_use_put in any::<bool>(),
        ) {
            // Ensure at least one method per router
            prop_assume!(router1_use_get || router1_use_post);
            prop_assume!(router2_use_get || router2_use_put);
            // Ensure prefixes are different
            prop_assume!(prefix1 != prefix2);

            let prefix1 = format!("/{}", prefix1);
            let prefix2 = format!("/{}", prefix2);
            let route_path = format!("/{}", route_segment);

            async fn handler() -> &'static str { "handler" }

            // Build router1 with selected methods
            let mut method_router1 = MethodRouter::new();
            let mut expected_methods1: Vec<Method> = Vec::new();
            if router1_use_get {
                let get_router = get(handler);
                for (method, h) in get_router.handlers {
                    method_router1.handlers.insert(method.clone(), h);
                    expected_methods1.push(method);
                }
            }
            if router1_use_post {
                let post_router = post(handler);
                for (method, h) in post_router.handlers {
                    method_router1.handlers.insert(method.clone(), h);
                    expected_methods1.push(method);
                }
            }

            // Build router2 with selected methods
            let mut method_router2 = MethodRouter::new();
            let mut expected_methods2: Vec<Method> = Vec::new();
            if router2_use_get {
                let get_router = get(handler);
                for (method, h) in get_router.handlers {
                    method_router2.handlers.insert(method.clone(), h);
                    expected_methods2.push(method);
                }
            }
            if router2_use_put {
                let put_router = put(handler);
                for (method, h) in put_router.handlers {
                    method_router2.handlers.insert(method.clone(), h);
                    expected_methods2.push(method);
                }
            }

            let router1 = Router::new().route(&route_path, method_router1);
            let router2 = Router::new().route(&route_path, method_router2);

            let app = Router::new()
                .nest(&prefix1, router1)
                .nest(&prefix2, router2);

            let full_path1 = format!("{}{}", prefix1, route_path);
            let full_path2 = format!("{}{}", prefix2, route_path);

            // Property: Router1's methods should be preserved
            for method in &expected_methods1 {
                match app.match_route(&full_path1, method) {
                    RouteMatch::Found { .. } => {}
                    _ => {
                        prop_assert!(false, "Method {:?} should be found for {}", method, full_path1);
                    }
                }
            }

            // Property: Router2's methods should be preserved
            for method in &expected_methods2 {
                match app.match_route(&full_path2, method) {
                    RouteMatch::Found { .. } => {}
                    _ => {
                        prop_assert!(false, "Method {:?} should be found for {}", method, full_path2);
                    }
                }
            }

            // Property: Methods not registered should return MethodNotAllowed
            if !expected_methods1.contains(&Method::DELETE) {
                match app.match_route(&full_path1, &Method::DELETE) {
                    RouteMatch::MethodNotAllowed { allowed } => {
                        for method in &expected_methods1 {
                            prop_assert!(
                                allowed.contains(method),
                                "Allowed methods for {} should contain {:?}",
                                full_path1, method
                            );
                        }
                    }
                    _ => {
                        prop_assert!(false, "DELETE should return MethodNotAllowed for {}", full_path1);
                    }
                }
            }
        }

        /// Property: Three or more routers can be composed without conflicts
        ///
        /// For any set of three routers nested under different prefixes,
        /// all routes should be registered without conflicts.
        #[test]
        fn prop_three_routers_composition(
            prefix1 in "[a-z]{1,3}",
            prefix2 in "[a-z]{4,6}",
            prefix3 in "[a-z]{7,9}",
            num_routes in 1..3usize,
        ) {
            let prefix1 = format!("/{}", prefix1);
            let prefix2 = format!("/{}", prefix2);
            let prefix3 = format!("/{}", prefix3);

            async fn handler() -> &'static str { "handler" }

            // Create three routers with same number of routes
            let mut router1 = Router::new();
            let mut router2 = Router::new();
            let mut router3 = Router::new();

            for i in 0..num_routes {
                let path = format!("/item{}", i);
                router1 = router1.route(&path, get(handler));
                router2 = router2.route(&path, get(handler));
                router3 = router3.route(&path, get(handler));
            }

            // Nest all three routers
            let app = Router::new()
                .nest(&prefix1, router1)
                .nest(&prefix2, router2)
                .nest(&prefix3, router3);

            let routes = app.registered_routes();

            // Property: Total route count should be 3 * num_routes
            let expected_count = 3 * num_routes;
            prop_assert_eq!(
                routes.len(),
                expected_count,
                "Should have {} routes, got {}",
                expected_count, routes.len()
            );

            // Property: All routes should be matchable
            for i in 0..num_routes {
                let path1 = format!("{}/item{}", prefix1, i);
                let path2 = format!("{}/item{}", prefix2, i);
                let path3 = format!("{}/item{}", prefix3, i);

                match app.match_route(&path1, &Method::GET) {
                    RouteMatch::Found { .. } => {}
                    _ => prop_assert!(false, "Route '{}' should be found", path1),
                }
                match app.match_route(&path2, &Method::GET) {
                    RouteMatch::Found { .. } => {}
                    _ => prop_assert!(false, "Route '{}' should be found", path2),
                }
                match app.match_route(&path3, &Method::GET) {
                    RouteMatch::Found { .. } => {}
                    _ => prop_assert!(false, "Route '{}' should be found", path3),
                }
            }
        }
    }
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Property: Nested routes with same path structure but different param names conflict
        ///
        /// For any existing route with a parameter and a nested route that would create
        /// the same path structure with a different parameter name, the router should
        /// detect and report the conflict.
        #[test]
        fn prop_nested_route_conflict_different_param_names(
            prefix_segments in prop::collection::vec("[a-z][a-z0-9]{1,5}", 1..3),
            route_segments in prop::collection::vec("[a-z][a-z0-9]{1,5}", 0..2),
            param1 in "[a-z][a-z0-9]{1,5}",
            param2 in "[a-z][a-z0-9]{1,5}",
        ) {
            // Ensure param names are different
            prop_assume!(param1 != param2);

            async fn handler1() -> &'static str { "handler1" }
            async fn handler2() -> &'static str { "handler2" }

            let prefix = format!("/{}", prefix_segments.join("/"));

            // Build the existing route path (with param1)
            let existing_path = if route_segments.is_empty() {
                format!("{}/{{{}}}", prefix, param1)
            } else {
                format!("{}/{}/{{{}}}", prefix, route_segments.join("/"), param1)
            };

            // Build the nested route path (with param2)
            let nested_path = if route_segments.is_empty() {
                format!("/{{{}}}", param2)
            } else {
                format!("/{}/{{{}}}", route_segments.join("/"), param2)
            };

            // Try to create a conflict
            let result = catch_unwind(AssertUnwindSafe(|| {
                let parent = Router::new().route(&existing_path, get(handler1));
                let nested = Router::new().route(&nested_path, get(handler2));
                let _app = parent.nest(&prefix, nested);
            }));

            // Property: Should detect conflict
            prop_assert!(
                result.is_err(),
                "Nested route '{}{}' should conflict with existing route '{}' but didn't",
                prefix, nested_path, existing_path
            );

            // Property: Error message should contain conflict information
            if let Err(panic_info) = result {
                if let Some(msg) = panic_info.downcast_ref::<String>() {
                    prop_assert!(
                        msg.contains("ROUTE CONFLICT DETECTED"),
                        "Error should contain 'ROUTE CONFLICT DETECTED', got: {}",
                        msg
                    );
                    prop_assert!(
                        msg.contains("Existing:") && msg.contains("New:"),
                        "Error should contain both 'Existing:' and 'New:' labels, got: {}",
                        msg
                    );
                }
            }
        }

        /// Property: Nested routes with exact same path conflict
        ///
        /// For any existing route and a nested route that would create the exact
        /// same path, the router should detect and report the conflict.
        #[test]
        fn prop_nested_route_conflict_exact_same_path(
            prefix_segments in prop::collection::vec("[a-z][a-z0-9]{1,5}", 1..3),
            route_segments in prop::collection::vec("[a-z][a-z0-9]{1,5}", 1..3),
        ) {
            async fn handler1() -> &'static str { "handler1" }
            async fn handler2() -> &'static str { "handler2" }

            let prefix = format!("/{}", prefix_segments.join("/"));
            let route_path = format!("/{}", route_segments.join("/"));

            // Build the full existing path
            let existing_path = format!("{}{}", prefix, route_path);

            // Try to create a conflict by nesting a route that creates the same path
            let result = catch_unwind(AssertUnwindSafe(|| {
                let parent = Router::new().route(&existing_path, get(handler1));
                let nested = Router::new().route(&route_path, get(handler2));
                let _app = parent.nest(&prefix, nested);
            }));

            // Property: Should detect conflict
            prop_assert!(
                result.is_err(),
                "Nested route '{}{}' should conflict with existing route '{}' but didn't",
                prefix, route_path, existing_path
            );
        }

        /// Property: Nested routes under different prefixes don't conflict
        ///
        /// For any two nested routers with the same internal routes but different
        /// prefixes, they should not conflict.
        #[test]
        fn prop_nested_routes_different_prefixes_no_conflict(
            prefix1_segments in prop::collection::vec("[a-z][a-z0-9]{1,5}", 1..3),
            prefix2_segments in prop::collection::vec("[a-z][a-z0-9]{1,5}", 1..3),
            route_segments in prop::collection::vec("[a-z][a-z0-9]{1,5}", 1..3),
            has_param in any::<bool>(),
        ) {
            // Build prefixes
            let prefix1 = format!("/{}", prefix1_segments.join("/"));
            let prefix2 = format!("/{}", prefix2_segments.join("/"));

            // Ensure prefixes are different
            prop_assume!(prefix1 != prefix2);

            async fn handler1() -> &'static str { "handler1" }
            async fn handler2() -> &'static str { "handler2" }

            // Build the route path
            let route_path = if has_param {
                format!("/{}/{{id}}", route_segments.join("/"))
            } else {
                format!("/{}", route_segments.join("/"))
            };

            // Try to nest both routers - should NOT conflict
            let result = catch_unwind(AssertUnwindSafe(|| {
                let nested1 = Router::new().route(&route_path, get(handler1));
                let nested2 = Router::new().route(&route_path, get(handler2));

                let app = Router::new()
                    .nest(&prefix1, nested1)
                    .nest(&prefix2, nested2);

                app.registered_routes().len()
            }));

            // Property: Should NOT conflict
            prop_assert!(
                result.is_ok(),
                "Routes under different prefixes '{}' and '{}' should not conflict",
                prefix1, prefix2
            );

            if let Ok(count) = result {
                prop_assert_eq!(count, 2, "Should have registered 2 routes");
            }
        }

        /// Property: Conflict error message contains resolution guidance
        ///
        /// When a nested route conflict is detected, the error message should
        /// include guidance on how to resolve the conflict.
        #[test]
        fn prop_nested_conflict_error_contains_guidance(
            prefix in "[a-z][a-z0-9]{1,5}",
            segment in "[a-z][a-z0-9]{1,5}",
            param1 in "[a-z][a-z0-9]{1,5}",
            param2 in "[a-z][a-z0-9]{1,5}",
        ) {
            prop_assume!(param1 != param2);

            async fn handler1() -> &'static str { "handler1" }
            async fn handler2() -> &'static str { "handler2" }

            let prefix = format!("/{}", prefix);
            let existing_path = format!("{}/{}/{{{}}}", prefix, segment, param1);
            let nested_path = format!("/{}/{{{}}}", segment, param2);

            let result = catch_unwind(AssertUnwindSafe(|| {
                let parent = Router::new().route(&existing_path, get(handler1));
                let nested = Router::new().route(&nested_path, get(handler2));
                let _app = parent.nest(&prefix, nested);
            }));

            prop_assert!(result.is_err(), "Should have detected conflict");

            if let Err(panic_info) = result {
                if let Some(msg) = panic_info.downcast_ref::<String>() {
                    prop_assert!(
                        msg.contains("How to resolve:"),
                        "Error should contain 'How to resolve:' guidance, got: {}",
                        msg
                    );
                    prop_assert!(
                        msg.contains("Use different path patterns") ||
                        msg.contains("different path patterns"),
                        "Error should suggest using different path patterns, got: {}",
                        msg
                    );
                }
            }
        }
    }
}