domainforge-core 0.17.0

Rust core library implementing the SEA DSL primitives and validation engine.
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
//! Protobuf Projection Engine
//!
//! This module provides functionality to project SEA semantic graphs to Protocol Buffer
//! (`.proto`) files. It supports:
//!
//! - Entity and Resource projection to Protobuf messages
//! - Type mapping from SEA types to Protobuf scalar types
//! - Deterministic field numbering for schema stability
//! - Governance message generation
//!
//! # Example
//!
//! ```rust,ignore
//! use domainforge_core::projection::protobuf::ProtobufEngine;
//! use domainforge_core::graph::Graph;
//!
//! let graph = build_graph_from_model();
//! let proto_file = ProtobufEngine::project(&graph, "my_namespace", "my.package");
//! println!("{}", proto_file.to_proto_string());
//! ```

use crate::graph::Graph;
use crate::primitives::{Entity, Resource};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::path::{Path, PathBuf};

// ============================================================================
// Protobuf IR Types
// ============================================================================

/// Represents a complete `.proto` file.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtoFile {
    /// The package name (e.g., "sea.example")
    pub package: String,
    /// Protobuf syntax version (always "proto3")
    pub syntax: String,
    /// Import statements
    pub imports: Vec<String>,
    /// File-level options
    pub options: ProtoOptions,
    /// Enum definitions
    pub enums: Vec<ProtoEnum>,
    /// Message definitions
    pub messages: Vec<ProtoMessage>,
    /// gRPC service definitions
    pub services: Vec<ProtoService>,
    /// Metadata about the projection
    pub metadata: ProtoMetadata,
}

impl ProtoFile {
    /// Create a new ProtoFile with the given package name.
    pub fn new(package: impl Into<String>) -> Self {
        Self {
            package: package.into(),
            syntax: "proto3".to_string(),
            imports: Vec::new(),
            options: ProtoOptions::default(),
            enums: Vec::new(),
            messages: Vec::new(),
            services: Vec::new(),
            metadata: ProtoMetadata::default(),
        }
    }

    /// Serialize the ProtoFile to `.proto` text format.
    pub fn to_proto_string(&self) -> String {
        let mut out = String::new();

        // Header comments
        out.push_str("// Generated by SEA Projection Framework\n");
        out.push_str(&format!(
            "// Projection: {}\n",
            self.metadata.projection_name
        ));
        out.push_str(&format!(
            "// Source Namespace: {}\n",
            self.metadata.source_namespace
        ));
        if let Some(ref version) = self.metadata.semantic_version {
            out.push_str(&format!("// Version: {}\n", version));
        }
        out.push_str(&format!(
            "// Generated At: {}\n",
            self.metadata.generated_at
        ));
        out.push_str("// DO NOT EDIT - This file is auto-generated\n\n");

        // Syntax
        out.push_str(&format!("syntax = \"{}\";\n\n", self.syntax));

        // Package
        out.push_str(&format!("package {};\n", self.package));

        // Options
        let options = &self.options;

        if let Some(ref pkg) = options.java_package {
            out.push_str(&format!("\noption java_package = \"{}\";", pkg));
        }
        if options.java_multiple_files {
            out.push_str("\noption java_multiple_files = true;");
        }
        if let Some(ref pkg) = options.go_package {
            out.push_str(&format!("\noption go_package = \"{}\";", pkg));
        }
        if let Some(ref ns) = options.csharp_namespace {
            out.push_str(&format!("\noption csharp_namespace = \"{}\";", ns));
        }
        if let Some(ref ns) = options.php_namespace {
            out.push_str(&format!("\noption php_namespace = \"{}\";", ns));
        }
        if let Some(ref pkg) = options.ruby_package {
            out.push_str(&format!("\noption ruby_package = \"{}\";", pkg));
        }
        if let Some(ref prefix) = options.swift_prefix {
            out.push_str(&format!("\noption swift_prefix = \"{}\";", prefix));
        }
        if let Some(ref prefix) = options.objc_class_prefix {
            out.push_str(&format!("\noption objc_class_prefix = \"{}\";", prefix));
        }
        if let Some(ref opt) = options.optimize_for {
            out.push_str(&format!("\noption optimize_for = {};", opt));
        }
        if options.deprecated {
            out.push_str("\noption deprecated = true;");
        }

        // Custom options
        for custom in &options.custom_options {
            out.push_str(&format!("\n{}", custom.to_proto_string()));
        }

        if options.java_package.is_some()
            || options.java_multiple_files
            || options.go_package.is_some()
            || options.csharp_namespace.is_some()
            || options.php_namespace.is_some()
            || options.ruby_package.is_some()
            || options.swift_prefix.is_some()
            || options.objc_class_prefix.is_some()
            || options.optimize_for.is_some()
            || options.deprecated
            || !options.custom_options.is_empty()
        {
            out.push('\n');
        }

        // Imports
        if !self.imports.is_empty() {
            out.push('\n');
            for import in &self.imports {
                out.push_str(&format!("import \"{}\";\n", import));
            }
        }

        // Enums
        for e in &self.enums {
            out.push('\n');
            out.push_str(&e.to_proto_string());
        }

        // Messages
        for m in &self.messages {
            out.push('\n');
            out.push_str(&m.to_proto_string());
        }

        // Services (gRPC)
        for s in &self.services {
            out.push('\n');
            out.push_str(&s.to_proto_string());
        }

        out
    }

    /// Scan all messages and automatically add required Well-Known Type imports.
    ///
    /// This method should be called after all messages are added to ensure
    /// proper imports are included for any WKT fields.
    pub fn add_wkt_imports(&mut self) {
        use std::collections::HashSet;
        let mut required_imports: HashSet<&'static str> = HashSet::new();

        // Scan all message fields for WKT references
        for msg in &self.messages {
            Self::collect_wkt_imports_from_message(msg, &mut required_imports);
        }

        // Add imports that aren't already present
        for import in required_imports {
            if !self.imports.contains(&import.to_string()) {
                self.imports.push(import.to_string());
            }
        }

        // Sort imports for deterministic output
        self.imports.sort();
    }

    fn collect_wkt_imports_from_message(
        msg: &ProtoMessage,
        imports: &mut std::collections::HashSet<&'static str>,
    ) {
        for field in &msg.fields {
            if let ProtoType::Message(ref type_name) = field.proto_type {
                if let Some(wkt) = WellKnownType::from_type_name(type_name) {
                    imports.insert(wkt.import_path());
                }
            }
        }

        // Recurse into nested messages
        for nested in &msg.nested_messages {
            Self::collect_wkt_imports_from_message(nested, imports);
        }
    }
}

/// File-level Protobuf options.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ProtoOptions {
    /// Java package for generated code
    pub java_package: Option<String>,
    /// Generate separate files for each message in Java
    pub java_multiple_files: bool,
    /// Go package path
    pub go_package: Option<String>,
    /// C# namespace
    pub csharp_namespace: Option<String>,
    /// PHP namespace
    pub php_namespace: Option<String>,
    /// Ruby package
    pub ruby_package: Option<String>,
    /// Swift prefix
    pub swift_prefix: Option<String>,
    /// Objective-C class prefix
    pub objc_class_prefix: Option<String>,
    /// Optimize for: SPEED, CODE_SIZE, or LITE_RUNTIME
    pub optimize_for: Option<String>,
    /// Mark all messages as deprecated
    pub deprecated: bool,
    /// Custom options (user-defined or extension options)
    pub custom_options: Vec<ProtoCustomOption>,
}

impl ProtoOptions {
    /// Set a standard option by name.
    pub fn set_option(&mut self, name: &str, value: ProtoOptionValue) {
        match name {
            "java_package" => {
                if let ProtoOptionValue::String(s) = value {
                    self.java_package = Some(s);
                }
            }
            "java_multiple_files" => {
                if let ProtoOptionValue::Bool(b) = value {
                    self.java_multiple_files = b;
                }
            }
            "go_package" => {
                if let ProtoOptionValue::String(s) = value {
                    self.go_package = Some(s);
                }
            }
            "csharp_namespace" => {
                if let ProtoOptionValue::String(s) = value {
                    self.csharp_namespace = Some(s);
                }
            }
            "php_namespace" => {
                if let ProtoOptionValue::String(s) = value {
                    self.php_namespace = Some(s);
                }
            }
            "ruby_package" => {
                if let ProtoOptionValue::String(s) = value {
                    self.ruby_package = Some(s);
                }
            }
            "swift_prefix" => {
                if let ProtoOptionValue::String(s) = value {
                    self.swift_prefix = Some(s);
                }
            }
            "objc_class_prefix" => {
                if let ProtoOptionValue::String(s) = value {
                    self.objc_class_prefix = Some(s);
                }
            }
            "optimize_for" => {
                if let ProtoOptionValue::String(s) = value {
                    self.optimize_for = Some(s);
                }
            }
            "deprecated" => {
                if let ProtoOptionValue::Bool(b) = value {
                    self.deprecated = b;
                }
            }
            _ => {
                // Unknown option, add as custom
                self.custom_options.push(ProtoCustomOption {
                    name: name.to_string(),
                    value,
                });
            }
        }
    }
}

/// A custom proto option (user-defined or extension).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ProtoCustomOption {
    /// Option name (e.g., "java_package" or "(myopt).field")
    pub name: String,
    /// Option value
    pub value: ProtoOptionValue,
}

impl ProtoCustomOption {
    /// Create a new custom option.
    pub fn new(name: impl Into<String>, value: ProtoOptionValue) -> Self {
        Self {
            name: name.into(),
            value,
        }
    }

    /// Serialize to proto option string.
    pub fn to_proto_string(&self) -> String {
        format!("option {} = {};", self.name, self.value.to_proto_string())
    }
}

/// Value for a proto option.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ProtoOptionValue {
    /// String value
    String(String),
    /// Integer value
    Int(i64),
    /// Float value
    Float(f64),
    /// Boolean value
    Bool(bool),
    /// Identifier/enum value (unquoted)
    Identifier(String),
}

impl ProtoOptionValue {
    /// Parse option value from JSON Value.
    pub fn from_json(value: &serde_json::Value) -> Self {
        match value {
            serde_json::Value::String(s) => ProtoOptionValue::String(s.clone()),
            serde_json::Value::Bool(b) => ProtoOptionValue::Bool(*b),
            serde_json::Value::Number(n) => {
                if let Some(i) = n.as_i64() {
                    ProtoOptionValue::Int(i)
                } else if let Some(f) = n.as_f64() {
                    ProtoOptionValue::Float(f)
                } else {
                    ProtoOptionValue::String(n.to_string())
                }
            }
            _ => ProtoOptionValue::String(value.to_string()),
        }
    }

    /// Serialize to proto option value string.
    pub fn to_proto_string(&self) -> String {
        match self {
            ProtoOptionValue::String(s) => {
                format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\""))
            }
            ProtoOptionValue::Int(i) => i.to_string(),
            ProtoOptionValue::Float(f) => f.to_string(),
            ProtoOptionValue::Bool(b) => b.to_string(),
            ProtoOptionValue::Identifier(s) => s.clone(),
        }
    }
}

/// Metadata about the projection source.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ProtoMetadata {
    /// Name of the projection that generated this file
    pub projection_name: String,
    /// Semantic version if available
    pub semantic_version: Option<String>,
    /// Source namespace from the SEA model
    pub source_namespace: String,
    /// Timestamp of generation
    pub generated_at: String,
}

// ============================================================================
// gRPC Service Types
// ============================================================================

/// Represents a gRPC service definition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtoService {
    /// Service name (e.g., "PaymentProcessorService")
    pub name: String,
    /// RPC methods in this service
    pub methods: Vec<ProtoRpcMethod>,
    /// Documentation comments
    pub comments: Vec<String>,
}

impl ProtoService {
    /// Create a new ProtoService with the given name.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            methods: Vec::new(),
            comments: Vec::new(),
        }
    }

    /// Serialize the service to `.proto` text format.
    pub fn to_proto_string(&self) -> String {
        let mut out = String::new();

        // Comments
        for comment in &self.comments {
            out.push_str(&format!("// {}\n", comment));
        }

        out.push_str(&format!("service {} {{\n", self.name));

        for method in &self.methods {
            out.push_str(&format!("  {}\n", method.to_proto_string()));
        }

        out.push_str("}\n");
        out
    }
}

/// Represents an RPC method in a gRPC service.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtoRpcMethod {
    /// Method name (e.g., "ProcessPayment")
    pub name: String,
    /// Request message type
    pub request_type: String,
    /// Response message type
    pub response_type: String,
    /// Streaming mode
    pub streaming: StreamingMode,
    /// Documentation comments
    pub comments: Vec<String>,
}

