reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
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
//! DeepSeek Protocol Validation Engine
//!
//! **Enterprise-Grade Reasoning Chain Validation**
//!
//! This module uses DeepSeek V3's 671B parameter scale to provide ultimate validation
//! of complete reasoning chains, detecting subtle flaws that 70B models miss.
//!
//! ## Core Features
//!
//! - **Reasoning Chain Integrity**: Validates logical flow and step dependencies
//! - **Statistical Significance**: Applies statistical testing to confidence scores
//! - **Compliance Alignment**: GDPR, bias detection, regulatory compliance
//! - **Meta-Cognitive Assessment**: Evaluates reasoning quality and methodology
//!
//! ## Enterprise Value
//!
//! - **671B Parameter Advantage**: 10x reasoning power vs standard models
//! - **Cross-Cultural Intelligence**: Unbiased validation for global business
//! - **Statistical Rigor**: Confidence significance testing and error bounds
//! - **Production Auditable**: Complete validation trace for compliance

use crate::error::Result;
use regex::Regex;
use serde::{Deserialize, Serialize};

use super::{
    executor::{ProtocolInput, ProtocolOutput},
    llm::{LlmClient, LlmRequest, UnifiedLlmClient},
    trace::ExecutionTrace,
};

/// Configuration for DeepSeek Validation Engine
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeepSeekValidationConfig {
    /// DeepSeek model to use (default: deepseek-chat)
    #[serde(default = "default_deepseek_model")]
    pub model: String,

    /// Temperature for validation (lower = more deterministic)
    #[serde(default = "default_validation_temperature")]
    pub temperature: f32,

    /// Maximum validation tokens per chain
    #[serde(default = "default_max_tokens")]
    pub max_tokens: u32,

    /// Enable statistical significance testing
    #[serde(default)]
    pub enable_statistical_testing: bool,

    /// Statistical significance threshold (alpha)
    #[serde(default = "default_alpha")]
    pub alpha: f64,

    /// Enable compliance validation (GDPR, bias detection)
    #[serde(default)]
    pub enable_compliance_validation: bool,

    /// Enable meta-cognitive assessment
    #[serde(default)]
    pub enable_meta_cognition: bool,

    /// Minimum confidence threshold for chain validation
    #[serde(default = "default_min_confidence")]
    pub min_confidence: f64,

    /// Maximum chain length for validation (memory optimization)
    #[serde(default = "default_max_chain_length")]
    pub max_chain_length: usize,
}

fn default_deepseek_model() -> String {
    "deepseek-chat".to_string()
}

fn default_validation_temperature() -> f32 {
    0.1 // Low temperature for deterministic validation
}

fn default_max_tokens() -> u32 {
    4000 // Conservative token limit for validation
}

fn default_alpha() -> f64 {
    0.05 // Standard statistical significance threshold
}

fn default_min_confidence() -> f64 {
    0.70 // Minimum confidence for chain to be considered valid
}

fn default_max_chain_length() -> usize {
    20 // Protect against excessively long chains
}

impl Default for DeepSeekValidationConfig {
    fn default() -> Self {
        Self {
            model: default_deepseek_model(),
            temperature: default_validation_temperature(),
            max_tokens: default_max_tokens(),
            enable_statistical_testing: false, // Default off for performance
            alpha: default_alpha(),
            enable_compliance_validation: true, // Default on for safety
            enable_meta_cognition: false,       // Default off for performance
            min_confidence: default_min_confidence(),
            max_chain_length: default_max_chain_length(),
        }
    }
}

impl DeepSeekValidationConfig {
    /// Create configuration for maximum validation rigor
    pub fn rigorous() -> Self {
        Self {
            enable_statistical_testing: true,
            enable_compliance_validation: true,
            enable_meta_cognition: true,
            temperature: 0.05,    // Very deterministic
            min_confidence: 0.85, // High threshold
            ..Default::default()
        }
    }

    /// Create configuration for performance-optimized validation
    pub fn performance() -> Self {
        Self {
            enable_statistical_testing: false,
            enable_compliance_validation: true, // Keep compliance on
            enable_meta_cognition: false,
            temperature: 0.2, // Slightly higher for speed
            max_tokens: 2000, // Reduced token limit
            ..Default::default()
        }
    }
}

/// Statistical significance test result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatisticalResult {
    /// Whether result is statistically significant
    pub significant: bool,
    /// p-value from significance test
    pub p_value: Option<f64>,
    /// Confidence interval (if calculable)
    pub confidence_interval: Option<(f64, f64)>,
    /// Sample size used for calculation
    pub sample_size: Option<usize>,
}

/// Compliance validation result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComplianceResult {
    /// GDPR compliance status
    pub gdpr_compliance: ComplianceStatus,
    /// Bias detection results
    pub bias_detection: BiasDetectionResult,
    /// Regulatory alignment assessment
    pub regulatory_alignment: RegulatoryStatus,
    /// Compliance violations found
    pub violations: Vec<ComplianceViolation>,
}

/// Meta-cognitive assessment result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetaCognitiveResult {
    /// Reasoning quality score (1-100)
    pub reasoning_quality: f64,
    /// Methodology evaluation
    pub methodology_quality: MethodologyStatus,
    /// Cognitive biases detected
    pub cognitive_biases: Vec<String>,
    /// Improvement recommendations
    pub recommendations: Vec<String>,
}

/// Complete validation result for a reasoning chain
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeepSeekValidationResult {
    /// Overall validation verdict
    pub verdict: ValidationVerdict,
    /// Chain integrity validation
    pub chain_integrity: ChainIntegrityResult,
    /// Statistical significance results (if enabled)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub statistical_results: Option<StatisticalResult>,
    /// Compliance validation results (if enabled)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub compliance_results: Option<ComplianceResult>,
    /// Meta-cognitive assessment (if enabled)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub meta_cognitive_results: Option<MetaCognitiveResult>,
    /// Validation confidence score
    pub validation_confidence: f64,
    /// Detailed validation findings
    pub findings: Vec<ValidationFinding>,
    /// Tokens consumed during validation
    pub tokens_used: TokenUsage,
    /// Performance metrics
    pub performance: ValidationPerformance,
}

use std::ops::{Add, Mul, Sub};

/// Chain integrity validation result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChainIntegrityResult {
    /// Logical flow consistency
    pub logical_flow: LogicalFlowStatus,
    /// Step dependency validation
    pub step_dependencies: DependencyStatus,
    /// Confidence progression analysis
    pub confidence_progression: ProgressionStatus,
    /// Gap analysis between steps
    pub gaps_detected: Vec<String>,
    /// Continuity score (0-1)
    pub continuity_score: f64,
}

impl Add<f32> for ChainIntegrityResult {
    type Output = f32;

    fn add(self, rhs: f32) -> Self::Output {
        self.continuity_score as f32 + rhs
    }
}

impl Sub<&f32> for ChainIntegrityResult {
    type Output = f32;

    fn sub(self, rhs: &f32) -> Self::Output {
        self.continuity_score as f32 - *rhs
    }
}

impl Mul<f64> for ChainIntegrityResult {
    type Output = f64;

    fn mul(self, rhs: f64) -> Self::Output {
        self.continuity_score * rhs
    }
}

impl Mul<f32> for ChainIntegrityResult {
    type Output = f32;

    fn mul(self, rhs: f32) -> Self::Output {
        self.continuity_score as f32 * rhs
    }
}

// Support Copy for f32 operations if needed, but the struct has Vec so it can't be Copy.
// We implement operations that consume self or take reference.

/// Validation performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationPerformance {
    /// Validation duration in milliseconds
    pub duration_ms: u64,
    /// Tokens per second (throughput)
    pub tokens_per_second: f64,
    /// Memory usage estimation
    pub memory_usage_mb: f64,
}

impl ValidationPerformance {
    pub fn new(duration_ms: u64, tokens_per_second: f64, memory_usage_mb: f64) -> Self {
        Self {
            duration_ms,
            tokens_per_second,
            memory_usage_mb,
        }
    }
}

impl Default for ValidationPerformance {
    fn default() -> Self {
        Self::new(0, 0.0, 0.0)
    }
}

