BREP_cam 0.5.0

CAM toolpath engine contracts and geometric core for the BREP CAD kernel: setups, tools, operations, typed motion streams, cutter profiles, mesh snapshots and triangle spatial indexing
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
//! Operations: one typed parameter set per strategy.
//!
//! `Strategy` is `#[non_exhaustive]`: consumers outside this crate match with
//! a wildcard arm. Inside the crate every match is exhaustive, so adding a
//! variant is a deliberate change reviewed with the lanes that match on it.
//! Every parameter struct derives `Default` and tags every field
//! `#[serde(default)]` so documents saved before a field existed still load.

use super::{FeedsSpeeds, Heights, OperationId, Point3, SetupId, TargetSelection, ToolId, WorkOffset};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// One machining operation inside a setup.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Operation {
    pub id: OperationId,
    #[serde(default)]
    pub name: String,
    pub setup: SetupId,
    pub tool: ToolId,
    /// Disabled operations are kept but neither generated nor posted.
    #[serde(default = "default_true")]
    pub enabled: bool,
    #[serde(default)]
    pub targets: TargetSelection,
    pub strategy: Strategy,
    /// Machining tolerance (chordal deviation of the path from the exact surface).
    #[serde(default = "default_tolerance")]
    pub tolerance: f64,
    #[serde(default)]
    pub stock_to_leave: StockToLeave,
    /// Overrides the tool's defaults when set.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub feeds: Option<FeedsSpeeds>,
    /// Overrides the setup's heights when set.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub heights: Option<Heights>,
    #[serde(default)]
    pub linking: LinkingParams,
    /// Fixed tool orientation for 3+2 work, in SETUP machine coordinates (`None`: +Z).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tool_axis: Option<Point3>,
    /// None selects live tooling automatically for milling on a mill-turn machine.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub live_tool: Option<bool>,
    /// Post-processor properties specific to this operation (free-form, ordered).
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub post_properties: BTreeMap<String, String>,
    /// Feed optimisation and smoothing applied after generation (lane C18); off by default.
    #[serde(default)]
    pub polish: PolishParams,
}

fn default_true() -> bool {
    true
}

fn default_tolerance() -> f64 {
    0.01
}

impl Default for Operation {
    fn default() -> Self {
        Self::new(OperationId::default(), "", SetupId::default(), ToolId::default(), Strategy::default())
    }
}

impl Operation {
    pub fn new(
        id: impl Into<OperationId>,
        name: impl Into<String>,
        setup: impl Into<SetupId>,
        tool: impl Into<ToolId>,
        strategy: Strategy,
    ) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            setup: setup.into(),
            tool: tool.into(),
            enabled: true,
            targets: TargetSelection::default(),
            strategy,
            tolerance: default_tolerance(),
            stock_to_leave: StockToLeave::default(),
            feeds: None,
            heights: None,
            linking: LinkingParams::default(),
            tool_axis: None,
            live_tool: None,
            post_properties: BTreeMap::new(),
            polish: PolishParams::default(),
        }
    }

    pub fn validate(&self) -> Result<(), String> {
        if !self.tolerance.is_finite() || self.tolerance <= 0.0 {
            return Err("machining tolerance must be finite and positive".into());
        }
        self.targets.validate()?;
        if self.enabled
            && self.targets.target_solids.is_empty()
            && self.targets.chains.is_empty()
            && !self.strategy.is_turning()
            && !self.strategy.is_targetless()
        {
            return Err("an enabled operation needs at least one target body".into());
        }
        if let Some(axis) = self.tool_axis {
            if axis.normalized().is_none() {
                return Err("tool axis must be a non-zero vector".into());
            }
        }
        if let Some(f) = &self.feeds {
            f.validate()?;
        }
        if let Some(h) = &self.heights {
            h.validate()?;
        }
        self.stock_to_leave.validate()?;
        self.linking.validate()?;
        self.polish.validate()?;
        self.strategy.validate()
    }
}

/// One enum-valued JSON field: its dotted path and the names it may take.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct EnumChoice {
    pub path: &'static str,
    pub variants: &'static [&'static str],
}

impl Operation {
    /// Enum-valued fields of an operation's JSON outside `strategy`.
    pub const ENUM_CHOICES: &'static [EnumChoice] = &[
        EnumChoice { path: "linking.retractPolicy", variants: RetractPolicy::VARIANTS },
        EnumChoice { path: "linking.leadIn.kind", variants: LeadKind::VARIANTS },
        EnumChoice { path: "linking.leadOut.kind", variants: LeadKind::VARIANTS },
        EnumChoice { path: "linking.ramp.kind", variants: RampKind::VARIANTS },
        EnumChoice { path: "feeds.spindleDirection", variants: super::SpindleDirection::VARIANTS },
        EnumChoice { path: "targets.chains[].source.kind", variants: super::ChainSource::VARIANTS },
        EnumChoice { path: "targets.chains[].side", variants: super::ChainSide::VARIANTS },
    ];
}

/// Feed optimisation and smoothing of a generated toolpath (lane C18).
/// Disabled settings are inert: their values are not validated.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct PolishParams {
    pub feed_optimization: FeedOptimizationParams,
    pub smoothing: SmoothingParams,
}

impl PolishParams {
    pub fn validate(&self) -> Result<(), String> {
        self.feed_optimization.validate()?;
        self.smoothing.validate()
    }
}

/// Slow the feed at corners (travel direction change above `max_angle_deg`,
/// either turn sense) and through arcs of radius up to `max_radius`, over
/// `distance` of path length on either side, to `reduced_feed_ratio` of the
/// cutting feed.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct FeedOptimizationParams {
    pub enabled: bool,
    pub max_angle_deg: f64,
    pub max_radius: f64,
    pub reduced_feed_ratio: f64,
    pub distance: f64,
}

impl Default for FeedOptimizationParams {
    fn default() -> Self {
        Self { enabled: false, max_angle_deg: 30.0, max_radius: 2.0, reduced_feed_ratio: 0.5, distance: 2.0 }
    }
}

impl FeedOptimizationParams {
    pub fn validate(&self) -> Result<(), String> {
        if !self.enabled {
            return Ok(());
        }
        if !self.max_angle_deg.is_finite() || self.max_angle_deg < 0.0 || self.max_angle_deg > 180.0 {
            return Err("feed optimisation angle must be finite and within [0, 180] degrees".into());
        }
        positive_or_zero("feed optimisation radius", self.max_radius)?;
        if !self.reduced_feed_ratio.is_finite() || self.reduced_feed_ratio <= 0.0 || self.reduced_feed_ratio > 1.0 {
            return Err("reduced feed ratio must be within (0, 1]".into());
        }
        positive_or_zero("feed optimisation distance", self.distance)
    }
}

/// Smooth the path within `tolerance`.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct SmoothingParams {
    pub enabled: bool,
    pub tolerance: f64,
}

impl Default for SmoothingParams {
    fn default() -> Self {
        Self { enabled: false, tolerance: 0.01 }
    }
}

impl SmoothingParams {
    pub fn validate(&self) -> Result<(), String> {
        if self.enabled {
            positive("smoothing tolerance", self.tolerance)?;
        }
        Ok(())
    }
}

/// Material intentionally left for a later operation.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct StockToLeave {
    pub radial: f64,
    pub axial: f64,
}

impl StockToLeave {
    pub fn validate(&self) -> Result<(), String> {
        if !self.radial.is_finite() || !self.axial.is_finite() {
            return Err("stock to leave is not finite".into());
        }
        Ok(())
    }
}

/// How passes are connected and how the tool enters and leaves material.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct LinkingParams {
    pub retract_policy: RetractPolicy,
    /// Links shorter than this stay down at feed instead of retracting.
    pub keep_down_distance: f64,
    pub lead_in: Lead,
    pub lead_out: Lead,
    pub ramp: Ramp,
    /// Fit arcs (G2/G3) to the output where the path allows.
    pub fit_arcs: bool,
    /// Smooth the path within this deviation (`0`: off).
    pub smoothing_tolerance: f64,
    /// Minimum distance kept above checked material by minimum retracts and feed heights.
    pub safe_distance: f64,
}