impl ProtoRpcMethod {
    /// Create a new unary RPC method.
    pub fn new(
        name: impl Into<String>,
        request_type: impl Into<String>,
        response_type: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            request_type: request_type.into(),
            response_type: response_type.into(),
            streaming: StreamingMode::Unary,
            comments: Vec::new(),
        }
    }

    /// Serialize the method to `.proto` text format.
    pub fn to_proto_string(&self) -> String {
        let request = match self.streaming {
            StreamingMode::ClientStreaming | StreamingMode::Bidirectional => {
                format!("stream {}", self.request_type)
            }
            _ => self.request_type.clone(),
        };

        let response = match self.streaming {
            StreamingMode::ServerStreaming | StreamingMode::Bidirectional => {
                format!("stream {}", self.response_type)
            }
            _ => self.response_type.clone(),
        };

        format!("rpc {}({}) returns ({});", self.name, request, response)
    }
}

/// gRPC streaming mode for RPC methods.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum StreamingMode {
    /// Unary RPC (default): single request, single response
    #[default]
    Unary,
    /// Server streaming: single request, stream of responses
    ServerStreaming,
    /// Client streaming: stream of requests, single response
    ClientStreaming,
    /// Bidirectional streaming: stream of requests, stream of responses
    Bidirectional,
}

impl StreamingMode {
    /// Parse streaming mode from a string (e.g., from Flow attributes).
    pub fn parse(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "streaming" | "server_streaming" | "serverstreaming" => StreamingMode::ServerStreaming,
            "client_streaming" | "clientstreaming" => StreamingMode::ClientStreaming,
            "bidirectional" | "bidi" | "duplex" => StreamingMode::Bidirectional,
            _ => StreamingMode::Unary,
        }
    }
}

impl std::fmt::Display for StreamingMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            StreamingMode::Unary => write!(f, "unary"),
            StreamingMode::ServerStreaming => write!(f, "server_streaming"),
            StreamingMode::ClientStreaming => write!(f, "client_streaming"),
            StreamingMode::Bidirectional => write!(f, "bidirectional"),
        }
    }
}

/// Represents a Protobuf message definition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtoMessage {
    /// Message name (PascalCase)
    pub name: String,
    /// Field definitions
    pub fields: Vec<ProtoField>,
    /// Nested message definitions
    pub nested_messages: Vec<ProtoMessage>,
    /// Nested enum definitions
    pub nested_enums: Vec<ProtoEnum>,
    /// Reserved field numbers
    pub reserved_numbers: Vec<u32>,
    /// Reserved field names
    pub reserved_names: Vec<String>,
    /// Documentation comments
    pub comments: Vec<String>,
}

impl ProtoMessage {
    /// Create a new empty ProtoMessage.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            fields: Vec::new(),
            nested_messages: Vec::new(),
            nested_enums: Vec::new(),
            reserved_numbers: Vec::new(),
            reserved_names: Vec::new(),
            comments: Vec::new(),
        }
    }

    /// Serialize the message to `.proto` text format.
    pub fn to_proto_string(&self) -> String {
        let mut out = String::new();

        // Comments
        for comment in &self.comments {
            out.push_str(&format!("// {}\n", comment));
        }

        out.push_str(&format!("message {} {{\n", self.name));

        // Reserved fields
        if !self.reserved_numbers.is_empty() {
            let nums: Vec<String> = self
                .reserved_numbers
                .iter()
                .map(|n| n.to_string())
                .collect();
            out.push_str(&format!("  reserved {};\n", nums.join(", ")));
        }
        if !self.reserved_names.is_empty() {
            let names: Vec<String> = self
                .reserved_names
                .iter()
                .map(|n| format!("\"{}\"", n))
                .collect();
            out.push_str(&format!("  reserved {};\n", names.join(", ")));
        }

        // Nested enums
        for e in &self.nested_enums {
            for line in e.to_proto_string().lines() {
                out.push_str(&format!("  {}\n", line));
            }
        }

        // Nested messages
        for m in &self.nested_messages {
            for line in m.to_proto_string().lines() {
                out.push_str(&format!("  {}\n", line));
            }
        }

        // Fields
        for field in &self.fields {
            out.push_str(&format!("  {}\n", field.to_proto_string()));
        }

        out.push_str("}\n");
        out
    }
}

/// Represents a Protobuf field definition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtoField {
    /// Field name (snake_case)
    pub name: String,
    /// Field number (must be unique within message)
    pub number: u32,
    /// Field type
    pub proto_type: ProtoType,
    /// Whether this is a repeated field
    pub repeated: bool,
    /// Whether this field is optional (proto3 optional)
    pub optional: bool,
    /// Documentation comments
    pub comments: Vec<String>,
}

impl ProtoField {
    /// Serialize the field to `.proto` text format.
    pub fn to_proto_string(&self) -> String {
        let mut parts = Vec::new();

        // Comments as inline
        if !self.comments.is_empty() {
            // We'll add comment at the end
        }

        if self.optional {
            parts.push("optional".to_string());
        }
        if self.repeated {
            parts.push("repeated".to_string());
        }

        parts.push(self.proto_type.to_proto_string());
        parts.push(self.name.clone());

        let mut line = format!("{} = {};", parts.join(" "), self.number);

        if !self.comments.is_empty() {
            line.push_str(&format!(" // {}", self.comments.join("; ")));
        }

        line
    }
}

/// Represents a Protobuf type reference.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ProtoType {
    /// Scalar types (int32, string, etc.)
    Scalar(ScalarType),
    /// Reference to another message type
    Message(String),
    /// Reference to an enum type
    Enum(String),
    /// Map type (map<key, value>)
    Map {
        key: Box<ProtoType>,
        value: Box<ProtoType>,
    },
}

impl ProtoType {
    /// Serialize the type to `.proto` text format.
    pub fn to_proto_string(&self) -> String {
        match self {
            ProtoType::Scalar(s) => s.to_proto_string(),
            ProtoType::Message(name) => name.clone(),
            ProtoType::Enum(name) => name.clone(),
            ProtoType::Map { key, value } => {
                format!(
                    "map<{}, {}>",
                    key.to_proto_string(),
                    value.to_proto_string()
                )
            }
        }
    }
}

/// Protobuf scalar types.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum ScalarType {
    Double,
    Float,
    Int32,
    Int64,
    Uint32,
    Uint64,
    Sint32,
    Sint64,
    Fixed32,
    Fixed64,
    Sfixed32,
    Sfixed64,
    Bool,
    String,
    Bytes,
}

impl ScalarType {
    /// Serialize the scalar type to `.proto` text format.
    pub fn to_proto_string(&self) -> String {
        match self {
            ScalarType::Double => "double",
            ScalarType::Float => "float",
            ScalarType::Int32 => "int32",
            ScalarType::Int64 => "int64",
            ScalarType::Uint32 => "uint32",
            ScalarType::Uint64 => "uint64",
            ScalarType::Sint32 => "sint32",
            ScalarType::Sint64 => "sint64",
            ScalarType::Fixed32 => "fixed32",
            ScalarType::Fixed64 => "fixed64",
            ScalarType::Sfixed32 => "sfixed32",
            ScalarType::Sfixed64 => "sfixed64",
            ScalarType::Bool => "bool",
            ScalarType::String => "string",
            ScalarType::Bytes => "bytes",
        }
        .to_string()
    }
}

/// Represents a Protobuf enum definition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtoEnum {
    /// Enum name (PascalCase)
    pub name: String,
    /// Enum values
    pub values: Vec<ProtoEnumValue>,
    /// Documentation comments
    pub comments: Vec<String>,
}

impl ProtoEnum {
    /// Create a new ProtoEnum with default UNSPECIFIED value.
    pub fn new(name: impl Into<String>) -> Self {
        let name = name.into();
        Self {
            values: vec![ProtoEnumValue {
                name: format!("{}_UNSPECIFIED", to_screaming_snake_case(&name)),
                number: 0,
            }],
            name,
            comments: Vec::new(),
        }
    }

    /// Add a value to the enum.
    pub fn add_value(&mut self, name: impl Into<String>) {
        let number = self.values.len() as i32;
        self.values.push(ProtoEnumValue {
            name: name.into(),
            number,
        });
    }

    /// Serialize the enum to `.proto` text format.
    pub fn to_proto_string(&self) -> String {
        let mut out = String::new();

        for comment in &self.comments {
            out.push_str(&format!("// {}\n", comment));
        }

        out.push_str(&format!("enum {} {{\n", self.name));

        for value in &self.values {
            out.push_str(&format!("  {} = {};\n", value.name, value.number));
        }

        out.push_str("}\n");
        out
    }
}

/// Represents a Protobuf enum value.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtoEnumValue {
    /// Value name (SCREAMING_SNAKE_CASE)
    pub name: String,
    /// Numeric value
    pub number: i32,
}

// ============================================================================
// Well-Known Types
// ============================================================================

/// Google Protobuf Well-Known Types.
///
/// These are standard types provided by Google that have special handling
/// in most Protobuf implementations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum WellKnownType {
    /// google.protobuf.Timestamp - for date/time values
    Timestamp,
    /// google.protobuf.Duration - for time spans
    Duration,
    /// google.protobuf.Any - for dynamic typing
    Any,
    /// google.protobuf.Struct - for JSON-like structures
    Struct,
    /// google.protobuf.Value - for dynamic JSON values
    Value,
    /// google.protobuf.ListValue - for JSON arrays
    ListValue,
    /// google.protobuf.Empty - for empty messages
    Empty,
    /// Wrapper types for nullable primitives
    Int32Value,
    Int64Value,
    UInt32Value,
    UInt64Value,
    FloatValue,
    DoubleValue,
    BoolValue,
    StringValue,
    BytesValue,
}

impl WellKnownType {
    /// Get the fully qualified type name.
    pub fn type_name(&self) -> &'static str {
        match self {
            WellKnownType::Timestamp => "google.protobuf.Timestamp",
            WellKnownType::Duration => "google.protobuf.Duration",
            WellKnownType::Any => "google.protobuf.Any",
            WellKnownType::Struct => "google.protobuf.Struct",
            WellKnownType::Value => "google.protobuf.Value",
            WellKnownType::ListValue => "google.protobuf.ListValue",
            WellKnownType::Empty => "google.protobuf.Empty",
            WellKnownType::Int32Value => "google.protobuf.Int32Value",
            WellKnownType::Int64Value => "google.protobuf.Int64Value",
            WellKnownType::UInt32Value => "google.protobuf.UInt32Value",
            WellKnownType::UInt64Value => "google.protobuf.UInt64Value",
            WellKnownType::FloatValue => "google.protobuf.FloatValue",
            WellKnownType::DoubleValue => "google.protobuf.DoubleValue",
            WellKnownType::BoolValue => "google.protobuf.BoolValue",
            WellKnownType::StringValue => "google.protobuf.StringValue",
            WellKnownType::BytesValue => "google.protobuf.BytesValue",
        }
    }

    /// Get the import path for this type.
    pub fn import_path(&self) -> &'static str {
        match self {
            WellKnownType::Timestamp => "google/protobuf/timestamp.proto",
            WellKnownType::Duration => "google/protobuf/duration.proto",
            WellKnownType::Any => "google/protobuf/any.proto",
            WellKnownType::Struct | WellKnownType::Value | WellKnownType::ListValue => {
                "google/protobuf/struct.proto"
            }
            WellKnownType::Empty => "google/protobuf/empty.proto",
            WellKnownType::Int32Value
            | WellKnownType::Int64Value
            | WellKnownType::UInt32Value
            | WellKnownType::UInt64Value
            | WellKnownType::FloatValue
            | WellKnownType::DoubleValue
            | WellKnownType::BoolValue
            | WellKnownType::StringValue
            | WellKnownType::BytesValue => "google/protobuf/wrappers.proto",
        }
    }

    /// Try to parse a type name into a WellKnownType.
    pub fn from_type_name(name: &str) -> Option<Self> {
        match name {
            "google.protobuf.Timestamp" => Some(WellKnownType::Timestamp),
            "google.protobuf.Duration" => Some(WellKnownType::Duration),
            "google.protobuf.Any" => Some(WellKnownType::Any),
            "google.protobuf.Struct" => Some(WellKnownType::Struct),
            "google.protobuf.Value" => Some(WellKnownType::Value),
            "google.protobuf.ListValue" => Some(WellKnownType::ListValue),
            "google.protobuf.Empty" => Some(WellKnownType::Empty),
            "google.protobuf.Int32Value" => Some(WellKnownType::Int32Value),
            "google.protobuf.Int64Value" => Some(WellKnownType::Int64Value),
            "google.protobuf.UInt32Value" => Some(WellKnownType::UInt32Value),
            "google.protobuf.UInt64Value" => Some(WellKnownType::UInt64Value),
            "google.protobuf.FloatValue" => Some(WellKnownType::FloatValue),
            "google.protobuf.DoubleValue" => Some(WellKnownType::DoubleValue),
            "google.protobuf.BoolValue" => Some(WellKnownType::BoolValue),
            "google.protobuf.StringValue" => Some(WellKnownType::StringValue),
            "google.protobuf.BytesValue" => Some(WellKnownType::BytesValue),
            _ => None,
        }
    }
}

