eguidev 0.1.0

AI-assisted development tooling and in-process instrumentation for egui apps
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
//! Data types used by DevMCP tooling.

use std::{
    any::Any,
    borrow::Cow,
    collections::{BTreeMap, BTreeSet},
    error::Error,
    fmt,
};

use egui::{Rect as EguiRect, Vec2 as EguiVec2};
use schemars::{JsonSchema, Schema, SchemaGenerator};
use serde::{
    Deserialize, Serialize,
    de::{self, Deserializer},
    ser::Serializer,
};

use crate::registry::viewport_id_to_string;

/// Error returned when a semantic viewport name is reserved or invalid.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ViewportNameError {
    /// Stable machine-readable error code.
    pub code: String,
    /// Human-readable error message.
    pub message: String,
}

impl ViewportNameError {
    fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            code: code.into(),
            message: message.into(),
        }
    }
}

impl fmt::Display for ViewportNameError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.message)
    }
}

impl Error for ViewportNameError {}

/// Error returned when parsing a viewport selector string fails.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ViewportSelParseError {
    /// Stable machine-readable error code.
    pub code: String,
    /// Human-readable error message.
    pub message: String,
}

impl ViewportSelParseError {
    fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            code: code.into(),
            message: message.into(),
        }
    }
}

impl fmt::Display for ViewportSelParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.message)
    }
}

impl Error for ViewportSelParseError {}

/// Explicit selector for a viewport.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ViewportSel {
    kind: ViewportSelKind,
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum ViewportSelKind {
    Root,
    Id(egui::ViewportId),
    RawId(u64),
    Name(String),
}

impl ViewportSel {
    /// Select the root viewport.
    pub fn root() -> Self {
        Self {
            kind: ViewportSelKind::Root,
        }
    }

    /// Select a concrete egui viewport id.
    pub fn id(id: egui::ViewportId) -> Self {
        Self {
            kind: ViewportSelKind::Id(id),
        }
    }

    /// Select a semantic viewport name.
    pub fn name(name: impl Into<String>) -> Result<Self, ViewportNameError> {
        let name = name.into();
        validate_viewport_name(&name)?;
        Ok(Self {
            kind: ViewportSelKind::Name(name),
        })
    }

    /// Parse the Luau/tool selector grammar: `root`, a semantic name, or `vp:<hex>`.
    pub fn parse(selector: impl AsRef<str>) -> Result<Self, ViewportSelParseError> {
        let selector = selector.as_ref();
        if selector.trim().is_empty() {
            return Err(ViewportSelParseError::new(
                "empty_viewport_selector",
                "viewport selector must not be empty",
            ));
        }
        if selector == "root" {
            return Ok(Self::root());
        }
        if let Some(raw) = selector.strip_prefix("vp:") {
            if raw.is_empty() || !raw.bytes().all(|byte| byte.is_ascii_hexdigit()) {
                return Err(ViewportSelParseError::new(
                    "invalid_viewport_id",
                    format!("viewport id selector `{selector}` must be `vp:<hex>`"),
                ));
            }
            let raw_id = u64::from_str_radix(raw, 16).map_err(|_| {
                ViewportSelParseError::new(
                    "invalid_viewport_id",
                    format!("viewport id selector `{selector}` must be `vp:<hex>`"),
                )
            })?;
            return Ok(Self {
                kind: ViewportSelKind::RawId(raw_id),
            });
        }
        validate_viewport_name(selector).map_err(|error| {
            ViewportSelParseError::new(
                error.code,
                format!("invalid viewport name: {}", error.message),
            )
        })?;
        Ok(Self {
            kind: ViewportSelKind::Name(selector.to_string()),
        })
    }

    /// Return the canonical string selector used in fixtures and scripts.
    pub fn to_selector_string(&self) -> String {
        match &self.kind {
            ViewportSelKind::Root => "root".to_string(),
            ViewportSelKind::Id(id) => viewport_id_to_string(*id),
            ViewportSelKind::RawId(raw_id) => format!("vp:{raw_id:x}"),
            ViewportSelKind::Name(name) => name.clone(),
        }
    }
}

impl From<egui::ViewportId> for ViewportSel {
    fn from(value: egui::ViewportId) -> Self {
        Self::id(value)
    }
}

pub fn validate_viewport_name(name: &str) -> Result<(), ViewportNameError> {
    if name.trim().is_empty() {
        return Err(ViewportNameError::new(
            "empty_viewport_name",
            "viewport name must not be empty",
        ));
    }
    if name == "root" || name.starts_with("vp:") {
        return Err(ViewportNameError::new(
            "reserved_viewport_name",
            format!("viewport name `{name}` is reserved"),
        ));
    }
    Ok(())
}

fn sanitize_f32(value: f32) -> f32 {
    if value.is_finite() { value } else { 0.0 }
}

/// A logical point in egui coordinates.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct Pos2 {
    /// X coordinate in points.
    pub x: f32,
    /// Y coordinate in points.
    pub y: f32,
}

impl From<egui::Pos2> for Pos2 {
    fn from(pos: egui::Pos2) -> Self {
        Self {
            x: sanitize_f32(pos.x),
            y: sanitize_f32(pos.y),
        }
    }
}

impl From<Pos2> for egui::Pos2 {
    fn from(pos: Pos2) -> Self {
        egui::pos2(pos.x, pos.y)
    }
}

/// A 2D vector in egui coordinates.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct Vec2 {
    /// X component in points.
    pub x: f32,
    /// Y component in points.
    pub y: f32,
}

impl From<EguiVec2> for Vec2 {
    fn from(vec: EguiVec2) -> Self {
        Self {
            x: sanitize_f32(vec.x),
            y: sanitize_f32(vec.y),
        }
    }
}

impl From<Vec2> for EguiVec2 {
    fn from(vec: Vec2) -> Self {
        egui::vec2(vec.x, vec.y)
    }
}

/// Axis-aligned rectangle in egui coordinates.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct Rect {
    /// Minimum point.
    pub min: Pos2,
    /// Maximum point.
    pub max: Pos2,
}

impl Rect {
    /// Return the center point of the rectangle in egui coordinates.
    pub fn center(self) -> Pos2 {
        Pos2 {
            x: (self.min.x + self.max.x) * 0.5,
            y: (self.min.y + self.max.y) * 0.5,
        }
    }
}

impl From<EguiRect> for Rect {
    fn from(rect: EguiRect) -> Self {
        Self {
            min: Pos2::from(rect.min),
            max: Pos2::from(rect.max),
        }
    }
}

impl From<Rect> for EguiRect {
    fn from(rect: Rect) -> Self {
        Self::from_min_max(rect.min.into(), rect.max.into())
    }
}

/// Keyboard modifier state.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, schemars::JsonSchema, Default)]
pub struct Modifiers {
    /// Ctrl key pressed.
    pub ctrl: bool,
    /// Shift key pressed.
    pub shift: bool,
    /// Alt key pressed.
    pub alt: bool,
    /// Command key pressed.
    pub command: bool,
}