impl Default for LinkingParams {
    fn default() -> Self {
        Self {
            retract_policy: RetractPolicy::MinimumRetract,
            keep_down_distance: 0.0,
            lead_in: Lead::default(),
            lead_out: Lead::default(),
            ramp: Ramp::default(),
            fit_arcs: true,
            smoothing_tolerance: 0.0,
            safe_distance: 2.0,
        }
    }
}

impl LinkingParams {
    pub fn validate(&self) -> Result<(), String> {
        if !self.keep_down_distance.is_finite() || self.keep_down_distance < 0.0 {
            return Err("keep-down distance must be finite and non-negative".into());
        }
        if !self.smoothing_tolerance.is_finite() || self.smoothing_tolerance < 0.0 {
            return Err("smoothing tolerance must be finite and non-negative".into());
        }
        if !self.safe_distance.is_finite() || self.safe_distance < 0.0 {
            return Err("safe distance must be finite and non-negative".into());
        }
        self.lead_in.validate()?;
        self.lead_out.validate()?;
        self.ramp.validate()
    }
}

/// Where the tool retracts to between passes.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum RetractPolicy {
    /// Always up to the clearance height.
    FullRetract,
    /// Up to the retract height.
    RetractHeight,
    /// Only as high as needed to clear checked material (plus feed height).
    #[default]
    MinimumRetract,
}

impl RetractPolicy {
    /// Serde names of every variant, in declaration order.
    pub const VARIANTS: &'static [&'static str] = &["full-retract", "retract-height", "minimum-retract"];
}

/// Lead-in / lead-out geometry.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct Lead {
    pub kind: LeadKind,
    /// Arc radius or linear length.
    pub radius: f64,
    /// Arc sweep in degrees (arc leads).
    pub sweep_deg: f64,
    /// Straight extension before the arc.
    pub linear_length: f64,
    /// Vertical component of the lead.
    pub vertical_radius: f64,
}

impl Default for Lead {
    fn default() -> Self {
        Self { kind: LeadKind::None, radius: 0.0, sweep_deg: 90.0, linear_length: 0.0, vertical_radius: 0.0 }
    }
}

impl Lead {
    pub fn validate(&self) -> Result<(), String> {
        for (name, v) in [
            ("lead radius", self.radius),
            ("lead sweep", self.sweep_deg),
            ("lead linear length", self.linear_length),
            ("lead vertical radius", self.vertical_radius),
        ] {
            if !v.is_finite() || v < 0.0 {
                return Err(format!("{name} must be finite and non-negative"));
            }
        }
        Ok(())
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum LeadKind {
    #[default]
    None,
    Arc,
    Linear,
    Vertical,
}

impl LeadKind {
    /// Serde names of every variant, in declaration order.
    pub const VARIANTS: &'static [&'static str] = &["none", "arc", "linear", "vertical"];
}

/// How the tool descends into material.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct Ramp {
    pub kind: RampKind,
    /// Maximum ramp angle in degrees.
    pub angle_deg: f64,
    /// Helix diameter (helix ramps).
    pub helix_diameter: f64,
    /// Clearance above the floor where the ramp levels out.
    pub clearance: f64,
}

impl Default for Ramp {
    fn default() -> Self {
        Self { kind: RampKind::Plunge, angle_deg: 2.0, helix_diameter: 0.0, clearance: 0.0 }
    }
}

impl Ramp {
    pub fn validate(&self) -> Result<(), String> {
        if !self.angle_deg.is_finite() || self.angle_deg < 0.0 || self.angle_deg > 90.0 {
            return Err("ramp angle must be between 0 and 90 degrees".into());
        }
        if !self.helix_diameter.is_finite() || self.helix_diameter < 0.0 {
            return Err("helix diameter must be finite and non-negative".into());
        }
        if !self.clearance.is_finite() || self.clearance < 0.0 {
            return Err("ramp clearance must be finite and non-negative".into());
        }
        Ok(())
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum RampKind {
    #[default]
    Plunge,
    Helix,
    Profile,
    ZigZag,
    Smooth,
}

impl RampKind {
    /// Serde names of every variant, in declaration order.
    pub const VARIANTS: &'static [&'static str] = &["plunge", "helix", "profile", "zig-zag", "smooth"];
}

/// Depth control shared by 2.5D and roughing strategies. Heights are MACHINE Z
/// unless `relative_to_stock` is set.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct Depths {
    /// Top of cut (`None`: stock top).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top: Option<f64>,
    /// Bottom of cut (`None`: model bottom / selected floor).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bottom: Option<f64>,
    /// Maximum roughing step-down (`0`: one depth).
    pub step_down: f64,
    /// Finishing step-down applied to the last `finishing_passes` depths.
    pub finishing_step_down: f64,
    pub finishing_passes: u32,
    /// Order: every depth of one region before moving on (`true`) or every
    /// region at one depth before the next depth (`false`).
    pub depth_first: bool,
}

impl Default for Depths {
    fn default() -> Self {
        Self {
            top: None,
            bottom: None,
            step_down: 0.0,
            finishing_step_down: 0.0,
            finishing_passes: 0,
            depth_first: true,
        }
    }
}

impl Depths {
    pub fn validate(&self) -> Result<(), String> {
        for (name, v) in [("step down", self.step_down), ("finishing step down", self.finishing_step_down)] {
            if !v.is_finite() || v < 0.0 {
                return Err(format!("{name} must be finite and non-negative"));
            }
        }
        if let (Some(t), Some(b)) = (self.top, self.bottom) {
            if !t.is_finite() || !b.is_finite() || t < b {
                return Err("top of cut must be above bottom of cut".into());
            }
        }
        Ok(())
    }
}

/// Radial stepover between adjacent passes.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct Stepover {
    /// Distance between passes in model units.
    pub distance: f64,
    /// Climb (default) or conventional milling.
    pub direction: CutDirection,
}

impl Default for Stepover {
    fn default() -> Self {
        Self { distance: 1.0, direction: CutDirection::Climb }
    }
}

impl Stepover {
    pub fn validate(&self) -> Result<(), String> {
        if !self.distance.is_finite() || self.distance <= 0.0 {
            return Err("stepover must be finite and positive".into());
        }
        Ok(())
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum CutDirection {
    #[default]
    Climb,
    Conventional,
    /// Alternate every pass (zig-zag).
    Either,
}

impl CutDirection {
    /// Serde names of every variant, in declaration order.
    pub const VARIANTS: &'static [&'static str] = &["climb", "conventional", "either"];
}

/// Which way a boundary limits the toolpath.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ContainmentMode {
    /// Tool centre stays inside the boundary.
    #[default]
    ToolCenterInside,
    /// Tool stays fully inside the boundary.
    ToolInside,
    /// Tool may reach outside until its far side touches the boundary.
    ToolOutside,
}

impl ContainmentMode {
    /// Serde names of every variant, in declaration order.
    pub const VARIANTS: &'static [&'static str] = &["tool-center-inside", "tool-inside", "tool-outside"];
}

/// Region limits for 3D strategies.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct Containment {
    /// Closed 2D boundary loops in machine XY (`empty`: the target silhouette).
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub boundary: Vec<Vec<super::Point2>>,
    pub mode: ContainmentMode,
    /// Extra offset applied to the boundary (positive grows it).
    pub offset: f64,
    /// Only machine surface slopes within this range, degrees from horizontal.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub slope_deg: Option<[f64; 2]>,
    /// Only machine between these machine Z levels.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub z_range: Option<[f64; 2]>,
}

impl Containment {
    pub fn validate(&self) -> Result<(), String> {
        if !self.offset.is_finite() {
            return Err("containment offset is not finite".into());
        }
        if let Some([lo, hi]) = self.slope_deg {
            if !lo.is_finite() || !hi.is_finite() || lo < 0.0 || hi > 90.0 || lo > hi {
                return Err("slope range must lie within 0..90 degrees".into());
            }
        }
        if let Some([lo, hi]) = self.z_range {
            if !lo.is_finite() || !hi.is_finite() || lo > hi {
                return Err("z range must have min <= max".into());
            }
        }
        for loop_ in &self.boundary {
            if loop_.len() < 3 {
                return Err("a containment boundary loop needs at least three points".into());
            }
            if loop_.iter().any(|p| !p.is_finite()) {
                return Err("a containment boundary point is not finite".into());
            }
        }
        Ok(())
    }
}

