mockforge-plugin-core 0.3.124

Core plugin interfaces and types for MockForge extensible architecture
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
//! Angular Client Generator Plugin
//!
//! Generates Angular services, HTTP interceptors, and TypeScript types from OpenAPI specifications
//! for easy integration with Angular applications using RxJS Observables and dependency injection.

use crate::client_generator::{
    ClientGenerationResult, ClientGeneratorConfig, ClientGeneratorPlugin, GeneratedFile,
    GenerationMetadata, OpenApiSpec,
};
use crate::types::{PluginError, PluginMetadata, Result};
use handlebars::Handlebars;
use serde_json::{json, Value};
use std::collections::HashMap;

/// Angular client generator plugin
pub struct AngularClientGenerator {
    /// Template registry for code generation
    templates: Handlebars<'static>,
}

impl AngularClientGenerator {
    /// Create a new Angular client generator
    pub fn new() -> Result<Self> {
        let mut templates = Handlebars::new();

        // Register templates for Angular code generation
        Self::register_templates(&mut templates)?;

        Ok(Self { templates })
    }

    /// Process a schema JSON value to add required flags to properties
    /// This makes it easier for Handlebars templates to check required fields
    fn process_schema_with_required_flags(mut schema: Value) -> Value {
        // First, extract and clone required fields list (to avoid borrow conflicts)
        let required_fields: Vec<String> = schema
            .get("required")
            .and_then(|r| r.as_array())
            .map(|arr| arr.iter().filter_map(|v| v.as_str()).map(|s| s.to_string()).collect())
            .unwrap_or_default();

        // Then, modify properties (mutable borrow)
        if let Some(properties) = schema.get_mut("properties").and_then(|p| p.as_object_mut()) {
            for (prop_name, prop_value) in properties.iter_mut() {
                // Add required flag to each property
                if let Some(prop_obj) = prop_value.as_object_mut() {
                    prop_obj.insert(
                        "required".to_string(),
                        Value::Bool(required_fields.contains(prop_name)),
                    );
                }
            }
        }
        schema
    }