impl From<egui::Modifiers> for Modifiers {
    fn from(modifiers: egui::Modifiers) -> Self {
        Self {
            ctrl: modifiers.ctrl,
            shift: modifiers.shift,
            alt: modifiers.alt,
            command: modifiers.command,
        }
    }
}

/// Fixture metadata advertised by an app.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct FixtureSpec {
    /// Fixture name.
    pub name: String,
    /// Fixture description.
    pub description: String,
    /// Declarative readiness anchors that must be satisfied before fixture application.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub preconditions: Vec<Anchor>,
    /// Declarative readiness anchors for the fixture baseline.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub anchors: Vec<Anchor>,
    /// Typed scalar params accepted by this fixture.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub params: Vec<FixtureParam>,
    /// Searchable fixture categories used by docs and CLI output.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,
}

/// A single readiness anchor for a fixture baseline.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct Anchor {
    /// Widget id to resolve from the registry.
    pub widget_id: String,
    /// Optional viewport selector (`root`, semantic name, or `vp:...`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub viewport_id: Option<String>,
    /// Readiness condition to evaluate against the widget state.
    pub check: AnchorCheck,
}

/// Declarative readiness checks for fixture anchors.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
pub enum AnchorCheck {
    /// Widget exists and is visible.
    Visible,
    /// Widget label matches exactly.
    Label(String),
    /// Widget value matches.
    Value(WidgetValue),
    /// Scroll area is initialized and stable across captures.
    ScrollReady,
    /// Scroll area is initialized, stable, and near the requested offset.
    ScrollAt {
        /// Target scroll offset.
        offset: Vec2,
        /// Allowed absolute error per axis.
        tolerance: f32,
    },
}

/// Supported scalar kinds for fixture parameters.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ParamKind {
    /// Boolean fixture parameter.
    Bool,
    /// Signed integer fixture parameter.
    Int,
    /// Floating-point fixture parameter.
    Float,
    /// String fixture parameter.
    Text,
}

/// One typed fixture parameter in a fixture catalog entry.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct FixtureParam {
    /// Parameter name.
    pub name: String,
    /// Parameter scalar kind.
    pub kind: ParamKind,
    /// Human-readable parameter description.
    pub description: String,
    /// Optional default. Missing default means the caller must supply the param.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default: Option<WidgetValue>,
    /// Optional exact allowed values.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub choices: Vec<WidgetValue>,
    /// Optional inclusive minimum for int/float params.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub min: Option<f64>,
    /// Optional inclusive maximum for int/float params.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max: Option<f64>,
}

/// Validated fixture params passed to a handler.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct FixtureParams(BTreeMap<String, WidgetValue>);

/// Fixture call passed to a registered handler.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct FixtureCall {
    /// Fixture name.
    pub name: String,
    /// Validated params with defaults filled in.
    pub params: FixtureParams,
}

/// Successful fixture handler response.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct FixtureResponse {
    /// Handler-returned values exposed to scripts and CLI output.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub values: BTreeMap<String, WidgetValue>,
    /// Handler-returned dynamic anchors waited on together with the spec anchors.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub anchors: Vec<Anchor>,
}

/// Result returned by a fixture handler.
pub type FixtureResult = Result<FixtureResponse, FixtureError>;

/// Structured fixture handler failure.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct FixtureError {
    /// Stable machine-readable error code.
    pub code: String,
    /// Human-readable error message.
    pub message: String,
    /// Optional machine-readable error details.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub details: Option<serde_json::Value>,
}