/// Validation finding details
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationFinding {
    /// Finding category
    pub category: ValidationCategory,
    /// Severity level
    pub severity: Severity,
    /// Description of the finding
    pub description: String,
    /// Step(s) where finding applies
    pub affected_steps: Vec<String>,
    /// Evidence supporting the finding
    pub evidence: Vec<String>,
    /// Recommended actions
    pub recommendations: Vec<String>,
}

/// Validation categories
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ValidationCategory {
    LogicalFlow,
    StatisticalSignificance,
    Compliance,
    BiasDetection,
    Methodology,
    ConfidenceScoring,
    TokenEfficiency,
    EnterpriseCompliance,
}

/// Severity levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Severity {
    Critical,
    High,
    Medium,
    Low,
    Info,
}

/// Validation verdict
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ValidationVerdict {
    Validated,
    PartiallyValidated,
    NeedsImprovement,
    Invalid,
    CriticalIssues,
}

/// Logical flow status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LogicalFlowStatus {
    Excellent,
    Good,
    Satisfactory,
    NeedsImprovement,
    Poor,
}

/// Dependency status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DependencyStatus {
    FullySatisfied,
    MostlySatisfied,
    PartiallySatisfied,
    MostlyUnsatisfied,
    Unsatisfied,
}

/// Progression status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProgressionStatus {
    Monotonic,
    SlowlyDecaying,
    Erratic,
    Unstable,
}

/// Compliance status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ComplianceStatus {
    Compliant,
    MinorIssues,
    NonCompliant,
    CriticalNonCompliance,
}

/// Bias detection result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BiasDetectionResult {
    /// Overall bias level
    pub overall_bias: BiasLevel,
    /// Detected biases
    pub detected_biases: Vec<DetectedBias>,
    /// Bias mitigation recommendations
    pub mitigation_recommendations: Vec<String>,
}

/// Bias level classification
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BiasLevel {
    Minimal,
    Low,
    Moderate,
    High,
    Severe,
}

/// Detected bias details
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DetectedBias {
    /// Bias type
    pub bias_type: String,
    /// Evidence of bias
    pub evidence: String,
    /// Severity
    pub severity: Severity,
    /// Confidence of detection
    pub detection_confidence: f64,
}

/// Regulatory status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RegulatoryStatus {
    FullyCompliant,
    NeedsValidation,
    RequiresUpdates,
    NonCompliant,
}

/// Compliance violation details
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComplianceViolation {
    /// Violation type
    pub violation_type: String,
    /// Severity
    pub severity: Severity,
    /// Description
    pub description: String,
    /// Remediation steps
    pub remediation: Vec<String>,
}

/// Methodology status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MethodologyStatus {
    Excellent,
    Good,
    Adequate,
    NeedsImprovement,
    Poor,
}

/// Token usage tracking
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TokenUsage {
    pub input_tokens: u32,
    pub output_tokens: u32,
    pub total_tokens: u32,
    pub cost_usd: f64,
}

impl TokenUsage {
    pub fn add(&mut self, other: &TokenUsage) {
        self.input_tokens += other.input_tokens;
        self.output_tokens += other.output_tokens;
        self.total_tokens += other.total_tokens;
        self.cost_usd += other.cost_usd;
    }

    pub fn new(input: u32, output: u32, cost: f64) -> Self {
        Self {
            input_tokens: input,
            output_tokens: output,
            total_tokens: input + output,
            cost_usd: cost,
        }
    }
}

/// DeepSeek Protocol Validation Engine
pub struct DeepSeekValidationEngine {
    config: DeepSeekValidationConfig,
    llm_client: UnifiedLlmClient,
}

impl DeepSeekValidationEngine {
    /// Create a new validation engine with default configuration
    pub fn new() -> Result<Self> {
        Self::with_config(DeepSeekValidationConfig::default())
    }

    /// Create validation engine with custom configuration
    pub fn with_config(config: DeepSeekValidationConfig) -> Result<Self> {
        // Configure LLM client for DeepSeek
        let llm_config = super::llm::LlmConfig {
            provider: super::llm::LlmProvider::DeepSeek,
            model: config.model.clone(),
            temperature: config.temperature as f64,
            max_tokens: config.max_tokens,
            ..Default::default()
        };

        let llm_client = UnifiedLlmClient::new(llm_config)?;

        Ok(Self { config, llm_client })
    }

    /// Validate a complete reasoning chain using DeepSeek V3
    ///
    /// This performs comprehensive validation including:
    /// - Reasoning chain integrity
    /// - Statistical significance testing
    /// - Compliance validation
    /// - Meta-cognitive assessment
    pub async fn validate_chain(
        &self,
        protocol_output: &ProtocolOutput,
        original_input: &ProtocolInput,
        trace: &ExecutionTrace,
    ) -> Result<DeepSeekValidationResult> {
        let start = std::time::Instant::now();

        // Build validation prompt with chain details
        let validation_prompt =
            self.build_validation_prompt(protocol_output, original_input, trace);

        // Execute DeepSeek validation
        let (validation_response, tokens) = self.execute_validation(&validation_prompt).await?;

        // Parse validation results
        let validation_result =
            self.parse_validation_response(&validation_response, protocol_output)?;

        let duration_ms = start.elapsed().as_millis() as u64;

        // Calculate performance metrics
        let performance = ValidationPerformance {
            duration_ms,
            tokens_per_second: (tokens.total_tokens as f64) / (duration_ms as f64 / 1000.0),
            memory_usage_mb: self.estimate_memory_usage(protocol_output, trace),
        };

        // Combine results
        let mut result = validation_result;
        result.tokens_used = tokens;
        result.performance = performance;

        Ok(result)
    }

    /// Validate a reasoning chain with statistical significance testing
    ///
    /// This performs bootstrap resampling and statistical tests to validate
    /// the statistical significance of the reasoning chain's conclusions.
    pub async fn validate_with_statistical_significance(
        &self,
        protocol_output: &ProtocolOutput,
        original_input: &ProtocolInput,
        trace: &ExecutionTrace,
    ) -> Result<DeepSeekValidationResult> {
        if !self.config.enable_statistical_testing {
            return self
                .validate_chain(protocol_output, original_input, trace)
                .await;
        }

        let mut result = self
            .validate_chain(protocol_output, original_input, trace)
            .await?;

        // Add statistical significance testing
        let statistical_results = self.perform_statistical_tests(protocol_output).await?;
        result.statistical_results = Some(statistical_results);

        Ok(result)
    }

    /// Quick validation - performance optimized for high-throughput
    pub async fn validate_quick(
        &self,
        protocol_output: &ProtocolOutput,
        original_input: &ProtocolInput,
    ) -> Result<DeepSeekValidationResult> {
        let config = DeepSeekValidationConfig::performance();
        let quick_engine = Self::with_config(config)?;

        quick_engine
            .validate_chain(protocol_output, original_input, &ExecutionTrace::default())
            .await
    }

    /// Rigorous validation - maximum scrutiny
    pub async fn validate_rigorous(
        &self,
        protocol_output: &ProtocolOutput,
        original_input: &ProtocolInput,
        trace: &ExecutionTrace,
    ) -> Result<DeepSeekValidationResult> {
        let config = DeepSeekValidationConfig::rigorous();
        let rigorous_engine = Self::with_config(config)?;

        let result = rigorous_engine
            .validate_chain(protocol_output, original_input, trace)
            .await?;

        // Add additional rigorous checks
        let enhanced_result = self
            .enhance_rigorous_validation(result, protocol_output)
            .await?;

        Ok(enhanced_result)
    }