// ============================================================================
// Type Mapping
// ============================================================================

/// Map SEA type strings to Protobuf types.
///
/// This function handles the common type names used in SEA attributes,
/// including mapping to Google Well-Known Types where appropriate.
pub fn map_sea_type_to_proto(sea_type: &str) -> ProtoType {
    match sea_type.to_lowercase().as_str() {
        // Scalar types
        "string" | "text" | "varchar" => ProtoType::Scalar(ScalarType::String),
        "int" | "integer" | "int64" | "long" => ProtoType::Scalar(ScalarType::Int64),
        "int32" | "short" => ProtoType::Scalar(ScalarType::Int32),
        "uint32" => ProtoType::Scalar(ScalarType::Uint32),
        "uint64" | "ulong" => ProtoType::Scalar(ScalarType::Uint64),
        "float" | "double" | "decimal" | "number" => ProtoType::Scalar(ScalarType::Double),
        "float32" => ProtoType::Scalar(ScalarType::Float),
        "bool" | "boolean" => ProtoType::Scalar(ScalarType::Bool),
        "bytes" | "binary" | "blob" => ProtoType::Scalar(ScalarType::Bytes),
        "uuid" | "guid" => ProtoType::Scalar(ScalarType::String),

        // Well-Known Types
        "date" | "datetime" | "timestamp" => {
            ProtoType::Message(WellKnownType::Timestamp.type_name().to_string())
        }
        "duration" | "timespan" | "interval" => {
            ProtoType::Message(WellKnownType::Duration.type_name().to_string())
        }
        "any" | "dynamic" | "object" => {
            ProtoType::Message(WellKnownType::Any.type_name().to_string())
        }
        "struct" | "json" | "jsonobject" => {
            ProtoType::Message(WellKnownType::Struct.type_name().to_string())
        }
        "value" | "jsonvalue" => ProtoType::Message(WellKnownType::Value.type_name().to_string()),
        "empty" | "void" | "unit" => {
            ProtoType::Message(WellKnownType::Empty.type_name().to_string())
        }

        // Nullable wrapper types
        "optional_int" | "nullable_int" | "int?" => {
            ProtoType::Message(WellKnownType::Int64Value.type_name().to_string())
        }
        "optional_int32" | "nullable_int32" | "int32?" => {
            ProtoType::Message(WellKnownType::Int32Value.type_name().to_string())
        }
        "optional_string" | "nullable_string" | "string?" => {
            ProtoType::Message(WellKnownType::StringValue.type_name().to_string())
        }
        "optional_bool" | "nullable_bool" | "bool?" => {
            ProtoType::Message(WellKnownType::BoolValue.type_name().to_string())
        }
        "optional_double" | "nullable_double" | "double?" => {
            ProtoType::Message(WellKnownType::DoubleValue.type_name().to_string())
        }
        "optional_float" | "nullable_float" | "float?" => {
            ProtoType::Message(WellKnownType::FloatValue.type_name().to_string())
        }
        "optional_bytes" | "nullable_bytes" | "bytes?" => {
            ProtoType::Message(WellKnownType::BytesValue.type_name().to_string())
        }

        // Custom types become messages
        _ => ProtoType::Message(sanitize_proto_ident(sea_type)),
    }
}

/// Infer a ProtoType from a serde_json::Value.
pub fn infer_proto_type_from_value(value: &Value) -> ProtoType {
    match value {
        Value::Null => ProtoType::Scalar(ScalarType::String),
        Value::Bool(_) => ProtoType::Scalar(ScalarType::Bool),
        Value::Number(n) => {
            if n.is_f64() {
                ProtoType::Scalar(ScalarType::Double)
            } else {
                ProtoType::Scalar(ScalarType::Int64)
            }
        }
        Value::String(_) => ProtoType::Scalar(ScalarType::String),
        Value::Array(_) => ProtoType::Scalar(ScalarType::String), // Arrays need special handling
        Value::Object(_) => ProtoType::Scalar(ScalarType::String), // Objects need special handling
    }
}

// ============================================================================
// Protobuf Engine
// ============================================================================

/// The Protobuf projection engine.
///
/// This struct provides methods to convert a SEA Graph into Protobuf IR
/// which can then be serialized to `.proto` text format.
pub struct ProtobufEngine;

impl ProtobufEngine {
    /// Project a SEA Graph to a ProtoFile.
    ///
    /// # Arguments
    ///
    /// * `graph` - The semantic graph to project
    /// * `namespace` - Filter to only include entities/resources from this namespace (empty = all)
    /// * `package` - The Protobuf package name to use
    ///
    /// # Returns
    ///
    /// A `ProtoFile` containing the generated Protobuf IR.
    pub fn project(graph: &Graph, namespace: &str, package: &str) -> ProtoFile {
        let mut proto = ProtoFile::new(package);
        proto.metadata.source_namespace = namespace.to_string();
        proto.metadata.generated_at = std::env::var("SOURCE_DATE_EPOCH")
            .ok()
            .and_then(|epoch| epoch.parse::<i64>().ok())
            .and_then(|epoch| chrono::DateTime::from_timestamp(epoch, 0))
            .map(|dt| dt.to_rfc3339())
            .unwrap_or_default();

        // Convert entities to messages
        for entity in graph.all_entities() {
            if namespace.is_empty() || entity.namespace() == namespace {
                proto.messages.push(Self::entity_to_message(entity));
            }
        }

        // Convert resources to messages
        for resource in graph.all_resources() {
            if namespace.is_empty() || resource.namespace() == namespace {
                proto.messages.push(Self::resource_to_message(resource));
            }
        }

        // Sort messages by name for deterministic output
        proto.messages.sort_by(|a, b| a.name.cmp(&b.name));

        // Auto-detect and add Well-Known Type imports
        proto.add_wkt_imports();

        proto
    }

    /// Project with options for a projection contract.
    pub fn project_with_options(
        graph: &Graph,
        namespace: &str,
        package: &str,
        projection_name: &str,
        include_governance: bool,
    ) -> ProtoFile {
        Self::project_with_full_options(
            graph,
            namespace,
            package,
            projection_name,
            include_governance,
            false, // include_services
        )
    }

    /// Project with all options including gRPC service generation.
    pub fn project_with_full_options(
        graph: &Graph,
        namespace: &str,
        package: &str,
        projection_name: &str,
        include_governance: bool,
        include_services: bool,
    ) -> ProtoFile {
        let mut proto = Self::project(graph, namespace, package);
        proto.metadata.projection_name = projection_name.to_string();

        if include_governance {
            proto.messages.extend(Self::generate_governance_messages());
        }

        if include_services {
            proto.services = Self::flows_to_services(graph, namespace);
            let response_msgs = Self::collect_response_messages(&proto.services);
            proto.messages.extend(response_msgs);
            proto.messages.sort_by(|a, b| a.name.cmp(&b.name));
        }

        // Re-add WKT imports in case governance messages need them
        proto.add_wkt_imports();

        proto
    }

    /// Project a SEA Graph to multiple ProtoFiles (one per namespace).
    ///
    /// This method partitions the graph by namespace, creating a separate `.proto` file
    /// for each. Cross-namespace references are automatically resolved by adding imports
    /// and fully qualifying type names.
    ///
    /// # Arguments
    ///
    /// * `graph` - The semantic graph to project
    /// * `base_package` - The root package name (e.g. "sea.generated")
    /// * `include_governance` - Whether to include governance messages
    /// * `include_services` - Whether to generate gRPC services
    ///
    /// # Returns
    ///
    /// A map of relative file paths to ProtoFile definitions.
    pub fn project_multi_file(
        graph: &Graph,
        base_package: &str,
        include_governance: bool,
        include_services: bool,
    ) -> BTreeMap<PathBuf, ProtoFile> {
        let mut files: BTreeMap<String, ProtoFile> = BTreeMap::new();
        // Index: TypeName -> (Namespace, FullPackage)
        let mut type_index: HashMap<String, (String, String)> = HashMap::new();

        // 1. Collect all namespaces and entities
        for entity in graph.all_entities() {
            let ns = entity.namespace();
            let package = if ns.is_empty() {
                base_package.to_string()
            } else {
                format!("{}.{}", base_package, ns)
            };

            type_index.insert(
                sanitize_proto_ident(entity.name()),
                (ns.to_string(), package.clone()),
            );

            files
                .entry(ns.to_string())
                .or_insert_with(|| ProtoFile::new(package))
                .messages
                .push(Self::entity_to_message(entity));
        }

        for resource in graph.all_resources() {
            let ns = resource.namespace();
            let package = if ns.is_empty() {
                base_package.to_string()
            } else {
                format!("{}.{}", base_package, ns)
            };

            type_index.insert(
                sanitize_proto_ident(resource.name()),
                (ns.to_string(), package.clone()),
            );

            files
                .entry(ns.to_string())
                .or_insert_with(|| ProtoFile::new(package))
                .messages
                .push(Self::resource_to_message(resource));
        }

        // 2. Add Services
        if include_services {
            let services = Self::flows_to_services(graph, "");
            let response_msgs = Self::collect_response_messages(&services);
            for service in services {
                let entity_name = service
                    .name
                    .strip_suffix("Service")
                    .unwrap_or(&service.name);

                if let Some((ns, package)) = type_index.get(entity_name) {
                    files
                        .entry(ns.clone())
                        .or_insert_with(|| ProtoFile::new(package.clone()))
                        .services
                        .push(service);
                } else {
                    let package = base_package.to_string();
                    files
                        .entry("".to_string())
                        .or_insert_with(|| ProtoFile::new(package))
                        .services
                        .push(service);
                }
            }
            for resp in response_msgs {
                type_index.insert(
                    resp.name.clone(),
                    ("".to_string(), base_package.to_string()),
                );
                files
                    .entry("".to_string())
                    .or_insert_with(|| ProtoFile::new(base_package.to_string()))
                    .messages
                    .push(resp);
            }
        }

        // 3. Add Governance (in root namespace usually)
        if include_governance {
            let root_file = files
                .entry("".to_string())
                .or_insert_with(|| ProtoFile::new(base_package.to_string()));

            let governance_msgs = Self::generate_governance_messages();
            for msg in &governance_msgs {
                type_index.insert(msg.name.clone(), ("".to_string(), base_package.to_string()));
            }
            root_file.messages.extend(governance_msgs);
        }

        // 4. Resolve Imports and Finalize
        for (ns, file) in files.iter_mut() {
            let mut imports_to_add = HashSet::new();

            // Check messages for cross-references
            for msg in &mut file.messages {
                Self::resolve_imports_in_message(msg, ns, &type_index, &mut imports_to_add);
            }

            // Check services
            for svc in &mut file.services {
                for method in &mut svc.methods {
                    // Check request types
                    if let Some((target_ns, target_pkg)) = type_index.get(&method.request_type) {
                        if target_ns != ns {
                            imports_to_add.insert(target_ns.clone());
                            method.request_type = format!("{}.{}", target_pkg, method.request_type);
                        }
                    }
                    // Check response types
                    if let Some((target_ns, target_pkg)) = type_index.get(&method.response_type) {
                        if target_ns != ns {
                            imports_to_add.insert(target_ns.clone());
                            method.response_type =
                                format!("{}.{}", target_pkg, method.response_type);
                        }
                    }
                }
            }

            // Add collected imports
            for target_ns in imports_to_add {
                // self-import is prevented by check above
                // root namespace usually doesn't need path prefix if files in same dir,
                // but assume we strictly follow directory structure.
                let import_path = if target_ns.is_empty() {
                    "projection.proto".to_string()
                } else {
                    format!("{}.proto", target_ns.replace('.', "/"))
                };
                file.imports.push(import_path);
            }

            // Sort and deduplicate imports (including WKTs)
            file.add_wkt_imports();
        }

        // Convert map to PathBuf keys
        let mut results = BTreeMap::new();
        for (ns, file) in files {
            let path = if ns.is_empty() {
                PathBuf::from("projection.proto")
            } else {
                PathBuf::from(format!("{}.proto", ns.replace('.', "/")))
            };
            results.insert(path, file);
        }

        results
    }