impl FixtureSpec {
    /// Create a new fixture specification.
    pub fn new(name: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            preconditions: Vec::new(),
            anchors: Vec::new(),
            params: Vec::new(),
            tags: Vec::new(),
        }
    }

    /// Add a visible-widget precondition checked before fixture application.
    pub fn precondition(self, widget_id: impl Into<String>) -> Self {
        self.push_precondition(widget_id.into(), None, AnchorCheck::Visible)
    }

    /// Add a visible-widget precondition scoped to a viewport.
    pub fn precondition_in(
        self,
        widget_id: impl Into<String>,
        viewport: impl Into<ViewportSel>,
    ) -> Self {
        self.push_precondition(
            widget_id.into(),
            Some(viewport.into().to_selector_string()),
            AnchorCheck::Visible,
        )
    }

    /// Add an exact-value precondition checked before fixture application.
    pub fn precondition_value(self, widget_id: impl Into<String>, value: WidgetValue) -> Self {
        self.push_precondition(widget_id.into(), None, AnchorCheck::Value(value))
    }

    /// Add an exact-value precondition scoped to a viewport.
    pub fn precondition_value_in(
        self,
        widget_id: impl Into<String>,
        value: WidgetValue,
        viewport: impl Into<ViewportSel>,
    ) -> Self {
        self.push_precondition(
            widget_id.into(),
            Some(viewport.into().to_selector_string()),
            AnchorCheck::Value(value),
        )
    }

    /// Add a visible-widget readiness anchor.
    pub fn anchor(self, widget_id: impl Into<String>) -> Self {
        self.push_anchor(widget_id.into(), None, AnchorCheck::Visible)
    }

    /// Add a visible-widget readiness anchor scoped to a viewport.
    pub fn anchor_in(self, widget_id: impl Into<String>, viewport: impl Into<ViewportSel>) -> Self {
        self.push_anchor(
            widget_id.into(),
            Some(viewport.into().to_selector_string()),
            AnchorCheck::Visible,
        )
    }

    /// Add an exact-label readiness anchor.
    pub fn anchor_label(self, widget_id: impl Into<String>, text: impl Into<String>) -> Self {
        self.push_anchor(widget_id.into(), None, AnchorCheck::Label(text.into()))
    }

    /// Add an exact-label readiness anchor scoped to a viewport.
    pub fn anchor_label_in(
        self,
        widget_id: impl Into<String>,
        text: impl Into<String>,
        viewport: impl Into<ViewportSel>,
    ) -> Self {
        self.push_anchor(
            widget_id.into(),
            Some(viewport.into().to_selector_string()),
            AnchorCheck::Label(text.into()),
        )
    }

    /// Add an exact-value readiness anchor.
    pub fn anchor_value(self, widget_id: impl Into<String>, value: WidgetValue) -> Self {
        self.push_anchor(widget_id.into(), None, AnchorCheck::Value(value))
    }

    /// Add an exact-value readiness anchor scoped to a viewport.
    pub fn anchor_value_in(
        self,
        widget_id: impl Into<String>,
        value: WidgetValue,
        viewport: impl Into<ViewportSel>,
    ) -> Self {
        self.push_anchor(
            widget_id.into(),
            Some(viewport.into().to_selector_string()),
            AnchorCheck::Value(value),
        )
    }

    /// Add a scroll-readiness anchor.
    pub fn anchor_scroll(self, widget_id: impl Into<String>) -> Self {
        self.push_anchor(widget_id.into(), None, AnchorCheck::ScrollReady)
    }

    /// Add a scroll-readiness anchor scoped to a viewport.
    pub fn anchor_scroll_in(
        self,
        widget_id: impl Into<String>,
        viewport: impl Into<ViewportSel>,
    ) -> Self {
        self.push_anchor(
            widget_id.into(),
            Some(viewport.into().to_selector_string()),
            AnchorCheck::ScrollReady,
        )
    }

    /// Add a scroll-position readiness anchor.
    pub fn anchor_scroll_at(
        self,
        widget_id: impl Into<String>,
        offset: impl Into<Vec2>,
        tolerance: f32,
    ) -> Self {
        self.push_anchor(
            widget_id.into(),
            None,
            AnchorCheck::ScrollAt {
                offset: offset.into(),
                tolerance,
            },
        )
    }

    /// Add a scroll-position readiness anchor scoped to a viewport.
    pub fn anchor_scroll_at_in(
        self,
        widget_id: impl Into<String>,
        offset: impl Into<Vec2>,
        tolerance: f32,
        viewport: impl Into<ViewportSel>,
    ) -> Self {
        self.push_anchor(
            widget_id.into(),
            Some(viewport.into().to_selector_string()),
            AnchorCheck::ScrollAt {
                offset: offset.into(),
                tolerance,
            },
        )
    }

    /// Add a typed fixture parameter.
    pub fn param(mut self, param: FixtureParam) -> Self {
        self.params.push(param);
        self
    }

    /// Add a fixture tag used by CLI/docs output.
    pub fn tag(mut self, tag: impl Into<String>) -> Self {
        self.tags.push(tag.into());
        self
    }

    /// Validate fixture metadata and readiness anchors.
    pub fn validate(&self, require_anchors: bool) -> Result<(), String> {
        if self.name.trim().is_empty() {
            return Err("fixture name must not be empty".to_string());
        }
        if self.description.trim().is_empty() {
            return Err(format!(
                "fixture {} description must not be empty",
                self.name
            ));
        }
        let mut param_names = BTreeSet::new();
        for (index, param) in self.params.iter().enumerate() {
            param
                .validate()
                .map_err(|error| format!("fixture {} param {}: {error}", self.name, index + 1))?;
            if !param_names.insert(param.name.as_str()) {
                return Err(format!(
                    "fixture {} duplicate param name: {}",
                    self.name, param.name
                ));
            }
        }
        let mut tags = BTreeSet::new();
        for tag in &self.tags {
            if tag.trim().is_empty() {
                return Err(format!("fixture {} tag must not be empty", self.name));
            }
            if !tags.insert(tag.as_str()) {
                return Err(format!("fixture {} duplicate tag: {tag}", self.name));
            }
        }
        for (index, anchor) in self.preconditions.iter().enumerate() {
            anchor.validate().map_err(|error| {
                format!("fixture {} precondition {}: {error}", self.name, index + 1)
            })?;
        }
        for (index, anchor) in self.anchors.iter().enumerate() {
            anchor
                .validate()
                .map_err(|error| format!("fixture {} anchor {}: {error}", self.name, index + 1))?;
        }
        if require_anchors && self.anchors.is_empty() {
            return Err(format!(
                "fixture {} must declare at least one readiness anchor",
                self.name
            ));
        }
        Ok(())
    }

    /// Validate and normalize a caller-supplied param map.
    pub fn validate_params(
        &self,
        mut supplied: BTreeMap<String, WidgetValue>,
    ) -> Result<FixtureParams, FixtureError> {
        let mut values = BTreeMap::new();
        if let Some(name) = supplied
            .keys()
            .find(|name| !self.params.iter().any(|param| param.name == **name))
            .cloned()
        {
            return Err(FixtureError::new(
                "unknown_param",
                format!("unknown param {name:?} for fixture {}", self.name),
            )
            .details(serde_json::json!({
                "fixture": self.name,
                "param": name,
                "allowed": self.params.iter().map(|param| param.name.as_str()).collect::<Vec<_>>(),
            })));
        }
        for param in &self.params {
            let value = match supplied.remove(&param.name) {
                Some(value) => value,
                None => match &param.default {
                    Some(value) => value.clone(),
                    None => {
                        return Err(FixtureError::new(
                            "missing_param",
                            format!(
                                "missing required param {:?} for fixture {}",
                                param.name, self.name
                            ),
                        )
                        .details(serde_json::json!({
                            "fixture": self.name,
                            "param": param.name,
                            "kind": param.kind.as_str(),
                        })));
                    }
                },
            };
            let value = param.normalize_value(value)?;
            values.insert(param.name.clone(), value);
        }
        Ok(FixtureParams(values))
    }

    /// Return a human-readable summary of the readiness contract.
    pub fn describe_readiness(&self) -> String {
        let preconditions = self
            .preconditions
            .iter()
            .map(Anchor::describe)
            .collect::<Vec<_>>();
        let anchors = self
            .anchors
            .iter()
            .map(Anchor::describe)
            .collect::<Vec<_>>();
        match (preconditions.is_empty(), anchors.is_empty()) {
            (true, true) => "No readiness anchors declared.".to_string(),
            (true, false) => anchors.join("; "),
            (false, true) => format!("preconditions: {}", preconditions.join("; ")),
            (false, false) => format!(
                "preconditions: {}; anchors: {}",
                preconditions.join("; "),
                anchors.join("; ")
            ),
        }
    }

    fn push_precondition(
        mut self,
        widget_id: String,
        viewport_id: Option<String>,
        check: AnchorCheck,
    ) -> Self {
        self.preconditions.push(Anchor {
            widget_id,
            viewport_id,
            check,
        });
        self
    }

    fn push_anchor(
        mut self,
        widget_id: String,
        viewport_id: Option<String>,
        check: AnchorCheck,
    ) -> Self {
        self.anchors.push(Anchor {
            widget_id,
            viewport_id,
            check,
        });
        self
    }
}

impl FixtureParam {
    /// Create a boolean fixture parameter.
    pub fn bool(name: impl Into<String>, description: impl Into<String>) -> Self {
        Self::new(name, ParamKind::Bool, description)
    }

    /// Create an integer fixture parameter.
    pub fn int(name: impl Into<String>, description: impl Into<String>) -> Self {
        Self::new(name, ParamKind::Int, description)
    }

    /// Create a floating-point fixture parameter.
    pub fn float(name: impl Into<String>, description: impl Into<String>) -> Self {
        Self::new(name, ParamKind::Float, description)
    }

    /// Create a text fixture parameter.
    pub fn text(name: impl Into<String>, description: impl Into<String>) -> Self {
        Self::new(name, ParamKind::Text, description)
    }