    /// Build comprehensive validation prompt
    fn build_validation_prompt(
        &self,
        protocol_output: &ProtocolOutput,
        original_input: &ProtocolInput,
        _trace: &ExecutionTrace,
    ) -> String {
        let mut prompt = String::new();

        prompt.push_str("# REASONING CHAIN VALIDATION ANALYSIS\n\n");

        prompt.push_str(&format!(
            concat!(
                "**Protocol**: {}\n",
                "**Input**: {}\n",
                "**Chain Length**: {} steps\n",
                "**Overall Confidence**: {:.1}%\n\n",
            ),
            protocol_output.protocol_id,
            self.summarize_input(original_input),
            protocol_output.steps.len(),
            protocol_output.confidence * 100.0
        ));

        prompt.push_str(&format!(
            "## STEP-BY-STEP ANALYSIS\n\n{}",
            self.format_step_analysis(protocol_output)
        ));

        if self.config.enable_compliance_validation {
            prompt.push_str(&format!(
                "\n\n## COMPLIANCE VALIDATION\n\n{}",
                self.build_compliance_section(protocol_output)
            ));
        }

        if self.config.enable_meta_cognition {
            prompt.push_str(&format!(
                "\n\n## META-COGNITIVE ASSESSMENT\n\n{}",
                self.build_meta_cognitive_section(protocol_output)
            ));
        }

        prompt.push_str(&format!(
            "\n\n## VALIDATION INSTRUCTIONS\n\n{}",
            self.build_validation_instructions()
        ));

        prompt
    }

    /// Execute validation using DeepSeek LLM
    async fn execute_validation(&self, prompt: &str) -> Result<(String, TokenUsage)> {
        let system_prompt = self.build_validation_system_prompt();

        let request = LlmRequest::new(prompt)
            .with_system(&system_prompt)
            .with_temperature(self.config.temperature.into())
            .with_max_tokens(self.config.max_tokens);

        let response = self.llm_client.complete(request).await?;

        let tokens = TokenUsage::new(
            response.usage.input_tokens,
            response.usage.output_tokens,
            response.usage.cost_usd(&self.config.model),
        );

        Ok((response.content, tokens))
    }

    /// Parse DeepSeek validation response
    fn parse_validation_response(
        &self,
        response: &str,
        protocol_output: &ProtocolOutput,
    ) -> Result<DeepSeekValidationResult> {
        // Extract structured validation results from response
        let (verdict, validation_confidence) = self.extract_verdict_and_confidence(response);
        let chain_integrity = self.extract_chain_integrity(response, protocol_output);
        let findings = self.extract_findings(response);

        let mut result = DeepSeekValidationResult {
            verdict,
            chain_integrity,
            statistical_results: None,
            compliance_results: None,
            meta_cognitive_results: None,
            validation_confidence,
            findings,
            tokens_used: TokenUsage::default(),
            performance: ValidationPerformance::default(),
        };

        // Extract additional validation sections if enabled
        if self.config.enable_compliance_validation {
            result.compliance_results = self.extract_compliance_results(response);
        }

        if self.config.enable_meta_cognition {
            result.meta_cognitive_results = self.extract_meta_cognitive_results(response);
        }

        Ok(result)
    }

    // Helper methods for parsing validation responses
    fn extract_verdict_and_confidence(&self, response: &str) -> (ValidationVerdict, f64) {
        // Sample parsing logic - in production, this would use more sophisticated parsing
        let verdict = if response.to_lowercase().contains("validated") {
            ValidationVerdict::Validated
        } else if response.to_lowercase().contains("partially") {
            ValidationVerdict::PartiallyValidated
        } else if response.to_lowercase().contains("critical") {
            ValidationVerdict::CriticalIssues
        } else {
            ValidationVerdict::NeedsImprovement
        };

        let confidence = self.extract_confidence_score(response).unwrap_or(0.7);

        (verdict, confidence)
    }

    fn extract_confidence_score(&self, text: &str) -> Option<f64> {
        let re = Regex::new(r"[Cc]onfidence:?\s*(\d+\.?\d*)").ok()?;
        re.captures(text)
            .and_then(|caps| caps.get(1))
            .and_then(|m| m.as_str().parse::<f64>().ok())
            .map(|v| v.min(1.0))
    }

    fn extract_chain_integrity(
        &self,
        _response: &str,
        _output: &ProtocolOutput,
    ) -> ChainIntegrityResult {
        // Simplified extraction - production would use more sophisticated parsing
        ChainIntegrityResult {
            logical_flow: LogicalFlowStatus::Good,
            step_dependencies: DependencyStatus::FullySatisfied,
            confidence_progression: ProgressionStatus::Monotonic,
            gaps_detected: Vec::new(),
            continuity_score: 0.85,
        }
    }

    fn extract_findings(&self, _response: &str) -> Vec<ValidationFinding> {
        // Placeholder - production would parse findings from response
        vec![]
    }

    fn extract_compliance_results(&self, _response: &str) -> Option<ComplianceResult> {
        // Placeholder - production would parse compliance results
        None
    }

    fn extract_meta_cognitive_results(&self, _response: &str) -> Option<MetaCognitiveResult> {
        // Placeholder - production would parse meta-cognitive results
        None
    }

    fn summarize_input(&self, input: &ProtocolInput) -> String {
        if let Some(query) = input.get_str("query") {
            if query.len() > 100 {
                format!("{}...", &query[..100])
            } else {
                query.to_string()
            }
        } else {
            "Complex multi-field input".to_string()
        }
    }

    fn format_step_analysis(&self, output: &ProtocolOutput) -> String {
        let mut analysis = String::new();

        for (i, step) in output.steps.iter().enumerate() {
            analysis.push_str(&format!(
                concat!(
                    "### Step {}: {}\n",
                    "- **Confidence**: {:.1}%\n",
                    "- **Status**: {}\n",
                    "- **Duration**: {}ms\n\n",
                ),
                i + 1,
                step.step_id,
                step.confidence * 100.0,
                if step.success { "Success" } else { "Failed" },
                step.duration_ms
            ));
        }

        analysis
    }

    fn build_compliance_section(&self, _output: &ProtocolOutput) -> String {
        "Evaluate GDPR compliance, bias detection, and regulatory alignment.".to_string()
    }

    fn build_meta_cognitive_section(&self, _output: &ProtocolOutput) -> String {
        "Assess reasoning methodology, cognitive biases, and improvement recommendations."
            .to_string()
    }

    fn build_validation_instructions(&self) -> String {
        let mut instructions = String::new();

        instructions.push_str(&format!(
            concat!(
                "**Validation Criteria**:\n",
                "1. Logical Flow Analysis - Check step sequencing and dependency satisfaction\n",
                "2. Confidence Progression - Analyze confidence trends across steps\n",
                "3. Gap Detection - Identify missing reasoning steps or assumptions\n",
                "4. Statistical Significance - {}\n",
                "5. Compliance Assessment - {}\n",
                "6. Meta-Cognitive Evaluation - {}\n\n",
            ),
            if self.config.enable_statistical_testing {
                "Enabled"
            } else {
                "Disabled"
            },
            if self.config.enable_compliance_validation {
                "Enabled"
            } else {
                "Disabled"
            },
            if self.config.enable_meta_cognition {
                "Enabled"
            } else {
                "Disabled"
            }
        ));

        instructions
    }

    fn build_validation_system_prompt(&self) -> String {
        concat!(
            "You are DeepSeek V3 (671B parameters), a reasoning chain validation expert.\n",
            "Your task is to validate AI reasoning chains for logical integrity, statistical significance, compliance, and reasoning quality.\n",
            "\n",
            "**Validation Guidelines**:\n",
            "1. BE BRUTALLY HONEST in your assessment\n",
            "2. Use DeepSeek's 671B parameter scale to detect subtle reasoning flaws\n",
            "3. Apply cross-cultural intelligence to detect biases\n",
            "4. Provide statistical rigor in confidence assessment\n",
            "5. Assess compliance with GDPR and other regulations\n",
            "6. Evaluate reasoning methodology and cognitive processes\n",
            "\n",
            "**Output Format**:\n",
            "- Overall verdict (Validated/PartiallyValidated/Invalid/CriticalIssues)\n",
            "- Chain integrity analysis\n",
            "- Statistical significance (if applicable)\n",
            "- Compliance assessment\n",
            "- Meta-cognitive evaluation\n",
            "- Detailed findings with severity levels\n",
            "- Confidence score for validation (0.0-1.0)\n",
        )
        .to_string()
    }

    async fn perform_statistical_tests(
        &self,
        _output: &ProtocolOutput,
    ) -> Result<StatisticalResult> {
        // Placeholder for statistical testing implementation
        // In production, this would perform bootstrap resampling, hypothesis testing, etc.
        Ok(StatisticalResult {
            significant: true,
            p_value: Some(0.03),
            confidence_interval: Some((0.75, 0.95)),
            sample_size: Some(1000),
        })
    }