/// Drilling cycle selection.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum DrillCycle {
    /// Simple drilling (G81).
    #[default]
    Drill,
    /// Drilling with dwell at depth (G82).
    DrillDwell,
    /// Deep drilling with full retract (G83).
    Peck,
    /// Chip breaking with short retract (G73).
    ChipBreak,
    /// Rigid tapping (G84 with synchronous feed).
    TapRigid,
    /// Floating-holder tapping (G84).
    TapFloat,
    /// Reaming: feed in, feed out (G85).
    Ream,
    /// Boring with spindle stop and shift (G76).
    BoreFine,
    /// Boring, stop spindle at bottom, rapid out (G86).
    Bore,
    /// Back boring (G87).
    BackBore,
    /// Boring with dwell and feed out (G89).
    BoreDwell,
}

impl DrillCycle {
    /// Serde names of every variant, in declaration order.
    pub const VARIANTS: &'static [&'static str] = &["drill", "drill-dwell", "peck", "chip-break", "tap-rigid", "tap-float", "ream", "bore-fine", "bore", "back-bore", "bore-dwell"];
}

/// The strategy and its parameters. Variants beyond the 3D-finishing core
/// start with the parameters a first implementation needs; lanes extend them.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase")]
#[non_exhaustive]
pub enum Strategy {
    // --- 2D / 2.5D ---
    Face(FaceParams),
    Pocket2D(Pocket2dParams),
    Contour2D(Contour2dParams),
    Adaptive2D(Adaptive2dParams),
    Slot(SlotParams),
    Trace(TraceParams),
    Chamfer2D(Chamfer2dParams),
    Engrave(EngraveParams),
    Bore(BoreParams),
    Circular(CircularParams),
    ThreadMill(ThreadMillParams),
    // --- Drilling ---
    Drill(DrillParams),
    // --- 3D roughing ---
    Adaptive3D(Adaptive3dParams),
    Pocket3D(Pocket3dParams),
    // --- 3D finishing ---
    Parallel(ParallelParams),
    Contour3D(Contour3dParams),
    Scallop(ScallopParams),
    Pencil(PencilParams),
    SteepShallow(SteepShallowParams),
    Radial(RadialParams),
    Spiral(SpiralParams),
    MorphedSpiral(MorphedSpiralParams),
    Horizontal(HorizontalParams),
    Ramp(RampFinishParams),
    Project(ProjectParams),
    Flow(FlowParams),
    Morph(MorphParams),
    RestFinish(RestFinishParams),
    /// Probing a work offset (lane C14).
    ProbeWcs(super::ProbeWcsParams),
    /// Probing geometry for inspection (lane C14).
    ProbeGeometry(super::ProbeGeometryParams),
    /// Pattern of other enabled operations: linear, circular, mirror or WCS offsets (lane C15).
    Pattern(PatternParams),
    /// A manual NC entry: stop, dwell, comment, pass-through text or tool change (lane C15).
    ManualNc(ManualNcParams),
    // --- Multi-axis ---
    Swarf(SwarfParams),
    MultiAxisContour(MultiAxisContourParams),
    RotaryWrap(RotaryWrapParams),
    /// Rotary finishing: the tool axis radial to a 4th axis, line or spiral passes (lane C10).
    RotaryFinish(RotaryFinishParams),
    /// Multi-axis finishing over an inner 3-axis finishing operation (lane C10b).
    MultiAxisFinish(MultiAxisFinishParams),
    // --- Turning ---
    TurnFace(TurnFaceParams),
    TurnProfile(TurnProfileParams),
    TurnGroove(TurnGrooveParams),
    TurnThread(TurnThreadParams),
    TurnPartOff(TurnPartOffParams),
}

impl Default for Strategy {
    fn default() -> Self {
        Strategy::Parallel(ParallelParams::default())
    }
}