    fn new(name: impl Into<String>, kind: ParamKind, description: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            kind,
            description: description.into(),
            default: None,
            choices: Vec::new(),
            min: None,
            max: None,
        }
    }

    /// Set this parameter's default value.
    pub fn default(mut self, value: impl Into<WidgetValue>) -> Self {
        self.default = Some(self.normalize_literal(value.into()));
        self
    }

    /// Restrict this parameter to exact choices.
    pub fn choices<I, V>(mut self, values: I) -> Self
    where
        I: IntoIterator<Item = V>,
        V: Into<WidgetValue>,
    {
        self.choices = values
            .into_iter()
            .map(Into::into)
            .map(|value| self.normalize_literal(value))
            .collect();
        self
    }

    /// Set an inclusive numeric range.
    pub fn range(mut self, min: f64, max: f64) -> Self {
        self.min = Some(min);
        self.max = Some(max);
        self
    }

    fn validate(&self) -> Result<(), String> {
        if self.name.trim().is_empty() {
            return Err("name must not be empty".to_string());
        }
        if self.description.trim().is_empty() {
            return Err(format!("param {} description must not be empty", self.name));
        }
        if (self.min.is_some() || self.max.is_some())
            && !matches!(self.kind, ParamKind::Int | ParamKind::Float)
        {
            return Err(format!(
                "param {} has a range but is not numeric",
                self.name
            ));
        }
        if let (Some(min), Some(max)) = (self.min, self.max) {
            if !min.is_finite() || !max.is_finite() {
                return Err(format!("param {} range must be finite", self.name));
            }
            if min > max {
                return Err(format!("param {} range min exceeds max", self.name));
            }
        }
        if let Some(default) = &self.default {
            self.normalize_value(default.clone())
                .map_err(|error| error.message)?;
        }
        for choice in &self.choices {
            self.normalize_value(choice.clone())
                .map_err(|error| error.message)?;
        }
        Ok(())
    }

    fn normalize_literal(&self, value: WidgetValue) -> WidgetValue {
        match (self.kind, value) {
            (ParamKind::Float, WidgetValue::Int(value)) => WidgetValue::Float(value as f64),
            (_, value) => value,
        }
    }

    fn validate_value(&self, value: &WidgetValue) -> Result<(), FixtureError> {
        if !self.kind.matches(value) {
            return Err(FixtureError::new(
                "invalid_param_type",
                format!(
                    "param {:?} expected {}, got {}",
                    self.name,
                    self.kind.as_str(),
                    value.kind_name()
                ),
            )
            .details(serde_json::json!({
                "param": self.name,
                "expected": self.kind.as_str(),
                "actual": value.kind_name(),
            })));
        }
        if !self.choices.is_empty() && !self.choices.iter().any(|choice| choice == value) {
            return Err(FixtureError::new(
                "invalid_param_choice",
                format!(
                    "param {:?} value is not one of its allowed choices",
                    self.name
                ),
            )
            .details(serde_json::json!({
                "param": self.name,
                "value": value,
                "choices": self.choices,
            })));
        }
        if let Some(number) = value.as_f64() {
            if let Some(min) = self.min
                && number < min
            {
                return Err(FixtureError::new(
                    "param_below_min",
                    format!("param {:?} must be >= {min}", self.name),
                )
                .details(serde_json::json!({
                    "param": self.name,
                    "value": value,
                    "min": min,
                })));
            }
            if let Some(max) = self.max
                && number > max
            {
                return Err(FixtureError::new(
                    "param_above_max",
                    format!("param {:?} must be <= {max}", self.name),
                )
                .details(serde_json::json!({
                    "param": self.name,
                    "value": value,
                    "max": max,
                })));
            }
        }
        Ok(())
    }

    fn normalize_value(&self, value: WidgetValue) -> Result<WidgetValue, FixtureError> {
        let value = self.normalize_literal(value);
        self.validate_value(&value)?;
        Ok(value)
    }
}

impl ParamKind {
    fn as_str(self) -> &'static str {
        match self {
            Self::Bool => "bool",
            Self::Int => "int",
            Self::Float => "float",
            Self::Text => "text",
        }
    }

    fn matches(self, value: &WidgetValue) -> bool {
        matches!(
            (self, value),
            (Self::Bool, WidgetValue::Bool(_))
                | (Self::Int, WidgetValue::Int(_))
                | (Self::Float, WidgetValue::Float(_))
                | (Self::Text, WidgetValue::Text(_))
        )
    }
}

impl FixtureParams {
    /// Get a bool param by name.
    pub fn bool(&self, name: &str) -> bool {
        match self.0.get(name) {
            Some(WidgetValue::Bool(value)) => *value,
            Some(_) => panic!("fixture param {name:?} is not a bool"),
            None => panic!("fixture param {name:?} is not declared"),
        }
    }

    /// Get an int param by name.
    pub fn int(&self, name: &str) -> i64 {
        match self.0.get(name) {
            Some(WidgetValue::Int(value)) => *value,
            Some(_) => panic!("fixture param {name:?} is not an int"),
            None => panic!("fixture param {name:?} is not declared"),
        }
    }

    /// Get a float param by name.
    pub fn float(&self, name: &str) -> f64 {
        match self.0.get(name) {
            Some(WidgetValue::Float(value)) => *value,
            Some(_) => panic!("fixture param {name:?} is not a float"),
            None => panic!("fixture param {name:?} is not declared"),
        }
    }

    /// Get a text param by name.
    pub fn text(&self, name: &str) -> &str {
        match self.0.get(name) {
            Some(WidgetValue::Text(value)) => value,
            Some(_) => panic!("fixture param {name:?} is not text"),
            None => panic!("fixture param {name:?} is not declared"),
        }
    }

    /// Get a param by name.
    pub fn get(&self, name: &str) -> Option<&WidgetValue> {
        self.0.get(name)
    }

    /// Return the validated params as a map.
    pub fn as_map(&self) -> &BTreeMap<String, WidgetValue> {
        &self.0
    }

    /// Consume this wrapper into the validated param map.
    pub fn into_map(self) -> BTreeMap<String, WidgetValue> {
        self.0
    }
}

impl FixtureResponse {
    /// Create an empty fixture response.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a handler-returned value.
    pub fn value(mut self, name: impl Into<String>, value: impl Into<WidgetValue>) -> Self {
        self.values.insert(name.into(), value.into());
        self
    }

    /// Add a handler-returned dynamic anchor.
    pub fn anchor(mut self, anchor: Anchor) -> Self {
        self.anchors.push(anchor);
        self
    }
}

impl FixtureError {
    /// Create a fixture error.
    pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            code: code.into(),
            message: message.into(),
            details: None,
        }
    }

    /// Attach structured error details.
    pub fn details<T: serde::Serialize>(mut self, details: T) -> Self {
        self.details = serde_json::to_value(details).ok();
        self
    }

    pub(crate) fn handler_panic(name: &str, panic: &(dyn Any + Send)) -> Self {
        Self::new(
            "panic",
            format!(
                "fixture handler {name:?} panicked: {}",
                panic_message(panic)
            ),
        )
    }
}