    /// Helper to resolve cross-namespace imports within a message.
    fn resolve_imports_in_message(
        msg: &mut ProtoMessage,
        current_ns: &str,
        index: &HashMap<String, (String, String)>,
        imports: &mut HashSet<String>,
    ) {
        for field in &mut msg.fields {
            match &mut field.proto_type {
                ProtoType::Message(name) | ProtoType::Enum(name) => {
                    // Ignore WKTs
                    if WellKnownType::from_type_name(name).is_some() {
                        continue;
                    }

                    if let Some((target_ns, target_pkg)) = index.get(name.as_str()) {
                        if target_ns != current_ns {
                            imports.insert(target_ns.clone());
                            *name = format!("{}.{}", target_pkg, name);
                        }
                    }
                }
                ProtoType::Map { key: _, value } => {
                    // Check recursively (simplified for now assuming only value can be message)
                    if let ProtoType::Message(name) = &mut **value {
                        if WellKnownType::from_type_name(name).is_some() {
                            continue;
                        }
                        if let Some((target_ns, target_pkg)) = index.get(name.as_str()) {
                            if target_ns != current_ns {
                                imports.insert(target_ns.clone());
                                *name = format!("{}.{}", target_pkg, name);
                            }
                        }
                    }
                }
                _ => {}
            }
        }

        // Recurse into nested messages
        for nested in &mut msg.nested_messages {
            Self::resolve_imports_in_message(nested, current_ns, index, imports);
        }
    }
    ///
    /// This method groups flows by their destination entity (service provider)
    /// and creates RPC methods for each flow. The naming convention follows
    /// gRPC best practices: `{DestinationEntity}Service`.
    ///
    /// # Example
    ///
    /// A flow `Customer -> PaymentProcessor of PaymentRequest` generates:
    /// ```protobuf
    /// service PaymentProcessorService {
    ///   rpc ProcessPaymentRequest(PaymentRequest) returns (PaymentRequestResponse);
    /// }
    /// ```
    pub fn flows_to_services(graph: &Graph, namespace: &str) -> Vec<ProtoService> {
        let mut services: BTreeMap<String, ProtoService> = BTreeMap::new();

        for flow in graph.all_flows() {
            if !namespace.is_empty() && flow.namespace() != namespace {
                continue;
            }

            let to_entity = match graph.get_entity(flow.to_id()) {
                Some(e) => e,
                None => continue,
            };

            let resource = match graph.get_resource(flow.resource_id()) {
                Some(r) => r,
                None => continue,
            };

            let service_name = format!("{}Service", sanitize_proto_ident(to_entity.name()));
            let method_name = format!("Process{}", sanitize_proto_ident(resource.name()));
            let request_type = sanitize_proto_ident(resource.name());
            let response_type = format!("{}Response", sanitize_proto_ident(resource.name()));

            let streaming = flow
                .get_attribute("streaming")
                .and_then(|v| v.as_str())
                .map(StreamingMode::parse)
                .unwrap_or(StreamingMode::Unary);

            let mut method = ProtoRpcMethod::new(&method_name, &request_type, &response_type);
            method.streaming = streaming;

            if let Some(from_entity) = graph.get_entity(flow.from_id()) {
                method.comments.push(format!(
                    "Flow: {} -> {} of {}",
                    from_entity.name(),
                    to_entity.name(),
                    resource.name()
                ));
            }

            services
                .entry(service_name.clone())
                .or_insert_with(|| {
                    let mut svc = ProtoService::new(&service_name);
                    svc.comments
                        .push(format!("gRPC service for {}", to_entity.name()));
                    svc
                })
                .methods
                .push(method);
        }

        services.into_values().collect()
    }

    pub fn collect_response_messages(services: &[ProtoService]) -> Vec<ProtoMessage> {
        let mut seen: HashSet<String> = HashSet::new();
        let mut messages = Vec::new();
        for service in services {
            for method in &service.methods {
                if !seen.contains(&method.response_type) {
                    seen.insert(method.response_type.clone());
                    let mut msg = ProtoMessage::new(&method.response_type);
                    msg.comments
                        .push(format!("Response message for {}", method.name));
                    msg.fields.push(ProtoField {
                        name: "success".to_string(),
                        number: 1,
                        proto_type: ProtoType::Scalar(ScalarType::Bool),
                        repeated: false,
                        optional: false,
                        comments: vec![],
                    });
                    msg.fields.push(ProtoField {
                        name: "message".to_string(),
                        number: 2,
                        proto_type: ProtoType::Scalar(ScalarType::String),
                        repeated: false,
                        optional: true,
                        comments: vec![],
                    });
                    messages.push(msg);
                }
            }
        }
        messages
    }

    /// Convert an Entity to a ProtoMessage.
    fn entity_to_message(entity: &Entity) -> ProtoMessage {
        let mut msg = ProtoMessage::new(sanitize_proto_ident(entity.name()));
        msg.comments.push(format!("SEA Entity: {}", entity.name()));
        msg.comments
            .push(format!("Namespace: {}", entity.namespace()));

        let mut field_number = 1u32;

        // Add id field (always first)
        msg.fields.push(ProtoField {
            name: "id".to_string(),
            number: field_number,
            proto_type: ProtoType::Scalar(ScalarType::String),
            repeated: false,
            optional: false,
            comments: vec!["Unique identifier".to_string()],
        });
        field_number += 1;

        // Add name field
        msg.fields.push(ProtoField {
            name: "name".to_string(),
            number: field_number,
            proto_type: ProtoType::Scalar(ScalarType::String),
            repeated: false,
            optional: false,
            comments: vec!["Entity name".to_string()],
        });
        field_number += 1;

        // Add attributes sorted alphabetically for deterministic output
        let mut sorted_attrs: BTreeMap<&String, &Value> = BTreeMap::new();
        for (key, value) in entity.attributes() {
            sorted_attrs.insert(key, value);
        }

        for (key, value) in sorted_attrs {
            msg.fields.push(ProtoField {
                name: to_snake_case(key),
                number: field_number,
                proto_type: infer_proto_type_from_value(value),
                repeated: matches!(value, Value::Array(_)),
                optional: true,
                comments: vec![],
            });
            field_number += 1;
        }

        msg
    }

    /// Convert a Resource to a ProtoMessage.
    fn resource_to_message(resource: &Resource) -> ProtoMessage {
        let mut msg = ProtoMessage::new(sanitize_proto_ident(resource.name()));
        msg.comments
            .push(format!("SEA Resource: {}", resource.name()));
        msg.comments
            .push(format!("Unit: {}", resource.unit().symbol()));

        let mut field_number = 1u32;

        // Add id field
        msg.fields.push(ProtoField {
            name: "id".to_string(),
            number: field_number,
            proto_type: ProtoType::Scalar(ScalarType::String),
            repeated: false,
            optional: false,
            comments: vec!["Unique identifier".to_string()],
        });
        field_number += 1;

        // Add name field
        msg.fields.push(ProtoField {
            name: "name".to_string(),
            number: field_number,
            proto_type: ProtoType::Scalar(ScalarType::String),
            repeated: false,
            optional: false,
            comments: vec!["Resource name".to_string()],
        });
        field_number += 1;

        // Add quantity field with unit comment
        msg.fields.push(ProtoField {
            name: "quantity".to_string(),
            number: field_number,
            proto_type: ProtoType::Scalar(ScalarType::Double),
            repeated: false,
            optional: true,
            comments: vec![format!("Quantity in {}", resource.unit().symbol())],
        });
        field_number += 1;

        // Add unit field
        msg.fields.push(ProtoField {
            name: "unit".to_string(),
            number: field_number,
            proto_type: ProtoType::Scalar(ScalarType::String),
            repeated: false,
            optional: false,
            comments: vec!["Unit of measurement".to_string()],
        });
        field_number += 1;

        // Add attributes
        let mut sorted_attrs: BTreeMap<&String, &Value> = BTreeMap::new();
        for (key, value) in resource.attributes() {
            sorted_attrs.insert(key, value);
        }

        for (key, value) in sorted_attrs {
            msg.fields.push(ProtoField {
                name: to_snake_case(key),
                number: field_number,
                proto_type: infer_proto_type_from_value(value),
                repeated: matches!(value, Value::Array(_)),
                optional: true,
                comments: vec![],
            });
            field_number += 1;
        }

        msg
    }