impl Strategy {
    /// Every strategy's kind name (the serde tag), in declaration order. The
    /// contract the application lists strategies from; `kind_name` reads it.
    pub const KINDS: &'static [&'static str] = &[
        "face",
        "pocket2D",
        "contour2D",
        "adaptive2D",
        "slot",
        "trace",
        "chamfer2D",
        "engrave",
        "bore",
        "circular",
        "threadMill",
        "drill",
        "adaptive3D",
        "pocket3D",
        "parallel",
        "contour3D",
        "scallop",
        "pencil",
        "steepShallow",
        "radial",
        "spiral",
        "morphedSpiral",
        "horizontal",
        "ramp",
        "project",
        "flow",
        "morph",
        "restFinish",
        "probeWcs",
        "probeGeometry",
        "pattern",
        "manualNc",
        "swarf",
        "multiAxisContour",
        "rotaryWrap",
        "rotaryFinish",
        "multiAxisFinish",
        "turnFace",
        "turnProfile",
        "turnGroove",
        "turnThread",
        "turnPartOff",
    ];

    /// Position of this variant in [`Strategy::KINDS`].
    pub fn kind_index(&self) -> usize {
        match self {
            Strategy::Face(_) => 0,
            Strategy::Pocket2D(_) => 1,
            Strategy::Contour2D(_) => 2,
            Strategy::Adaptive2D(_) => 3,
            Strategy::Slot(_) => 4,
            Strategy::Trace(_) => 5,
            Strategy::Chamfer2D(_) => 6,
            Strategy::Engrave(_) => 7,
            Strategy::Bore(_) => 8,
            Strategy::Circular(_) => 9,
            Strategy::ThreadMill(_) => 10,
            Strategy::Drill(_) => 11,
            Strategy::Adaptive3D(_) => 12,
            Strategy::Pocket3D(_) => 13,
            Strategy::Parallel(_) => 14,
            Strategy::Contour3D(_) => 15,
            Strategy::Scallop(_) => 16,
            Strategy::Pencil(_) => 17,
            Strategy::SteepShallow(_) => 18,
            Strategy::Radial(_) => 19,
            Strategy::Spiral(_) => 20,
            Strategy::MorphedSpiral(_) => 21,
            Strategy::Horizontal(_) => 22,
            Strategy::Ramp(_) => 23,
            Strategy::Project(_) => 24,
            Strategy::Flow(_) => 25,
            Strategy::Morph(_) => 26,
            Strategy::RestFinish(_) => 27,
            Strategy::ProbeWcs(_) => 28,
            Strategy::ProbeGeometry(_) => 29,
            Strategy::Pattern(_) => 30,
            Strategy::ManualNc(_) => 31,
            Strategy::Swarf(_) => 32,
            Strategy::MultiAxisContour(_) => 33,
            Strategy::RotaryWrap(_) => 34,
            Strategy::RotaryFinish(_) => 35,
            Strategy::MultiAxisFinish(_) => 36,
            Strategy::TurnFace(_) => 37,
            Strategy::TurnProfile(_) => 38,
            Strategy::TurnGroove(_) => 39,
            Strategy::TurnThread(_) => 40,
            Strategy::TurnPartOff(_) => 41,
        }
    }

    /// Stable lowercase name of the strategy (the serde tag).
    pub fn kind_name(&self) -> &'static str {
        Self::KINDS[self.kind_index()]
    }

    /// Enum-valued fields of the strategy kind `kind`: the dotted JSON path of
    /// every string-tagged field and the names it may take. Variant-dependent
    /// payload fields are listed too where a nested tag selects them.
    pub fn enum_choices(kind: &str) -> &'static [EnumChoice] {
        const STEPOVER: EnumChoice = EnumChoice { path: "stepover.direction", variants: CutDirection::VARIANTS };
        const DIRECTION: EnumChoice = EnumChoice { path: "direction", variants: CutDirection::VARIANTS };
        const CONTAINMENT: EnumChoice = EnumChoice { path: "containment.mode", variants: ContainmentMode::VARIANTS };
        match kind {
            "face" | "pocket2D" | "circular" => &[STEPOVER],
            "contour2D" => &[DIRECTION],
            "adaptive2D" => &[DIRECTION, EnumChoice { path: "restSource", variants: RestSource::VARIANTS }],
            "slot" | "trace" | "chamfer2D" | "engrave" | "bore" | "threadMill" | "swarf" | "turnFace" | "turnGroove"
            | "turnThread" | "turnPartOff" | "probeGeometry" => &[],
            "drill" => &[EnumChoice { path: "cycle", variants: DrillCycle::VARIANTS }],
            "pencil" | "radial" | "project" | "multiAxisContour" => &[CONTAINMENT],
            "adaptive3D" => &[CONTAINMENT, EnumChoice { path: "restSource", variants: RestSource::VARIANTS }],
            "parallel" | "scallop" | "spiral" | "morphedSpiral" | "horizontal" | "flow" | "morph" => {
                &[STEPOVER, CONTAINMENT]
            }
            "pocket3D" => &[STEPOVER, CONTAINMENT, EnumChoice { path: "restSource", variants: RestSource::VARIANTS }],
            "restFinish" => &[STEPOVER, CONTAINMENT, EnumChoice { path: "source", variants: RestSource::VARIANTS }],
            "contour3D" | "ramp" => &[DIRECTION, CONTAINMENT],
            "steepShallow" => &[EnumChoice { path: "shallowStepover.direction", variants: CutDirection::VARIANTS }, CONTAINMENT],
            "rotaryWrap" | "rotaryFinish" => &[STEPOVER],
            "multiAxisFinish" => &[EnumChoice { path: "mode.kind", variants: AxisMode::VARIANTS }],
            "probeWcs" => &[EnumChoice { path: "probeKind", variants: super::ProbeKind::VARIANTS }],
            "pattern" => &[
                EnumChoice { path: "patternKind.kind", variants: PatternKind::VARIANTS },
                EnumChoice { path: "order", variants: PatternOrder::VARIANTS },
            ],
            "manualNc" => &[EnumChoice { path: "manualKind.kind", variants: ManualNcKind::VARIANTS }],
            "turnProfile" => &[EnumChoice { path: "direction", variants: RoughingDirection::VARIANTS }],
            _ => &[],
        }
    }

    /// The default parameters of the strategy named `kind`, or `None` for an
    /// unknown name. The returned value may still fail `validate` where a
    /// strategy needs user input (morph curves, a reference tool).
    pub fn default_of(kind: &str) -> Option<Strategy> {
        Some(match kind {
            "face" => Strategy::Face(Default::default()),
            "pocket2D" => Strategy::Pocket2D(Default::default()),
            "contour2D" => Strategy::Contour2D(Default::default()),
            "adaptive2D" => Strategy::Adaptive2D(Default::default()),
            "slot" => Strategy::Slot(Default::default()),
            "trace" => Strategy::Trace(Default::default()),
            "chamfer2D" => Strategy::Chamfer2D(Default::default()),
            "engrave" => Strategy::Engrave(Default::default()),
            "bore" => Strategy::Bore(Default::default()),
            "circular" => Strategy::Circular(Default::default()),
            "threadMill" => Strategy::ThreadMill(Default::default()),
            "drill" => Strategy::Drill(Default::default()),
            "adaptive3D" => Strategy::Adaptive3D(Default::default()),
            "pocket3D" => Strategy::Pocket3D(Default::default()),
            "parallel" => Strategy::Parallel(Default::default()),
            "contour3D" => Strategy::Contour3D(Default::default()),
            "scallop" => Strategy::Scallop(Default::default()),
            "pencil" => Strategy::Pencil(Default::default()),
            "steepShallow" => Strategy::SteepShallow(Default::default()),
            "radial" => Strategy::Radial(Default::default()),
            "spiral" => Strategy::Spiral(Default::default()),
            "morphedSpiral" => Strategy::MorphedSpiral(Default::default()),
            "horizontal" => Strategy::Horizontal(Default::default()),
            "ramp" => Strategy::Ramp(Default::default()),
            "project" => Strategy::Project(Default::default()),
            "flow" => Strategy::Flow(Default::default()),
            "morph" => Strategy::Morph(Default::default()),
            "restFinish" => Strategy::RestFinish(Default::default()),
            "probeWcs" => Strategy::ProbeWcs(Default::default()),
            "probeGeometry" => Strategy::ProbeGeometry(Default::default()),
            "pattern" => Strategy::Pattern(Default::default()),
            "manualNc" => Strategy::ManualNc(Default::default()),
            "swarf" => Strategy::Swarf(Default::default()),
            "multiAxisContour" => Strategy::MultiAxisContour(Default::default()),
            "rotaryWrap" => Strategy::RotaryWrap(Default::default()),
            "rotaryFinish" => Strategy::RotaryFinish(Default::default()),
            "multiAxisFinish" => Strategy::MultiAxisFinish(Default::default()),
            "turnFace" => Strategy::TurnFace(Default::default()),
            "turnProfile" => Strategy::TurnProfile(Default::default()),
            "turnGroove" => Strategy::TurnGroove(Default::default()),
            "turnThread" => Strategy::TurnThread(Default::default()),
            "turnPartOff" => Strategy::TurnPartOff(Default::default()),
            _ => return None,
        })
    }

    /// Strategies that machine nothing themselves and need no target body:
    /// patterns of other operations and manual NC entries.
    pub fn is_targetless(&self) -> bool {
        matches!(self, Strategy::Pattern(_) | Strategy::ManualNc(_))
    }

    pub fn is_turning(&self) -> bool {
        matches!(
            self,
            Strategy::TurnFace(_)
                | Strategy::TurnProfile(_)
                | Strategy::TurnGroove(_)
                | Strategy::TurnThread(_)
                | Strategy::TurnPartOff(_)
        )
    }

    pub fn validate(&self) -> Result<(), String> {
        match self {
            Strategy::Face(p) => {
                p.depths.validate()?;
                p.stepover.validate()
            }
            Strategy::Pocket2D(p) => {
                p.depths.validate()?;
                p.stepover.validate()
            }
            Strategy::Contour2D(p) => {
                p.depths.validate()?;
                positive_or_zero("finishing overlap", p.finishing_overlap)?;
                positive_or_zero("roughing stepover", p.roughing_stepover)
            }
            Strategy::Adaptive2D(p) => {
                p.depths.validate()?;
                positive("optimal load", p.optimal_load)?;
                positive_or_zero("minimum cutting radius", p.minimum_cutting_radius)
            }
            Strategy::Slot(p) => {
                p.depths.validate()?;
                positive("slot width", p.width)
            }
            Strategy::Trace(p) => p.depths.validate(),
            Strategy::Chamfer2D(p) => {
                positive("chamfer width", p.width)?;
                positive_or_zero("chamfer tip offset", p.tip_offset)
            }
            Strategy::Engrave(p) => p.depths.validate(),
            Strategy::Bore(p) => {
                p.depths.validate()?;
                positive_or_zero("bore pitch", p.pitch)
            }
            Strategy::Circular(p) => {
                p.depths.validate()?;
                p.stepover.validate()
            }
            Strategy::ThreadMill(p) => {
                p.depths.validate()?;
                positive("thread pitch", p.pitch)?;
                positive("thread diameter", p.diameter)
            }
            Strategy::Drill(p) => {
                positive_or_zero("peck depth", p.peck_depth)?;
                positive_or_zero("dwell", p.dwell_seconds)?;
                positive_or_zero("break-through depth", p.break_through)?;
                positive_or_zero("chip-break retract", p.chip_break_retract)?;
                positive_or_zero("bore shift", p.bore_shift)?;
                positive_or_zero("back-bore clearance", p.back_bore_clearance)?;
                if p.cycle == DrillCycle::ChipBreak {
                    positive("chip-break retract", p.chip_break_retract)?;
                }
                if p.cycle == DrillCycle::BackBore {
                    positive("back-bore clearance", p.back_bore_clearance)?;
                }
                Ok(())
            }
            Strategy::Adaptive3D(p) => {
                p.depths.validate()?;
                positive("optimal load", p.optimal_load)?;
                positive_or_zero("fine step down", p.fine_step_down)
            }
            Strategy::Pocket3D(p) => {
                p.depths.validate()?;
                p.stepover.validate()
            }
            Strategy::Parallel(p) => {
                p.stepover.validate()?;
                p.containment.validate()?;
                finite("pass angle", p.pass_angle_deg)
            }
            Strategy::Contour3D(p) => {
                p.containment.validate()?;
                positive("step down", p.step_down)
            }
            Strategy::Scallop(p) => {
                p.stepover.validate()?;
                if let Some(h) = p.cusp_height {
                    positive("cusp height", h)?;
                }
                p.containment.validate()
            }
            Strategy::Pencil(p) => {
                p.containment.validate()?;
                positive_or_zero("pencil bitangency angle", p.bitangency_angle_deg)
            }
            Strategy::SteepShallow(p) => {
                p.containment.validate()?;
                positive("steep step down", p.steep_step_down)?;
                p.shallow_stepover.validate()?;
                positive_or_zero("steep/shallow overlap", p.overlap)?;
                finite("threshold angle", p.threshold_angle_deg)
            }
            Strategy::Radial(p) => {
                p.containment.validate()?;
                positive("radial angular step", p.angular_step_deg)
            }
            Strategy::Spiral(p) => {
                p.containment.validate()?;
                p.stepover.validate()
            }
            Strategy::MorphedSpiral(p) => {
                p.containment.validate()?;
                p.stepover.validate()
            }
            Strategy::Horizontal(p) => {
                p.containment.validate()?;
                p.stepover.validate()
            }
            Strategy::Ramp(p) => {
                p.containment.validate()?;
                positive("ramp step down", p.step_down)
            }
            Strategy::Project(p) => p.containment.validate(),
            Strategy::Flow(p) => {
                p.containment.validate()?;
                p.stepover.validate()
            }
            Strategy::Morph(p) => {
                p.containment.validate()?;
                p.stepover.validate()?;
                for curve in [&p.first_curve, &p.second_curve] {
                    if curve.len() < 2 || !curve.iter().all(super::Point2::is_finite) {
                        return Err("morph needs two finite boundary polylines".into());
                    }
                }
                Ok(())
            }
            Strategy::RestFinish(p) => {
                p.containment.validate()?;
                p.stepover.validate()?;
                positive_or_zero("rest threshold", p.minimum_rest)?;
                if !p.pass_angle_deg.is_finite() || (p.source == RestSource::ReferenceTool && p.reference_tool.0.is_empty()) {
                    return Err("rest finishing needs a reference tool and finite pass angle".into());
                }
                Ok(())
            }
            Strategy::ProbeWcs(p) => p.validate(),
            Strategy::ProbeGeometry(p) => p.validate(),
            Strategy::Pattern(p) => p.validate(),
            Strategy::ManualNc(p) => p.validate(),
            Strategy::Swarf(p) => positive_or_zero("swarf tilt", p.tilt_deg),
            Strategy::MultiAxisContour(p) => {
                p.containment.validate()?;
                positive_or_zero("lead angle", p.lead_deg)?;
                positive_or_zero("tilt angle", p.tilt_deg)
            }
            Strategy::RotaryWrap(p) => {
                p.stepover.validate()?;
                positive("wrap diameter", p.wrap_diameter)
            }
            Strategy::RotaryFinish(p) => p.validate(),
            Strategy::MultiAxisFinish(p) => p.validate(),
            Strategy::TurnFace(p) => {
                positive("step over", p.step_over)?;
                positive_or_zero("facing end x", p.end_x)?;
                positive_or_zero("finish allowance", p.finish_allowance)
            }
            Strategy::TurnProfile(p) => positive("depth of cut", p.depth_of_cut),
            Strategy::TurnGroove(p) => {
                positive("groove width", p.width)?;
                positive_or_zero("groove depth", p.depth)?;
                positive("groove step over", p.step_over)?;
                positive_or_zero("finish allowance", p.finish_allowance)
            }
            Strategy::TurnThread(p) => {
                positive("thread pitch", p.pitch)?;
                positive("thread depth", p.depth)?;
                if p.passes == 0 {
                    return Err("thread passes must be at least 1".into());
                }
                if !p.infeed_angle_deg.is_finite() || p.infeed_angle_deg < 0.0 || p.infeed_angle_deg >= 60.0 {
                    return Err("thread infeed angle must be finite and within [0, 60) degrees".into());
                }
                Ok(())
            }
            Strategy::TurnPartOff(p) => {
                positive_or_zero("part-off x", p.end_x)?;
                positive_or_zero("part-off chamfer", p.chamfer)?;
                positive_or_zero("peck depth", p.peck_depth)?;
                positive_or_zero("peck retract", p.peck_retract)?;
                positive_or_zero("reduced feed radius", p.reduced_feed_radius)?;
                if !p.reduced_feed_factor.is_finite() || p.reduced_feed_factor <= 0.0 || p.reduced_feed_factor > 1.0 {
                    return Err("reduced feed factor must be within (0, 1]".into());
                }
                Ok(())
            }
        }
    }
}