fn panic_message(panic: &(dyn Any + Send)) -> String {
    if let Some(message) = panic.downcast_ref::<&'static str>() {
        (*message).to_string()
    } else if let Some(message) = panic.downcast_ref::<String>() {
        message.clone()
    } else {
        "unknown panic payload".to_string()
    }
}

impl Anchor {
    /// Create a visible-widget anchor.
    pub fn visible(widget_id: impl Into<String>) -> Self {
        Self {
            widget_id: widget_id.into(),
            viewport_id: None,
            check: AnchorCheck::Visible,
        }
    }

    /// Create an exact-label anchor.
    pub fn label(widget_id: impl Into<String>, label: impl Into<String>) -> Self {
        Self {
            widget_id: widget_id.into(),
            viewport_id: None,
            check: AnchorCheck::Label(label.into()),
        }
    }

    /// Create an exact-value anchor.
    pub fn value(widget_id: impl Into<String>, value: impl Into<WidgetValue>) -> Self {
        Self {
            widget_id: widget_id.into(),
            viewport_id: None,
            check: AnchorCheck::Value(value.into()),
        }
    }

    /// Create a scroll-readiness anchor.
    pub fn scroll_ready(widget_id: impl Into<String>) -> Self {
        Self {
            widget_id: widget_id.into(),
            viewport_id: None,
            check: AnchorCheck::ScrollReady,
        }
    }

    /// Create a scroll-position anchor.
    pub fn scroll_at(
        widget_id: impl Into<String>,
        offset: impl Into<Vec2>,
        tolerance: f32,
    ) -> Self {
        Self {
            widget_id: widget_id.into(),
            viewport_id: None,
            check: AnchorCheck::ScrollAt {
                offset: offset.into(),
                tolerance,
            },
        }
    }

    /// Scope this anchor to a viewport selector.
    pub fn in_viewport(mut self, viewport: impl Into<ViewportSel>) -> Self {
        self.viewport_id = Some(viewport.into().to_selector_string());
        self
    }

    /// Return a human-readable description of the anchor.
    pub fn describe(&self) -> String {
        let target = match &self.viewport_id {
            Some(viewport_id) => format!("{} in {}", self.widget_id, viewport_id),
            None => self.widget_id.clone(),
        };
        format!("{target} {}", self.check)
    }

    /// Validate the anchor contents.
    pub fn validate(&self) -> Result<(), String> {
        if self.widget_id.trim().is_empty() {
            return Err("widget_id must not be empty".to_string());
        }
        if let Some(viewport_id) = &self.viewport_id
            && viewport_id.trim().is_empty()
        {
            return Err("viewport_id must not be empty when provided".to_string());
        }
        match &self.check {
            AnchorCheck::Label(text) if text.is_empty() => {
                Err("label anchors must not be empty".to_string())
            }
            AnchorCheck::ScrollAt { tolerance, .. }
                if !tolerance.is_finite() || *tolerance <= 0.0 =>
            {
                Err("scroll_at tolerance must be finite and greater than 0".to_string())
            }
            _ => Ok(()),
        }
    }
}

impl fmt::Display for AnchorCheck {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Visible => f.write_str("visible"),
            Self::Label(text) => write!(f, "label == \"{text}\""),
            Self::Value(value) => match value {
                WidgetValue::Text(text) => write!(f, "value == \"{text}\""),
                _ => write!(f, "value == {}", value.to_text()),
            },
            Self::ScrollReady => f.write_str("scroll_ready"),
            Self::ScrollAt { offset, tolerance } => write!(
                f,
                "scroll_at ({:.1}, {:.1}) ± {:.2}",
                offset.x, offset.y, tolerance
            ),
        }
    }
}

impl From<Modifiers> for egui::Modifiers {
    fn from(modifiers: Modifiers) -> Self {
        Self {
            ctrl: modifiers.ctrl,
            shift: modifiers.shift,
            alt: modifiers.alt,
            command: modifiers.command,
            mac_cmd: modifiers.command,
        }
    }
}

/// Widget reference used in tool calls.
///
/// Matching rules:
/// - `id` is the canonical widget selector.
/// - `viewport_id` acts as an additional selector to narrow matches.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct WidgetRef {
    /// Canonical widget id.
    ///
    /// If instrumentation provides an explicit id, eguidev uses it verbatim.
    /// Otherwise eguidev generates an opaque hex id that is best-effort stable
    /// within the current app session.
    #[serde(default)]
    pub id: Option<String>,
    /// Optional viewport selector (`root` or `vp:...`).
    #[serde(default)]
    pub viewport_id: Option<String>,
}

/// Widget role taxonomy for automation and scripting filters.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
#[allow(missing_docs)]
pub enum WidgetRole {
    Button,
    Link,
    Image,
    Label,
    TextEdit,
    Slider,
    Checkbox,
    ComboBox,
    Radio,
    DragValue,
    Toggle,
    Selectable,
    Separator,
    Spinner,
    ScrollArea,
    MenuButton,
    CollapsingHeader,
    Window,
    ProgressBar,
    ColorPicker,
    #[default]
    Unknown,
}

/// Captured widget value for stateful controls.
#[derive(Debug, Clone, PartialEq)]
pub enum WidgetValue {
    /// Boolean value from checkboxes/toggles.
    Bool(bool),
    /// Floating-point value from sliders/drag values.
    Float(f64),
    /// Integer value from drag values/combos.
    Int(i64),
    /// Text value from text edits.
    Text(String),
}

impl WidgetValue {
    /// String representation matching Luau `tostring()` semantics.
    pub fn to_text(&self) -> String {
        match self {
            Self::Bool(v) => v.to_string(),
            Self::Float(v) => v.to_string(),
            Self::Int(v) => v.to_string(),
            Self::Text(v) => v.clone(),
        }
    }

    fn kind_name(&self) -> &'static str {
        match self {
            Self::Bool(_) => "bool",
            Self::Float(_) => "float",
            Self::Int(_) => "int",
            Self::Text(_) => "text",
        }
    }

    fn as_f64(&self) -> Option<f64> {
        match self {
            Self::Float(value) => Some(*value),
            Self::Int(value) => Some(*value as f64),
            Self::Bool(_) | Self::Text(_) => None,
        }
    }
}

impl From<bool> for WidgetValue {
    fn from(value: bool) -> Self {
        Self::Bool(value)
    }
}

impl From<i64> for WidgetValue {
    fn from(value: i64) -> Self {
        Self::Int(value)
    }
}

impl From<f64> for WidgetValue {
    fn from(value: f64) -> Self {
        Self::Float(value)
    }
}

impl From<String> for WidgetValue {
    fn from(value: String) -> Self {
        Self::Text(value)
    }
}

impl From<&str> for WidgetValue {
    fn from(value: &str) -> Self {
        Self::Text(value.to_string())
    }
}