    /// Generate standard governance messages.
    fn generate_governance_messages() -> Vec<ProtoMessage> {
        let mut messages = Vec::new();

        // PolicyViolation message
        let mut violation = ProtoMessage::new("PolicyViolation");
        violation
            .comments
            .push("Represents a policy violation event".to_string());
        violation.fields = vec![
            ProtoField {
                name: "policy_name".to_string(),
                number: 1,
                proto_type: ProtoType::Scalar(ScalarType::String),
                repeated: false,
                optional: false,
                comments: vec!["Name of the violated policy".to_string()],
            },
            ProtoField {
                name: "entity_id".to_string(),
                number: 2,
                proto_type: ProtoType::Scalar(ScalarType::String),
                repeated: false,
                optional: false,
                comments: vec!["ID of the entity that violated the policy".to_string()],
            },
            ProtoField {
                name: "severity".to_string(),
                number: 3,
                proto_type: ProtoType::Scalar(ScalarType::String),
                repeated: false,
                optional: false,
                comments: vec!["Severity level (error, warn, info)".to_string()],
            },
            ProtoField {
                name: "message".to_string(),
                number: 4,
                proto_type: ProtoType::Scalar(ScalarType::String),
                repeated: false,
                optional: false,
                comments: vec!["Human-readable violation message".to_string()],
            },
            ProtoField {
                name: "timestamp".to_string(),
                number: 5,
                proto_type: ProtoType::Scalar(ScalarType::String),
                repeated: false,
                optional: false,
                comments: vec!["When the violation occurred".to_string()],
            },
        ];
        messages.push(violation);

        // MetricEvent message
        let mut metric = ProtoMessage::new("MetricEvent");
        metric
            .comments
            .push("Represents a metric measurement event".to_string());
        metric.fields = vec![
            ProtoField {
                name: "metric_name".to_string(),
                number: 1,
                proto_type: ProtoType::Scalar(ScalarType::String),
                repeated: false,
                optional: false,
                comments: vec!["Name of the metric".to_string()],
            },
            ProtoField {
                name: "value".to_string(),
                number: 2,
                proto_type: ProtoType::Scalar(ScalarType::Double),
                repeated: false,
                optional: false,
                comments: vec!["Measured value".to_string()],
            },
            ProtoField {
                name: "unit".to_string(),
                number: 3,
                proto_type: ProtoType::Scalar(ScalarType::String),
                repeated: false,
                optional: true,
                comments: vec!["Unit of measurement".to_string()],
            },
            ProtoField {
                name: "timestamp".to_string(),
                number: 4,
                proto_type: ProtoType::Scalar(ScalarType::String),
                repeated: false,
                optional: false,
                comments: vec!["When the measurement was taken".to_string()],
            },
        ];
        messages.push(metric);

        messages
    }
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Convert a string to PascalCase.
///
/// Preserves original casing of characters after the first letter of each word,
/// so "PaymentProcessor" stays "PaymentProcessor" (not "Paymentprocessor").
fn to_pascal_case(s: &str) -> String {
    s.split(|c: char| !c.is_alphanumeric())
        .filter(|part| !part.is_empty())
        .map(|part| {
            let mut chars = part.chars();
            match chars.next() {
                Some(first) => first.to_uppercase().to_string() + chars.as_str(),
                None => String::new(),
            }
        })
        .collect()
}

fn sanitize_proto_ident(raw: &str) -> String {
    let pascal = to_pascal_case(raw);
    if pascal.is_empty() {
        return "SeaUnnamed".to_string();
    }
    let mut result = String::new();
    for (i, c) in pascal.chars().enumerate() {
        if i == 0 && !c.is_ascii_alphabetic() && c != '_' {
            result.push_str("Sea");
        }
        if c.is_ascii_alphanumeric() || c == '_' {
            result.push(c);
        }
    }
    if result.is_empty() {
        return "SeaUnnamed".to_string();
    }
    let reserved = [
        "syntax", "package", "message", "service", "rpc", "enum", "import", "option", "returns",
        "stream", "reserved", "bool", "string", "bytes", "double", "float", "int32", "int64",
        "uint32", "uint64", "true", "false",
    ];
    if reserved.contains(&result.to_lowercase().as_str()) {
        result = format!("Sea{}", result);
    }
    result
}

/// Convert a string to snake_case.
fn to_snake_case(s: &str) -> String {
    let mut result = String::new();
    let mut prev_is_uppercase = false;

    for (i, c) in s.chars().enumerate() {
        if c.is_uppercase() {
            if i > 0 && !prev_is_uppercase {
                result.push('_');
            }
            result.push(c.to_lowercase().next().unwrap_or(c));
            prev_is_uppercase = true;
        } else if c.is_alphanumeric() {
            result.push(c);
            prev_is_uppercase = false;
        } else {
            if !result.is_empty() && !result.ends_with('_') {
                result.push('_');
            }
            prev_is_uppercase = false;
        }
    }

    result.trim_matches('_').to_string()
}

/// Convert a string to SCREAMING_SNAKE_CASE.
fn to_screaming_snake_case(s: &str) -> String {
    to_snake_case(s).to_uppercase()
}

pub fn validate_proto_package_namespace(ns: &str) -> Result<(), String> {
    if ns.is_empty() {
        return Ok(());
    }
    if ns.contains('/')
        || ns.contains('\\')
        || ns.contains("..")
        || ns.starts_with('.')
        || ns.starts_with('/')
    {
        return Err(format!(
            "Invalid protobuf namespace '{}': must not contain path separators or traversal",
            ns
        ));
    }
    let mut chars = ns.chars().peekable();
    while let Some(first) = chars.next() {
        if !first.is_ascii_alphabetic() && first != '_' {
            return Err(format!(
                "Invalid protobuf namespace '{}': must be dotted identifiers (e.g., com.example.api)",
                ns
            ));
        }
        loop {
            match chars.peek() {
                Some('.') => {
                    chars.next();
                    break;
                }
                Some(c) if c.is_ascii_alphanumeric() || *c == '_' => {
                    chars.next();
                }
                Some(_) => {
                    return Err(format!(
                        "Invalid protobuf namespace '{}': must be dotted identifiers (e.g., com.example.api)",
                        ns
                    ));
                }
                None => break,
            }
        }
    }
    Ok(())
}

pub fn validate_output_path(output_root: &Path, target: &Path) -> Result<(), String> {
    let canonical_output = output_root.canonicalize().map_err(|e| {
        format!(
            "Cannot resolve output root '{}': {}",
            output_root.display(),
            e
        )
    })?;

    let rel = target
        .strip_prefix(output_root)
        .or_else(|_| target.strip_prefix(&canonical_output))
        .map_err(|_| {
            format!(
                "Security: output path '{}' escapes output directory",
                target.display()
            )
        })?;

    // Lexically reject traversal even when the file does not exist yet.
    if rel.components().any(|c| {
        matches!(
            c,
            std::path::Component::ParentDir
                | std::path::Component::RootDir
                | std::path::Component::Prefix(_)
        )
    }) {
        return Err(format!(
            "Security: output path '{}' escapes output directory",
            target.display()
        ));
    }

    let canonical_target_base = canonical_output.join(rel);
    let target_for_canonical_checks = if target.is_absolute() {
        target
    } else {
        canonical_target_base.as_path()
    };

    if let Some(canonical_parent) = canonical_target_base
        .parent()
        .filter(|p| p.exists())
        .and_then(|p| p.canonicalize().ok())
    {
        if !canonical_parent.starts_with(&canonical_output) {
            return Err(format!(
                "Security: output path '{}' escapes output directory",
                target.display()
            ));
        }
    }

    if let Some(canonical_parent) = target_for_canonical_checks
        .parent()
        .filter(|p| p.exists())
        .and_then(|p| p.canonicalize().ok())
    {
        if !canonical_parent.starts_with(&canonical_output) {
            return Err(format!(
                "Security: output path '{}' escapes output directory",
                target.display()
            ));
        }
    }

    // Best-effort canonicalization for existing paths catches symlink escapes.
    if let Ok(canonical_target) = target_for_canonical_checks.canonicalize() {
        if !canonical_target.starts_with(&canonical_output) {
            return Err(format!(
                "Security: output path '{}' escapes output directory",
                target.display()
            ));
        }
    }

    Ok(())
}

// ============================================================================
// Compatibility Enforcement
// ============================================================================

/// Compatibility mode for schema evolution.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum CompatibilityMode {
    /// Only additions allowed - strictest mode for public APIs
    Additive,
    /// Removals become reserved fields - default for internal APIs
    #[default]
    Backward,
    /// All changes allowed - for breaking releases
    Breaking,
}

impl std::fmt::Display for CompatibilityMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CompatibilityMode::Additive => write!(f, "additive"),
            CompatibilityMode::Backward => write!(f, "backward"),
            CompatibilityMode::Breaking => write!(f, "breaking"),
        }
    }
}

impl std::str::FromStr for CompatibilityMode {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "additive" | "strict" => Ok(CompatibilityMode::Additive),
            "backward" | "backwards" | "default" => Ok(CompatibilityMode::Backward),
            "breaking" | "none" => Ok(CompatibilityMode::Breaking),
            _ => Err(format!("Unknown compatibility mode: {}", s)),
        }
    }
}

/// A compatibility violation found during schema comparison.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompatibilityViolation {
    /// The message name where the violation occurred
    pub message_name: String,
    /// The field name involved (if applicable)
    pub field_name: Option<String>,
    /// The field number involved (if applicable)
    pub field_number: Option<u32>,
    /// Type of violation
    pub violation_type: ViolationType,
    /// Human-readable description
    pub description: String,
}

/// Types of compatibility violations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ViolationType {
    /// A field was removed
    FieldRemoved,
    /// A field number was reused with a different name/type
    FieldNumberReused,
    /// A field type was changed
    FieldTypeChanged,
    /// A field was renamed (same number, different name)
    FieldRenamed,
    /// A message was removed
    MessageRemoved,
    /// A required field was added (breaking in proto3)
    RequiredFieldAdded,
}

impl std::fmt::Display for ViolationType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ViolationType::FieldRemoved => write!(f, "field_removed"),
            ViolationType::FieldNumberReused => write!(f, "field_number_reused"),
            ViolationType::FieldTypeChanged => write!(f, "field_type_changed"),
            ViolationType::FieldRenamed => write!(f, "field_renamed"),
            ViolationType::MessageRemoved => write!(f, "message_removed"),
            ViolationType::RequiredFieldAdded => write!(f, "required_field_added"),
        }
    }
}

/// Result of a compatibility check.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompatibilityResult {
    /// Whether the schemas are compatible under the given mode
    pub is_compatible: bool,
    /// The mode used for checking
    pub mode: CompatibilityMode,
    /// List of violations found
    pub violations: Vec<CompatibilityViolation>,
    /// Suggested fixes (reserved fields to add)
    pub suggested_reserved_numbers: BTreeMap<String, Vec<u32>>,
    /// Suggested reserved names
    pub suggested_reserved_names: BTreeMap<String, Vec<String>>,
}

impl CompatibilityResult {
    /// Create a compatible (empty) result.
    pub fn compatible(mode: CompatibilityMode) -> Self {
        Self {
            is_compatible: true,
            mode,
            violations: Vec::new(),
            suggested_reserved_numbers: BTreeMap::new(),
            suggested_reserved_names: BTreeMap::new(),
        }
    }

    /// Check if there are any violations.
    pub fn has_violations(&self) -> bool {
        !self.violations.is_empty()
    }

    /// Format violations as a human-readable report.
    pub fn to_report(&self) -> String {
        let mut out = String::new();
        out.push_str(&format!("Compatibility Check (mode: {})\n", self.mode));
        out.push_str(&format!(
            "Result: {}\n",
            if self.is_compatible { "PASS" } else { "FAIL" }
        ));

        if !self.violations.is_empty() {
            out.push_str(&format!("\nViolations ({}):\n", self.violations.len()));
            for v in &self.violations {
                out.push_str(&format!(
                    "  - [{}] {}: {}\n",
                    v.violation_type, v.message_name, v.description
                ));
            }
        }

        if !self.suggested_reserved_numbers.is_empty() {
            out.push_str("\nSuggested Reserved Fields:\n");
            for (msg, nums) in &self.suggested_reserved_numbers {
                let nums_str: Vec<String> = nums.iter().map(|n| n.to_string()).collect();
                out.push_str(&format!(
                    "  message {}: reserved {};\n",
                    msg,
                    nums_str.join(", ")
                ));
            }
        }

        out
    }
}

/// Schema compatibility checker.
pub struct CompatibilityChecker;

impl CompatibilityChecker {
    /// Check compatibility between an old and new ProtoFile.
    ///
    /// # Arguments
    /// * `old` - The previous schema version
    /// * `new` - The new schema version  
    /// * `mode` - The compatibility mode to enforce
    ///
    /// # Returns
    /// A CompatibilityResult with violations and suggested fixes.
    pub fn check(old: &ProtoFile, new: &ProtoFile, mode: CompatibilityMode) -> CompatibilityResult {
        let mut result = CompatibilityResult::compatible(mode);

        // Build lookup maps for old schema
        let old_messages: BTreeMap<&str, &ProtoMessage> =
            old.messages.iter().map(|m| (m.name.as_str(), m)).collect();

        let new_messages: BTreeMap<&str, &ProtoMessage> =
            new.messages.iter().map(|m| (m.name.as_str(), m)).collect();

        // Check for removed messages
        for name in old_messages.keys() {
            if !new_messages.contains_key(name) {
                result.violations.push(CompatibilityViolation {
                    message_name: name.to_string(),
                    field_name: None,
                    field_number: None,
                    violation_type: ViolationType::MessageRemoved,
                    description: format!("Message '{}' was removed", name),
                });
            }
        }

        // Check each message that exists in both
        for (name, old_msg) in &old_messages {
            if let Some(new_msg) = new_messages.get(name) {
                Self::check_message(old_msg, new_msg, &mut result);
            }
        }

        // Determine if compatible based on mode
        result.is_compatible = match mode {
            CompatibilityMode::Breaking => true, // Always compatible in breaking mode
            CompatibilityMode::Backward => {
                // Compatible if no field number reuse or type changes
                !result.violations.iter().any(|v| {
                    matches!(
                        v.violation_type,
                        ViolationType::FieldNumberReused | ViolationType::FieldTypeChanged
                    )
                })
            }
            CompatibilityMode::Additive => {
                // Any removal or change is incompatible
                result.violations.is_empty()
            }
        };

        result
    }

    /// Check compatibility between two messages.
    fn check_message(old: &ProtoMessage, new: &ProtoMessage, result: &mut CompatibilityResult) {
        // Build field maps by number and by name
        let old_by_number: BTreeMap<u32, &ProtoField> =
            old.fields.iter().map(|f| (f.number, f)).collect();

        let new_by_number: BTreeMap<u32, &ProtoField> =
            new.fields.iter().map(|f| (f.number, f)).collect();

        let old_by_name: BTreeMap<&str, &ProtoField> =
            old.fields.iter().map(|f| (f.name.as_str(), f)).collect();

        // Check for removed fields
        for (number, old_field) in &old_by_number {
            if !new_by_number.contains_key(number) {
                result.violations.push(CompatibilityViolation {
                    message_name: old.name.clone(),
                    field_name: Some(old_field.name.clone()),
                    field_number: Some(*number),
                    violation_type: ViolationType::FieldRemoved,
                    description: format!(
                        "Field '{}' (number {}) was removed",
                        old_field.name, number
                    ),
                });

                // Suggest reserving this field number
                result
                    .suggested_reserved_numbers
                    .entry(old.name.clone())
                    .or_default()
                    .push(*number);

                result
                    .suggested_reserved_names
                    .entry(old.name.clone())
                    .or_default()
                    .push(old_field.name.clone());
            }
        }

        // Check for field number reuse with different name/type
        for (number, new_field) in &new_by_number {
            if let Some(old_field) = old_by_number.get(number) {
                // Check name change
                if old_field.name != new_field.name {
                    result.violations.push(CompatibilityViolation {
                        message_name: old.name.clone(),
                        field_name: Some(new_field.name.clone()),
                        field_number: Some(*number),
                        violation_type: ViolationType::FieldRenamed,
                        description: format!(
                            "Field number {} renamed from '{}' to '{}'",
                            number, old_field.name, new_field.name
                        ),
                    });
                }

                // Check type change
                if old_field.proto_type != new_field.proto_type {
                    result.violations.push(CompatibilityViolation {
                        message_name: old.name.clone(),
                        field_name: Some(new_field.name.clone()),
                        field_number: Some(*number),
                        violation_type: ViolationType::FieldTypeChanged,
                        description: format!(
                            "Field '{}' type changed from {} to {}",
                            new_field.name,
                            old_field.proto_type.to_proto_string(),
                            new_field.proto_type.to_proto_string()
                        ),
                    });
                }
            } else {
                // New field - check if it reuses a previously removed name
                if old_by_name.contains_key(new_field.name.as_str()) {
                    let old_field = old_by_name[new_field.name.as_str()];
                    if old_field.number != *number {
                        result.violations.push(CompatibilityViolation {
                            message_name: old.name.clone(),
                            field_name: Some(new_field.name.clone()),
                            field_number: Some(*number),
                            violation_type: ViolationType::FieldNumberReused,
                            description: format!(
                                "Field '{}' changed number from {} to {}",
                                new_field.name, old_field.number, number
                            ),
                        });
                    }
                }
            }
        }
    }