    fn estimate_memory_usage(&self, _output: &ProtocolOutput, _trace: &ExecutionTrace) -> f64 {
        // Estimate memory usage based on chain complexity
        50.0 // Conservative estimate in MB
    }

    async fn enhance_rigorous_validation(
        &self,
        mut result: DeepSeekValidationResult,
        _output: &ProtocolOutput,
    ) -> Result<DeepSeekValidationResult> {
        // Add rigorous validation enhancements
        result.validation_confidence *= 0.9; // Apply rigorous discount
        Ok(result)
    }
}

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

// ═══════════════════════════════════════════════════════════════════════════════════════════════
// COMPREHENSIVE TEST SUITE
// ═══════════════════════════════════════════════════════════════════════════════════════════════

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

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // CONFIGURATION TESTS
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod config_tests {
        use super::*;

        #[test]
        fn test_default_config_values() {
            let config = DeepSeekValidationConfig::default();

            assert_eq!(config.model, "deepseek-chat");
            assert!((config.temperature - 0.1).abs() < f32::EPSILON);
            assert_eq!(config.max_tokens, 4000);
            assert!(!config.enable_statistical_testing);
            assert!((config.alpha - 0.05).abs() < f64::EPSILON);
            assert!(config.enable_compliance_validation);
            assert!(!config.enable_meta_cognition);
            assert!((config.min_confidence - 0.70).abs() < f64::EPSILON);
            assert_eq!(config.max_chain_length, 20);
        }

        #[test]
        fn test_rigorous_config() {
            let config = DeepSeekValidationConfig::rigorous();

            assert!(config.enable_statistical_testing);
            assert!(config.enable_compliance_validation);
            assert!(config.enable_meta_cognition);
            assert!((config.temperature - 0.05).abs() < f32::EPSILON);
            assert!((config.min_confidence - 0.85).abs() < f64::EPSILON);
        }

        #[test]
        fn test_performance_config() {
            let config = DeepSeekValidationConfig::performance();

            assert!(!config.enable_statistical_testing);
            assert!(config.enable_compliance_validation);
            assert!(!config.enable_meta_cognition);
            assert!((config.temperature - 0.2).abs() < f32::EPSILON);
            assert_eq!(config.max_tokens, 2000);
        }

        #[test]
        fn test_config_serialization_roundtrip() {
            let config = DeepSeekValidationConfig::rigorous();
            let json = serde_json::to_string(&config).unwrap();
            let deserialized: DeepSeekValidationConfig = serde_json::from_str(&json).unwrap();

            assert_eq!(config.model, deserialized.model);
            assert!((config.temperature - deserialized.temperature).abs() < f32::EPSILON);
            assert_eq!(
                config.enable_statistical_testing,
                deserialized.enable_statistical_testing
            );
        }

        #[test]
        fn test_config_deserialization_with_defaults() {
            // Minimal JSON - should use defaults for missing fields
            let json = r#"{"model": "custom-model"}"#;
            let config: DeepSeekValidationConfig = serde_json::from_str(json).unwrap();

            assert_eq!(config.model, "custom-model");
            // All other fields should have defaults
            assert!((config.temperature - 0.1).abs() < f32::EPSILON);
            assert_eq!(config.max_tokens, 4000);
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // TOKEN USAGE TESTS
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod token_usage_tests {
        use super::*;

        #[test]
        fn test_token_usage_new() {
            let usage = TokenUsage::new(100, 50, 0.01);

            assert_eq!(usage.input_tokens, 100);
            assert_eq!(usage.output_tokens, 50);
            assert_eq!(usage.total_tokens, 150);
            assert!((usage.cost_usd - 0.01).abs() < f64::EPSILON);
        }

        #[test]
        fn test_token_usage_default() {
            let usage = TokenUsage::default();

            assert_eq!(usage.input_tokens, 0);
            assert_eq!(usage.output_tokens, 0);
            assert_eq!(usage.total_tokens, 0);
            assert!((usage.cost_usd - 0.0).abs() < f64::EPSILON);
        }

        #[test]
        fn test_token_usage_add() {
            let mut usage1 = TokenUsage::new(100, 50, 0.01);
            let usage2 = TokenUsage::new(200, 100, 0.02);

            usage1.add(&usage2);

            assert_eq!(usage1.input_tokens, 300);
            assert_eq!(usage1.output_tokens, 150);
            assert_eq!(usage1.total_tokens, 450);
            assert!((usage1.cost_usd - 0.03).abs() < f64::EPSILON);
        }

        #[test]
        fn test_token_usage_add_to_zero() {
            let mut usage = TokenUsage::default();
            let other = TokenUsage::new(500, 250, 0.05);

            usage.add(&other);

            assert_eq!(usage.input_tokens, 500);
            assert_eq!(usage.output_tokens, 250);
            assert_eq!(usage.total_tokens, 750);
        }

        #[test]
        fn test_token_usage_boundary_max_u32() {
            let usage = TokenUsage::new(u32::MAX - 10, 5, 0.0);
            assert_eq!(usage.total_tokens, u32::MAX - 5);
        }

        #[test]
        fn test_token_usage_serialization() {
            let usage = TokenUsage::new(1000, 500, 0.123);
            let json = serde_json::to_string(&usage).unwrap();
            let deserialized: TokenUsage = serde_json::from_str(&json).unwrap();

            assert_eq!(usage.input_tokens, deserialized.input_tokens);
            assert_eq!(usage.output_tokens, deserialized.output_tokens);
            assert_eq!(usage.total_tokens, deserialized.total_tokens);
            assert!((usage.cost_usd - deserialized.cost_usd).abs() < 0.0001);
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // CHAIN INTEGRITY RESULT ARITHMETIC TESTS
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod chain_integrity_arithmetic_tests {
        use super::*;

        fn create_chain_integrity(continuity_score: f64) -> ChainIntegrityResult {
            ChainIntegrityResult {
                logical_flow: LogicalFlowStatus::Good,
                step_dependencies: DependencyStatus::FullySatisfied,
                confidence_progression: ProgressionStatus::Monotonic,
                gaps_detected: vec![],
                continuity_score,
            }
        }

        #[test]
        fn test_chain_integrity_add_f32() {
            let result = create_chain_integrity(0.85);
            let sum = result + 0.15f32;
            assert!((sum - 1.0).abs() < 0.0001);
        }

        #[test]
        fn test_chain_integrity_sub_f32_ref() {
            let result = create_chain_integrity(0.85);
            let diff = result - &0.35f32;
            assert!((diff - 0.5).abs() < 0.0001);
        }

        #[test]
        fn test_chain_integrity_mul_f64() {
            let result = create_chain_integrity(0.5);
            let product = result * 2.0f64;
            assert!((product - 1.0).abs() < f64::EPSILON);
        }

        #[test]
        fn test_chain_integrity_mul_f32() {
            let result = create_chain_integrity(0.5);
            let product = result * 4.0f32;
            assert!((product - 2.0).abs() < 0.0001);
        }

        #[test]
        fn test_chain_integrity_boundary_zero() {
            let result = create_chain_integrity(0.0);
            let product = result * 100.0f64;
            assert!((product - 0.0).abs() < f64::EPSILON);
        }

        #[test]
        fn test_chain_integrity_boundary_one() {
            let result = create_chain_integrity(1.0);
            let product = result * 0.5f64;
            assert!((product - 0.5).abs() < f64::EPSILON);
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // VALIDATION PERFORMANCE TESTS
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod validation_performance_tests {
        use super::*;

        #[test]
        fn test_validation_performance_new() {
            let perf = ValidationPerformance::new(1000, 500.0, 128.5);

            assert_eq!(perf.duration_ms, 1000);
            assert!((perf.tokens_per_second - 500.0).abs() < f64::EPSILON);
            assert!((perf.memory_usage_mb - 128.5).abs() < f64::EPSILON);
        }

        #[test]
        fn test_validation_performance_default() {
            let perf = ValidationPerformance::default();

            assert_eq!(perf.duration_ms, 0);
            assert!((perf.tokens_per_second - 0.0).abs() < f64::EPSILON);
            assert!((perf.memory_usage_mb - 0.0).abs() < f64::EPSILON);
        }

        #[test]
        fn test_validation_performance_serialization() {
            let perf = ValidationPerformance::new(5000, 1200.5, 256.0);
            let json = serde_json::to_string(&perf).unwrap();
            let deserialized: ValidationPerformance = serde_json::from_str(&json).unwrap();

            assert_eq!(perf.duration_ms, deserialized.duration_ms);
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // ENUM SERIALIZATION TESTS
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod enum_serialization_tests {
        use super::*;

        #[test]
        fn test_validation_verdict_serialization() {
            let cases = [
                (ValidationVerdict::Validated, "\"validated\""),
                (
                    ValidationVerdict::PartiallyValidated,
                    "\"partially_validated\"",
                ),
                (ValidationVerdict::NeedsImprovement, "\"needs_improvement\""),
                (ValidationVerdict::Invalid, "\"invalid\""),
                (ValidationVerdict::CriticalIssues, "\"critical_issues\""),
            ];

            for (verdict, expected_json) in cases {
                let json = serde_json::to_string(&verdict).unwrap();
                assert_eq!(json, expected_json);

                let deserialized: ValidationVerdict = serde_json::from_str(&json).unwrap();
                assert_eq!(verdict, deserialized);
            }
        }

        #[test]
        fn test_severity_serialization() {
            let cases = [
                (Severity::Critical, "\"critical\""),
                (Severity::High, "\"high\""),
                (Severity::Medium, "\"medium\""),
                (Severity::Low, "\"low\""),
                (Severity::Info, "\"info\""),
            ];

            for (severity, expected_json) in cases {
                let json = serde_json::to_string(&severity).unwrap();
                assert_eq!(json, expected_json);

                let deserialized: Severity = serde_json::from_str(&json).unwrap();
                assert_eq!(severity, deserialized);
            }
        }

        #[test]
        fn test_bias_level_serialization() {
            let cases = [
                (BiasLevel::Minimal, "\"minimal\""),
                (BiasLevel::Low, "\"low\""),
                (BiasLevel::Moderate, "\"moderate\""),
                (BiasLevel::High, "\"high\""),
                (BiasLevel::Severe, "\"severe\""),
            ];

            for (level, expected_json) in cases {
                let json = serde_json::to_string(&level).unwrap();
                assert_eq!(json, expected_json);
            }
        }

        #[test]
        fn test_logical_flow_status_serialization() {
            let status = LogicalFlowStatus::Excellent;
            let json = serde_json::to_string(&status).unwrap();
            assert_eq!(json, "\"excellent\"");
        }

        #[test]
        fn test_dependency_status_serialization() {
            let status = DependencyStatus::PartiallySatisfied;
            let json = serde_json::to_string(&status).unwrap();
            assert_eq!(json, "\"partially_satisfied\"");
        }

        #[test]
        fn test_progression_status_serialization() {
            let status = ProgressionStatus::SlowlyDecaying;
            let json = serde_json::to_string(&status).unwrap();
            assert_eq!(json, "\"slowly_decaying\"");
        }

        #[test]
        fn test_compliance_status_serialization() {
            let status = ComplianceStatus::CriticalNonCompliance;
            let json = serde_json::to_string(&status).unwrap();
            assert_eq!(json, "\"critical_non_compliance\"");
        }

        #[test]
        fn test_regulatory_status_serialization() {
            let status = RegulatoryStatus::NeedsValidation;
            let json = serde_json::to_string(&status).unwrap();
            assert_eq!(json, "\"needs_validation\"");
        }

        #[test]
        fn test_methodology_status_serialization() {
            let status = MethodologyStatus::NeedsImprovement;
            let json = serde_json::to_string(&status).unwrap();
            assert_eq!(json, "\"needs_improvement\"");
        }

        #[test]
        fn test_validation_category_serialization() {
            let category = ValidationCategory::EnterpriseCompliance;
            let json = serde_json::to_string(&category).unwrap();
            assert_eq!(json, "\"enterprise_compliance\"");
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // STATISTICAL RESULT TESTS
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod statistical_result_tests {
        use super::*;

        #[test]
        fn test_statistical_result_significant() {
            let result = StatisticalResult {
                significant: true,
                p_value: Some(0.01),
                confidence_interval: Some((0.80, 0.95)),
                sample_size: Some(1000),
            };

            assert!(result.significant);
            assert!(result.p_value.unwrap() < 0.05);
        }

        #[test]
        fn test_statistical_result_not_significant() {
            let result = StatisticalResult {
                significant: false,
                p_value: Some(0.15),
                confidence_interval: None,
                sample_size: Some(50),
            };

            assert!(!result.significant);
            assert!(result.p_value.unwrap() > 0.05);
        }

        #[test]
        fn test_statistical_result_serialization() {
            let result = StatisticalResult {
                significant: true,
                p_value: Some(0.03),
                confidence_interval: Some((0.75, 0.95)),
                sample_size: Some(500),
            };

            let json = serde_json::to_string(&result).unwrap();
            let deserialized: StatisticalResult = serde_json::from_str(&json).unwrap();

            assert_eq!(result.significant, deserialized.significant);
            assert_eq!(result.p_value, deserialized.p_value);
            assert_eq!(result.confidence_interval, deserialized.confidence_interval);
        }

        #[test]
        fn test_statistical_result_boundary_p_value() {
            // Test boundary at alpha = 0.05
            let exactly_significant = StatisticalResult {
                significant: true,
                p_value: Some(0.049),
                confidence_interval: None,
                sample_size: None,
            };
            assert!(exactly_significant.p_value.unwrap() < 0.05);

            let not_significant = StatisticalResult {
                significant: false,
                p_value: Some(0.051),
                confidence_interval: None,
                sample_size: None,
            };
            assert!(not_significant.p_value.unwrap() > 0.05);
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // VALIDATION FINDING TESTS
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod validation_finding_tests {
        use super::*;

        #[test]
        fn test_validation_finding_creation() {
            let finding = ValidationFinding {
                category: ValidationCategory::LogicalFlow,
                severity: Severity::High,
                description: "Gap detected between steps 2 and 3".to_string(),
                affected_steps: vec!["step_2".to_string(), "step_3".to_string()],
                evidence: vec!["Missing intermediate reasoning".to_string()],
                recommendations: vec!["Add bridging step".to_string()],
            };

            assert_eq!(finding.category, ValidationCategory::LogicalFlow);
            assert_eq!(finding.severity, Severity::High);
            assert_eq!(finding.affected_steps.len(), 2);
        }

        #[test]
        fn test_validation_finding_with_unicode() {
            let finding = ValidationFinding {
                category: ValidationCategory::BiasDetection,
                severity: Severity::Medium,
                description: "Unicode test: \u{1F4A1} \u{1F4CA} \u{2705}".to_string(),
                affected_steps: vec!["step_\u{03B1}".to_string()],
                evidence: vec!["Evidence with emoji: \u{1F914}".to_string()],
                recommendations: vec!["\u{2728} Improve coverage".to_string()],
            };

            let json = serde_json::to_string(&finding).unwrap();
            let deserialized: ValidationFinding = serde_json::from_str(&json).unwrap();

            assert!(deserialized.description.contains('\u{1F4A1}'));
        }

        #[test]
        fn test_validation_finding_empty_vectors() {
            let finding = ValidationFinding {
                category: ValidationCategory::Methodology,
                severity: Severity::Info,
                description: "Minor observation".to_string(),
                affected_steps: vec![],
                evidence: vec![],
                recommendations: vec![],
            };

            assert!(finding.affected_steps.is_empty());
            assert!(finding.evidence.is_empty());
            assert!(finding.recommendations.is_empty());
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // DETECTED BIAS TESTS
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod detected_bias_tests {
        use super::*;

        #[test]
        fn test_detected_bias_creation() {
            let bias = DetectedBias {
                bias_type: "confirmation_bias".to_string(),
                evidence: "Selectively interpreting data".to_string(),
                severity: Severity::Medium,
                detection_confidence: 0.85,
            };

            assert_eq!(bias.bias_type, "confirmation_bias");
            assert!((bias.detection_confidence - 0.85).abs() < f64::EPSILON);
        }

        #[test]
        fn test_detected_bias_confidence_boundaries() {
            let low_confidence = DetectedBias {
                bias_type: "anchoring".to_string(),
                evidence: "Weak signal".to_string(),
                severity: Severity::Low,
                detection_confidence: 0.0,
            };
            assert!((low_confidence.detection_confidence - 0.0).abs() < f64::EPSILON);

            let high_confidence = DetectedBias {
                bias_type: "anchoring".to_string(),
                evidence: "Strong signal".to_string(),
                severity: Severity::Critical,
                detection_confidence: 1.0,
            };
            assert!((high_confidence.detection_confidence - 1.0).abs() < f64::EPSILON);
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // COMPLIANCE VIOLATION TESTS
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod compliance_violation_tests {
        use super::*;

        #[test]
        fn test_compliance_violation_creation() {
            let violation = ComplianceViolation {
                violation_type: "GDPR_ARTICLE_17".to_string(),
                severity: Severity::Critical,
                description: "Right to erasure not implemented".to_string(),
                remediation: vec![
                    "Implement data deletion endpoint".to_string(),
                    "Add audit logging".to_string(),
                ],
            };

            assert_eq!(violation.violation_type, "GDPR_ARTICLE_17");
            assert_eq!(violation.remediation.len(), 2);
        }

        #[test]
        fn test_compliance_violation_serialization() {
            let violation = ComplianceViolation {
                violation_type: "SOX_COMPLIANCE".to_string(),
                severity: Severity::High,
                description: "Audit trail incomplete".to_string(),
                remediation: vec!["Add comprehensive logging".to_string()],
            };

            let json = serde_json::to_string(&violation).unwrap();
            assert!(json.contains("SOX_COMPLIANCE"));

            let deserialized: ComplianceViolation = serde_json::from_str(&json).unwrap();
            assert_eq!(violation.violation_type, deserialized.violation_type);
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // BIAS DETECTION RESULT TESTS
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod bias_detection_result_tests {
        use super::*;

        #[test]
        fn test_bias_detection_result_no_biases() {
            let result = BiasDetectionResult {
                overall_bias: BiasLevel::Minimal,
                detected_biases: vec![],
                mitigation_recommendations: vec![],
            };

            assert_eq!(result.overall_bias, BiasLevel::Minimal);
            assert!(result.detected_biases.is_empty());
        }

        #[test]
        fn test_bias_detection_result_multiple_biases() {
            let result = BiasDetectionResult {
                overall_bias: BiasLevel::Moderate,
                detected_biases: vec![
                    DetectedBias {
                        bias_type: "confirmation".to_string(),
                        evidence: "Evidence 1".to_string(),
                        severity: Severity::Medium,
                        detection_confidence: 0.7,
                    },
                    DetectedBias {
                        bias_type: "availability".to_string(),
                        evidence: "Evidence 2".to_string(),
                        severity: Severity::Low,
                        detection_confidence: 0.6,
                    },
                ],
                mitigation_recommendations: vec![
                    "Consider alternative perspectives".to_string(),
                    "Use structured decision frameworks".to_string(),
                ],
            };

            assert_eq!(result.detected_biases.len(), 2);
            assert_eq!(result.mitigation_recommendations.len(), 2);
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // META-COGNITIVE RESULT TESTS
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod meta_cognitive_result_tests {
        use super::*;

        #[test]
        fn test_meta_cognitive_result_creation() {
            let result = MetaCognitiveResult {
                reasoning_quality: 85.5,
                methodology_quality: MethodologyStatus::Good,
                cognitive_biases: vec!["overconfidence".to_string()],
                recommendations: vec!["Seek external validation".to_string()],
            };

            assert!((result.reasoning_quality - 85.5).abs() < f64::EPSILON);
            assert_eq!(result.methodology_quality, MethodologyStatus::Good);
        }

        #[test]
        fn test_meta_cognitive_result_boundary_scores() {
            let min_score = MetaCognitiveResult {
                reasoning_quality: 0.0,
                methodology_quality: MethodologyStatus::Poor,
                cognitive_biases: vec![],
                recommendations: vec![],
            };
            assert!((min_score.reasoning_quality - 0.0).abs() < f64::EPSILON);

            let max_score = MetaCognitiveResult {
                reasoning_quality: 100.0,
                methodology_quality: MethodologyStatus::Excellent,
                cognitive_biases: vec![],
                recommendations: vec![],
            };
            assert!((max_score.reasoning_quality - 100.0).abs() < f64::EPSILON);
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // CHAIN INTEGRITY RESULT TESTS
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod chain_integrity_result_tests {
        use super::*;

        #[test]
        fn test_chain_integrity_result_creation() {
            let result = ChainIntegrityResult {
                logical_flow: LogicalFlowStatus::Excellent,
                step_dependencies: DependencyStatus::FullySatisfied,
                confidence_progression: ProgressionStatus::Monotonic,
                gaps_detected: vec![],
                continuity_score: 0.95,
            };

            assert_eq!(result.logical_flow, LogicalFlowStatus::Excellent);
            assert!((result.continuity_score - 0.95).abs() < f64::EPSILON);
        }

        #[test]
        fn test_chain_integrity_result_with_gaps() {
            let result = ChainIntegrityResult {
                logical_flow: LogicalFlowStatus::NeedsImprovement,
                step_dependencies: DependencyStatus::PartiallySatisfied,
                confidence_progression: ProgressionStatus::Erratic,
                gaps_detected: vec![
                    "Missing justification between step 1 and 2".to_string(),
                    "Unexplained confidence drop at step 4".to_string(),
                ],
                continuity_score: 0.45,
            };

            assert_eq!(result.gaps_detected.len(), 2);
            assert!(result.continuity_score < 0.5);
        }

        #[test]
        fn test_chain_integrity_continuity_score_boundaries() {
            let zero_score = ChainIntegrityResult {
                logical_flow: LogicalFlowStatus::Poor,
                step_dependencies: DependencyStatus::Unsatisfied,
                confidence_progression: ProgressionStatus::Unstable,
                gaps_detected: vec![],
                continuity_score: 0.0,
            };
            assert!((zero_score.continuity_score - 0.0).abs() < f64::EPSILON);

            let perfect_score = ChainIntegrityResult {
                logical_flow: LogicalFlowStatus::Excellent,
                step_dependencies: DependencyStatus::FullySatisfied,
                confidence_progression: ProgressionStatus::Monotonic,
                gaps_detected: vec![],
                continuity_score: 1.0,
            };
            assert!((perfect_score.continuity_score - 1.0).abs() < f64::EPSILON);
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // DEEPSEEK VALIDATION RESULT TESTS
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod deepseek_validation_result_tests {
        use super::*;

        fn create_minimal_result() -> DeepSeekValidationResult {
            DeepSeekValidationResult {
                verdict: ValidationVerdict::Validated,
                chain_integrity: ChainIntegrityResult {
                    logical_flow: LogicalFlowStatus::Good,
                    step_dependencies: DependencyStatus::FullySatisfied,
                    confidence_progression: ProgressionStatus::Monotonic,
                    gaps_detected: vec![],
                    continuity_score: 0.85,
                },
                statistical_results: None,
                compliance_results: None,
                meta_cognitive_results: None,
                validation_confidence: 0.90,
                findings: vec![],
                tokens_used: TokenUsage::default(),
                performance: ValidationPerformance::default(),
            }
        }

        #[test]
        fn test_validation_result_minimal() {
            let result = create_minimal_result();

            assert_eq!(result.verdict, ValidationVerdict::Validated);
            assert!(result.statistical_results.is_none());
            assert!(result.compliance_results.is_none());
            assert!(result.meta_cognitive_results.is_none());
        }

        #[test]
        fn test_validation_result_serialization_skip_none() {
            let result = create_minimal_result();
            let json = serde_json::to_string(&result).unwrap();

            // Optional None fields should be skipped due to skip_serializing_if
            assert!(!json.contains("statistical_results"));
            assert!(!json.contains("compliance_results"));
            assert!(!json.contains("meta_cognitive_results"));
        }

        #[test]
        fn test_validation_result_with_all_options() {
            let result = DeepSeekValidationResult {
                verdict: ValidationVerdict::PartiallyValidated,
                chain_integrity: ChainIntegrityResult {
                    logical_flow: LogicalFlowStatus::Satisfactory,
                    step_dependencies: DependencyStatus::MostlySatisfied,
                    confidence_progression: ProgressionStatus::SlowlyDecaying,
                    gaps_detected: vec!["Minor gap".to_string()],
                    continuity_score: 0.75,
                },
                statistical_results: Some(StatisticalResult {
                    significant: true,
                    p_value: Some(0.02),
                    confidence_interval: Some((0.70, 0.90)),
                    sample_size: Some(100),
                }),
                compliance_results: Some(ComplianceResult {
                    gdpr_compliance: ComplianceStatus::MinorIssues,
                    bias_detection: BiasDetectionResult {
                        overall_bias: BiasLevel::Low,
                        detected_biases: vec![],
                        mitigation_recommendations: vec![],
                    },
                    regulatory_alignment: RegulatoryStatus::NeedsValidation,
                    violations: vec![],
                }),
                meta_cognitive_results: Some(MetaCognitiveResult {
                    reasoning_quality: 78.0,
                    methodology_quality: MethodologyStatus::Good,
                    cognitive_biases: vec![],
                    recommendations: vec![],
                }),
                validation_confidence: 0.80,
                findings: vec![ValidationFinding {
                    category: ValidationCategory::LogicalFlow,
                    severity: Severity::Low,
                    description: "Minor observation".to_string(),
                    affected_steps: vec![],
                    evidence: vec![],
                    recommendations: vec![],
                }],
                tokens_used: TokenUsage::new(1000, 500, 0.05),
                performance: ValidationPerformance::new(2000, 750.0, 64.0),
            };

            let json = serde_json::to_string(&result).unwrap();
            let deserialized: DeepSeekValidationResult = serde_json::from_str(&json).unwrap();

            assert_eq!(result.verdict, deserialized.verdict);
            assert!(deserialized.statistical_results.is_some());
            assert!(deserialized.compliance_results.is_some());
            assert!(deserialized.meta_cognitive_results.is_some());
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // COMPLIANCE RESULT TESTS
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod compliance_result_tests {
        use super::*;

        #[test]
        fn test_compliance_result_fully_compliant() {
            let result = ComplianceResult {
                gdpr_compliance: ComplianceStatus::Compliant,
                bias_detection: BiasDetectionResult {
                    overall_bias: BiasLevel::Minimal,
                    detected_biases: vec![],
                    mitigation_recommendations: vec![],
                },
                regulatory_alignment: RegulatoryStatus::FullyCompliant,
                violations: vec![],
            };

            assert_eq!(result.gdpr_compliance, ComplianceStatus::Compliant);
            assert!(result.violations.is_empty());
        }

        #[test]
        fn test_compliance_result_with_violations() {
            let result = ComplianceResult {
                gdpr_compliance: ComplianceStatus::NonCompliant,
                bias_detection: BiasDetectionResult {
                    overall_bias: BiasLevel::High,
                    detected_biases: vec![DetectedBias {
                        bias_type: "demographic".to_string(),
                        evidence: "Unequal treatment detected".to_string(),
                        severity: Severity::High,
                        detection_confidence: 0.9,
                    }],
                    mitigation_recommendations: vec!["Implement fairness constraints".to_string()],
                },
                regulatory_alignment: RegulatoryStatus::NonCompliant,
                violations: vec![
                    ComplianceViolation {
                        violation_type: "GDPR_CONSENT".to_string(),
                        severity: Severity::Critical,
                        description: "Missing consent mechanism".to_string(),
                        remediation: vec!["Add consent UI".to_string()],
                    },
                    ComplianceViolation {
                        violation_type: "GDPR_DATA_RETENTION".to_string(),
                        severity: Severity::High,
                        description: "Data retained beyond policy".to_string(),
                        remediation: vec!["Implement data lifecycle management".to_string()],
                    },
                ],
            };

            assert_eq!(result.violations.len(), 2);
            assert_eq!(result.bias_detection.detected_biases.len(), 1);
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // UNICODE AND SPECIAL CHARACTER TESTS
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod unicode_tests {
        use super::*;

        #[test]
        fn test_unicode_in_chain_integrity_gaps() {
            let result = ChainIntegrityResult {
                logical_flow: LogicalFlowStatus::Satisfactory,
                step_dependencies: DependencyStatus::MostlySatisfied,
                confidence_progression: ProgressionStatus::SlowlyDecaying,
                gaps_detected: vec![
                    "\u{4E2D}\u{6587}\u{6D4B}\u{8BD5}".to_string(), // Chinese: "Chinese test"
                    "\u{65E5}\u{672C}\u{8A9E}".to_string(),         // Japanese: "Japanese"
                    "\u{D55C}\u{AD6D}\u{C5B4}".to_string(),         // Korean: "Korean"
                    "\u{0410}\u{0411}\u{0412}".to_string(),         // Russian: "ABV"
                    "\u{05D0}\u{05D1}\u{05D2}".to_string(),         // Hebrew: "Alef Bet Gimel"
                    "\u{0627}\u{0628}\u{062A}".to_string(),         // Arabic: letters
                ],
                continuity_score: 0.7,
            };

            let json = serde_json::to_string(&result).unwrap();
            let deserialized: ChainIntegrityResult = serde_json::from_str(&json).unwrap();

            assert_eq!(result.gaps_detected.len(), deserialized.gaps_detected.len());
            for (original, parsed) in result
                .gaps_detected
                .iter()
                .zip(deserialized.gaps_detected.iter())
            {
                assert_eq!(original, parsed);
            }
        }

        #[test]
        fn test_unicode_emoji_in_descriptions() {
            let finding = ValidationFinding {
                category: ValidationCategory::Methodology,
                severity: Severity::Info,
                description: "Status: \u{2705} Pass \u{274C} Fail \u{26A0} Warning".to_string(),
                affected_steps: vec!["\u{1F3AF} Target Step".to_string()],
                evidence: vec!["\u{1F4CA} Chart data shows trend".to_string()],
                recommendations: vec!["\u{1F4A1} Consider improvement".to_string()],
            };

            let json = serde_json::to_string(&finding).unwrap();
            let deserialized: ValidationFinding = serde_json::from_str(&json).unwrap();

            assert!(deserialized.description.contains('\u{2705}'));
            assert!(deserialized.description.contains('\u{274C}'));
        }

        #[test]
        fn test_special_characters_in_strings() {
            let violation = ComplianceViolation {
                violation_type: "TEST_<>\"'&".to_string(),
                severity: Severity::Low,
                description: "Special chars: <script>alert('xss')</script>".to_string(),
                remediation: vec!["Use proper escaping: &amp; &lt; &gt;".to_string()],
            };

            let json = serde_json::to_string(&violation).unwrap();
            let deserialized: ComplianceViolation = serde_json::from_str(&json).unwrap();

            assert!(deserialized.violation_type.contains('<'));
            assert!(deserialized.violation_type.contains('>'));
        }

        #[test]
        fn test_empty_strings() {
            let bias = DetectedBias {
                bias_type: "".to_string(),
                evidence: "".to_string(),
                severity: Severity::Info,
                detection_confidence: 0.0,
            };

            let json = serde_json::to_string(&bias).unwrap();
            let deserialized: DetectedBias = serde_json::from_str(&json).unwrap();

            assert!(deserialized.bias_type.is_empty());
            assert!(deserialized.evidence.is_empty());
        }

        #[test]
        fn test_very_long_strings() {
            let long_string = "A".repeat(100_000);

            let finding = ValidationFinding {
                category: ValidationCategory::TokenEfficiency,
                severity: Severity::Low,
                description: long_string.clone(),
                affected_steps: vec![],
                evidence: vec![],
                recommendations: vec![],
            };

            let json = serde_json::to_string(&finding).unwrap();
            let deserialized: ValidationFinding = serde_json::from_str(&json).unwrap();

            assert_eq!(deserialized.description.len(), 100_000);
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // PROPERTY-BASED TESTS (using proptest)
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod property_tests {
        use super::*;

        proptest! {
            #[test]
            fn test_token_usage_add_is_associative(
                input1 in 0u32..1000,
                output1 in 0u32..1000,
                cost1 in 0.0f64..10.0,
                input2 in 0u32..1000,
                output2 in 0u32..1000,
                cost2 in 0.0f64..10.0,
            ) {
                let mut usage1 = TokenUsage::new(input1, output1, cost1);
                let usage2 = TokenUsage::new(input2, output2, cost2);

                usage1.add(&usage2);

                prop_assert_eq!(usage1.input_tokens, input1 + input2);
                prop_assert_eq!(usage1.output_tokens, output1 + output2);
                prop_assert_eq!(usage1.total_tokens, input1 + output1 + input2 + output2);
                prop_assert!((usage1.cost_usd - (cost1 + cost2)).abs() < 0.0001);
            }

            #[test]
            fn test_token_usage_new_total_is_sum(input in 0u32..u32::MAX/2, output in 0u32..u32::MAX/2) {
                let usage = TokenUsage::new(input, output, 0.0);
                prop_assert_eq!(usage.total_tokens, input + output);
            }

            #[test]
            fn test_chain_integrity_mul_f64_preserves_zero(rhs in -1000.0f64..1000.0) {
                let result = ChainIntegrityResult {
                    logical_flow: LogicalFlowStatus::Good,
                    step_dependencies: DependencyStatus::FullySatisfied,
                    confidence_progression: ProgressionStatus::Monotonic,
                    gaps_detected: vec![],
                    continuity_score: 0.0,
                };

                let product = result * rhs;
                prop_assert!((product - 0.0).abs() < f64::EPSILON);
            }

            #[test]
            fn test_chain_integrity_mul_identity(score in 0.0f64..1.0) {
                let result = ChainIntegrityResult {
                    logical_flow: LogicalFlowStatus::Good,
                    step_dependencies: DependencyStatus::FullySatisfied,
                    confidence_progression: ProgressionStatus::Monotonic,
                    gaps_detected: vec![],
                    continuity_score: score,
                };

                let product = result * 1.0f64;
                prop_assert!((product - score).abs() < f64::EPSILON);
            }

            #[test]
            fn test_validation_performance_serialization_roundtrip(
                duration in 0u64..u64::MAX,
                tps in 0.0f64..10000.0,
                memory in 0.0f64..10000.0,
            ) {
                let perf = ValidationPerformance::new(duration, tps, memory);
                let json = serde_json::to_string(&perf).unwrap();
                let deserialized: ValidationPerformance = serde_json::from_str(&json).unwrap();

                prop_assert_eq!(perf.duration_ms, deserialized.duration_ms);
                // Floating point comparison with tolerance
                prop_assert!((perf.tokens_per_second - deserialized.tokens_per_second).abs() < 0.0001);
            }

            #[test]
            fn test_config_temperature_in_valid_range(temp in 0.0f32..2.0) {
                // Temperature should be preserved through serialization
                let config = DeepSeekValidationConfig {
                    temperature: temp,
                    ..Default::default()
                };

                let json = serde_json::to_string(&config).unwrap();
                let deserialized: DeepSeekValidationConfig = serde_json::from_str(&json).unwrap();

                prop_assert!((config.temperature - deserialized.temperature).abs() < 0.0001);
            }

            #[test]
            fn test_arbitrary_string_in_gaps(s in "\\PC*") {
                // Test that any valid UTF-8 string can be stored in gaps
                let result = ChainIntegrityResult {
                    logical_flow: LogicalFlowStatus::Good,
                    step_dependencies: DependencyStatus::FullySatisfied,
                    confidence_progression: ProgressionStatus::Monotonic,
                    gaps_detected: vec![s.clone()],
                    continuity_score: 0.5,
                };

                let json = serde_json::to_string(&result).unwrap();
                let deserialized: ChainIntegrityResult = serde_json::from_str(&json).unwrap();

                prop_assert_eq!(result.gaps_detected, deserialized.gaps_detected);
            }
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // VALIDATION ENGINE TESTS (Requires API key - skipped in CI)
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod engine_tests {
        use super::*;

        #[tokio::test]
        async fn test_validation_engine_creation() {
            // This test may fail if DEEPSEEK_API_KEY is not set
            // In production, this would be mocked
            let result = DeepSeekValidationEngine::new();

            // Engine creation should succeed regardless of API key (key checked on use)
            assert!(result.is_ok());

            if let Ok(engine) = result {
                assert_eq!(engine.config.model, "deepseek-chat");
            }
        }

        #[test]
        fn test_configuration_options() {
            let config = DeepSeekValidationConfig::rigorous();
            assert!(config.enable_statistical_testing);
            assert!(config.enable_compliance_validation);
            assert!(config.enable_meta_cognition);

            let perf_config = DeepSeekValidationConfig::performance();
            assert!(!perf_config.enable_statistical_testing);
            assert!(perf_config.enable_compliance_validation);
        }

        #[test]
        fn test_validation_engine_with_custom_config() {
            let config = DeepSeekValidationConfig {
                model: "deepseek-coder".to_string(),
                temperature: 0.0,
                max_tokens: 8000,
                enable_statistical_testing: true,
                alpha: 0.01,
                enable_compliance_validation: true,
                enable_meta_cognition: true,
                min_confidence: 0.90,
                max_chain_length: 50,
            };

            let result = DeepSeekValidationEngine::with_config(config);
            assert!(result.is_ok());

            if let Ok(engine) = result {
                assert_eq!(engine.config.model, "deepseek-coder");
                assert_eq!(engine.config.max_tokens, 8000);
                assert!((engine.config.alpha - 0.01).abs() < f64::EPSILON);
            }
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────────────────────
    // EDGE CASE TESTS
    // ─────────────────────────────────────────────────────────────────────────────────────────────

    mod edge_case_tests {
        use super::*;

        #[test]
        fn test_token_usage_zero_values() {
            let usage = TokenUsage::new(0, 0, 0.0);
            assert_eq!(usage.total_tokens, 0);
            assert!((usage.cost_usd - 0.0).abs() < f64::EPSILON);
        }

        #[test]
        fn test_validation_performance_zero_duration() {
            let perf = ValidationPerformance::new(0, 0.0, 0.0);
            // Division by zero should be handled gracefully in actual usage
            assert_eq!(perf.duration_ms, 0);
        }

        #[test]
        fn test_continuity_score_exactly_zero() {
            let result = ChainIntegrityResult {
                logical_flow: LogicalFlowStatus::Poor,
                step_dependencies: DependencyStatus::Unsatisfied,
                confidence_progression: ProgressionStatus::Unstable,
                gaps_detected: vec![],
                continuity_score: 0.0,
            };

            // Multiplying by any value should still be zero
            let product = result.clone() * 100.0f64;
            assert!((product - 0.0).abs() < f64::EPSILON);
        }

        #[test]
        fn test_continuity_score_exactly_one() {
            let result = ChainIntegrityResult {
                logical_flow: LogicalFlowStatus::Excellent,
                step_dependencies: DependencyStatus::FullySatisfied,
                confidence_progression: ProgressionStatus::Monotonic,
                gaps_detected: vec![],
                continuity_score: 1.0,
            };

            // Multiplying by identity should preserve value
            let product = result * 1.0f64;
            assert!((product - 1.0).abs() < f64::EPSILON);
        }

        #[test]
        fn test_negative_cost_handling() {
            // While unusual, negative cost might be used for credits/refunds
            let usage = TokenUsage::new(100, 50, -0.01);
            assert!(usage.cost_usd < 0.0);
        }

        #[test]
        fn test_very_small_confidence_values() {
            let result = StatisticalResult {
                significant: true,
                p_value: Some(1e-100),
                confidence_interval: Some((0.999999, 0.9999999)),
                sample_size: Some(1_000_000),
            };

            let json = serde_json::to_string(&result).unwrap();
            let deserialized: StatisticalResult = serde_json::from_str(&json).unwrap();

            // Very small p-values should be preserved
            assert!(deserialized.p_value.unwrap() < 1e-50);
        }

        #[test]
        fn test_default_function_values() {
            // Test all default functions directly
            assert_eq!(default_deepseek_model(), "deepseek-chat");
            assert!((default_validation_temperature() - 0.1).abs() < f32::EPSILON);
            assert_eq!(default_max_tokens(), 4000);
            assert!((default_alpha() - 0.05).abs() < f64::EPSILON);
            assert!((default_min_confidence() - 0.70).abs() < f64::EPSILON);
            assert_eq!(default_max_chain_length(), 20);
        }
    }
}