impl<'de> Deserialize<'de> for WidgetValue {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = serde_json::Value::deserialize(deserializer)?;
        widget_value_from_json(value).map_err(de::Error::custom)
    }
}

impl Serialize for WidgetValue {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Self::Bool(value) => serializer.serialize_bool(*value),
            Self::Float(value) => serializer.serialize_f64(*value),
            Self::Int(value) => serializer.serialize_i64(*value),
            Self::Text(value) => serializer.serialize_str(value),
        }
    }
}

#[doc(hidden)]
impl JsonSchema for WidgetRole {
    fn schema_name() -> Cow<'static, str> {
        "WidgetRole".into()
    }

    fn json_schema(_generator: &mut SchemaGenerator) -> Schema {
        schemars::json_schema!({
            "type": "string",
            "enum": [
                "button",
                "link",
                "image",
                "label",
                "text_edit",
                "slider",
                "checkbox",
                "combo_box",
                "radio",
                "drag_value",
                "toggle",
                "selectable",
                "separator",
                "spinner",
                "scroll_area",
                "menu_button",
                "collapsing_header",
                "window",
                "progress_bar",
                "color_picker",
                "unknown"
            ]
        })
    }
}

#[doc(hidden)]
impl JsonSchema for WidgetValue {
    fn schema_name() -> Cow<'static, str> {
        "WidgetValue".into()
    }

    fn json_schema(_generator: &mut SchemaGenerator) -> Schema {
        schemars::json_schema!({
            "oneOf": [
                { "type": "boolean" },
                { "type": "integer" },
                { "type": "number" },
                { "type": "string" }
            ]
        })
    }
}

fn widget_value_from_json(value: serde_json::Value) -> Result<WidgetValue, String> {
    match value {
        serde_json::Value::Object(map) => {
            if map.len() != 1 {
                return Err("WidgetValue must include exactly one field".to_string());
            }
            let (key, value) = map.into_iter().next().expect("map entry");
            match key.as_str() {
                "bool" => match value {
                    serde_json::Value::Bool(value) => Ok(WidgetValue::Bool(value)),
                    _ => Err("WidgetValue bool must be a boolean".to_string()),
                },
                "float" => match value {
                    serde_json::Value::Number(number) => number
                        .as_f64()
                        .map(WidgetValue::Float)
                        .ok_or_else(|| "WidgetValue float must be a number".to_string()),
                    _ => Err("WidgetValue float must be a number".to_string()),
                },
                "int" => match value {
                    serde_json::Value::Number(number) => number
                        .as_i64()
                        .or_else(|| number.as_u64().and_then(|value| i64::try_from(value).ok()))
                        .map(WidgetValue::Int)
                        .ok_or_else(|| "WidgetValue int must be an integer".to_string()),
                    _ => Err("WidgetValue int must be an integer".to_string()),
                },
                "text" => match value {
                    serde_json::Value::String(value) => Ok(WidgetValue::Text(value)),
                    _ => Err("WidgetValue text must be a string".to_string()),
                },
                _ => Err("WidgetValue field must be one of bool, float, int, text".to_string()),
            }
        }
        serde_json::Value::Bool(value) => Ok(WidgetValue::Bool(value)),
        serde_json::Value::Number(number) => {
            if let Some(value) = number.as_i64() {
                Ok(WidgetValue::Int(value))
            } else if let Some(value) = number.as_u64() {
                i64::try_from(value)
                    .map(WidgetValue::Int)
                    .map_err(|_| "WidgetValue int is out of range".to_string())
            } else if let Some(value) = number.as_f64() {
                Ok(WidgetValue::Float(value))
            } else {
                Err("WidgetValue number must be int or float".to_string())
            }
        }
        serde_json::Value::String(value) => {
            let trimmed = value.trim();
            if trimmed.starts_with('{')
                && let Ok(parsed) = serde_json::from_str::<serde_json::Value>(trimmed)
            {
                return widget_value_from_json(parsed);
            }
            Ok(WidgetValue::Text(value))
        }
        _ => Err("WidgetValue must be bool, number, string, or tagged object".to_string()),
    }
}

/// Layout metadata captured for a widget when available.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct WidgetLayout {
    /// Desired size of the widget before layout constraints.
    pub desired_size: Vec2,
    /// Actual size assigned to the widget.
    pub actual_size: Vec2,
    /// Clip rect for the widget at layout time.
    pub clip_rect: Rect,
    /// Whether any part of the widget is clipped.
    pub clipped: bool,
    /// Whether the widget extends beyond its allocated layout slot.
    pub overflow: bool,
    /// Available rect before the widget was laid out.
    pub available_rect: Rect,
    /// Visible fraction of the widget within the clip rect.
    pub visible_fraction: f32,
}

/// Scroll metadata captured for a scroll area.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ScrollAreaMeta {
    /// Current scroll offset.
    pub offset: Vec2,
    /// Viewport size available to the scroll contents.
    pub viewport_size: Vec2,
    /// Total content size within the scroll area.
    pub content_size: Vec2,
    /// Maximum reachable scroll offset after clamping.
    pub max_offset: Vec2,
}

impl ScrollAreaMeta {
    /// Build scroll metadata and derive the clamped maximum offset.
    pub fn new(offset: Vec2, viewport_size: Vec2, content_size: Vec2) -> Self {
        Self {
            offset,
            viewport_size,
            content_size,
            max_offset: Vec2 {
                x: (content_size.x - viewport_size.x).max(0.0),
                y: (content_size.y - viewport_size.y).max(0.0),
            },
        }
    }
}

/// Min/max bounds for a numeric widget.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct WidgetRange {
    /// Minimum allowed value.
    pub min: f64,
    /// Maximum allowed value.
    pub max: f64,
}

impl WidgetRange {
    /// Check whether the range contains the provided value.
    pub fn contains(self, value: f64) -> bool {
        self.min <= value && value <= self.max
    }
}

/// Role-specific widget metadata kept on internal registry entries.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum RoleState {
    /// Scroll area metadata.
    ScrollArea {
        /// Current scroll offset.
        offset: Vec2,
        /// Viewport size available to the scroll contents.
        viewport_size: Vec2,
        /// Total content size within the scroll area.
        content_size: Vec2,
    },
    /// Slider range metadata.
    Slider {
        /// Allowed numeric range.
        range: WidgetRange,
    },
    /// Drag value range metadata.
    DragValue {
        /// Allowed numeric range when constrained by the app.
        range: Option<WidgetRange>,
    },
    /// Combo box option labels.
    ComboBox {
        /// Available option labels.
        options: Vec<String>,
    },
    /// Selected/toggled button state.
    Button {
        /// Whether the button is in a selected state.
        selected: bool,
    },
    /// Checkbox third-state metadata.
    Checkbox {
        /// Whether the checkbox is visually indeterminate.
        indeterminate: bool,
    },
    /// Text edit configuration metadata.
    TextEdit {
        /// Whether the edit is multiline.
        multiline: bool,
        /// Whether the edit masks its input.
        password: bool,
    },
}