    /// Apply compatibility fixes to a new ProtoFile based on an old one.
    ///
    /// This adds reserved field numbers and names for removed fields.
    pub fn apply_backward_compatibility(old: &ProtoFile, new: &mut ProtoFile) {
        let result = Self::check(old, new, CompatibilityMode::Backward);

        // Apply suggested reserved numbers
        for msg in &mut new.messages {
            if let Some(reserved_nums) = result.suggested_reserved_numbers.get(&msg.name) {
                for num in reserved_nums {
                    if !msg.reserved_numbers.contains(num) {
                        msg.reserved_numbers.push(*num);
                    }
                }
                msg.reserved_numbers.sort();
            }

            if let Some(reserved_names) = result.suggested_reserved_names.get(&msg.name) {
                for name in reserved_names {
                    if !msg.reserved_names.contains(name) {
                        msg.reserved_names.push(name.clone());
                    }
                }
                msg.reserved_names.sort();
            }
        }
    }
}

/// File-based schema history storage.
pub struct SchemaHistory {
    /// Directory where schema history is stored
    history_dir: std::path::PathBuf,
}

impl SchemaHistory {
    /// Create a new SchemaHistory with the given directory.
    pub fn new(history_dir: impl Into<std::path::PathBuf>) -> Self {
        Self {
            history_dir: history_dir.into(),
        }
    }

    /// Get the path for a schema file.
    fn schema_path(&self, package: &str) -> std::path::PathBuf {
        let filename = format!("{}.json", package.replace('.', "_"));
        self.history_dir.join(filename)
    }

    /// Load the previous schema for a package.
    pub fn load(&self, package: &str) -> Result<Option<ProtoFile>, String> {
        let path = self.schema_path(package);
        if !path.exists() {
            return Ok(None);
        }

        let content = std::fs::read_to_string(&path)
            .map_err(|e| format!("Failed to read schema history: {}", e))?;

        let proto: ProtoFile = serde_json::from_str(&content)
            .map_err(|e| format!("Failed to parse schema history: {}", e))?;

        Ok(Some(proto))
    }

    /// Save a schema to history.
    pub fn save(&self, proto: &ProtoFile) -> Result<(), String> {
        // Ensure directory exists
        std::fs::create_dir_all(&self.history_dir)
            .map_err(|e| format!("Failed to create history directory: {}", e))?;

        let path = self.schema_path(&proto.package);
        let content = serde_json::to_string_pretty(proto)
            .map_err(|e| format!("Failed to serialize schema: {}", e))?;

        std::fs::write(&path, content)
            .map_err(|e| format!("Failed to write schema history: {}", e))?;

        Ok(())
    }