fn finite(name: &str, v: f64) -> Result<(), String> {
    if v.is_finite() {
        Ok(())
    } else {
        Err(format!("{name} is not finite"))
    }
}

fn positive(name: &str, v: f64) -> Result<(), String> {
    if v.is_finite() && v > 0.0 {
        Ok(())
    } else {
        Err(format!("{name} must be finite and positive"))
    }
}

fn positive_or_zero(name: &str, v: f64) -> Result<(), String> {
    if v.is_finite() && v >= 0.0 {
        Ok(())
    } else {
        Err(format!("{name} must be finite and non-negative"))
    }
}

// ---------------------------------------------------------------------------
// 2D / 2.5D
// ---------------------------------------------------------------------------

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct FaceParams {
    pub depths: Depths,
    pub stepover: Stepover,
    /// Pass direction in degrees from machine +X.
    pub pass_angle_deg: f64,
    /// Extend passes past the stock edge by this much.
    pub pass_extension: f64,
    pub zig_zag: bool,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct Pocket2dParams {
    pub depths: Depths,
    pub stepover: Stepover,
    /// Offset pattern (true) or morphed spiral (false).
    pub offset_pattern: bool,
    /// Run a finishing wall pass at each depth.
    pub wall_finish: bool,
    /// Islands are kept (true) or cut through (false).
    pub keep_islands: bool,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct Contour2dParams {
    pub depths: Depths,
    /// Tool stays outside the chain (true) or inside (false).
    pub outside: bool,
    /// Number of radial roughing passes before the finishing pass.
    pub roughing_passes: u32,
    pub roughing_stepover: f64,
    /// Repeat the finishing pass this many times.
    pub finishing_passes: u32,
    /// Overlap at the start/end of a closed contour.
    pub finishing_overlap: f64,
    pub tabs: Vec<Tab>,
    pub direction: CutDirection,
}

impl Default for Contour2dParams {
    fn default() -> Self {
        Self {
            depths: Depths::default(),
            outside: true,
            roughing_passes: 0,
            roughing_stepover: 0.0,
            finishing_passes: 1,
            finishing_overlap: 0.0,
            tabs: Vec::new(),
            direction: CutDirection::Climb,
        }
    }
}

/// A holding tab left on a contour.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct Tab {
    /// Position along the chain as a fraction of its length.
    pub at: f64,
    pub width: f64,
    pub height: f64,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct Adaptive2dParams {
    pub depths: Depths,
    /// Target radial engagement (constant-engagement roughing).
    pub optimal_load: f64,
    pub minimum_cutting_radius: f64,
    /// Use the rest material of previous operations as the starting stock.
    pub rest_machining: bool,
    /// Where the rest material comes from when `rest_machining` is on.
    pub rest_source: RestSource,
    pub direction: CutDirection,
}

impl Default for Adaptive2dParams {
    fn default() -> Self {
        Self {
            depths: Depths::default(),
            optimal_load: 1.0,
            minimum_cutting_radius: 0.0,
            rest_machining: false,
            rest_source: RestSource::default(),
            direction: CutDirection::Climb,
        }
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct SlotParams {
    pub depths: Depths,
    /// Slot width; the tool diameter when equal.
    pub width: f64,
    pub trochoidal: bool,
}

impl Default for SlotParams {
    fn default() -> Self {
        Self { depths: Depths::default(), width: 6.0, trochoidal: false }
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct TraceParams {
    pub depths: Depths,
    /// Tool centre on the chain (0), left (-1) or right (+1) of it.
    pub side: i8,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct Chamfer2dParams {
    /// Chamfer leg length.
    pub width: f64,
    /// Keep the tool tip this far below the chamfer's lower edge.
    pub tip_offset: f64,
}

impl Default for Chamfer2dParams {
    fn default() -> Self {
        Self { width: 1.0, tip_offset: 0.5 }
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct EngraveParams {
    pub depths: Depths,
    /// V-carve: depth follows the region width.
    pub v_carve: bool,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct BoreParams {
    pub depths: Depths,
    /// Helical pitch per revolution (`0`: circular passes at each depth).
    pub pitch: f64,
    pub finish_pass: bool,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct CircularParams {
    pub depths: Depths,
    pub stepover: Stepover,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct ThreadMillParams {
    pub depths: Depths,
    pub pitch: f64,
    /// Major diameter of the thread.
    pub diameter: f64,
    pub internal: bool,
    pub left_hand: bool,
    pub passes: u32,
}

impl Default for ThreadMillParams {
    fn default() -> Self {
        Self { depths: Depths::default(), pitch: 1.0, diameter: 10.0, internal: true, left_hand: false, passes: 1 }
    }
}

// ---------------------------------------------------------------------------
// Drilling
// ---------------------------------------------------------------------------

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct DrillParams {
    pub cycle: DrillCycle,
    /// Hole depth below the hole top (`None`: through the recognised hole).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub depth: Option<f64>,
    pub peck_depth: f64,
    pub dwell_seconds: f64,
    /// Extra depth past the hole bottom for through holes.
    pub break_through: f64,
    /// Order holes to minimise travel.
    pub optimise_order: bool,
    /// Use the tool's tip (true) or shoulder (false) as the depth reference.
    pub tip_depth: bool,
    /// G73 partial retract distance; must be positive for chip-break.
    pub chip_break_retract: f64,
    /// G76/G87 lateral shift along machine +X.
    pub bore_shift: f64,
    /// G87 axial entry below finished back face (positive distance).
    pub back_bore_clearance: f64,
}

// ---------------------------------------------------------------------------
// 3D roughing
// ---------------------------------------------------------------------------

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct Adaptive3dParams {
    pub depths: Depths,
    pub optimal_load: f64,
    /// Smaller step-down used to clear flat areas between the main depths.
    pub fine_step_down: f64,
    pub rest_machining: bool,
    /// Where the rest material comes from when `rest_machining` is on.
    pub rest_source: RestSource,
    pub containment: Containment,
}

impl Default for Adaptive3dParams {
    fn default() -> Self {
        Self {
            depths: Depths::default(),
            optimal_load: 1.0,
            fine_step_down: 0.0,
            rest_machining: false,
            rest_source: RestSource::default(),
            containment: Containment::default(),
        }
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct Pocket3dParams {
    pub depths: Depths,
    pub stepover: Stepover,
    pub rest_machining: bool,
    /// Where the rest material comes from when `rest_machining` is on.
    pub rest_source: RestSource,
    pub containment: Containment,
}

// ---------------------------------------------------------------------------
// 3D finishing
// ---------------------------------------------------------------------------

/// Parallel (raster) finishing: drop-cutter along parallel lines.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct ParallelParams {
    pub stepover: Stepover,
    /// Pass direction in degrees from machine +X.
    pub pass_angle_deg: f64,
    /// Alternate pass direction (zig-zag) instead of unidirectional passes.
    pub zig_zag: bool,
    /// Spacing of samples along a pass before adaptive refinement.
    pub sample_step: f64,
    /// Refine samples until the chord error is within the operation tolerance.
    pub adaptive_sampling: bool,
    pub containment: Containment,
    /// Lowest Z the cutter may drop to when nothing is below it.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub floor_z: Option<f64>,
}

fn default_sample_step() -> f64 {
    0.5
}

impl Default for ParallelParams {
    fn default() -> Self {
        Self {
            stepover: Stepover::default(),
            pass_angle_deg: 0.0,
            zig_zag: true,
            sample_step: default_sample_step(),
            adaptive_sampling: true,
            containment: Containment::default(),
            floor_z: None,
        }
    }
}

/// Contour / waterline finishing: closed loops at constant Z.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct Contour3dParams {
    pub step_down: f64,
    /// Add intermediate levels where the slope leaves scallops above tolerance.
    /// Off by default until the waterline generator supports it (lane C3).
    pub adaptive_step_down: bool,
    /// Fiber spacing for the push-cutter sweep before adaptive refinement.
    pub fiber_step: f64,
    pub direction: CutDirection,
    pub containment: Containment,
}

impl Default for Contour3dParams {
    fn default() -> Self {
        Self {
            step_down: 1.0,
            adaptive_step_down: false,
            fiber_step: default_sample_step(),
            direction: CutDirection::Climb,
            containment: Containment::default(),
        }
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct ScallopParams {
    pub stepover: Stepover,
    pub containment: Containment,
    /// Keep passes inside-out (true) or outside-in (false).
    pub inside_out: bool,
    /// Maximum cusp (scallop) height in model units. When set it wins over
    /// `stepover`: the stepover is derived from the cutter and this height.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cusp_height: Option<f64>,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct PencilParams {
    pub containment: Containment,
    /// Only trace corners whose bitangency angle is sharper than this.
    pub bitangency_angle_deg: f64,
    /// Extra parallel passes beside the pencil trace.
    pub extra_passes: u32,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct SteepShallowParams {
    pub containment: Containment,
    /// Slope that separates steep (waterline) from shallow (scallop) regions.
    pub threshold_angle_deg: f64,
    pub steep_step_down: f64,
    pub shallow_stepover: Stepover,
    /// Distance the steep and shallow regions overlap across the threshold
    /// slope (Fusion's steep/shallow overlap), in model units.
    pub overlap: f64,
}

impl Default for SteepShallowParams {
    fn default() -> Self {
        Self {
            containment: Containment::default(),
            threshold_angle_deg: 45.0,
            steep_step_down: 1.0,
            shallow_stepover: Stepover::default(),
            overlap: 0.0,
        }
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct RadialParams {
    pub containment: Containment,
    /// Centre of the radial pattern in machine XY.
    pub center: super::Point2,
    pub angular_step_deg: f64,
    pub inner_radius: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub outer_radius: Option<f64>,
}

impl Default for RadialParams {
    fn default() -> Self {
        Self {
            containment: Containment::default(),
            center: super::Point2::default(),
            angular_step_deg: 1.0,
            inner_radius: 0.0,
            outer_radius: None,
        }
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct SpiralParams {
    pub containment: Containment,
    pub center: super::Point2,
    pub stepover: Stepover,
    pub inside_out: bool,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct MorphedSpiralParams {
    pub containment: Containment,
    pub stepover: Stepover,
}

/// Horizontal (flat-area) finishing.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct HorizontalParams {
    pub containment: Containment,
    pub stepover: Stepover,
    /// Treat surfaces within this slope of horizontal as flat.
    pub flat_angle_deg: f64,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct RampFinishParams {
    pub containment: Containment,
    pub step_down: f64,
    pub direction: CutDirection,
}

impl Default for RampFinishParams {
    fn default() -> Self {
        Self { containment: Containment::default(), step_down: 1.0, direction: CutDirection::Climb }
    }
}

/// Project 2D curves onto the surface.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct ProjectParams {
    pub containment: Containment,
    /// Curves to project, as 2D polylines in machine XY.
    pub curves: Vec<Vec<super::Point2>>,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct FlowParams {
    pub containment: Containment,
    pub stepover: Stepover,
    /// Follow the surface's U (true) or V (false) direction.
    pub along_u: bool,
}

/// Boundary curves use the same direction and normalized arc-length correspondence.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct MorphParams {
    pub containment: Containment,
    pub stepover: Stepover,
    pub first_curve: Vec<super::Point2>,
    pub second_curve: Vec<super::Point2>,
}

/// Source of residual material for rest finishing.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum RestSource { #[default] ReferenceTool, Stock }

impl RestSource {
    /// Serde names of every variant, in declaration order.
    pub const VARIANTS: &'static [&'static str] = &["referenceTool", "stock"];
}

/// Reference-tool accessibility or simulated-stock rest finishing.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct RestFinishParams {
    pub source: RestSource,
    pub containment: Containment,
    pub stepover: Stepover,
    pub reference_tool: super::ToolId,
    pub minimum_rest: f64,
    pub pass_angle_deg: f64,
}

// ---------------------------------------------------------------------------
// Multi-axis
// ---------------------------------------------------------------------------

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct SwarfParams {
    /// Extra tilt away from the wall in degrees.
    pub tilt_deg: f64,
    /// Number of depth passes down the wall.
    pub passes: u32,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct MultiAxisContourParams {
    pub containment: Containment,
    pub lead_deg: f64,
    pub tilt_deg: f64,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct RotaryWrapParams {
    pub stepover: Stepover,
    /// Diameter the flat toolpath is wrapped onto.
    pub wrap_diameter: f64,
    /// Rotary axis letter (`A` or `B`).
    pub axis: String,
}

fn default_rotary_axis() -> String {
    "A".to_string()
}

impl Default for RotaryWrapParams {
    fn default() -> Self {
        Self { stepover: Stepover::default(), wrap_diameter: 50.0, axis: default_rotary_axis() }
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct RotaryFinishParams {
    /// Rotary axis letter (`A` about X, `B` about Y).
    pub axis: String,
    /// Diameter the passes start from (the stock), also the stepover's reference.
    pub stock_diameter: f64,
    /// Pass spacing as arc length at the stock diameter (line passes) or
    /// axial advance per turn (spiral).
    pub stepover: Stepover,
    /// One continuous spiral instead of line passes.
    pub spiral: bool,
    /// Alternate the direction of successive line passes.
    pub zig_zag: bool,
    /// Sample spacing along the axis before refinement.
    pub sample_step: f64,
    /// Axial range to machine (`None`: the target mesh's extent along the axis).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub range: Option<[f64; 2]>,
}

impl Default for RotaryFinishParams {
    fn default() -> Self {
        Self { axis: "A".into(), stock_diameter: 50.0, stepover: Stepover::default(), spiral: false, zig_zag: true, sample_step: 1.0, range: None }
    }
}

impl RotaryFinishParams {
    pub fn validate(&self) -> Result<(), String> {
        if !matches!(self.axis.as_str(), "A" | "a" | "B" | "b") {
            return Err("rotary axis must be A or B".into());
        }
        if !self.stock_diameter.is_finite() || self.stock_diameter <= 0.0 {
            return Err("rotary stock diameter must be positive".into());
        }
        if !self.stepover.distance.is_finite() || self.stepover.distance <= 0.0 {
            return Err("rotary stepover must be positive".into());
        }
        if !self.sample_step.is_finite() || self.sample_step <= 0.0 {
            return Err("rotary sample step must be positive".into());
        }
        if let Some([lo, hi]) = self.range {
            if !lo.is_finite() || !hi.is_finite() || lo >= hi {
                return Err("rotary range must be finite with lo < hi".into());
            }
        }
        Ok(())
    }
}

/// How a multi-axis finishing pass orients the tool axis at each contact
/// (lane C10b; the geometric evaluation lives in `multiaxis::orientation`).
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]
pub enum AxisMode {
    /// The surface normal rotated by `lead_deg` about the binormal (forward
    /// positive) and `tilt_deg` about the tangent (left positive).
    Normal {
        #[serde(default)]
        lead_deg: f64,
        #[serde(default)]
        tilt_deg: f64,
    },
    /// Along the line from `point` through the contact (`away: true`) or the
    /// reverse (the tool points at `point`).
    Point { point: Point3, away: bool },
    /// Perpendicular to the line through `origin` with `direction`, through
    /// the contact point, away from (`away: true`) or toward the line.
    Line { origin: Point3, direction: Point3, away: bool },
    /// Constant: `angle_deg` from +Z toward the azimuth `azimuth_deg` (from +X).
    FixedFromVertical { angle_deg: f64, azimuth_deg: f64 },
}

impl AxisMode {
    /// Serde names of every variant, in declaration order.
    pub const VARIANTS: &'static [&'static str] = &["normal", "point", "line", "fixedFromVertical"];
}

impl Default for AxisMode {
    fn default() -> Self {
        AxisMode::Normal { lead_deg: 0.0, tilt_deg: 0.0 }
    }
}

impl AxisMode {
    pub fn validate(&self) -> Result<(), String> {
        match self {
            AxisMode::Normal { lead_deg, tilt_deg } => {
                for (n, v) in [("lead", lead_deg), ("tilt", tilt_deg)] {
                    if !v.is_finite() || v.abs() >= 90.0 {
                        return Err(format!("axis {n} must be within (-90, 90) degrees"));
                    }
                }
                Ok(())
            }
            AxisMode::Point { point, .. } => {
                if !point.is_finite() {
                    return Err("axis point is not finite".into());
                }
                Ok(())
            }
            AxisMode::Line { origin, direction, .. } => {
                if !origin.is_finite() || direction.normalized().is_none() {
                    return Err("axis line needs a finite origin and a non-zero direction".into());
                }
                Ok(())
            }
            AxisMode::FixedFromVertical { angle_deg, azimuth_deg } => {
                if !angle_deg.is_finite() || *angle_deg < 0.0 || *angle_deg >= 90.0 || !azimuth_deg.is_finite() {
                    return Err("fixed axis angle must be in [0, 90) degrees with a finite azimuth".into());
                }
                Ok(())
            }
        }
    }
}

/// Multi-axis finishing over an inner 3-axis finishing operation (lane C10b).
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct MultiAxisFinishParams {
    pub mode: AxisMode,
    /// Largest axis change between consecutive samples, degrees.
    pub max_axis_step_deg: f64,
    /// Largest angle of the axis from +Z, degrees.
    pub max_from_vertical_deg: f64,
    /// Largest extra tilt used to clear the shank/holder or the machine
    /// limits, degrees.
    pub reach_tilt_deg: f64,
    /// What a CL point no tilt within the reach clears does to the path
    /// (absent: `trim`, so documents written before the option read the same).
    #[serde(default, skip_serializing_if = "UnreachablePolicy::is_trim")]
    pub unreachable: UnreachablePolicy,
}

/// A multi-axis finish's handling of CL points where the shank or holder
/// collides at every tilt within the reach.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum UnreachablePolicy {
    /// Break the path there: retract, link and re-enter past them; the
    /// material left is reported (a warning and an empty region per point).
    #[default]
    Trim,
    /// Refuse the operation (its report fails) when any point is unreachable.
    Fail,
}

impl UnreachablePolicy {
    /// Serde names of every variant, in declaration order.
    pub const VARIANTS: &'static [&'static str] = &["trim", "fail"];

    pub fn is_trim(&self) -> bool {
        *self == UnreachablePolicy::Trim
    }
}

impl Default for MultiAxisFinishParams {
    fn default() -> Self {
        Self { mode: AxisMode::default(), max_axis_step_deg: 5.0, max_from_vertical_deg: 60.0, reach_tilt_deg: 30.0, unreachable: UnreachablePolicy::Trim }
    }
}

impl MultiAxisFinishParams {
    pub fn validate(&self) -> Result<(), String> {
        let bad = |m: &str| Err(format!("multi-axis finish: {m}"));
        if let Err(e) = self.mode.validate() {
            return bad(&e);
        }
        if !self.max_axis_step_deg.is_finite() || self.max_axis_step_deg <= 0.0 {
            return bad("axis step must be positive");
        }
        if !self.max_from_vertical_deg.is_finite() || self.max_from_vertical_deg < 0.0 || self.max_from_vertical_deg >= 90.0 {
            return bad("angle from vertical must be in [0, 90)");
        }
        if !self.reach_tilt_deg.is_finite() || self.reach_tilt_deg < 0.0 || self.reach_tilt_deg >= 90.0 {
            return bad("reach tilt must be in [0, 90)");
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Turning
// ---------------------------------------------------------------------------

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct TurnFaceParams {
    pub step_over: f64,
    pub finish_pass: bool,
    /// X (radius) the facing passes end at; `0` faces to centre.
    pub end_x: f64,
    /// Material left for the finishing pass.
    pub finish_allowance: f64,
}

impl Default for TurnFaceParams {
    fn default() -> Self {
        Self { step_over: 1.0, finish_pass: true, end_x: 0.0, finish_allowance: 0.1 }
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct TurnProfileParams {
    pub depth_of_cut: f64,
    pub roughing: bool,
    pub finishing: bool,
    /// Outside diameter (true) or inside/bore (false).
    pub outside: bool,
    /// Roughing pass direction: along the axis (Z) or radial (X plunges).
    pub direction: RoughingDirection,
}

impl Default for TurnProfileParams {
    fn default() -> Self {
        Self { depth_of_cut: 1.0, roughing: true, finishing: true, outside: true, direction: RoughingDirection::Axial }
    }
}

/// Direction of turning roughing passes.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum RoughingDirection {
    #[default]
    Axial,
    Radial,
}

impl RoughingDirection {
    /// Serde names of every variant, in declaration order.
    pub const VARIANTS: &'static [&'static str] = &["axial", "radial"];
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct TurnGrooveParams {
    pub width: f64,
    pub depth: f64,
    /// Axial step between plunges when the groove is wider than the blade.
    pub step_over: f64,
    pub finish_allowance: f64,
    pub finish_pass: bool,
}

impl Default for TurnGrooveParams {
    fn default() -> Self {
        Self { width: 3.0, depth: 2.0, step_over: 1.0, finish_allowance: 0.1, finish_pass: true }
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct TurnThreadParams {
    pub pitch: f64,
    pub depth: f64,
    pub passes: u32,
    pub internal: bool,
    /// Equal chip area per pass (true) or equal depth per pass (false).
    pub constant_area: bool,
    /// Infeed angle in degrees: `0` radial, positive along the flank.
    pub infeed_angle_deg: f64,
    pub alternate_flanks: bool,
    pub spring_passes: u32,
}

impl Default for TurnThreadParams {
    fn default() -> Self {
        Self { pitch: 1.5, depth: 0.92, passes: 6, internal: false, constant_area: true, infeed_angle_deg: 0.0, alternate_flanks: false, spring_passes: 0 }
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct TurnPartOffParams {
    /// X (radius) the part-off ends at; `0` cuts to centre.
    pub end_x: f64,
    pub chamfer: f64,
    /// Peck depth in X (`0`: no pecking).
    pub peck_depth: f64,
    pub peck_retract: f64,
    /// Below this radius the feed is multiplied by `reduced_feed_factor`.
    pub reduced_feed_radius: f64,
    pub reduced_feed_factor: f64,
}

impl Default for TurnPartOffParams {
    fn default() -> Self {
        Self { end_x: 0.0, chamfer: 0.0, peck_depth: 0.0, peck_retract: 0.0, reduced_feed_radius: 0.0, reduced_feed_factor: 1.0 }
    }
}

// ---------------------------------------------------------------------------
// Patterns and manual NC (lane C15)
// ---------------------------------------------------------------------------

/// A pattern of other enabled source operations. Count INCLUDES the original
/// (linear, circular); a mirror emits the original and the reflected instance;
/// WCS offsets re-run the sources at each listed work offset with identical
/// source-relative tool geometry.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct PatternParams {
    pub sources: Vec<OperationId>,
    /// Serialized as `patternKind`: the strategy's own `kind` tag sits at the same level.
    #[serde(rename = "patternKind")]
    pub kind: PatternKind,
    pub order: PatternOrder,
}

impl Default for PatternParams {
    fn default() -> Self {
        Self { sources: Vec::new(), kind: PatternKind::default(), order: PatternOrder::default() }
    }
}

impl PatternParams {
    pub fn validate(&self) -> Result<(), String> {
        if self.sources.is_empty() || self.sources.iter().any(|s| s.as_str().is_empty()) {
            return Err("a pattern needs at least one source operation".into());
        }
        self.kind.validate()
    }
}

/// Shape of a pattern.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]
pub enum PatternKind {
    /// `count` instances (including the original) `spacing` apart along `direction`.
    Linear { count: u32, spacing: f64, direction: Point3 },
    /// `count` instances (including the original) stepped by `angle_deg` about `axis` through `origin`.
    Circular { count: u32, angle_deg: f64, axis: Point3, origin: Point3 },
    /// The original and its reflection in the plane through `origin` with `normal`.
    Mirror { origin: Point3, normal: Point3 },
    /// The sources re-run at each work offset.
    WcsOffsets { list: Vec<WorkOffset> },
}

impl PatternKind {
    /// Serde names of every variant, in declaration order.
    pub const VARIANTS: &'static [&'static str] = &["linear", "circular", "mirror", "wcsOffsets"];
}

impl Default for PatternKind {
    fn default() -> Self {
        PatternKind::Linear { count: 2, spacing: 10.0, direction: Point3::X }
    }
}

impl PatternKind {
    pub fn validate(&self) -> Result<(), String> {
        match self {
            PatternKind::Linear { count, spacing, direction } => {
                if *count < 1 {
                    return Err("linear pattern count must be at least 1".into());
                }
                positive("pattern spacing", *spacing)?;
                if direction.normalized().is_none() {
                    return Err("linear pattern direction must be a non-zero vector".into());
                }
                Ok(())
            }
            PatternKind::Circular { count, angle_deg, axis, origin } => {
                if *count < 1 {
                    return Err("circular pattern count must be at least 1".into());
                }
                if !angle_deg.is_finite() || *angle_deg == 0.0 {
                    return Err("circular pattern angle must be finite and non-zero".into());
                }
                if axis.normalized().is_none() || !origin.is_finite() {
                    return Err("circular pattern needs a non-zero axis and a finite origin".into());
                }
                Ok(())
            }
            PatternKind::Mirror { origin, normal } => {
                if normal.normalized().is_none() || !origin.is_finite() {
                    return Err("mirror pattern needs a non-zero normal and a finite origin".into());
                }
                Ok(())
            }
            PatternKind::WcsOffsets { list } => {
                if list.is_empty() {
                    return Err("WCS pattern needs at least one work offset".into());
                }
                if list.iter().any(|w| w.number == 0 || !w.origin.is_finite()) {
                    return Err("WCS pattern offsets need a number of at least 1 and a finite origin".into());
                }
                Ok(())
            }
        }
    }
}

/// Order of a pattern's expanded instances.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum PatternOrder {
    /// Every source at one instance, then the next instance.
    #[default]
    ByInstance,
    /// Every instance of one tool's operations before the next tool.
    ByTool,
}

impl PatternOrder {
    /// Serde names of every variant, in declaration order.
    pub const VARIANTS: &'static [&'static str] = &["by-instance", "by-tool"];
}

/// A manual NC entry: exactly the requested typed entry, no implicit
/// machining state (no tool, spindle or coolant preamble).
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct ManualNcParams {
    /// Serialized as `manualKind`: the strategy's own `kind` tag sits at the same level.
    #[serde(rename = "manualKind")]
    pub kind: ManualNcKind,
}

impl ManualNcParams {
    pub fn validate(&self) -> Result<(), String> {
        match &self.kind {
            ManualNcKind::Dwell { seconds } => positive_or_zero("manual dwell", *seconds),
            ManualNcKind::ToolChange { tool } if tool.as_str().is_empty() => Err("manual tool change needs a tool".into()),
            _ => Ok(()),
        }
    }
}

/// The manual entries. `PassThrough` text reaches the post byte for byte.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]
pub enum ManualNcKind {
    Stop,
    OptionalStop,
    Dwell { seconds: f64 },
    Comment { text: String },
    PassThrough { text: String },
    ToolChange { tool: ToolId },
}

impl ManualNcKind {
    /// Serde names of every variant, in declaration order.
    pub const VARIANTS: &'static [&'static str] = &["stop", "optionalStop", "dwell", "comment", "passThrough", "toolChange"];
}

impl Default for ManualNcKind {
    fn default() -> Self {
        ManualNcKind::Comment { text: String::new() }
    }
}