impl RoleState {
    /// Project scroll-area metadata into the flat scripting shape.
    pub fn scroll_state(&self) -> Option<ScrollAreaMeta> {
        match self {
            Self::ScrollArea {
                offset,
                viewport_size,
                content_size,
            } => Some(ScrollAreaMeta::new(*offset, *viewport_size, *content_size)),
            _ => None,
        }
    }

    /// Project numeric range metadata into the flat scripting shape.
    pub fn range(&self) -> Option<WidgetRange> {
        match self {
            Self::Slider { range } => Some(*range),
            Self::DragValue { range } => *range,
            _ => None,
        }
    }

    /// Return combo-box options when present.
    pub fn options(&self) -> Option<&[String]> {
        match self {
            Self::ComboBox { options } => Some(options),
            _ => None,
        }
    }

    /// Return button selected metadata when present.
    pub fn selected(&self) -> Option<bool> {
        match self {
            Self::Button { selected } => Some(*selected),
            _ => None,
        }
    }

    /// Return checkbox indeterminate metadata when present.
    pub fn indeterminate(&self) -> Option<bool> {
        match self {
            Self::Checkbox { indeterminate } => Some(*indeterminate),
            _ => None,
        }
    }

    /// Return text-edit metadata when present: `(multiline, password)`.
    pub fn text_edit(&self) -> Option<(bool, bool)> {
        match self {
            Self::TextEdit {
                multiline,
                password,
            } => Some((*multiline, *password)),
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use super::{
        FixtureParam, FixtureSpec, RoleState, Vec2, ViewportSel, WidgetValue,
        validate_viewport_name,
    };

    fn fixture_param_map<const N: usize>(
        entries: [(&str, WidgetValue); N],
    ) -> BTreeMap<String, WidgetValue> {
        entries
            .into_iter()
            .map(|(name, value)| (name.to_string(), value))
            .collect()
    }

    fn param_spec() -> FixtureSpec {
        FixtureSpec::new("param.demo", "Parameterized fixture")
            .param(
                FixtureParam::text("mode", "Mode to apply.")
                    .default("fast")
                    .choices(["fast", "slow"]),
            )
            .param(FixtureParam::float("offset", "Offset in points.").range(0.0, 10.0))
            .param(FixtureParam::int("count", "Item count."))
    }

    #[test]
    fn widget_value_deserializes_tagged_object() {
        let value: WidgetValue = serde_json::from_value(serde_json::json!({"bool": false}))
            .expect("deserialize tagged bool");
        assert_eq!(value, WidgetValue::Bool(false));
    }

    #[test]
    fn widget_value_deserializes_stringified_object() {
        let value: WidgetValue = serde_json::from_value(serde_json::json!("{\"bool\": false}"))
            .expect("deserialize stringified bool");
        assert_eq!(value, WidgetValue::Bool(false));
    }

    #[test]
    fn widget_value_deserializes_plain_text() {
        let value: WidgetValue =
            serde_json::from_value(serde_json::json!("hello")).expect("deserialize text");
        assert_eq!(value, WidgetValue::Text("hello".to_string()));
    }

    #[test]
    fn widget_value_serialization() {
        let v = WidgetValue::Bool(true);
        assert_eq!(serde_json::to_string(&v).unwrap(), "true");
    }

    #[test]
    fn fixture_params_validate_defaults_and_int_to_float() {
        let params = param_spec()
            .validate_params(fixture_param_map([
                ("offset", WidgetValue::Int(3)),
                ("count", WidgetValue::Int(2)),
            ]))
            .expect("valid params");

        assert_eq!(params.text("mode"), "fast");
        assert_eq!(params.float("offset"), 3.0);
        assert_eq!(params.int("count"), 2);
        assert_eq!(
            params.as_map().get("offset"),
            Some(&WidgetValue::Float(3.0))
        );
    }

    #[test]
    fn fixture_float_param_choices_normalize_int_literals() {
        let spec = FixtureSpec::new("float.choice", "Float choice fixture").param(
            FixtureParam::float("scale", "Scale factor.")
                .default(1_i64)
                .choices([1_i64, 2_i64]),
        );

        let defaults = spec
            .validate_params(BTreeMap::new())
            .expect("default params");
        assert_eq!(defaults.float("scale"), 1.0);

        let explicit = spec
            .validate_params(fixture_param_map([("scale", WidgetValue::Int(2))]))
            .expect("explicit params");
        assert_eq!(explicit.float("scale"), 2.0);
    }

    #[test]
    fn fixture_params_reject_unknown_and_missing_params() {
        let unknown = param_spec()
            .validate_params(fixture_param_map([
                ("offset", WidgetValue::Float(3.0)),
                ("count", WidgetValue::Int(2)),
                ("extra", WidgetValue::Bool(true)),
            ]))
            .expect_err("unknown param rejected");
        assert_eq!(unknown.code, "unknown_param");

        let missing = param_spec()
            .validate_params(fixture_param_map([(
                "mode",
                WidgetValue::Text("fast".to_string()),
            )]))
            .expect_err("missing param rejected");
        assert_eq!(missing.code, "missing_param");
    }

    #[test]
    fn fixture_params_reject_type_choice_and_range_errors() {
        let wrong_type = param_spec()
            .validate_params(fixture_param_map([
                ("mode", WidgetValue::Text("fast".to_string())),
                ("offset", WidgetValue::Text("three".to_string())),
                ("count", WidgetValue::Int(2)),
            ]))
            .expect_err("type rejected");
        assert_eq!(wrong_type.code, "invalid_param_type");

        let bad_choice = param_spec()
            .validate_params(fixture_param_map([
                ("mode", WidgetValue::Text("medium".to_string())),
                ("offset", WidgetValue::Float(3.0)),
                ("count", WidgetValue::Int(2)),
            ]))
            .expect_err("choice rejected");
        assert_eq!(bad_choice.code, "invalid_param_choice");

        let below_min = param_spec()
            .validate_params(fixture_param_map([
                ("mode", WidgetValue::Text("fast".to_string())),
                ("offset", WidgetValue::Float(-1.0)),
                ("count", WidgetValue::Int(2)),
            ]))
            .expect_err("range min rejected");
        assert_eq!(below_min.code, "param_below_min");

        let above_max = param_spec()
            .validate_params(fixture_param_map([
                ("mode", WidgetValue::Text("fast".to_string())),
                ("offset", WidgetValue::Float(11.0)),
                ("count", WidgetValue::Int(2)),
            ]))
            .expect_err("range max rejected");
        assert_eq!(above_max.code, "param_above_max");
    }

    #[test]
    fn viewport_selector_parses_canonical_strings() {
        assert_eq!(
            ViewportSel::parse("root").unwrap().to_selector_string(),
            "root"
        );
        assert_eq!(
            ViewportSel::parse("details").unwrap().to_selector_string(),
            "details"
        );
        assert_eq!(
            ViewportSel::parse("vp:AB").unwrap().to_selector_string(),
            "vp:ab"
        );
        assert_eq!(
            ViewportSel::parse("vp:00ff").unwrap().to_selector_string(),
            "vp:ff"
        );
    }

    #[test]
    fn viewport_selector_rejects_invalid_raw_ids() {
        for selector in ["vp:", "vp:+ff", "vp:zz"] {
            assert!(
                ViewportSel::parse(selector).is_err(),
                "{selector} should be rejected"
            );
        }
    }

    #[test]
    fn viewport_name_validation_rejects_empty_and_reserved_names() {
        for name in ["", "   "] {
            let error = validate_viewport_name(name).expect_err("empty name rejected");
            assert_eq!(error.code, "empty_viewport_name");
        }
        for name in ["root", "vp:123"] {
            let error = validate_viewport_name(name).expect_err("reserved name rejected");
            assert_eq!(error.code, "reserved_viewport_name");
        }
    }

    #[test]
    fn scroll_area_meta_computes_max_offset() {
        let scroll = RoleState::ScrollArea {
            offset: Vec2 { x: 2.0, y: 3.0 },
            viewport_size: Vec2 { x: 100.0, y: 40.0 },
            content_size: Vec2 { x: 180.0, y: 150.0 },
        }
        .scroll_state()
        .expect("scroll metadata");

        assert_eq!(scroll.max_offset.x, 80.0);
        assert_eq!(scroll.max_offset.y, 110.0);
    }

    #[test]
    fn scroll_area_meta_clamps_negative_max_offset() {
        let scroll = RoleState::ScrollArea {
            offset: Vec2 { x: 0.0, y: 0.0 },
            viewport_size: Vec2 { x: 100.0, y: 40.0 },
            content_size: Vec2 { x: 80.0, y: 20.0 },
        }
        .scroll_state()
        .expect("scroll metadata");

        assert_eq!(scroll.max_offset.x, 0.0);
        assert_eq!(scroll.max_offset.y, 0.0);
    }
}

/// Widget registry entry captured per frame.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct WidgetRegistryEntry {
    /// Canonical widget id.
    pub id: String,
    /// Whether the id was explicitly provided by instrumentation.
    #[serde(skip_serializing, skip_deserializing)]
    #[schemars(skip)]
    pub explicit_id: bool,
    /// Raw egui widget id used for low-level engine interactions.
    #[serde(skip_serializing, skip_deserializing)]
    #[schemars(skip)]
    pub native_id: u64,
    /// Viewport id string.
    pub viewport_id: String,
    /// Layer id rendered as a stable string (internal use only, e.g. debug overlay).
    #[serde(skip_serializing, skip_deserializing)]
    #[schemars(skip)]
    pub layer_id: String,
    /// Widget rect.
    pub rect: Rect,
    /// Widget interaction rect.
    pub interact_rect: Rect,
    /// Role taxonomy entry.
    pub role: WidgetRole,
    /// Optional label.
    pub label: Option<String>,
    /// Optional widget value for stateful controls.
    pub value: Option<WidgetValue>,
    /// Structured app-domain metadata attached to this widget.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub data: Option<serde_json::Value>,
    /// Optional layout metadata.
    pub layout: Option<WidgetLayout>,
    /// Optional role-specific metadata encoded as a nested enum.
    #[serde(default)]
    pub role_state: Option<RoleState>,
    /// Optional parent id for container scoping.
    pub parent_id: Option<String>,
    /// Whether the widget is enabled.
    pub enabled: bool,
    /// Whether the widget is visible.
    pub visible: bool,
    /// Whether the widget reported egui focus in the captured frame (may lag keyboard focus).
    pub focused: bool,
}

/// Live widget snapshot exposed to scripting surfaces.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct WidgetState {
    /// Widget rect.
    pub rect: Rect,
    /// Widget interaction rect.
    pub interact_rect: Rect,
    /// Role taxonomy entry.
    pub role: WidgetRole,
    /// Optional label.
    pub label: Option<String>,
    /// Optional widget value for stateful controls.
    pub value: Option<WidgetValue>,
    /// Structured app-domain metadata attached to this widget.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub data: Option<serde_json::Value>,
    /// String representation of the widget value. Empty string when value is
    /// `None`. For `Bool` → `"true"`/`"false"`, `Float` → decimal, `Int` →
    /// decimal, `Text` → verbatim.
    pub value_text: String,
    /// Optional layout metadata.
    pub layout: Option<WidgetLayout>,
    /// Optional scroll metadata for scroll areas.
    #[serde(rename = "scroll_state")]
    pub scroll: Option<ScrollAreaMeta>,
    /// Optional numeric range for sliders and ranged drag values.
    pub range: Option<WidgetRange>,
    /// Optional option labels for combo boxes.
    pub options: Option<Vec<String>>,
    /// Optional selected/toggled state for selected-aware buttons.
    pub selected: Option<bool>,
    /// Optional third visual state for indeterminate checkboxes.
    pub indeterminate: Option<bool>,
    /// Optional multiline flag for text edits.
    pub multiline: Option<bool>,
    /// Optional password-masking flag for text edits.
    pub password: Option<bool>,
    /// Whether the widget is enabled.
    pub enabled: bool,
    /// Whether the widget is visible.
    pub visible: bool,
    /// Whether the widget reported egui focus in the captured frame (may lag keyboard focus).
    pub focused: bool,
}