    /// Check compatibility and optionally apply fixes.
    pub fn check_and_update(
        &self,
        new: &mut ProtoFile,
        mode: CompatibilityMode,
        apply_fixes: bool,
    ) -> Result<CompatibilityResult, String> {
        let old = self.load(&new.package)?;

        let result = match old {
            Some(ref old_proto) => {
                if apply_fixes && mode == CompatibilityMode::Backward {
                    CompatibilityChecker::apply_backward_compatibility(old_proto, new);
                }
                CompatibilityChecker::check(old_proto, new, mode)
            }
            None => CompatibilityResult::compatible(mode),
        };

        // Save the new schema if compatible (or in breaking mode)
        if result.is_compatible || mode == CompatibilityMode::Breaking {
            self.save(new)?;
        }

        Ok(result)
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_to_pascal_case() {
        assert_eq!(to_pascal_case("hello_world"), "HelloWorld");
        assert_eq!(to_pascal_case("my-entity"), "MyEntity");
        assert_eq!(to_pascal_case("already PascalCase"), "AlreadyPascalCase");
        assert_eq!(to_pascal_case("UPPERCASE"), "UPPERCASE");
        assert_eq!(to_pascal_case("PaymentProcessor"), "PaymentProcessor");
    }

    #[test]
    fn test_sanitize_proto_ident() {
        assert_eq!(sanitize_proto_ident("PaymentProcessor"), "PaymentProcessor");
        assert_eq!(sanitize_proto_ident("123Invalid"), "Sea123Invalid");
        assert_eq!(sanitize_proto_ident("message"), "SeaMessage");
        assert_eq!(sanitize_proto_ident("String"), "SeaString");
        assert_eq!(sanitize_proto_ident("hello_world"), "HelloWorld");
        assert_eq!(sanitize_proto_ident(""), "SeaUnnamed");
        assert_eq!(sanitize_proto_ident("rpc"), "SeaRpc");
        assert_eq!(sanitize_proto_ident("service"), "SeaService");
    }

    #[cfg(unix)]
    #[test]
    fn test_validate_output_path_accepts_symlinked_output_root() {
        let tmp = tempfile::tempdir().unwrap();
        let real_output = tmp.path().join("real-output");
        let linked_output = tmp.path().join("linked-output");
        std::fs::create_dir_all(&real_output).unwrap();
        std::os::unix::fs::symlink(&real_output, &linked_output).unwrap();

        let target = linked_output.join("generated.proto");

        assert!(validate_output_path(&linked_output, &target).is_ok());
    }

    #[test]
    fn test_validate_output_path_rejects_traversal() {
        let tmp = tempfile::tempdir().unwrap();
        let output = tmp.path().join("out");
        std::fs::create_dir_all(&output).unwrap();
        let target = output.join("..").join("escape.proto");

        assert!(validate_output_path(&output, &target).is_err());
    }

    #[test]
    fn test_to_snake_case() {
        assert_eq!(to_snake_case("HelloWorld"), "hello_world");
        assert_eq!(to_snake_case("myEntity"), "my_entity");
        assert_eq!(to_snake_case("already_snake"), "already_snake");
        // Consecutive uppercase chars become lowercase without underscore separation
        assert_eq!(to_snake_case("XMLParser"), "xmlparser");
        assert_eq!(to_snake_case("PaymentID"), "payment_id");
    }

    #[test]
    fn test_to_screaming_snake_case() {
        assert_eq!(to_screaming_snake_case("MyEnum"), "MY_ENUM");
        assert_eq!(to_screaming_snake_case("StatusCode"), "STATUS_CODE");
    }

    #[test]
    fn test_map_sea_type_to_proto() {
        assert_eq!(
            map_sea_type_to_proto("string"),
            ProtoType::Scalar(ScalarType::String)
        );
        assert_eq!(
            map_sea_type_to_proto("int"),
            ProtoType::Scalar(ScalarType::Int64)
        );
        assert_eq!(
            map_sea_type_to_proto("boolean"),
            ProtoType::Scalar(ScalarType::Bool)
        );
        assert_eq!(
            map_sea_type_to_proto("timestamp"),
            ProtoType::Message("google.protobuf.Timestamp".to_string())
        );
        assert_eq!(
            map_sea_type_to_proto("CustomType"),
            ProtoType::Message("CustomType".to_string())
        );
    }

    #[test]
    fn test_proto_field_to_string() {
        let field = ProtoField {
            name: "my_field".to_string(),
            number: 1,
            proto_type: ProtoType::Scalar(ScalarType::String),
            repeated: false,
            optional: false,
            comments: vec![],
        };
        assert_eq!(field.to_proto_string(), "string my_field = 1;");

        let optional_field = ProtoField {
            name: "optional_field".to_string(),
            number: 2,
            proto_type: ProtoType::Scalar(ScalarType::Int64),
            repeated: false,
            optional: true,
            comments: vec![],
        };
        assert_eq!(
            optional_field.to_proto_string(),
            "optional int64 optional_field = 2;"
        );

        let repeated_field = ProtoField {
            name: "items".to_string(),
            number: 3,
            proto_type: ProtoType::Scalar(ScalarType::String),
            repeated: true,
            optional: false,
            comments: vec![],
        };
        assert_eq!(
            repeated_field.to_proto_string(),
            "repeated string items = 3;"
        );
    }

    #[test]
    fn test_proto_enum_to_string() {
        let mut e = ProtoEnum::new("Status");
        e.add_value("STATUS_ACTIVE");
        e.add_value("STATUS_INACTIVE");

        let output = e.to_proto_string();
        assert!(output.contains("enum Status {"));
        assert!(output.contains("STATUS_UNSPECIFIED = 0;"));
        assert!(output.contains("STATUS_ACTIVE = 1;"));
        assert!(output.contains("STATUS_INACTIVE = 2;"));
    }

    #[test]
    fn test_proto_message_to_string() {
        let mut msg = ProtoMessage::new("Person");
        msg.fields.push(ProtoField {
            name: "name".to_string(),
            number: 1,
            proto_type: ProtoType::Scalar(ScalarType::String),
            repeated: false,
            optional: false,
            comments: vec![],
        });
        msg.fields.push(ProtoField {
            name: "age".to_string(),
            number: 2,
            proto_type: ProtoType::Scalar(ScalarType::Int32),
            repeated: false,
            optional: true,
            comments: vec![],
        });

        let output = msg.to_proto_string();
        assert!(output.contains("message Person {"));
        assert!(output.contains("string name = 1;"));
        assert!(output.contains("optional int32 age = 2;"));
    }

    #[test]
    fn test_proto_file_to_string() {
        let mut proto = ProtoFile::new("test.package");
        proto.metadata.projection_name = "TestProjection".to_string();
        proto.metadata.source_namespace = "test".to_string();

        let mut msg = ProtoMessage::new("TestMessage");
        msg.fields.push(ProtoField {
            name: "id".to_string(),
            number: 1,
            proto_type: ProtoType::Scalar(ScalarType::String),
            repeated: false,
            optional: false,
            comments: vec![],
        });
        proto.messages.push(msg);

        let output = proto.to_proto_string();
        assert!(output.contains("syntax = \"proto3\";"));
        assert!(output.contains("package test.package;"));
        assert!(output.contains("message TestMessage {"));
        assert!(output.contains("string id = 1;"));
    }

    #[test]
    fn test_entity_to_message() {
        use serde_json::json;

        let mut entity = Entity::new_with_namespace("Warehouse", "logistics");
        entity.set_attribute("capacity", json!(5000));
        entity.set_attribute("location", json!("Building A"));

        let msg = ProtobufEngine::entity_to_message(&entity);

        assert_eq!(msg.name, "Warehouse");
        assert!(msg.fields.iter().any(|f| f.name == "id"));
        assert!(msg.fields.iter().any(|f| f.name == "name"));
        assert!(msg.fields.iter().any(|f| f.name == "capacity"));
        assert!(msg.fields.iter().any(|f| f.name == "location"));

        // Check field numbers are sequential
        let numbers: Vec<u32> = msg.fields.iter().map(|f| f.number).collect();
        assert_eq!(numbers, vec![1, 2, 3, 4]); // id, name, capacity, location (sorted)
    }

    #[test]
    fn test_governance_messages() {
        let messages = ProtobufEngine::generate_governance_messages();
        assert_eq!(messages.len(), 2);

        let violation = messages.iter().find(|m| m.name == "PolicyViolation");
        assert!(violation.is_some());

        let metric = messages.iter().find(|m| m.name == "MetricEvent");
        assert!(metric.is_some());
    }

    // ========================================================================
    // Well-Known Type Tests
    // ========================================================================

    #[test]
    fn test_wkt_type_name() {
        assert_eq!(
            WellKnownType::Timestamp.type_name(),
            "google.protobuf.Timestamp"
        );
        assert_eq!(
            WellKnownType::Duration.type_name(),
            "google.protobuf.Duration"
        );
        assert_eq!(WellKnownType::Any.type_name(), "google.protobuf.Any");
        assert_eq!(WellKnownType::Struct.type_name(), "google.protobuf.Struct");
        assert_eq!(WellKnownType::Empty.type_name(), "google.protobuf.Empty");
        assert_eq!(
            WellKnownType::Int64Value.type_name(),
            "google.protobuf.Int64Value"
        );
    }

    #[test]
    fn test_wkt_import_path() {
        assert_eq!(
            WellKnownType::Timestamp.import_path(),
            "google/protobuf/timestamp.proto"
        );
        assert_eq!(
            WellKnownType::Duration.import_path(),
            "google/protobuf/duration.proto"
        );
        assert_eq!(
            WellKnownType::Any.import_path(),
            "google/protobuf/any.proto"
        );
        assert_eq!(
            WellKnownType::Struct.import_path(),
            "google/protobuf/struct.proto"
        );
        assert_eq!(
            WellKnownType::Value.import_path(),
            "google/protobuf/struct.proto"
        );
        assert_eq!(
            WellKnownType::Empty.import_path(),
            "google/protobuf/empty.proto"
        );
        assert_eq!(
            WellKnownType::Int64Value.import_path(),
            "google/protobuf/wrappers.proto"
        );
        assert_eq!(
            WellKnownType::StringValue.import_path(),
            "google/protobuf/wrappers.proto"
        );
    }

    #[test]
    fn test_wkt_from_type_name() {
        assert_eq!(
            WellKnownType::from_type_name("google.protobuf.Timestamp"),
            Some(WellKnownType::Timestamp)
        );
        assert_eq!(
            WellKnownType::from_type_name("google.protobuf.Duration"),
            Some(WellKnownType::Duration)
        );
        assert_eq!(
            WellKnownType::from_type_name("google.protobuf.Any"),
            Some(WellKnownType::Any)
        );
        assert_eq!(
            WellKnownType::from_type_name("google.protobuf.StringValue"),
            Some(WellKnownType::StringValue)
        );
        assert_eq!(WellKnownType::from_type_name("SomeOtherType"), None);
    }

    #[test]
    fn test_map_sea_type_to_wkt() {
        // Timestamp types
        assert_eq!(
            map_sea_type_to_proto("timestamp"),
            ProtoType::Message("google.protobuf.Timestamp".to_string())
        );
        assert_eq!(
            map_sea_type_to_proto("datetime"),
            ProtoType::Message("google.protobuf.Timestamp".to_string())
        );

        // Duration types
        assert_eq!(
            map_sea_type_to_proto("duration"),
            ProtoType::Message("google.protobuf.Duration".to_string())
        );
        assert_eq!(
            map_sea_type_to_proto("timespan"),
            ProtoType::Message("google.protobuf.Duration".to_string())
        );

        // Dynamic types
        assert_eq!(
            map_sea_type_to_proto("any"),
            ProtoType::Message("google.protobuf.Any".to_string())
        );
        assert_eq!(
            map_sea_type_to_proto("json"),
            ProtoType::Message("google.protobuf.Struct".to_string())
        );

        // Empty type
        assert_eq!(
            map_sea_type_to_proto("void"),
            ProtoType::Message("google.protobuf.Empty".to_string())
        );

        // Nullable/optional types
        assert_eq!(
            map_sea_type_to_proto("optional_string"),
            ProtoType::Message("google.protobuf.StringValue".to_string())
        );
        assert_eq!(
            map_sea_type_to_proto("nullable_int"),
            ProtoType::Message("google.protobuf.Int64Value".to_string())
        );
    }

    #[test]
    fn test_add_wkt_imports() {
        let mut proto = ProtoFile::new("test");

        // Add a message with a Timestamp field
        let mut msg = ProtoMessage::new("Event");
        msg.fields.push(ProtoField {
            name: "created_at".to_string(),
            number: 1,
            proto_type: ProtoType::Message("google.protobuf.Timestamp".to_string()),
            repeated: false,
            optional: false,
            comments: vec![],
        });
        msg.fields.push(ProtoField {
            name: "duration".to_string(),
            number: 2,
            proto_type: ProtoType::Message("google.protobuf.Duration".to_string()),
            repeated: false,
            optional: false,
            comments: vec![],
        });
        proto.messages.push(msg);

        proto.add_wkt_imports();

        assert!(proto
            .imports
            .contains(&"google/protobuf/timestamp.proto".to_string()));
        assert!(proto
            .imports
            .contains(&"google/protobuf/duration.proto".to_string()));
    }

    #[test]
    fn test_resolve_imports_in_message() {
        // Setup index with a target message
        let mut index = HashMap::new();
        index.insert(
            "TargetType".to_string(),
            ("other.ns".to_string(), "base.other.ns".to_string()),
        );

        let mut msg = ProtoMessage::new("SourceMessage");
        msg.fields.push(ProtoField {
            name: "field1".to_string(),
            number: 1,
            proto_type: ProtoType::Message("TargetType".to_string()),
            repeated: false,
            optional: false,
            comments: vec![],
        });

        let mut imports = HashSet::new();

        // Resolve imports
        ProtobufEngine::resolve_imports_in_message(&mut msg, "current.ns", &index, &mut imports);

        // Should find import for other.ns
        assert!(imports.contains("other.ns"));

        // Should update type name to fully qualified
        let field_type = if let ProtoType::Message(ref name) = msg.fields[0].proto_type {
            name.clone()
        } else {
            panic!("Wrong type");
        };
        assert_eq!(field_type, "base.other.ns.TargetType");
    }

    #[test]
    fn test_resolve_imports_in_nested_message() {
        let mut index = HashMap::new();
        index.insert(
            "NestedTarget".to_string(),
            ("other.ns".to_string(), "base.other.ns".to_string()),
        );

        let mut msg = ProtoMessage::new("Outer");
        let mut inner = ProtoMessage::new("Inner");
        inner.fields.push(ProtoField {
            name: "field".to_string(),
            number: 1,
            proto_type: ProtoType::Message("NestedTarget".to_string()),
            repeated: false,
            optional: false,
            comments: vec![],
        });
        msg.nested_messages.push(inner);

        let mut imports = HashSet::new();
        ProtobufEngine::resolve_imports_in_message(&mut msg, "current.ns", &index, &mut imports);

        assert!(imports.contains("other.ns"));
    }

    #[test]
    fn test_wkt_imports_in_proto_string() {
        let mut proto = ProtoFile::new("test.wkt");

        let mut msg = ProtoMessage::new("AuditLog");
        msg.fields.push(ProtoField {
            name: "timestamp".to_string(),
            number: 1,
            proto_type: ProtoType::Message("google.protobuf.Timestamp".to_string()),
            repeated: false,
            optional: false,
            comments: vec![],
        });
        proto.messages.push(msg);
        proto.add_wkt_imports();

        let output = proto.to_proto_string();
        assert!(output.contains("import \"google/protobuf/timestamp.proto\";"));
    }

    #[test]
    fn test_wkt_no_duplicate_imports() {
        let mut proto = ProtoFile::new("test");

        // Add multiple messages using the same WKT
        let mut msg1 = ProtoMessage::new("Event1");
        msg1.fields.push(ProtoField {
            name: "time1".to_string(),
            number: 1,
            proto_type: ProtoType::Message("google.protobuf.Timestamp".to_string()),
            repeated: false,
            optional: false,
            comments: vec![],
        });

        let mut msg2 = ProtoMessage::new("Event2");
        msg2.fields.push(ProtoField {
            name: "time2".to_string(),
            number: 1,
            proto_type: ProtoType::Message("google.protobuf.Timestamp".to_string()),
            repeated: false,
            optional: false,
            comments: vec![],
        });

        proto.messages.push(msg1);
        proto.messages.push(msg2);
        proto.add_wkt_imports();

        // Should only have one timestamp import
        let timestamp_count = proto
            .imports
            .iter()
            .filter(|i| i.contains("timestamp"))
            .count();
        assert_eq!(timestamp_count, 1);
    }

    // ========================================================================
    // Custom Options Tests
    // ========================================================================

    #[test]
    fn test_proto_option_value_string() {
        let val = ProtoOptionValue::String("com.example.api".to_string());
        assert_eq!(val.to_proto_string(), "\"com.example.api\"");
    }

    #[test]
    fn test_proto_option_value_string_escaping() {
        let val = ProtoOptionValue::String("path\\to\\file".to_string());
        assert_eq!(val.to_proto_string(), "\"path\\\\to\\\\file\"");

        let val2 = ProtoOptionValue::String("say \"hello\"".to_string());
        assert_eq!(val2.to_proto_string(), "\"say \\\"hello\\\"\"");
    }

    #[test]
    fn test_proto_option_value_int() {
        let val = ProtoOptionValue::Int(42);
        assert_eq!(val.to_proto_string(), "42");

        let neg = ProtoOptionValue::Int(-100);
        assert_eq!(neg.to_proto_string(), "-100");
    }

    #[test]
    fn test_proto_option_value_float() {
        let val = ProtoOptionValue::Float(3.15);
        assert_eq!(val.to_proto_string(), "3.15");
    }

    #[test]
    fn test_proto_option_value_bool() {
        assert_eq!(ProtoOptionValue::Bool(true).to_proto_string(), "true");
        assert_eq!(ProtoOptionValue::Bool(false).to_proto_string(), "false");
    }

    #[test]
    fn test_proto_option_value_identifier() {
        let val = ProtoOptionValue::Identifier("SPEED".to_string());
        assert_eq!(val.to_proto_string(), "SPEED");
    }

    #[test]
    fn test_proto_custom_option_to_string() {
        let opt = ProtoCustomOption::new(
            "java_package",
            ProtoOptionValue::String("com.example".to_string()),
        );
        assert_eq!(
            opt.to_proto_string(),
            "option java_package = \"com.example\";"
        );
    }

    #[test]
    fn test_proto_custom_option_extension() {
        // Extension option with parentheses
        let opt = ProtoCustomOption::new("(mycompany.api_version)", ProtoOptionValue::Int(2));
        assert_eq!(opt.to_proto_string(), "option (mycompany.api_version) = 2;");
    }

    #[test]
    fn test_proto_options_set_standard_options() {
        let mut opts = ProtoOptions::default();

        opts.set_option(
            "java_package",
            ProtoOptionValue::String("com.example".to_string()),
        );
        opts.set_option("java_multiple_files", ProtoOptionValue::Bool(true));
        opts.set_option(
            "go_package",
            ProtoOptionValue::String("github.com/example".to_string()),
        );
        opts.set_option(
            "csharp_namespace",
            ProtoOptionValue::String("Example.Api".to_string()),
        );
        opts.set_option("deprecated", ProtoOptionValue::Bool(true));

        assert_eq!(opts.java_package, Some("com.example".to_string()));
        assert!(opts.java_multiple_files);
        assert_eq!(opts.go_package, Some("github.com/example".to_string()));
        assert_eq!(opts.csharp_namespace, Some("Example.Api".to_string()));
        assert!(opts.deprecated);
    }

    #[test]
    fn test_proto_options_set_custom_option() {
        let mut opts = ProtoOptions::default();

        opts.set_option(
            "my_custom_option",
            ProtoOptionValue::String("custom_value".to_string()),
        );

        assert_eq!(opts.custom_options.len(), 1);
        assert_eq!(opts.custom_options[0].name, "my_custom_option");
    }

    #[test]
    fn test_proto_file_with_all_options() {
        let mut proto = ProtoFile::new("test.api");
        proto.options.java_package = Some("com.example.api".to_string());
        proto.options.java_multiple_files = true;
        proto.options.go_package = Some("github.com/example/api".to_string());
        proto.options.csharp_namespace = Some("Example.Api".to_string());
        proto.options.optimize_for = Some("SPEED".to_string());
        proto.options.custom_options.push(ProtoCustomOption::new(
            "(api.version)",
            ProtoOptionValue::Int(1),
        ));

        let output = proto.to_proto_string();
        assert!(output.contains("option java_package = \"com.example.api\";"));
        assert!(output.contains("option java_multiple_files = true;"));
        assert!(output.contains("option go_package = \"github.com/example/api\";"));
        assert!(output.contains("option csharp_namespace = \"Example.Api\";"));
        assert!(output.contains("option optimize_for = SPEED;"));
        assert!(output.contains("option (api.version) = 1;"));
    }

    #[test]
    fn test_proto_option_value_from_json() {
        use serde_json::json;

        assert_eq!(
            ProtoOptionValue::from_json(&json!("hello")),
            ProtoOptionValue::String("hello".to_string())
        );
        assert_eq!(
            ProtoOptionValue::from_json(&json!(true)),
            ProtoOptionValue::Bool(true)
        );
        assert_eq!(
            ProtoOptionValue::from_json(&json!(42)),
            ProtoOptionValue::Int(42)
        );
        assert_eq!(
            ProtoOptionValue::from_json(&json!(3.15)),
            ProtoOptionValue::Float(3.15)
        );
    }

    // ========================================================================
    // gRPC Service Tests
    // ========================================================================

    #[test]
    fn test_proto_service_to_string() {
        let mut service = ProtoService::new("PaymentService");
        service
            .comments
            .push("Payment processing service".to_string());

        service.methods.push(ProtoRpcMethod::new(
            "ProcessPayment",
            "PaymentRequest",
            "PaymentResponse",
        ));

        let output = service.to_proto_string();
        assert!(output.contains("service PaymentService {"));
        assert!(output.contains("rpc ProcessPayment(PaymentRequest) returns (PaymentResponse);"));
        assert!(output.contains("// Payment processing service"));
    }

    #[test]
    fn test_proto_rpc_method_unary() {
        let method = ProtoRpcMethod::new("GetUser", "GetUserRequest", "User");
        let output = method.to_proto_string();
        assert_eq!(output, "rpc GetUser(GetUserRequest) returns (User);");
    }

    #[test]
    fn test_proto_rpc_method_server_streaming() {
        let mut method = ProtoRpcMethod::new("ListEvents", "ListEventsRequest", "Event");
        method.streaming = StreamingMode::ServerStreaming;
        let output = method.to_proto_string();
        assert_eq!(
            output,
            "rpc ListEvents(ListEventsRequest) returns (stream Event);"
        );
    }

    #[test]
    fn test_proto_rpc_method_client_streaming() {
        let mut method = ProtoRpcMethod::new("UploadChunks", "DataChunk", "UploadResult");
        method.streaming = StreamingMode::ClientStreaming;
        let output = method.to_proto_string();
        assert_eq!(
            output,
            "rpc UploadChunks(stream DataChunk) returns (UploadResult);"
        );
    }

    #[test]
    fn test_proto_rpc_method_bidirectional() {
        let mut method = ProtoRpcMethod::new("Chat", "ChatMessage", "ChatMessage");
        method.streaming = StreamingMode::Bidirectional;
        let output = method.to_proto_string();
        assert_eq!(
            output,
            "rpc Chat(stream ChatMessage) returns (stream ChatMessage);"
        );
    }

    #[test]
    fn test_streaming_mode_from_str() {
        assert_eq!(
            StreamingMode::parse("streaming"),
            StreamingMode::ServerStreaming
        );
        assert_eq!(
            StreamingMode::parse("server_streaming"),
            StreamingMode::ServerStreaming
        );
        assert_eq!(
            StreamingMode::parse("client_streaming"),
            StreamingMode::ClientStreaming
        );
        assert_eq!(
            StreamingMode::parse("bidirectional"),
            StreamingMode::Bidirectional
        );
        assert_eq!(StreamingMode::parse("bidi"), StreamingMode::Bidirectional);
        assert_eq!(StreamingMode::parse("duplex"), StreamingMode::Bidirectional);
        assert_eq!(StreamingMode::parse("unary"), StreamingMode::Unary);
        assert_eq!(StreamingMode::parse(""), StreamingMode::Unary);
    }

    #[test]
    fn test_streaming_mode_display() {
        assert_eq!(format!("{}", StreamingMode::Unary), "unary");
        assert_eq!(
            format!("{}", StreamingMode::ServerStreaming),
            "server_streaming"
        );
        assert_eq!(
            format!("{}", StreamingMode::ClientStreaming),
            "client_streaming"
        );
        assert_eq!(format!("{}", StreamingMode::Bidirectional), "bidirectional");
    }

    #[test]
    fn test_proto_file_with_services() {
        let mut proto = ProtoFile::new("test.api");

        let mut service = ProtoService::new("GreeterService");
        service.methods.push(ProtoRpcMethod::new(
            "SayHello",
            "HelloRequest",
            "HelloResponse",
        ));
        proto.services.push(service);

        let output = proto.to_proto_string();
        assert!(output.contains("service GreeterService {"));
        assert!(output.contains("rpc SayHello(HelloRequest) returns (HelloResponse);"));
    }

    // ========================================================================
    // Compatibility Tests
    // ========================================================================

    fn make_test_proto(messages: Vec<ProtoMessage>) -> ProtoFile {
        ProtoFile {
            package: "test.package".to_string(),
            syntax: "proto3".to_string(),
            imports: vec![],
            options: ProtoOptions::default(),
            enums: vec![],
            messages,
            services: vec![],
            metadata: ProtoMetadata::default(),
        }
    }

    fn make_test_message(name: &str, fields: Vec<ProtoField>) -> ProtoMessage {
        ProtoMessage {
            name: name.to_string(),
            fields,
            nested_messages: vec![],
            nested_enums: vec![],
            reserved_numbers: vec![],
            reserved_names: vec![],
            comments: vec![],
        }
    }

    fn make_test_field(name: &str, number: u32, proto_type: ProtoType) -> ProtoField {
        ProtoField {
            name: name.to_string(),
            number,
            proto_type,
            repeated: false,
            optional: false,
            comments: vec![],
        }
    }

    #[test]
    fn test_compatibility_no_changes() {
        let old = make_test_proto(vec![make_test_message(
            "Person",
            vec![
                make_test_field("id", 1, ProtoType::Scalar(ScalarType::String)),
                make_test_field("name", 2, ProtoType::Scalar(ScalarType::String)),
            ],
        )]);

        let new = old.clone();

        let result = CompatibilityChecker::check(&old, &new, CompatibilityMode::Additive);
        assert!(result.is_compatible);
        assert!(result.violations.is_empty());
    }

    #[test]
    fn test_compatibility_field_added() {
        let old = make_test_proto(vec![make_test_message(
            "Person",
            vec![make_test_field(
                "id",
                1,
                ProtoType::Scalar(ScalarType::String),
            )],
        )]);

        let new = make_test_proto(vec![make_test_message(
            "Person",
            vec![
                make_test_field("id", 1, ProtoType::Scalar(ScalarType::String)),
                make_test_field("name", 2, ProtoType::Scalar(ScalarType::String)),
            ],
        )]);

        // Adding fields is compatible in all modes
        let result = CompatibilityChecker::check(&old, &new, CompatibilityMode::Additive);
        assert!(result.is_compatible);
        assert!(result.violations.is_empty());
    }

    #[test]
    fn test_compatibility_field_removed_additive() {
        let old = make_test_proto(vec![make_test_message(
            "Person",
            vec![
                make_test_field("id", 1, ProtoType::Scalar(ScalarType::String)),
                make_test_field("name", 2, ProtoType::Scalar(ScalarType::String)),
            ],
        )]);

        let new = make_test_proto(vec![make_test_message(
            "Person",
            vec![make_test_field(
                "id",
                1,
                ProtoType::Scalar(ScalarType::String),
            )],
        )]);

        // Removing fields is NOT compatible in additive mode
        let result = CompatibilityChecker::check(&old, &new, CompatibilityMode::Additive);
        assert!(!result.is_compatible);
        assert_eq!(result.violations.len(), 1);
        assert_eq!(
            result.violations[0].violation_type,
            ViolationType::FieldRemoved
        );
    }

    #[test]
    fn test_compatibility_field_removed_backward() {
        let old = make_test_proto(vec![make_test_message(
            "Person",
            vec![
                make_test_field("id", 1, ProtoType::Scalar(ScalarType::String)),
                make_test_field("name", 2, ProtoType::Scalar(ScalarType::String)),
            ],
        )]);

        let new = make_test_proto(vec![make_test_message(
            "Person",
            vec![make_test_field(
                "id",
                1,
                ProtoType::Scalar(ScalarType::String),
            )],
        )]);

        // Removing fields IS compatible in backward mode (with warnings)
        let result = CompatibilityChecker::check(&old, &new, CompatibilityMode::Backward);
        assert!(result.is_compatible); // Still compatible, just has violations
        assert!(!result.violations.is_empty());

        // Should suggest reserving the field
        assert!(result.suggested_reserved_numbers.contains_key("Person"));
        assert!(result.suggested_reserved_numbers["Person"].contains(&2));
    }

    #[test]
    fn test_compatibility_type_change() {
        let old = make_test_proto(vec![make_test_message(
            "Person",
            vec![make_test_field(
                "age",
                1,
                ProtoType::Scalar(ScalarType::Int32),
            )],
        )]);

        let new = make_test_proto(vec![make_test_message(
            "Person",
            vec![make_test_field(
                "age",
                1,
                ProtoType::Scalar(ScalarType::String),
            )],
        )]);

        // Type changes are NOT compatible in backward mode
        let result = CompatibilityChecker::check(&old, &new, CompatibilityMode::Backward);
        assert!(!result.is_compatible);
        assert!(result
            .violations
            .iter()
            .any(|v| v.violation_type == ViolationType::FieldTypeChanged));
    }

    #[test]
    fn test_compatibility_breaking_mode() {
        let old = make_test_proto(vec![make_test_message(
            "Person",
            vec![make_test_field(
                "id",
                1,
                ProtoType::Scalar(ScalarType::String),
            )],
        )]);

        let new = make_test_proto(vec![make_test_message(
            "Person",
            vec![make_test_field(
                "uuid",
                1,
                ProtoType::Scalar(ScalarType::Int64),
            )],
        )]);

        // Breaking mode allows everything
        let result = CompatibilityChecker::check(&old, &new, CompatibilityMode::Breaking);
        assert!(result.is_compatible);
        // Violations are still reported for informational purposes
        assert!(!result.violations.is_empty());
    }

    #[test]
    fn test_apply_backward_compatibility() {
        let old = make_test_proto(vec![make_test_message(
            "Person",
            vec![
                make_test_field("id", 1, ProtoType::Scalar(ScalarType::String)),
                make_test_field("name", 2, ProtoType::Scalar(ScalarType::String)),
                make_test_field("email", 3, ProtoType::Scalar(ScalarType::String)),
            ],
        )]);

        let mut new = make_test_proto(vec![make_test_message(
            "Person",
            vec![
                make_test_field("id", 1, ProtoType::Scalar(ScalarType::String)),
                // name (2) removed
                // email (3) removed
                make_test_field("phone", 4, ProtoType::Scalar(ScalarType::String)),
            ],
        )]);

        CompatibilityChecker::apply_backward_compatibility(&old, &mut new);

        // Should have added reserved numbers
        let person = &new.messages[0];
        assert!(person.reserved_numbers.contains(&2));
        assert!(person.reserved_numbers.contains(&3));
        assert!(person.reserved_names.contains(&"name".to_string()));
        assert!(person.reserved_names.contains(&"email".to_string()));
    }

    #[test]
    fn test_compatibility_message_removed() {
        let old = make_test_proto(vec![
            make_test_message("Person", vec![]),
            make_test_message("Address", vec![]),
        ]);

        let new = make_test_proto(vec![make_test_message("Person", vec![])]);

        let result = CompatibilityChecker::check(&old, &new, CompatibilityMode::Additive);
        assert!(!result.is_compatible);
        assert!(result.violations.iter().any(|v| {
            v.violation_type == ViolationType::MessageRemoved && v.message_name == "Address"
        }));
    }

    #[test]
    fn test_compatibility_result_report() {
        let old = make_test_proto(vec![make_test_message(
            "Person",
            vec![make_test_field(
                "name",
                1,
                ProtoType::Scalar(ScalarType::String),
            )],
        )]);

        let new = make_test_proto(vec![make_test_message("Person", vec![])]);

        let result = CompatibilityChecker::check(&old, &new, CompatibilityMode::Backward);
        let report = result.to_report();

        assert!(report.contains("Compatibility Check"));
        assert!(report.contains("field_removed"));
        assert!(report.contains("Person"));
    }

    #[test]
    fn test_compatibility_mode_parsing() {
        assert_eq!(
            "additive".parse::<CompatibilityMode>().unwrap(),
            CompatibilityMode::Additive
        );
        assert_eq!(
            "backward".parse::<CompatibilityMode>().unwrap(),
            CompatibilityMode::Backward
        );
        assert_eq!(
            "breaking".parse::<CompatibilityMode>().unwrap(),
            CompatibilityMode::Breaking
        );
        assert!("invalid".parse::<CompatibilityMode>().is_err());
    }
}