    /// Register Handlebars templates for Angular code generation
    fn register_templates(templates: &mut Handlebars<'static>) -> Result<()> {
        // Register JSON helper for serializing schemas
        templates.register_helper(
            "json",
            Box::new(
                |h: &handlebars::Helper,
                 _: &Handlebars,
                 _: &handlebars::Context,
                 _: &mut handlebars::RenderContext,
                 out: &mut dyn handlebars::Output|
                 -> handlebars::HelperResult {
                    let value = h.param(0).ok_or_else(|| {
                        handlebars::RenderError::new("json helper requires a parameter")
                    })?;
                    let json_str = serde_json::to_string(&value.value()).map_err(|e| {
                        handlebars::RenderError::new(format!("Failed to serialize to JSON: {}", e))
                    })?;
                    out.write(&json_str)?;
                    Ok(())
                },
            ),
        );

        // Register helper for eq comparison
        templates.register_helper(
            "eq",
            Box::new(
                |h: &handlebars::Helper,
                 _: &Handlebars,
                 _: &handlebars::Context,
                 _: &mut handlebars::RenderContext,
                 out: &mut dyn handlebars::Output|
                 -> handlebars::HelperResult {
                    let a = h.param(0).and_then(|v| v.value().as_str());
                    let b = h.param(1).and_then(|v| v.value().as_str());
                    if a == b {
                        out.write("true")?;
                    }
                    Ok(())
                },
            ),
        );

        // Register helper for toLowerCase
        templates.register_helper(
            "toLowerCase",
            Box::new(
                |h: &handlebars::Helper,
                 _: &Handlebars,
                 _: &handlebars::Context,
                 _: &mut handlebars::RenderContext,
                 out: &mut dyn handlebars::Output|
                 -> handlebars::HelperResult {
                    let param = h.param(0).and_then(|v| v.value().as_str());
                    if let Some(s) = param {
                        out.write(&s.to_lowercase())?;
                    }
                    Ok(())
                },
            ),
        );

        // TypeScript types template (same as React/Vue/Svelte)
        templates
            .register_template_string(
                "types",
                r#"// Generated TypeScript types for {{api_title}}
// API Version: {{api_version}}

{{#each schemas}}
export interface {{@key}} {
{{#each this.properties}}
  {{#if (lookup ../this.required @key)}}
  {{@key}}: {{> typescript_type this}};
  {{else}}
  {{@key}}?: {{> typescript_type this}};
  {{/if}}
{{/each}}
}

{{/each}}

// API Response types
{{#each operations}}
export interface {{response_type_name}} {
{{#each responses}}
{{#if (eq @key "200")}}
{{#if this.content}}
{{#each this.content}}
{{#if (eq @key "application/json")}}
{{#if this.schema}}
{{#if this.schema.properties}}
{{#each this.schema.properties}}
  {{@key}}{{#unless this.required}}?{{/unless}}: {{> typescript_type this}};
{{/each}}
{{else}}
{{#if (eq this.schema.type "object")}}
  [key: string]: any;
{{else}}
{{#if this.schema.type}}
  value: {{> typescript_type this.schema}};
{{/if}}
{{/if}}
{{/if}}
{{/if}}
{{/if}}
{{/each}}
{{/if}}
{{/if}}
{{/each}}
}

{{/each}}

// API Request types
{{#each operations}}
{{#if request_body}}
export interface {{request_type_name}} {
{{#each request_body.content}}
{{#if (eq @key "application/json")}}
{{#if this.schema}}
{{#if this.schema.properties}}
{{#each this.schema.properties}}
  {{@key}}{{#unless this.required}}?{{/unless}}: {{> typescript_type this}};
{{/each}}
{{else}}
{{> typescript_type this.schema}}
{{/if}}
{{/if}}
{{/if}}
{{/each}}
}

{{/if}}
{{/each}}"#,
            )
            .map_err(|e| {
                PluginError::execution(format!("Failed to register types template: {}", e))
            })?;

        // Core infrastructure template (ApiError, TokenStorage, Config interfaces)
        // This will be a very large template - I'll create it in parts
        // For now, let me create a comprehensive single template file
        // Due to size constraints, I'll need to build this incrementally

        // TypeScript type helper template (partial)
        templates
            .register_template_string(
                "typescript_type",
                r#"{{#if (eq type "string")}}string{{/if}}{{#if (eq type "integer")}}number{{/if}}{{#if (eq type "number")}}number{{/if}}{{#if (eq type "boolean")}}boolean{{/if}}{{#if (eq type "array")}}{{#if items}}Array<{{> typescript_type items}}>{{else}}any[]{{/if}}{{/if}}{{#if (eq type "object")}}{{#if properties}}{
  {{#each properties}}
  {{@key}}{{#unless (lookup ../required @key)}}?{{/unless}}: {{> typescript_type this}};{{#unless @last}}

{{/unless}}{{/each}}
}{{else}}Record<string, any>{{/if}}{{/if}}{{#unless type}}any{{/unless}}"#,
            )
            .map_err(|e| {
                PluginError::execution(format!("Failed to register typescript_type template: {}", e))
            })?;

        // Core infrastructure template (ApiError, Config interfaces)
        // This will be adapted for Angular/RxJS patterns
        templates
            .register_template_string(
                "core",
                r#"// Generated Angular core infrastructure for {{api_title}}
// API Version: {{api_version}}

import { Injectable } from '@angular/core';
import { HttpErrorResponse } from '@angular/common/http';

// ============================================================================
// Error Handling
// ============================================================================

/**
 * Base API Error class with structured error information
 * Compatible with Angular HttpErrorResponse
 */
export class ApiError extends Error {
  constructor(
    public status: number,
    public statusText: string,
    public body?: any,
    message?: string
  ) {
    super(message || `API Error: ${status} ${statusText}`);
    this.name = 'ApiError';
    Object.setPrototypeOf(this, ApiError.prototype);
  }

  /**
   * Create ApiError from Angular HttpErrorResponse
   */
  static fromHttpErrorResponse(error: HttpErrorResponse): ApiError {
    return new ApiError(
      error.status,
      error.statusText,
      error.error,
      error.message
    );
  }

  /**
   * Check if error is a client error (4xx)
   */
  isClientError(): boolean {
    return this.status >= 400 && this.status < 500;
  }

  /**
   * Check if error is a server error (5xx)
   */
  isServerError(): boolean {
    return this.status >= 500;
  }

  /**
   * Get error details from response body if available
   */
  getErrorDetails(): any {
    return this.body;
  }

  /**
   * Get verbose error message with validation details
   */
  getVerboseMessage(): string {
    let message = `${this.status} ${this.statusText}`;

    if (this.body) {
      if (typeof this.body === 'object') {
        // Handle validation errors
        if (this.body.errors && Array.isArray(this.body.errors)) {
          const validationErrors = this.body.errors
            .map((err: any) => {
              if (typeof err === 'string') return err;
              if (err.field && err.message) return `${err.field}: ${err.message}`;
              if (err.message) return err.message;
              return JSON.stringify(err);
            })
            .join('; ');
          message += ` - Validation errors: ${validationErrors}`;
        } else if (this.body.message) {
          message += ` - ${this.body.message}`;
        } else if (this.body.error) {
          message += ` - ${this.body.error}`;
          if (this.body.error_description) {
            message += ` (${this.body.error_description})`;
          }
        } else {
          message += ` - ${JSON.stringify(this.body)}`;
        }
      } else if (typeof this.body === 'string') {
        message += ` - ${this.body}`;
      }
    }

    return message;
  }
}

/**
 * Error thrown when a required parameter is missing
 */
export class RequiredError extends Error {
  constructor(public field: string, message?: string) {
    super(message || `Required parameter '${field}' was null or undefined`);
    this.name = 'RequiredError';
    Object.setPrototypeOf(this, RequiredError.prototype);
  }
}

/**
 * Contract validation error with schema path and contract diff reference
 *
 * This error type provides detailed information about validation failures
 * and can link back to contract diff entries for tracking breaking changes.
 */
export class ContractValidationError extends ApiError {
  constructor(
    status: number,
    statusText: string,
    public schemaPath: string,
    public expectedType: string,
    public actualValue?: any,
    public contractDiffId?: string,
    public isBreakingChange: boolean = false,
    body?: any,
    message?: string
  ) {
    super(
      status,
      statusText,
      body,
      message || `Contract validation failed at '${schemaPath}': expected ${expectedType}${actualValue !== undefined ? `, got ${JSON.stringify(actualValue)}` : ''}`
    );
    this.name = 'ContractValidationError';
    Object.setPrototypeOf(this, ContractValidationError.prototype);
  }

  /**
   * Get a detailed error message with contract diff information
   */
  getDetailedMessage(): string {
    let msg = `Validation failed at '${this.schemaPath}': expected ${this.expectedType}`;
    if (this.actualValue !== undefined) {
      msg += `, got ${typeof this.actualValue === 'object' ? JSON.stringify(this.actualValue) : String(this.actualValue)}`;
    }
    if (this.contractDiffId) {
      msg += ` (Contract Diff ID: ${this.contractDiffId})`;
    }
    if (this.isBreakingChange) {
      msg += ' [BREAKING CHANGE]';
    }
    return msg;
  }
}

// ============================================================================
// Configuration Interfaces
// ============================================================================

/**
 * OAuth2 Flow Configuration
 *
 * ⚠️ SECURITY WARNING:
 * - NEVER include clientSecret in browser/client-side code
 * - Client secrets should only be used in server-side applications
 * - For browser apps, use authorization_code flow with PKCE (recommended)
 */
export interface OAuth2Config {
  /** OAuth2 client ID */
  clientId: string;
  /**
   * OAuth2 client secret (for client_credentials flow)
   * ⚠️ SECURITY: Only use in server-side apps. NEVER expose in browser code!
   */
  clientSecret?: string;
  /** Authorization URL (for authorization_code flow) */
  authorizationUrl?: string;
  /** Token URL for obtaining access tokens */
  tokenUrl: string;
  /** Redirect URI (for authorization_code flow) */
  redirectUri?: string;
  /** Scopes to request */
  scopes?: string[];
  /** OAuth2 flow type */
  flow?: 'authorization_code' | 'client_credentials' | 'implicit' | 'password';
  /** Token storage key (default: 'oauth2_token') */
  tokenStorageKey?: string;
  /** Callback for token refresh */
  onTokenRefresh?: (token: string) => void | Promise<void>;
  /** State parameter for CSRF protection (auto-generated if not provided) */
  state?: string;
  /** PKCE code verifier for authorization_code flow (recommended for browser apps) */
  codeVerifier?: string;
}

/**
 * JWT Token Configuration
 * Handles JWT token refresh on 401 errors
 */
export interface JwtConfig {
  /** Refresh endpoint URL (default: '/api/v1/auth/refresh') */
  refreshEndpoint?: string;
  /** Refresh token (static or dynamic function) */
  refreshToken?: string | (() => string | Promise<string>);
  /** Callback invoked when token is refreshed */
  onTokenRefresh?: (token: string) => void | Promise<void>;
  /** Callback invoked when authentication fails (refresh token invalid/expired) */
  onAuthError?: () => void | Promise<void>;
  /** Refresh token if it expires within this many seconds (default: 300) */
  refreshThreshold?: number;
  /** Check token expiration before making requests (default: true) */
  checkExpirationBeforeRequest?: boolean;
}

/**
 * Retry Configuration
 * Configures automatic retry behavior for failed requests
 */
export interface RetryConfig {
  /** Maximum number of retry attempts (default: 3) */
  maxRetries?: number;
  /** Base delay in milliseconds for exponential backoff (default: 1000) */
  baseDelay?: number;
  /** Maximum delay in milliseconds (default: 10000) */
  maxDelay?: number;
  /** HTTP status codes that should be retried (default: [408, 429, 500, 502, 503, 504]) */
  retryableStatusCodes?: number[];
  /** Whether to retry on network errors (default: true) */
  retryOnNetworkError?: boolean;
}

/**
 * API Configuration with support for authentication and interceptors
 */
export interface ApiConfig {
  /** Base URL for API requests */
  baseUrl: string;
  /** Default headers to include with every request */
  headers?: Record<string, string>;
  /** Bearer token for authentication */
  accessToken?: string | (() => string | Promise<string>);
  /** API key for authentication (supports function for dynamic keys) */
  apiKey?: string | ((name: string) => string | Promise<string>);
  /** Username for basic authentication */
  username?: string;
  /** Password for basic authentication */
  password?: string;
  /** OAuth2 configuration for OAuth flows */
  oauth2?: OAuth2Config;
  /** JWT token configuration for automatic refresh on 401 */
  jwt?: JwtConfig;
  /** Retry configuration for automatic retry on failures */
  retry?: RetryConfig;
  /** Timeout in milliseconds (default: 30000) */
  timeout?: number;
  /** Enable request/response validation (default: false) */
  validateRequests?: boolean;
  /** Enable response validation (default: false) */
  validateResponses?: boolean;
  /** Enable verbose error messages (default: false) */
  verboseErrors?: boolean;
  /** Automatically unwrap ApiResponse<T> format to return data directly (default: false) */
  unwrapResponse?: boolean;
  /** Schema registry for runtime validation (schema_id -> JSON Schema) */
  schemas?: Record<string, any>;
  /** Whether to include contract diff references in validation errors */
  includeContractDiffs?: boolean;
}

/**
 * Token Storage Interface
 * Allows different storage backends (localStorage, httpOnly cookies, secure storage)
 */
export interface TokenStorage {
  /** Get access token from storage */
  getAccessToken(): string | null | Promise<string | null>;
  /** Store access token with optional expiration (in seconds) */
  setAccessToken(token: string, expiresIn?: number): void | Promise<void>;
  /** Get refresh token from storage */
  getRefreshToken(): string | null | Promise<string | null>;
  /** Store refresh token */
  setRefreshToken(token: string): void | Promise<void>;
  /** Clear all tokens from storage */
  clearTokens(): void | Promise<void>;
}"#,
            )
            .map_err(|e| {
                PluginError::execution(format!("Failed to register core template: {}", e))
            })?;

        // Token Storage Service template
        templates
            .register_template_string(
                "token-storage",
                r#"// Generated Angular Token Storage Service for {{api_title}}
// API Version: {{api_version}}

import { Injectable } from '@angular/core';
import { TokenStorage } from './core';

/**
 * LocalStorage-based token storage service implementation
 * ⚠️ SECURITY: localStorage is vulnerable to XSS attacks
 * Consider using httpOnly cookies or secure storage for production apps
 */
@Injectable({
  providedIn: 'root'
})
export class LocalStorageTokenStorageService implements TokenStorage {
  private accessTokenKey: string;
  private refreshTokenKey: string;

  constructor(
    accessTokenKey: string = 'access_token',
    refreshTokenKey: string = 'refresh_token'
  ) {
    this.accessTokenKey = accessTokenKey;
    this.refreshTokenKey = refreshTokenKey;
  }

  getAccessToken(): string | null {
    if (typeof localStorage === 'undefined') {
      return null;
    }

    const stored = localStorage.getItem(this.accessTokenKey);
    if (!stored) return null;

    try {
      // Try to parse as JSON (with expiration) or use as plain string
      const parsed = JSON.parse(stored);
      if (parsed.token && parsed.expiresAt) {
        // Check if token is expired
        if (Date.now() >= parsed.expiresAt * 1000) {
          localStorage.removeItem(this.accessTokenKey);
          return null;
        }
        return parsed.token;
      }
      // Legacy format (plain string) - return as token
      return typeof parsed === 'string' ? parsed : parsed.token || parsed;
    } catch {
      // Plain string format
      return stored;
    }
  }

  setAccessToken(token: string, expiresIn?: number): void {
    if (typeof localStorage === 'undefined') {
      return;
    }

    // Store token with expiration if provided (expiresIn is in seconds)
    const tokenData = expiresIn
      ? JSON.stringify({
          token,
          expiresAt: Math.floor(Date.now() / 1000) + expiresIn,
        })
      : token;
    localStorage.setItem(this.accessTokenKey, tokenData);
  }

  getRefreshToken(): string | null {
    if (typeof localStorage === 'undefined') {
      return null;
    }
    return localStorage.getItem(this.refreshTokenKey);
  }

  setRefreshToken(token: string): void {
    if (typeof localStorage === 'undefined') {
      return;
    }
    localStorage.setItem(this.refreshTokenKey, token);
  }

  clearTokens(): void {
    if (typeof localStorage === 'undefined') {
      return;
    }
    localStorage.removeItem(this.accessTokenKey);
    localStorage.removeItem(this.refreshTokenKey);
  }
}"#,
            )
            .map_err(|e| {
                PluginError::execution(format!("Failed to register token-storage template: {}", e))
            })?;

        // Auth Interceptor template
        templates
            .register_template_string(
                "auth-interceptor",
                r#"// Generated Angular Auth Interceptor for {{api_title}}
// API Version: {{api_version}}

import { Injectable, Optional } from '@angular/core';
import {
  HttpInterceptor,
  HttpRequest,
  HttpHandler,
  HttpEvent,
  HttpErrorResponse,
} from '@angular/common/http';
import { Observable, throwError, from, firstValueFrom } from 'rxjs';
import { switchMap, catchError, tap } from 'rxjs/operators';
import { ApiError } from './core';
import { LocalStorageTokenStorageService } from './token-storage.service';
import { OAuth2TokenManagerService } from './oauth2-token-manager.service';
import { ApiConfigService } from './api-config.service';

/**
 * HTTP Interceptor for authentication (Bearer tokens, OAuth2, JWT refresh)
 * Automatically adds authentication headers and handles token refresh on 401
 */
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
  private refreshPromise: Promise<string> | null = null;

  constructor(
    private tokenStorage: LocalStorageTokenStorageService,
    @Optional() private oauth2Manager: OAuth2TokenManagerService | null,
    private apiConfig: ApiConfigService
  ) {}

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    // Get config
    const config = this.apiConfig.getConfig();

    // Check if this is a refresh request (avoid infinite loop)
    const isRefreshRequest = config.jwt?.refreshEndpoint &&
      req.url.includes(config.jwt.refreshEndpoint!);

    // Get authentication headers
    return from(this.getAuthHeaders(config, isRefreshRequest)).pipe(
      switchMap((authHeaders) => {
        // Clone request with auth headers
        let clonedReq = req;
        if (Object.keys(authHeaders).length > 0) {
          clonedReq = req.clone({
            setHeaders: authHeaders,
          });
        }

        // Handle request
        return next.handle(clonedReq).pipe(
          catchError((error: HttpErrorResponse) => {
            // Handle 401 errors with JWT refresh
            if (error.status === 401 && config.jwt && !isRefreshRequest) {
              return this.handle401Error(req, next, config);
            }

            // Convert to ApiError
            throw ApiError.fromHttpErrorResponse(error);
          })
        );
      })
    );
  }

  /**
   * Get authentication headers based on config
   */
  private async getAuthHeaders(
    config: any,
    isRefreshRequest: boolean
  ): Promise<Record<string, string>> {
    const headers: Record<string, string> = {};

    // Skip auth headers for refresh requests
    if (isRefreshRequest) {
      return headers;
    }

    // OAuth2 authentication (takes priority)
    if (config.oauth2 && this.oauth2Manager) {
      const token = await firstValueFrom(this.oauth2Manager.getAccessToken(config.oauth2));
      if (token) {
        headers['Authorization'] = `Bearer ${token}`;
        return headers;
      }
    }

    // Bearer token authentication
    if (config.accessToken) {
      const token = typeof config.accessToken === 'function'
        ? await config.accessToken()
        : config.accessToken;
      if (token) {
        headers['Authorization'] = `Bearer ${token}`;
      }
    }

    // JWT from token storage
    if (config.jwt && !headers['Authorization']) {
      const token = await Promise.resolve(this.tokenStorage.getAccessToken());
      if (token) {
        headers['Authorization'] = `Bearer ${token}`;
      }
    }

    // API key authentication
    if (config.apiKey) {
      const apiKey = typeof config.apiKey === 'function'
        ? await config.apiKey('X-API-Key')
        : config.apiKey;
      if (apiKey) {
        headers['X-API-Key'] = apiKey;
      }
    }

    // Basic authentication
    if (config.username && config.password) {
      const credentials = btoa(`${config.username}:${config.password}`);
      headers['Authorization'] = `Basic ${credentials}`;
    }

    return headers;
  }

  /**
   * Handle 401 error with JWT token refresh
   */
  private handle401Error(
    req: HttpRequest<any>,
    next: HttpHandler,
    config: any
  ): Observable<HttpEvent<any>> {
    // If refresh is already in progress, wait for it
    if (this.refreshPromise) {
      return from(this.refreshPromise).pipe(
        switchMap(() => {
          // Retry original request with new token
          return this.retryRequest(req, next, config);
        }),
        catchError((error) => {
          // Refresh failed
          if (config.jwt?.onAuthError) {
            config.jwt.onAuthError();
          }
          return throwError(() => error);
        })
      );
    }

    // Start refresh
    this.refreshPromise = this.refreshJwtToken(config);

    return from(this.refreshPromise).pipe(
      switchMap(() => {
        // Retry original request with new token
        return this.retryRequest(req, next, config);
      }),
      catchError((error) => {
        // Refresh failed
        if (config.jwt?.onAuthError) {
          config.jwt.onAuthError();
        }
        return throwError(() => error);
      }),
      tap(() => {
        // Clear refresh promise after success
        this.refreshPromise = null;
      })
    );
  }

  /**
   * Refresh JWT token
   */
  private async refreshJwtToken(config: any): Promise<string> {
    const jwtConfig = config.jwt;
    if (!jwtConfig) {
      throw new Error('JWT configuration not found');
    }

    // Get refresh token
    const refreshTokenValue = typeof jwtConfig.refreshToken === 'function'
      ? await jwtConfig.refreshToken()
      : jwtConfig.refreshToken || await Promise.resolve(this.tokenStorage.getRefreshToken());

    if (!refreshTokenValue) {
      throw new Error('Refresh token not available');
    }

    // Get refresh endpoint
    const refreshEndpoint = jwtConfig.refreshEndpoint || '/api/v1/auth/refresh';
    const refreshUrl = `${config.baseUrl}${refreshEndpoint}`;

    // Make refresh request
    const response = await fetch(refreshUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        ...config.headers,
      },
      body: JSON.stringify({ refreshToken: refreshTokenValue }),
    });

    if (!response.ok) {
      const errorBody = await response.json().catch(() => ({}));
      throw new ApiError(
        response.status,
        response.statusText,
        errorBody,
        `JWT refresh failed: ${response.status} ${response.statusText}`
      );
    }

    // Parse response
    const responseData = await response.json();
    let tokenData: any;

    // Check if response is wrapped in ApiResponse format
    if (responseData.success === true && responseData.data) {
      tokenData = responseData.data;
    } else {
      tokenData = responseData;
    }

    // Extract tokens (support both camelCase and snake_case)
    const accessToken = tokenData.accessToken || tokenData.access_token;
    const refreshToken = tokenData.refreshToken || tokenData.refresh_token;
    const expiresIn = tokenData.expiresIn || tokenData.expires_in;

    if (!accessToken) {
      throw new Error('No access token in refresh response');
    }

    // Store tokens
    await Promise.resolve(this.tokenStorage.setAccessToken(accessToken, expiresIn));
    if (refreshToken) {
      await Promise.resolve(this.tokenStorage.setRefreshToken(refreshToken));
    }

    // Call onTokenRefresh callback if provided
    if (jwtConfig.onTokenRefresh) {
      await jwtConfig.onTokenRefresh(accessToken);
    }

    return accessToken;
  }

  /**
   * Retry original request with new token
   */
  private retryRequest(
    req: HttpRequest<any>,
    next: HttpHandler,
    config: any
  ): Observable<HttpEvent<any>> {
    // Get new token and add to headers
    return from(this.getAuthHeaders(config, false)).pipe(
      switchMap((authHeaders) => {
        const clonedReq = req.clone({
          setHeaders: authHeaders,
        });
        return next.handle(clonedReq);
      })
    );
  }
}"#,
            )
            .map_err(|e| {
                PluginError::execution(format!(
                    "Failed to register auth-interceptor template: {}",
                    e
                ))
            })?;

        // Retry Interceptor template
        templates
            .register_template_string(
                "retry-interceptor",
                r#"// Generated Angular Retry Interceptor for {{api_title}}
// API Version: {{api_version}}

import { Injectable } from '@angular/core';
import {
  HttpInterceptor,
  HttpRequest,
  HttpHandler,
  HttpEvent,
  HttpErrorResponse,
} from '@angular/common/http';
import { Observable, throwError, timer } from 'rxjs';
import { retryWhen, mergeMap, take } from 'rxjs/operators';
import { ApiError } from './core';
import { ApiConfigService } from './api-config.service';

/**
 * HTTP Interceptor for retry logic with exponential backoff
 * Automatically retries failed requests based on configuration
 */
@Injectable()
export class RetryInterceptor implements HttpInterceptor {
  constructor(private apiConfig: ApiConfigService) {}

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const config = this.apiConfig.getConfig();
    const retryConfig = config.retry || {};
    const maxRetries = retryConfig.maxRetries ?? 3;
    const retryableStatusCodes = retryConfig.retryableStatusCodes || [408, 429, 500, 502, 503, 504];

    return next.handle(req).pipe(
      retryWhen((errors: Observable<HttpErrorResponse>) => {
        return errors.pipe(
          mergeMap((error: HttpErrorResponse, index: number) => {
            // Don't retry if exceeded max retries
            if (index >= maxRetries) {
              return throwError(() => ApiError.fromHttpErrorResponse(error));
            }

            // Check if error is retryable
            const isRetryable = this.isRetryableError(error, retryConfig, retryableStatusCodes);

            if (!isRetryable) {
              return throwError(() => ApiError.fromHttpErrorResponse(error));
            }

            // Don't retry 401 errors (handled by AuthInterceptor)
            if (error.status === 401) {
              return throwError(() => ApiError.fromHttpErrorResponse(error));
            }

            // Don't retry 403 errors (authorization failure)
            if (error.status === 403) {
              return throwError(() => ApiError.fromHttpErrorResponse(error));
            }

            // Calculate backoff delay
            const delay = this.calculateBackoffDelay(index, retryConfig);

            // Wait before retrying
            return timer(delay);
          }),
          take(maxRetries + 1)
        );
      })
    );
  }

  /**
   * Check if error is retryable
   */
  private isRetryableError(
    error: HttpErrorResponse,
    retryConfig: any,
    retryableStatusCodes: number[]
  ): boolean {
    // Check status code
    if (error.status && retryableStatusCodes.includes(error.status)) {
      return true;
    }

    // Check network errors
    if (retryConfig.retryOnNetworkError !== false) {
      if (error.status === 0 || error.status === undefined) {
        return true; // Network error
      }
    }

    return false;
  }

  /**
   * Calculate exponential backoff delay with jitter
   */
  private calculateBackoffDelay(retryCount: number, retryConfig: any): number {
    const baseDelay = retryConfig.baseDelay || 1000;
    const maxDelay = retryConfig.maxDelay || 10000;

    // Exponential backoff: baseDelay * 2^retryCount
    const exponentialDelay = Math.min(baseDelay * Math.pow(2, retryCount), maxDelay);

    // Add jitter: random(0, 0.3 * delay)
    const jitter = Math.random() * 0.3 * exponentialDelay;

    return Math.floor(exponentialDelay + jitter);
  }
}"#,
            )
            .map_err(|e| {
                PluginError::execution(format!(
                    "Failed to register retry-interceptor template: {}",
                    e
                ))
            })?;

        // API Config Service template
        templates
            .register_template_string(
                "api-config",
                r#"// Generated Angular API Config Service for {{api_title}}
// API Version: {{api_version}}

import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { ApiConfig } from './core';

/**
 * Service for managing API configuration
 * Provides reactive configuration updates
 */
@Injectable({
  providedIn: 'root'
})
export class ApiConfigService {
  private configSubject: BehaviorSubject<ApiConfig>;
  public config$: Observable<ApiConfig>;

  constructor() {
    // Base URL resolver for environment-based switching
    const getEnvVar = (name: string): string | null => {
      if (typeof process !== 'undefined' && process.env) {
        if (process.env[name]) return process.env[name];
        const prefixes = ['VITE_', 'REACT_APP_', 'NEXT_PUBLIC_', 'NUXT_PUBLIC_'];
        for (const prefix of prefixes) {
          if (process.env[prefix + name]) return process.env[prefix + name];
        }
      }
      return null;
    };

    const resolveBaseUrl = (mockBaseUrl: string, realBaseUrl: string, explicitBaseUrl?: string): string => {
      const envBaseUrl = getEnvVar('MOCKFORGE_BASE_URL');
      if (envBaseUrl) return envBaseUrl;
      if (explicitBaseUrl) return explicitBaseUrl;
      const mode = getEnvVar('MOCKFORGE_MODE');
      if (mode === 'real') return realBaseUrl;
      if (mode === 'hybrid') return mockBaseUrl;
      return mockBaseUrl;
    };

    // Bundled schemas for runtime validation
    const bundledSchemas: Record<string, any> = {{#if bundled_schemas}}{
      {{#each bundled_schemas}}
      '{{@key}}': {{json this}},
      {{/each}}
    }{{else}}{}{{/if}};

    const defaultConfig: ApiConfig = {
      baseUrl: resolveBaseUrl(
        '{{base_url}}',
        '{{real_base_url}}',
        undefined
      ),
      headers: {
        'Content-Type': 'application/json',
      },
      timeout: 30000,
      schemas: bundledSchemas, // Include bundled schemas for runtime validation
      includeContractDiffs: true, // Enable contract diff references in errors
    };
    this.configSubject = new BehaviorSubject<ApiConfig>(defaultConfig);
    this.config$ = this.configSubject.asObservable();
  }

  /**
   * Get current configuration
   */
  getConfig(): ApiConfig {
    return this.configSubject.value;
  }

  /**
   * Update configuration
   */
  updateConfig(updates: Partial<ApiConfig>): void {
    const currentConfig = this.configSubject.value;
    const newConfig = { ...currentConfig, ...updates };
    this.configSubject.next(newConfig);
  }

  /**
   * Reset configuration to defaults
   */
  resetConfig(): void {
    const defaultConfig: ApiConfig = {
      baseUrl: '{{base_url}}',
      headers: {
        'Content-Type': 'application/json',
      },
      timeout: 30000,
    };
    this.configSubject.next(defaultConfig);
  }
}"#,
            )
            .map_err(|e| {
                PluginError::execution(format!("Failed to register api-config template: {}", e))
            })?;

        // Main API Service template with all operations
        templates
            .register_template_string(
                "api-service",
                r#"// Generated Angular API Service for {{api_title}}
// API Version: {{api_version}}

import { Injectable } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { map, catchError, timeout } from 'rxjs/operators';
import { ApiError, ApiConfig } from './core';
import { ApiConfigService } from './api-config.service';
{{#each operations}}
import type { {{response_type_name}}{{#if request_type_name}}, {{request_type_name}}{{/if}} } from './types';
{{/each}}

/**
 * Main API service with all generated operations
 * Uses Angular HttpClient with RxJS Observables
 */
@Injectable({
  providedIn: 'root'
})
export class {{api_title}}Service {
  private baseUrl: string;

  constructor(
    private http: HttpClient,
    private apiConfig: ApiConfigService
  ) {
    this.baseUrl = this.apiConfig.getConfig().baseUrl;

    // Subscribe to config changes
    this.apiConfig.config$.subscribe(config => {
      this.baseUrl = config.baseUrl;
    });
  }

  /**
   * Unwrap ApiResponse<T> format if configured
   */
  private unwrapResponse<T>(data: any, config: ApiConfig): T {
    if (!config.unwrapResponse) {
      return data;
    }

    // Check if response matches ApiResponse<T> format
    if (data && typeof data === 'object' && data.success === true && 'data' in data) {
      return data.data;
    }

    // Return as-is if not wrapped
    return data;
  }

  /**
   * Validate request data against schema (if validation enabled)
   * Supports both basic validation (required fields) and full JSON Schema validation
   */
  private validateRequest(data: any, requiredFields?: string[], schemaId?: string, config?: ApiConfig): void {
    const cfg = config || this.apiConfig.getConfig();
    if (!cfg.validateRequests) {
      return;
    }

    if (!data || typeof data !== 'object') {
      return;
    }

    // Check required fields (basic validation)
    if (requiredFields && Array.isArray(requiredFields)) {
      const missingFields: string[] = [];
      for (const field of requiredFields) {
        if (!(field in data) || data[field] === undefined || data[field] === null) {
          missingFields.push(field);
        }
      }

      if (missingFields.length > 0) {
        throw new RequiredError(
          missingFields.join(', '),
          `Missing required fields: ${missingFields.join(', ')}`
        );
      }
    }

    // Full JSON Schema validation (if schema provided and ajv available)
    if (schemaId && cfg.schemas && cfg.schemas[schemaId]) {
      this.validateAgainstSchema(data, cfg.schemas[schemaId], schemaId, 'request', cfg);
    }
  }

  /**
   * Validate data against JSON Schema using ajv (if available)
   * Falls back to basic validation if ajv is not available
   */
  private validateAgainstSchema(
    data: any,
    schema: any,
    schemaId: string,
    context: 'request' | 'response',
    config: ApiConfig
  ): void {
    try {
      let Ajv: any;
      if (typeof window !== 'undefined' && (window as any).ajv) {
        Ajv = (window as any).ajv;
      } else if (typeof require !== 'undefined') {
        Ajv = require('ajv');
      } else {
        console.warn('ajv not available, using basic validation only. Install ajv for full schema validation: npm install ajv');
        return;
      }

      const ajv = new Ajv({ allErrors: true, strict: false });
      const validate = ajv.compile(schema);
      const valid = validate(data);

      if (!valid && validate.errors) {
        const firstError = validate.errors[0];
        const schemaPath = firstError.instancePath || firstError.schemaPath || '';
        const expectedType = firstError.schema?.type || firstError.params?.type || 'unknown';
        const actualValue = firstError.data;
        const contractDiffId = schema['x-contract-diff-id'] || schema.contractDiffId;
        const isBreakingChange = schema['x-breaking-change'] || schema.isBreakingChange || false;

        throw new ContractValidationError(
          400,
          'Validation Error',
          schemaPath || `${context}.${schemaId}`,
          expectedType,
          actualValue,
          contractDiffId,
          isBreakingChange,
          { errors: validate.errors },
          `Schema validation failed for ${context}`
        );
      }
    } catch (e) {
      if (e instanceof ContractValidationError) {
        throw e;
      }
      console.warn('ajv not available, using basic validation only. Install ajv for full schema validation: npm install ajv');
    }
  }

  /**
   * Validate response data against schema (if validation enabled)
   */
  private validateResponse(data: any, schemaId?: string, config?: ApiConfig): void {
    const cfg = config || this.apiConfig.getConfig();
    if (!cfg.validateResponses) {
      return;
    }

    if (!data) {
      return;
    }

    // Full JSON Schema validation (if schema provided)
    if (schemaId && cfg.schemas && cfg.schemas[schemaId]) {
      this.validateAgainstSchema(data, cfg.schemas[schemaId], schemaId, 'response', cfg);
    }
  }

  {{#each operations}}
  /**
   * {{summary}}
   * {{#if description}}
   * {{description}}
   * {{/if}}
   */
  {{operation_id}}({{method_params}}): Observable<{{response_type_name}}> {
    const config = this.apiConfig.getConfig();
    {{#if path_params}}
    const endpoint = `{{endpoint_path}}`;
    {{else}}
    const endpoint = '{{endpoint_path}}';
    {{/if}}

    {{#if request_body}}
    // Validate request data if validation is enabled
    if (data && config.validateRequests) {
      const schemaId = '{{operation_id}}Request';
      this.validateRequest(data, {{#if required_fields}}[{{#each required_fields}}'{{this}}'{{#unless @last}}, {{/unless}}{{/each}}]{{else}}undefined{{/if}}, schemaId, config);
    }
    {{/if}}

    {{#if (eq method "GET")}}
    {{#if query_params}}
    // Build query parameters
    let params = new HttpParams();
    if (queryParams) {
      Object.keys(queryParams).forEach(key => {
        const value = queryParams[key];
        if (value !== undefined && value !== null) {
          params = params.set(key, String(value));
        }
      });
    }
    {{/if}}
    return this.http.get<{{response_type_name}}>(`${this.baseUrl}${endpoint}`{{#if query_params}}, { params }{{/if}}).pipe(
      timeout(config.timeout || 30000),
      map(response => {
        const unwrapped = this.unwrapResponse(response, config);
        if (config.validateResponses) {
          const schemaId = '{{operation_id}}Response';
          this.validateResponse(unwrapped, schemaId, config);
        }
        return unwrapped;
      }),
      catchError((error) => {
        return throwError(() => ApiError.fromHttpErrorResponse(error));
      })
    );
    {{else}}
    {{#if query_params}}
    // Build query parameters
    let params = new HttpParams();
    if (queryParams) {
      Object.keys(queryParams).forEach(key => {
        const value = queryParams[key];
        if (value !== undefined && value !== null) {
          params = params.set(key, String(value));
        }
      });
    }
    {{/if}}
    {{#if request_body}}
    {{#if query_params}}
    return this.http.{{toLowerCase method}}<{{response_type_name}}>(`${this.baseUrl}${endpoint}`, data, { params }).pipe(
    {{else}}
    return this.http.{{toLowerCase method}}<{{response_type_name}}>(`${this.baseUrl}${endpoint}`, data).pipe(
    {{/if}}
    {{else}}
    {{#if query_params}}
    return this.http.{{toLowerCase method}}<{{response_type_name}}>(`${this.baseUrl}${endpoint}`, null, { params }).pipe(
    {{else}}
    return this.http.{{toLowerCase method}}<{{response_type_name}}>(`${this.baseUrl}${endpoint}`).pipe(
    {{/if}}
    {{/if}}
      timeout(config.timeout || 30000),
      map(response => {
        const unwrapped = this.unwrapResponse(response, config);
        if (config.validateResponses) {
          const schemaId = '{{operation_id}}Response';
          this.validateResponse(unwrapped, schemaId, config);
        }
        return unwrapped;
      }),
      catchError((error) => {
        return throwError(() => ApiError.fromHttpErrorResponse(error));
      })
    );
    {{/if}}
  }

  {{/each}}
}"#,
            )
            .map_err(|e| {
                PluginError::execution(format!("Failed to register api-service template: {}", e))
            })?;

        // Configuration Module template
        templates
            .register_template_string(
                "api-module",
                r#"// Generated Angular API Module for {{api_title}}
// API Version: {{api_version}}

import { NgModule, Optional, SkipSelf } from '@angular/core';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { AuthInterceptor } from './auth-interceptor';
import { RetryInterceptor } from './retry-interceptor';
import { ApiConfigService } from './api-config.service';
import { LocalStorageTokenStorageService } from './token-storage.service';
import { OAuth2TokenManagerService } from './oauth2-token-manager.service';
import { {{api_title}}Service } from './{{api_title_lowercase}}.service';

/**
 * API Module - Import this module in your AppModule or use standalone components
 *
 * Usage in AppModule:
 * ```typescript
 * import { ApiModule } from './api/api-module';
 *
 * @NgModule({
 *   imports: [
 *     HttpClientModule,
 *     ApiModule
 *   ],
 *   ...
 * })
 * export class AppModule { }
 * ```
 *
 * Or for standalone components:
 * ```typescript
 * import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
 * import { ApiConfigService, AuthInterceptor, RetryInterceptor } from './api';
 *
 * bootstrapApplication(AppComponent, {
 *   providers: [
 *     provideHttpClient(withInterceptorsFromDi()),
 *     ApiConfigService,
 *     LocalStorageTokenStorageService,
 *     { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true },
 *     { provide: HTTP_INTERCEPTORS, useClass: RetryInterceptor, multi: true },
 *     {{api_title}}Service
 *   ]
 * });
 * ```
 */
@NgModule({
  imports: [
    HttpClientModule
  ],
  providers: [
    ApiConfigService,
    LocalStorageTokenStorageService,
    OAuth2TokenManagerService, // Optional - can be removed if OAuth2 not needed
    {
      provide: HTTP_INTERCEPTORS,
      useClass: AuthInterceptor,
      multi: true
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: RetryInterceptor,
      multi: true
    },
    {{api_title}}Service
  ]
})
export class ApiModule {
  constructor(@Optional() @SkipSelf() parentModule?: ApiModule) {
    if (parentModule) {
      throw new Error('ApiModule is already loaded. Import it in the AppModule only.');
    }
  }
}"#,
            )
            .map_err(|e| {
                PluginError::execution(format!("Failed to register api-module template: {}", e))
            })?;

        // OAuth2 Token Manager Service template (simplified - full implementation can be added later)
        templates
            .register_template_string(
                "oauth2-token-manager",
                r#"// Generated Angular OAuth2 Token Manager Service for {{api_title}}
// API Version: {{api_version}}

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, from, throwError } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { OAuth2Config, ApiError } from './core';
import { TokenStorage } from './core';
import { LocalStorageTokenStorageService } from './token-storage.service';

/**
 * OAuth2 Token Manager Service
 * Handles OAuth2 flows and token refresh
 */
@Injectable({
  providedIn: 'root'
})
export class OAuth2TokenManagerService {
  private tokenStorage: TokenStorage;

  constructor(
    private http: HttpClient,
    tokenStorage?: TokenStorage
  ) {
    // Use provided token storage or create default
    this.tokenStorage = tokenStorage || new LocalStorageTokenStorageService();
  }

  /**
   * Get access token via client_credentials flow
   * ⚠️ SECURITY WARNING: Only use in server-side applications!
   */
  getClientCredentialsToken(config: OAuth2Config): Observable<string> {
    if (!config.clientSecret) {
      console.warn('⚠️ SECURITY WARNING: client_credentials flow with client secret in browser code is insecure.');
      return throwError(() => new Error('Client secret required for client_credentials flow'));
    }

    const params = new URLSearchParams();
    params.set('grant_type', 'client_credentials');
    params.set('client_id', config.clientId);
    params.set('client_secret', config.clientSecret);
    if (config.scopes && config.scopes.length > 0) {
      params.set('scope', config.scopes.join(' '));
    }

    return this.http.post<any>(config.tokenUrl, params.toString(), {
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).pipe(
      map(response => {
        const token = response.access_token;
        if (!token) {
          throw new Error('No access_token in OAuth2 response');
        }
        // Store token with expiration
        this.tokenStorage.setAccessToken(token, response.expires_in);
        if (config.onTokenRefresh) {
          config.onTokenRefresh(token);
        }
        return token;
      }),
      catchError((error) => {
        return throwError(() => ApiError.fromHttpErrorResponse(error));
      })
    );
  }

  /**
   * Get current access token (from storage or fetch new for client_credentials)
   */
  getAccessToken(config?: OAuth2Config): Observable<string | null> {
    // Try to get stored token first
    const stored = this.tokenStorage.getAccessToken();
    if (stored) {
      return new Observable(observer => {
        observer.next(stored);
        observer.complete();
      });
    }

    // If no stored token and client_credentials flow, fetch new token
    if (config && config.flow === 'client_credentials') {
      return this.getClientCredentialsToken(config);
    }

    return new Observable(observer => {
      observer.next(null);
      observer.complete();
    });
  }
}"#,
            )
            .map_err(|e| {
                PluginError::execution(format!("Failed to register oauth2-token-manager template: {}", e))
            })?;

        Ok(())
    }

    /// Generate Angular client code from OpenAPI specification
    fn generate_angular_client(
        &self,
        spec: &OpenApiSpec,
        config: &ClientGeneratorConfig,
    ) -> Result<ClientGenerationResult> {
        let mut files = Vec::new();
        let warnings = Vec::new();

        // Prepare template context
        let context = self.prepare_template_context(spec, config)?;

        // Generate TypeScript types
        let types_content = self.templates.render("types", &context).map_err(|e| {
            PluginError::execution(format!("Failed to render types template: {}", e))
        })?;
        files.push(GeneratedFile {
            path: "types.ts".to_string(),
            content: types_content,
            file_type: "typescript".to_string(),
        });

        // Generate core infrastructure
        let core_content = self.templates.render("core", &context).map_err(|e| {
            PluginError::execution(format!("Failed to render core template: {}", e))
        })?;
        files.push(GeneratedFile {
            path: "core.ts".to_string(),
            content: core_content,
            file_type: "typescript".to_string(),
        });

        // Generate token storage service
        let token_storage_content =
            self.templates.render("token-storage", &context).map_err(|e| {
                PluginError::execution(format!("Failed to render token-storage template: {}", e))
            })?;
        files.push(GeneratedFile {
            path: "token-storage.service.ts".to_string(),
            content: token_storage_content,
            file_type: "typescript".to_string(),
        });

        // Generate API config service
        let api_config_content = self.templates.render("api-config", &context).map_err(|e| {
            PluginError::execution(format!("Failed to render api-config template: {}", e))
        })?;
        files.push(GeneratedFile {
            path: "api-config.service.ts".to_string(),
            content: api_config_content,
            file_type: "typescript".to_string(),
        });

        // Generate OAuth2 token manager service
        let oauth2_content =
            self.templates.render("oauth2-token-manager", &context).map_err(|e| {
                PluginError::execution(format!(
                    "Failed to render oauth2-token-manager template: {}",
                    e
                ))
            })?;
        files.push(GeneratedFile {
            path: "oauth2-token-manager.service.ts".to_string(),
            content: oauth2_content,
            file_type: "typescript".to_string(),
        });

        // Generate auth interceptor
        let auth_interceptor_content =
            self.templates.render("auth-interceptor", &context).map_err(|e| {
                PluginError::execution(format!("Failed to render auth-interceptor template: {}", e))
            })?;
        files.push(GeneratedFile {
            path: "auth-interceptor.ts".to_string(),
            content: auth_interceptor_content,
            file_type: "typescript".to_string(),
        });

        // Generate retry interceptor
        let retry_interceptor_content =
            self.templates.render("retry-interceptor", &context).map_err(|e| {
                PluginError::execution(format!(
                    "Failed to render retry-interceptor template: {}",
                    e
                ))
            })?;
        files.push(GeneratedFile {
            path: "retry-interceptor.ts".to_string(),
            content: retry_interceptor_content,
            file_type: "typescript".to_string(),
        });

        // Generate main API service
        let api_title_lowercase = spec.info.title.to_lowercase().replace(' ', "-");
        let api_service_content = self.templates.render("api-service", &context).map_err(|e| {
            PluginError::execution(format!("Failed to render api-service template: {}", e))
        })?;
        files.push(GeneratedFile {
            path: format!("{}.service.ts", api_title_lowercase),
            content: api_service_content,
            file_type: "typescript".to_string(),
        });

        // Generate API module
        let api_module_content = self.templates.render("api-module", &context).map_err(|e| {
            PluginError::execution(format!("Failed to render api-module template: {}", e))
        })?;
        files.push(GeneratedFile {
            path: "api-module.ts".to_string(),
            content: api_module_content,
            file_type: "typescript".to_string(),
        });

        // Generate package.json
        let package_json = self.generate_package_json(spec, config)?;
        files.push(GeneratedFile {
            path: "package.json".to_string(),
            content: package_json,
            file_type: "json".to_string(),
        });

        // Generate README
        let readme = self.generate_readme(spec, config)?;
        files.push(GeneratedFile {
            path: "README.md".to_string(),
            content: readme,
            file_type: "markdown".to_string(),
        });

        let metadata = GenerationMetadata {
            framework: "angular".to_string(),
            client_name: format!("{}-client", spec.info.title.to_lowercase().replace(' ', "-")),
            api_title: spec.info.title.clone(),
            api_version: spec.info.version.clone(),
            operation_count: self.count_operations(spec),
            schema_count: self.count_schemas(spec),
        };

        Ok(ClientGenerationResult {
            files,
            warnings,
            metadata,
        })
    }

    /// Prepare template context from OpenAPI spec
    fn prepare_template_context(
        &self,
        spec: &OpenApiSpec,
        config: &ClientGeneratorConfig,
    ) -> Result<Value> {
        // Similar to Vue/Svelte - prepare operations, schemas, etc.
        let mut operations = Vec::new();
        let mut schemas = HashMap::new();
        let mut operation_id_counts = HashMap::new();

        // Process operations (similar to Vue generator)
        for (path, path_item) in &spec.paths {
            for (method, operation) in &path_item.operations {
                let mut normalized_op =
                    crate::client_generator::helpers::normalize_operation(method, path, operation);

                // Handle duplicate operation IDs by adding numeric suffixes
                let base_operation_id = normalized_op.operation_id.clone();
                let count = operation_id_counts.entry(base_operation_id.clone()).or_insert(0);
                *count += 1;

                if *count > 1 {
                    // Add suffix for duplicates: postRecordEnvironmentalData2, postRecordEnvironmentalData3, etc.
                    normalized_op.operation_id = format!("{}{}", base_operation_id, *count);
                }

                // Extract path parameters
                let path_params = crate::client_generator::helpers::extract_path_parameters(path);

                // Generate endpoint path with template literals
                let mut endpoint_path = normalized_op.path.clone();
                for param in &path_params {
                    endpoint_path = endpoint_path
                        .replace(&format!("{{{}}}", param), &format!("${{{}}}", param));
                }

                // Extract query parameters
                let mut query_params = Vec::new();
                let mut query_param_types = Vec::new();

                for param in &normalized_op.parameters {
                    if param.r#in == "query" {
                        let param_type = if let Some(schema) = &param.schema {
                            crate::client_generator::helpers::schema_to_typescript_type(schema)
                        } else {
                            "string".to_string()
                        };

                        let required = param.required.unwrap_or(false);
                        query_params.push(json!({
                            "name": param.name,
                            "required": required,
                            "type": param_type.clone(),
                        }));

                        if required {
                            query_param_types.push(format!("{}: {}", param.name, param_type));
                        } else {
                            query_param_types.push(format!("{}?: {}", param.name, param_type));
                        }
                    }
                }

                // Build method parameter list
                let mut method_params_parts = Vec::new();

                // Add path parameters (all required)
                for param in &path_params {
                    method_params_parts.push(format!("{}: string", param));
                }

                // Add query parameters (as an object)
                if !query_params.is_empty() {
                    method_params_parts
                        .push(format!("queryParams?: {{ {} }}", query_param_types.join(", ")));
                }

                // Check if request body has JSON content
                let has_json_request_body = normalized_op
                    .request_body
                    .as_ref()
                    .is_some_and(|rb| rb.content.contains_key("application/json"));

                // Add request body parameter if present
                if has_json_request_body && normalized_op.method != "GET" {
                    let type_name = crate::client_generator::helpers::generate_type_name(
                        &normalized_op.operation_id,
                        "Request",
                    );
                    method_params_parts.push(format!("data: {}", type_name));
                }

                // Join parameters
                let method_params = if method_params_parts.is_empty() {
                    String::new()
                } else {
                    method_params_parts.join(", ")
                };

                // Generate type names
                let response_type_name = crate::client_generator::helpers::generate_type_name(
                    &normalized_op.operation_id,
                    "Response",
                );

                let request_type_name = if has_json_request_body {
                    crate::client_generator::helpers::generate_type_name(
                        &normalized_op.operation_id,
                        "Request",
                    )
                } else {
                    String::new()
                };

                // Extract required fields
                let mut required_fields: Vec<String> = Vec::new();
                let processed_request_body = if has_json_request_body {
                    if let Some(ref rb) = normalized_op.request_body {
                        let mut processed_rb = json!(rb);
                        if let Some(content) = processed_rb.get_mut("content") {
                            if let Some(json_content) = content.get_mut("application/json") {
                                if let Some(schema) = json_content.get_mut("schema") {
                                    if let Some(required) =
                                        schema.get("required").and_then(|r| r.as_array())
                                    {
                                        required_fields = required
                                            .iter()
                                            .filter_map(|v| v.as_str().map(|s| s.to_string()))
                                            .collect();
                                    }

                                    *schema =
                                        Self::process_schema_with_required_flags(schema.take());
                                }
                            }
                        }
                        Some(processed_rb)
                    } else {
                        None
                    }
                } else {
                    None
                };

                // Pre-process response schemas
                let mut processed_responses: HashMap<String, Value> =
                    normalized_op.responses.iter().map(|(k, v)| (k.clone(), json!(v))).collect();
                for (_status_code, response) in processed_responses.iter_mut() {
                    if let Some(content) =
                        response.get_mut("content").and_then(|c| c.as_object_mut())
                    {
                        if let Some(json_content) = content.get_mut("application/json") {
                            if let Some(schema) = json_content.get_mut("schema") {
                                *schema = Self::process_schema_with_required_flags(schema.take());
                            }
                        }
                    }
                }

                operations.push(json!({
                    "method": normalized_op.method,
                    "path": normalized_op.path,
                    "endpoint_path": endpoint_path,
                    "operation_id": normalized_op.operation_id,
                    "response_type_name": response_type_name,
                    "request_type_name": request_type_name,
                    "summary": normalized_op.summary,
                    "description": normalized_op.description,
                    "parameters": normalized_op.parameters,
                    "path_params": path_params,
                    "query_params": query_params,
                    "query_param_types": query_param_types,
                    "method_params": method_params,
                    "request_body": processed_request_body,
                    "required_fields": if !required_fields.is_empty() { Some(required_fields) } else { None },
                    "responses": processed_responses,
                    "tags": normalized_op.tags,
                }));
            }
        }

        // Process schemas
        if let Some(components) = &spec.components {
            if let Some(spec_schemas) = &components.schemas {
                for (name, schema) in spec_schemas {
                    schemas.insert(name.clone(), schema.clone());
                }
            }
        }

        let api_title_lowercase = spec.info.title.to_lowercase().replace(' ', "-");

        // Extract real base URL from OpenAPI spec servers or use a default
        let real_base_url = spec
            .servers
            .as_ref()
            .and_then(|servers| servers.first())
            .map(|server| server.url.clone())
            .unwrap_or_else(|| "https://api.production.com".to_string());

        // Prepare schemas for bundling (convert to JSON Schema format for runtime validation)
        let mut bundled_schemas = serde_json::Map::new();
        for (schema_name, schema) in &schemas {
            // Convert OpenAPI schema to JSON Schema format
            // Add schema metadata for contract diff tracking
            let mut schema_json = json!(schema);
            if let Some(schema_obj) = schema_json.as_object_mut() {
                // Add schema ID for lookup
                schema_obj.insert("$id".to_string(), json!(schema_name));
                // Add metadata fields for contract diff tracking (can be populated by drift detection)
                schema_obj.insert("x-schema-id".to_string(), json!(schema_name));
            }
            bundled_schemas.insert(schema_name.clone(), schema_json);
        }

        Ok(json!({
            "api_title": spec.info.title,
            "api_title_lowercase": api_title_lowercase,
            "api_version": spec.info.version,
            "api_description": spec.info.description,
            "base_url": config.base_url.as_ref().unwrap_or(&"http://localhost:3000".to_string()),
            "real_base_url": real_base_url,
            "operations": operations,
            "schemas": schemas,
            "bundled_schemas": bundled_schemas,
        }))
    }

    /// Count operations in the spec
    fn count_operations(&self, spec: &OpenApiSpec) -> usize {
        spec.paths.values().map(|path_item| path_item.operations.len()).sum()
    }

    /// Count schemas in the spec
    fn count_schemas(&self, spec: &OpenApiSpec) -> usize {
        spec.components
            .as_ref()
            .and_then(|c| c.schemas.as_ref())
            .map(|s| s.len())
            .unwrap_or(0)
    }

    /// Generate package.json for the Angular client
    fn generate_package_json(
        &self,
        spec: &OpenApiSpec,
        _config: &ClientGeneratorConfig,
    ) -> Result<String> {
        let package_name = format!("{}-client", spec.info.title.to_lowercase().replace(' ', "-"));

        let package_json = json!({
            "name": package_name,
            "version": "1.0.0",
            "description": format!("Angular client for {}", spec.info.title),
            "main": format!("{}.service.ts", spec.info.title.to_lowercase().replace(' ', "-")),
            "types": "types.ts",
            "scripts": {
                "build": "tsc",
                "dev": "tsc --watch"
            },
            "dependencies": {
                "@angular/common": "^17.0.0",
                "@angular/core": "^17.0.0",
                "rxjs": "^7.8.0"
            },
            "devDependencies": {
                "typescript": "^5.0.0"
            },
            "peerDependencies": {
                "@angular/common": ">=17.0.0",
                "@angular/core": ">=17.0.0",
                "rxjs": ">=7.0.0"
            }
        });

        serde_json::to_string_pretty(&package_json)
            .map_err(|e| PluginError::execution(format!("Failed to serialize package.json: {}", e)))
    }

    /// Generate README for the Angular client
    fn generate_readme(
        &self,
        spec: &OpenApiSpec,
        config: &ClientGeneratorConfig,
    ) -> Result<String> {
        let api_description = spec
            .info
            .description
            .as_ref()
            .map(|d| format!("\n\n{}\n", d))
            .unwrap_or_default();

        let operation_count = self.count_operations(spec);
        let schema_count = self.count_schemas(spec);
        let default_url = "http://localhost:3000".to_string();
        let base_url = config.base_url.as_ref().unwrap_or(&default_url);
        let api_title_lowercase = spec.info.title.to_lowercase().replace(' ', "-");
        let service_name = spec.info.title.replace(' ', "");
        let service_name_import = format!("{{ {} }}", service_name);
        let operations_list = self.generate_operations_list(spec);

        let readme = format!(
            r#"# {} Angular Client

Generated Angular client for {} API (v{}).{}
## Features

✅ **Auto-generated Angular service** - {} operations with RxJS Observables
✅ **Enterprise error handling** - Structured ApiError class compatible with HttpErrorResponse
✅ **OAuth2 flow support** - Authorization code, client credentials flows
✅ **Authentication support** - Bearer tokens, API keys, Basic auth, OAuth2
✅ **JWT token refresh** - Automatic refresh on 401 with promise deduplication
✅ **Retry logic** - Exponential backoff with jitter using RxJS operators
✅ **HTTP Interceptors** - Auth and retry interceptors for global request handling
✅ **Token storage service** - Injectable service for secure token management
✅ **TypeScript types** - {} fully-typed interfaces
✅ **Timeout handling** - Configurable request timeouts
✅ **ApiResponse wrapper** - Support for unwrapping wrapped API responses
✅ **100% endpoint coverage** - All {} API operations included

## Installation

```bash
npm install
```

## Quick Start

### Using Angular Service

```typescript
import {{ Component, OnInit }} from '@angular/core';
import {{ {}Service }} from './{}.service';
import {{ ApiError }} from './core';

@Component({{
  selector: 'app-my-component',
  template: `
    <div *ngIf="loading">Loading...</div>
    <div *ngIf="error">Error: {{{{error.message}}}}</div>
    <div *ngIf="data">
      <div *ngFor="let item of data">
        {{{{item.name}}}}
      </div>
    </div>
  `,
}})
export class MyComponent implements OnInit {{
  data: any[] | null = null;
  loading: boolean = false;
  error: ApiError | null = null;

  constructor(private apiService: MyApiService) {{}}

  ngOnInit(): void {{
    this.loading = true;
    this.apiService.getUsers().subscribe({{
      next: (data) => {{
        this.data = data;
        this.loading = false;
      }},
      error: (err) => {{
        this.error = err instanceof ApiError ? err : new ApiError(0, 'Unknown Error', err);
        this.loading = false;
      }}
    }});
  }}
}}
```

### Module Setup

Import the `ApiModule` in your `AppModule`:

```typescript
import {{ NgModule }} from '@angular/core';
import {{ BrowserModule }} from '@angular/platform-browser';
import {{ HttpClientModule }} from '@angular/common/http';
import {{ ApiModule }} from './api/api-module';
import {{ AppComponent }} from './app.component';

@NgModule({{
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    HttpClientModule,
    ApiModule
  ],
  bootstrap: [AppComponent]
}})
export class AppModule {{ }}
```

### Standalone Components Setup

For standalone components (Angular 14+):

```typescript
import {{ provideHttpClient, withInterceptorsFromDi }} from '@angular/common/http';
import {{ bootstrapApplication }} from '@angular/platform-browser';
import {{ ApiConfigService, LocalStorageTokenStorageService, AuthInterceptor, RetryInterceptor }} from './api';
import {{ {}Service }} from './api/{}.service';
import {{ HTTP_INTERCEPTORS }} from '@angular/common/http';

bootstrapApplication(AppComponent, {{
  providers: [
    provideHttpClient(withInterceptorsFromDi()),
    ApiConfigService,
    LocalStorageTokenStorageService,
    {{
      provide: HTTP_INTERCEPTORS,
      useClass: AuthInterceptor,
      multi: true
    }},
    {{
      provide: HTTP_INTERCEPTORS,
      useClass: RetryInterceptor,
      multi: true
    }},
    {}Service
  ]
}});
```

## Configuration

The client is configured to use the following base URL: `{}`

### Basic Configuration

```typescript
import {{ ApiConfigService }} from './api/api-config.service';

constructor(private apiConfig: ApiConfigService) {{}}

ngOnInit() {{
  // Update configuration
  this.apiConfig.updateConfig({{
    baseUrl: 'https://api.production.com'
  }});
}}
```

### Authentication

```typescript
import {{ ApiConfigService }} from './api/api-config.service';

// Bearer token authentication
this.apiConfig.updateConfig({{
  accessToken: 'your-jwt-token'
}});

// Dynamic token
this.apiConfig.updateConfig({{
  accessToken: () => localStorage.getItem('authToken') || ''
}});

// API key authentication
this.apiConfig.updateConfig({{
  apiKey: 'your-api-key'
}});

// Basic authentication
this.apiConfig.updateConfig({{
  username: 'user',
  password: 'pass'
}});
```

### JWT Token Refresh

```typescript
import {{ ApiConfigService }} from './api/api-config.service';
import {{ LocalStorageTokenStorageService }} from './api/token-storage.service';

// Configure JWT token refresh
this.apiConfig.updateConfig({{
  jwt: {{
    refreshEndpoint: '/api/v1/auth/refresh',
    refreshToken: () => this.tokenStorage.getRefreshToken() || '',
    onTokenRefresh: (token) => {{
      console.log('Token refreshed:', token);
    }},
    onAuthError: () => {{
      // Redirect to login on auth failure
      this.router.navigate(['/login']);
    }},
    refreshThreshold: 300, // Refresh if expires within 5 minutes
    checkExpirationBeforeRequest: true
  }}
}});
```

### Retry Logic

```typescript
import {{ ApiConfigService }} from './api/api-config.service';

// Configure retry behavior
this.apiConfig.updateConfig({{
  retry: {{
    maxRetries: 3,
    baseDelay: 1000, // 1 second
    maxDelay: 10000, // 10 seconds
    retryableStatusCodes: [408, 429, 500, 502, 503, 504],
    retryOnNetworkError: true
  }}
}});
```

## Error Handling

```typescript
import {{ ApiError }} from './api/core';

this.apiService.getUsers().subscribe({{
  next: (data) => {{
    console.log(data);
  }},
  error: (err) => {{
    if (err instanceof ApiError) {{
      // Check error type
      if (err.isClientError()) {{
        console.error('Client error:', err.status);
      }} else if (err.isServerError()) {{
        console.error('Server error:', err.status);
      }}

      // Get detailed error message
      console.error('Verbose message:', err.getVerboseMessage());

      // Get error details
      const details = err.getErrorDetails();
      console.error('Error details:', details);
    }}
  }}
}});
```

## Generated Files

- `types.ts` - TypeScript type definitions ({} schemas)
- `core.ts` - Core infrastructure (ApiError, Config interfaces)
- `token-storage.service.ts` - Token storage service
- `api-config.service.ts` - API configuration service
- `oauth2-token-manager.service.ts` - OAuth2 token manager
- `auth-interceptor.ts` - HTTP interceptor for authentication
- `retry-interceptor.ts` - HTTP interceptor for retry logic
- `{}.service.ts` - Main API service ({} operations)
- `api-module.ts` - Angular module for easy setup
- `package.json` - Package configuration
- `README.md` - This documentation

## API Operations

{}

## Development

```bash
# Build TypeScript
npm run build

# Watch mode
npm run dev
```
"#,
            spec.info.title,
            spec.info.title,
            spec.info.version,
            api_description,
            operation_count,
            schema_count,
            operation_count,
            service_name_import,
            api_title_lowercase,
            service_name_import,
            api_title_lowercase,
            service_name,
            base_url,
            schema_count,
            api_title_lowercase,
            operation_count,
            operations_list
        );

        Ok(readme)
    }

    /// Generate list of operations for README
    fn generate_operations_list(&self, spec: &OpenApiSpec) -> String {
        let mut operations = Vec::new();

        for (path, path_item) in &spec.paths {
            for (method, operation) in &path_item.operations {
                let fallback_summary = format!("{} {}", method.to_uppercase(), path);
                let summary = operation
                    .summary
                    .as_ref()
                    .unwrap_or(operation.operation_id.as_ref().unwrap_or(&fallback_summary));

                operations.push(format!("- **{} {}** - {}", method.to_uppercase(), path, summary));
            }
        }

        operations.join("\n")
    }
}

#[async_trait::async_trait]
impl ClientGeneratorPlugin for AngularClientGenerator {
    fn framework_name(&self) -> &str {
        "angular"
    }

    fn supported_extensions(&self) -> Vec<&str> {
        vec!["ts"]
    }

    async fn generate_client(
        &self,
        spec: &OpenApiSpec,
        config: &ClientGeneratorConfig,
    ) -> Result<ClientGenerationResult> {
        self.generate_angular_client(spec, config)
    }

    async fn get_metadata(&self) -> PluginMetadata {
        PluginMetadata::new("Angular Client Generator").with_capability("client_generator")
    }
}

impl Default for AngularClientGenerator {
    fn default() -> Self {
        Self::new().expect("Failed to create AngularClientGenerator")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::client_generator::{ApiInfo, OpenApiSpec};
    use std::collections::HashMap;

    #[test]
    fn test_angular_client_generator_creation() {
        let generator = AngularClientGenerator::new();
        assert!(generator.is_ok());
    }

    #[test]
    fn test_framework_name() {
        let generator = AngularClientGenerator::new().unwrap();
        assert_eq!(generator.framework_name(), "angular");
    }

    #[test]
    fn test_supported_extensions() {
        let generator = AngularClientGenerator::new().unwrap();
        let extensions = generator.supported_extensions();
        assert!(extensions.contains(&"ts"));
    }

    #[tokio::test]
    async fn test_generate_client() {
        let generator = AngularClientGenerator::new().unwrap();

        let spec = OpenApiSpec {
            openapi: "3.0.0".to_string(),
            info: ApiInfo {
                title: "Test API".to_string(),
                version: "1.0.0".to_string(),
                description: Some("Test API".to_string()),
            },
            servers: None,
            paths: HashMap::new(),
            components: None,
        };

        let config = ClientGeneratorConfig {
            output_dir: "./output".to_string(),
            base_url: Some("http://localhost:3000".to_string()),
            include_types: true,
            include_mocks: false,
            template_dir: None,
            options: HashMap::new(),
        };

        let result = generator.generate_client(&spec, &config).await;
        assert!(result.is_ok());

        let result = result.unwrap();
        assert!(!result.files.is_empty());
        assert_eq!(result.metadata.framework, "angular");
    }
}