impl From<&WidgetRegistryEntry> for WidgetState {
    fn from(entry: &WidgetRegistryEntry) -> Self {
        let value_text = entry
            .value
            .as_ref()
            .map(|v| v.to_text())
            .unwrap_or_default();
        let scroll = entry.role_state.as_ref().and_then(RoleState::scroll_state);
        let range = entry.role_state.as_ref().and_then(RoleState::range);
        let options = entry
            .role_state
            .as_ref()
            .and_then(RoleState::options)
            .map(<[String]>::to_vec);
        let selected = entry.role_state.as_ref().and_then(RoleState::selected);
        let indeterminate = entry.role_state.as_ref().and_then(RoleState::indeterminate);
        let (multiline, password) = entry
            .role_state
            .as_ref()
            .and_then(RoleState::text_edit)
            .map_or((None, None), |(multiline, password)| {
                (Some(multiline), Some(password))
            });
        Self {
            rect: entry.rect,
            interact_rect: entry.interact_rect,
            role: entry.role.clone(),
            label: entry.label.clone(),
            value: entry.value.clone(),
            data: entry.data.clone(),
            value_text,
            layout: entry.layout.clone(),
            scroll,
            range,
            options,
            selected,
            indeterminate,
            multiline,
            password,
            enabled: entry.enabled,
            visible: entry.visible,
            focused: entry.focused,
        }
    }
}