lens-core 1.0.0

High-performance code search engine with LSP integration and benchmarking
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
// CALIB_V22 Production Aftercare System - D1-D7 Operations
// Phase 2: Production dashboards, operations runbook, and alert tuning

use std::sync::Arc;
use std::time::{Duration, SystemTime};
use std::collections::HashMap;
use tokio::time::{interval, sleep};
use tracing::{info, warn, error, debug};
use serde::{Serialize, Deserialize};
use thiserror::Error;

use crate::calibration::{
    sla_monitoring::{SlaMonitor, SlaMetrics},
    production_manifest::{ProductionManifestSystem, WeeklyDriftPack},
};

/// Production Aftercare Controller for D1-D7 Operations
pub struct ProductionAftercareController {
    /// Dashboard system for real-time monitoring
    dashboard: ProductionDashboard,
    
    /// Operations runbook executor
    runbook: OperationsRunbook,
    
    /// Alert tuning and management system
    alert_tuner: AlertTuningSystem,
    
    /// SLA monitoring integration
    sla_monitor: Arc<SlaMonitor>,
    
    /// Aftercare configuration
    config: AftercareConfig,
}

#[derive(Debug, Clone)]
pub struct AftercareConfig {
    /// Dashboard refresh interval
    pub dashboard_refresh_interval: Duration,
    
    /// Alert evaluation window
    pub alert_evaluation_window: Duration,
    
    /// SLA monitoring frequency
    pub sla_monitoring_frequency: Duration,
    
    /// Runbook response timeout
    pub runbook_response_timeout: Duration,
    
    /// Alert de-duplication window
    pub alert_dedup_window: Duration,
}

/// Production Dashboard System
pub struct ProductionDashboard {
    /// Current dashboard state
    state: Arc<tokio::sync::RwLock<DashboardState>>,
    
    /// Metrics collectors
    metrics_collectors: Vec<MetricsCollector>,
    
    /// Dashboard configuration
    config: DashboardConfig,
}

#[derive(Debug, Clone)]
pub struct DashboardConfig {
    /// Metrics retention duration
    pub retention_duration: Duration,
    
    /// Trend analysis window
    pub trend_window: Duration,
    
    /// Alert threshold configurations
    pub thresholds: DashboardThresholds,
    
    /// Visual refresh rate
    pub refresh_rate: Duration,
}

#[derive(Debug, Clone)]
pub struct DashboardThresholds {
    /// AECE threshold for highlighting
    pub aece_highlight_threshold: f64,
    
    /// DECE threshold for warnings
    pub dece_warning_threshold: f64,
    
    /// Brier score alert threshold
    pub brier_alert_threshold: f64,
    
    /// Alpha drift threshold (weekly)
    pub alpha_drift_threshold: f64,
    
    /// Clamp rate warning threshold
    pub clamp_rate_warning_threshold: f64,
    
    /// Merged bin warning threshold
    pub merged_bin_warning_threshold: f64,
    
    /// Critical merged bin threshold
    pub merged_bin_critical_threshold: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DashboardState {
    /// Last update timestamp
    pub last_update: SystemTime,
    
    /// Current calibration metrics
    pub current_metrics: CalibrationMetrics,
    
    /// Trending data (past 7 days)
    pub trends: TrendingData,
    
    /// Per-slice AECE-τ highlighting
    pub slice_highlights: HashMap<String, SliceHighlight>,
    
    /// Real-time SLA status
    pub sla_status: SlaStatus,
    
    /// Active alerts summary
    pub active_alerts: Vec<AlertSummary>,
    
    /// System health indicators
    pub health_indicators: HealthIndicators,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CalibrationMetrics {
    /// Adaptive Expected Calibration Error
    pub aece: f64,
    
    /// Dynamic Expected Calibration Error
    pub dece: f64,
    
    /// Brier score
    pub brier: f64,
    
    /// Alpha parameter
    pub alpha: f64,
    
    /// Clamp rate percentage
    pub clamp_rate_percent: f64,
    
    /// Merged bin percentage
    pub merged_bin_percent: f64,
    
    /// Measurement timestamp
    pub timestamp: SystemTime,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrendingData {
    /// AECE trend over time
    pub aece_trend: Vec<TimestampedValue>,
    
    /// DECE trend over time
    pub dece_trend: Vec<TimestampedValue>,
    
    /// Brier score trend
    pub brier_trend: Vec<TimestampedValue>,
    
    /// Alpha parameter trend
    pub alpha_trend: Vec<TimestampedValue>,
    
    /// Clamp rate trend
    pub clamp_rate_trend: Vec<TimestampedValue>,
    
    /// Merged bin percentage trend
    pub merged_bin_trend: Vec<TimestampedValue>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimestampedValue {
    pub timestamp: SystemTime,
    pub value: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SliceHighlight {
    /// Slice identifier
    pub slice_name: String,
    
    /// Current AECE-τ value
    pub aece_tau_value: f64,
    
    /// Threshold used for comparison
    pub threshold: f64,
    
    /// Highlight severity
    pub severity: HighlightSeverity,
    
    /// Intent classification
    pub intent: Option<String>,
    
    /// Language classification
    pub language: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum HighlightSeverity {
    Normal,
    Warning,
    Critical,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SlaStatus {
    /// Overall SLA compliance percentage
    pub overall_compliance: f64,
    
    /// Per-component SLA status
    pub component_status: HashMap<String, ComponentSlaStatus>,
    
    /// Current SLA violations
    pub active_violations: Vec<SlaViolation>,
    
    /// Statistical enforcement results
    pub statistical_enforcement: StatisticalEnforcement,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComponentSlaStatus {
    /// Component name
    pub name: String,
    
    /// Current compliance percentage
    pub compliance: f64,
    
    /// Target compliance
    pub target: f64,
    
    /// Status classification
    pub status: ComplianceStatus,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ComplianceStatus {
    Compliant,
    Warning,
    Violation,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SlaViolation {
    /// Violation type
    pub violation_type: String,
    
    /// Affected component
    pub component: String,
    
    /// Violation start time
    pub start_time: SystemTime,
    
    /// Current severity
    pub severity: ViolationSeverity,
    
    /// Violation details
    pub details: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ViolationSeverity {
    Low,
    Medium,
    High,
    Critical,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatisticalEnforcement {
    /// Statistical tests performed
    pub tests_performed: Vec<StatisticalTest>,
    
    /// Confidence intervals
    pub confidence_intervals: HashMap<String, ConfidenceInterval>,
    
    /// Significance levels
    pub significance_levels: HashMap<String, f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatisticalTest {
    /// Test name
    pub test_name: String,
    
    /// Test statistic
    pub statistic: f64,
    
    /// P-value
    pub p_value: f64,
    
    /// Test result
    pub result: TestResult,
    
    /// Test timestamp
    pub timestamp: SystemTime,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum TestResult {
    Passed,
    Failed,
    Inconclusive,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfidenceInterval {
    /// Lower bound
    pub lower_bound: f64,
    
    /// Upper bound
    pub upper_bound: f64,
    
    /// Confidence level
    pub confidence_level: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertSummary {
    /// Alert ID
    pub alert_id: String,
    
    /// Alert type
    pub alert_type: AlertType,
    
    /// Alert severity
    pub severity: AlertSeverity,
    
    /// Alert message
    pub message: String,
    
    /// Alert creation time
    pub created_at: SystemTime,
    
    /// Alert status
    pub status: AlertStatus,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AlertType {
    MaskMismatch,
    ScoresOutOfRange,
    AlphaDrift,
    MergedBinThreshold,
    SlaViolation,
    CalibrationFailure,
    SystemHealth,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AlertSeverity {
    Info,
    Warning,
    Critical,
    Emergency,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AlertStatus {
    Active,
    Acknowledged,
    Resolved,
    Suppressed,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthIndicators {
    /// Overall system health
    pub overall_health: SystemHealthStatus,
    
    /// Component health breakdown
    pub component_health: HashMap<String, ComponentHealth>,
    
    /// Performance indicators
    pub performance_indicators: PerformanceIndicators,
    
    /// Reliability indicators
    pub reliability_indicators: ReliabilityIndicators,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum SystemHealthStatus {
    Healthy,
    Degraded,
    Unhealthy,
    Critical,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComponentHealth {
    /// Component name
    pub name: String,
    
    /// Health status
    pub status: SystemHealthStatus,
    
    /// Health score (0-100)
    pub score: f64,
    
    /// Last health check
    pub last_check: SystemTime,
    
    /// Health details
    pub details: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceIndicators {
    /// Average response time
    pub avg_response_time_ms: f64,
    
    /// P95 response time
    pub p95_response_time_ms: f64,
    
    /// P99 response time
    pub p99_response_time_ms: f64,
    
    /// Throughput (requests per second)
    pub throughput_rps: f64,
    
    /// Error rate percentage
    pub error_rate_percent: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReliabilityIndicators {
    /// Uptime percentage (last 24h)
    pub uptime_24h: f64,
    
    /// Mean time between failures
    pub mtbf_hours: f64,
    
    /// Mean time to recovery
    pub mttr_minutes: f64,
    
    /// Service availability
    pub availability_percent: f64,
}

/// Metrics Collection System
pub struct MetricsCollector {
    /// Collector name
    name: String,
    
    /// Collection function
    collector_fn: Box<dyn Fn() -> Result<MetricsData, CollectionError> + Send + Sync>,
    
    /// Collection interval
    interval: Duration,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricsData {
    /// Metric name
    pub name: String,
    
    /// Metric values
    pub values: HashMap<String, f64>,
    
    /// Collection timestamp
    pub timestamp: SystemTime,
}

/// Operations Runbook System
pub struct OperationsRunbook {
    /// Runbook procedures
    procedures: HashMap<String, RunbookProcedure>,
    
    /// Decision trees
    decision_trees: HashMap<String, DecisionTree>,
    
    /// Runbook configuration
    config: RunbookConfig,
}

#[derive(Debug, Clone)]
pub struct RunbookConfig {
    /// Procedure timeout
    pub procedure_timeout: Duration,
    
    /// Data capture window
    pub data_capture_window: Duration,
    
    /// Decision timeout
    pub decision_timeout: Duration,
}

#[derive(Debug, Clone)]
pub struct RunbookProcedure {
    /// Procedure name
    pub name: String,
    
    /// Symptom patterns
    pub symptoms: Vec<SymptomPattern>,
    
    /// Data collection steps
    pub data_collection: Vec<DataCollectionStep>,
    
    /// Decision logic
    pub decision_tree: String,
    
    /// Revert procedures
    pub revert_procedures: Vec<RevertProcedure>,
}

#[derive(Debug, Clone)]
pub struct SymptomPattern {
    /// Pattern name
    pub name: String,
    
    /// Pattern description
    pub description: String,
    
    /// Detection criteria
    pub criteria: Vec<DetectionCriterion>,
}

#[derive(Debug, Clone)]
pub struct DetectionCriterion {
    /// Metric name
    pub metric: String,
    
    /// Comparison operator
    pub operator: ComparisonOperator,
    
    /// Threshold value
    pub threshold: f64,
    
    /// Duration requirement
    pub duration: Option<Duration>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ComparisonOperator {
    GreaterThan,
    LessThan,
    Equal,
    NotEqual,
    GreaterThanOrEqual,
    LessThanOrEqual,
}

#[derive(Debug, Clone)]
pub struct DataCollectionStep {
    /// Step name
    pub name: String,
    
    /// Data to collect
    pub data_items: Vec<DataItem>,
    
    /// Collection timeout
    pub timeout: Duration,
}

#[derive(Debug, Clone)]
pub struct DataItem {
    /// Item name
    pub name: String,
    
    /// Item type
    pub item_type: DataItemType,
    
    /// Collection method
    pub collection_method: String,
}

#[derive(Debug, Clone, PartialEq)]
pub enum DataItemType {
    BinTable,
    AlphaValue,
    TauValue,
    AeceTauValue,
    MaskCounts,
    SystemMetrics,
}

#[derive(Debug, Clone)]
pub struct DecisionTree {
    /// Tree name
    pub name: String,
    
    /// Root decision node
    pub root: DecisionNode,
}

#[derive(Debug, Clone)]
pub struct DecisionNode {
    /// Node condition
    pub condition: String,
    
    /// Condition evaluation
    pub evaluation: ConditionEvaluation,
    
    /// True branch
    pub true_branch: Box<Option<DecisionAction>>,
    
    /// False branch
    pub false_branch: Box<Option<DecisionAction>>,
}

#[derive(Debug, Clone)]
pub enum ConditionEvaluation {
    Threshold { metric: String, operator: ComparisonOperator, value: f64 },
    Pattern { pattern: String },
    Custom { function: String },
}

#[derive(Debug, Clone)]
pub enum DecisionAction {
    /// Continue to another decision node
    Node(DecisionNode),
    
    /// Execute a specific action
    Action(ActionType),
    
    /// Escalate to human
    Escalate(EscalationReason),
}

#[derive(Debug, Clone)]
pub enum ActionType {
    /// Raise ĉ via bootstrap
    RaiseBootstrap { factor: f64 },
    
    /// Execute revert
    ExecuteRevert,
    
    /// Update fingerprint
    UpdateFingerprint,
    
    /// Collect additional data
    CollectData { items: Vec<String> },
    
    /// No action required
    NoAction,
}

#[derive(Debug, Clone)]
pub struct EscalationReason {
    /// Reason for escalation
    pub reason: String,
    
    /// Escalation urgency
    pub urgency: EscalationUrgency,
    
    /// Required expertise
    pub expertise: Vec<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum EscalationUrgency {
    Low,
    Medium,
    High,
    Emergency,
}

#[derive(Debug, Clone)]
pub struct RevertProcedure {
    /// Procedure name
    pub name: String,
    
    /// Revert steps
    pub steps: Vec<RevertStep>,
    
    /// Validation steps
    pub validation: Vec<ValidationStep>,
}

#[derive(Debug, Clone)]
pub struct RevertStep {
    /// Step description
    pub description: String,
    
    /// Step command or action
    pub action: String,
    
    /// Expected outcome
    pub expected_outcome: String,
    
    /// Timeout
    pub timeout: Duration,
}

#[derive(Debug, Clone)]
pub struct ValidationStep {
    /// Validation description
    pub description: String,
    
    /// Validation check
    pub check: ValidationCheck,
    
    /// Pass criteria
    pub pass_criteria: String,
}

#[derive(Debug, Clone)]
pub enum ValidationCheck {
    MetricThreshold { metric: String, operator: ComparisonOperator, value: f64 },
    SystemCheck { check_type: String },
    Custom { function: String },
}

/// Alert Tuning System
pub struct AlertTuningSystem {
    /// Alert rules
    alert_rules: HashMap<String, AlertRule>,
    
    /// De-duplication engine
    dedup_engine: AlertDeduplicationEngine,
    
    /// Escalation manager
    escalation_manager: EscalationManager,
    
    /// Tuning configuration
    config: AlertTuningConfig,
}

#[derive(Debug, Clone)]
pub struct AlertTuningConfig {
    /// De-duplication window
    pub dedup_window: Duration,
    
    /// Escalation delays
    pub escalation_delays: HashMap<AlertSeverity, Duration>,
    
    /// Suppression rules
    pub suppression_rules: Vec<SuppressionRule>,
    
    /// Critical alert thresholds
    pub critical_thresholds: CriticalThresholds,
}

#[derive(Debug, Clone)]
pub struct AlertRule {
    /// Rule name
    pub name: String,
    
    /// Rule condition
    pub condition: AlertCondition,
    
    /// Alert severity
    pub severity: AlertSeverity,
    
    /// Alert message template
    pub message_template: String,
    
    /// Escalation policy
    pub escalation_policy: String,
}

#[derive(Debug, Clone)]
pub enum AlertCondition {
    /// Threshold-based condition
    Threshold { metric: String, operator: ComparisonOperator, value: f64, duration: Duration },
    
    /// Pattern-based condition
    Pattern { pattern: String, window: Duration },
    
    /// Composite condition
    Composite { conditions: Vec<AlertCondition>, logic: LogicOperator },
}

#[derive(Debug, Clone, PartialEq)]
pub enum LogicOperator {
    And,
    Or,
    Not,
}

#[derive(Debug, Clone)]
pub struct SuppressionRule {
    /// Rule name
    pub name: String,
    
    /// Suppression condition
    pub condition: SuppressionCondition,
    
    /// Suppression duration
    pub duration: Duration,
    
    /// Affected alert types
    pub alert_types: Vec<AlertType>,
}

#[derive(Debug, Clone)]
pub enum SuppressionCondition {
    /// Time-based suppression
    TimeWindow { start: SystemTime, end: SystemTime },
    
    /// Condition-based suppression
    ConditionalSuppression { condition: String },
    
    /// Manual suppression
    ManualSuppression { reason: String },
}

#[derive(Debug, Clone)]
pub struct CriticalThresholds {
    /// Mask mismatch tolerance
    pub mask_mismatch_tolerance: f64,
    
    /// Score range validation
    pub score_range_min: f64,
    pub score_range_max: f64,
    
    /// Alpha drift threshold (weekly)
    pub alpha_drift_wow_threshold: f64,
    
    /// Merged bin critical percentage
    pub merged_bin_critical_percent: f64,
}

pub struct AlertDeduplicationEngine {
    /// Active alert fingerprints
    active_fingerprints: HashMap<String, AlertFingerprint>,
    
    /// Deduplication window
    window: Duration,
}

#[derive(Debug, Clone)]
pub struct AlertFingerprint {
    /// Alert type
    pub alert_type: AlertType,
    
    /// Alert content hash
    pub content_hash: String,
    
    /// First occurrence
    pub first_occurrence: SystemTime,
    
    /// Last occurrence
    pub last_occurrence: SystemTime,
    
    /// Occurrence count
    pub count: u32,
}

pub struct EscalationManager {
    /// Escalation policies
    policies: HashMap<String, EscalationPolicy>,
    
    /// Active escalations
    active_escalations: HashMap<String, ActiveEscalation>,
}

#[derive(Debug, Clone)]
pub struct EscalationPolicy {
    /// Policy name
    pub name: String,
    
    /// Escalation levels
    pub levels: Vec<EscalationLevel>,
    
    /// Policy configuration
    pub config: EscalationPolicyConfig,
}

#[derive(Debug, Clone)]
pub struct EscalationLevel {
    /// Level number
    pub level: u32,
    
    /// Delay before escalation
    pub delay: Duration,
    
    /// Escalation targets
    pub targets: Vec<EscalationTarget>,
    
    /// Escalation actions
    pub actions: Vec<EscalationAction>,
}

#[derive(Debug, Clone)]
pub enum EscalationTarget {
    /// Email notification
    Email(String),
    
    /// Slack channel
    Slack(String),
    
    /// PagerDuty
    PagerDuty(String),
    
    /// On-call rotation
    OnCall(String),
}

#[derive(Debug, Clone)]
pub enum EscalationAction {
    /// Send notification
    Notify,
    
    /// Create incident
    CreateIncident,
    
    /// Execute automated response
    AutomatedResponse(String),
    
    /// Page on-call
    PageOnCall,
}

#[derive(Debug, Clone)]
pub struct EscalationPolicyConfig {
    /// Maximum escalation levels
    pub max_levels: u32,
    
    /// Escalation timeout
    pub timeout: Duration,
    
    /// Auto-resolve condition
    pub auto_resolve: Option<String>,
}

#[derive(Debug, Clone)]
pub struct ActiveEscalation {
    /// Alert ID
    pub alert_id: String,
    
    /// Current level
    pub current_level: u32,
    
    /// Escalation start time
    pub start_time: SystemTime,
    
    /// Next escalation time
    pub next_escalation: SystemTime,
    
    /// Escalation status
    pub status: EscalationStatus,
}

#[derive(Debug, Clone, PartialEq)]
pub enum EscalationStatus {
    Active,
    Paused,
    Resolved,
    Timeout,
}

#[derive(Debug, Error)]
pub enum AftercareError {
    #[error("Dashboard error: {0}")]
    DashboardError(String),
    
    #[error("Metrics collection failed: {0}")]
    MetricsError(String),
    
    #[error("Runbook execution failed: {0}")]
    RunbookError(String),
    
    #[error("Alert processing failed: {0}")]
    AlertError(String),
    
    #[error("Configuration error: {0}")]
    ConfigError(String),
    
    #[error("Data collection error: {0}")]
    DataCollectionError(String),
}

#[derive(Debug, Error)]
pub enum CollectionError {
    #[error("Collection timeout")]
    Timeout,
    
    #[error("Connection failed: {0}")]
    ConnectionFailed(String),
    
    #[error("Data parsing failed: {0}")]
    ParseError(String),
    
    #[error("Authentication failed")]
    AuthenticationFailed,
}

impl ProductionAftercareController {
    pub fn new(
        sla_monitor: Arc<SlaMonitor>,
        config: AftercareConfig,
    ) -> Result<Self, AftercareError> {
        let dashboard = ProductionDashboard::new(DashboardConfig::default())?;
        let runbook = OperationsRunbook::new(RunbookConfig::default())?;
        let alert_tuner = AlertTuningSystem::new(AlertTuningConfig::default())?;
        
        Ok(Self {
            dashboard,
            runbook,
            alert_tuner,
            sla_monitor,
            config,
        })
    }
    
    /// Start D1-D7 aftercare monitoring
    pub async fn start_aftercare_monitoring(&mut self) -> Result<(), AftercareError> {
        info!("🔍 Starting CALIB_V22 D1-D7 aftercare monitoring");
        
        // Start dashboard updates
        self.start_dashboard_monitoring().await?;
        
        // Start alert monitoring
        self.start_alert_monitoring().await?;
        
        // Initialize runbook procedures
        self.initialize_runbook_procedures().await?;
        
        info!("✅ Aftercare monitoring started successfully");
        Ok(())
    }
    
    async fn start_dashboard_monitoring(&mut self) -> Result<(), AftercareError> {
        let dashboard_interval = self.config.dashboard_refresh_interval;
        let mut interval = interval(dashboard_interval);
        
        // Background task for dashboard updates
        tokio::spawn(async move {
            loop {
                interval.tick().await;
                // Dashboard update logic would go here
                debug!("📊 Dashboard update cycle");
            }
        });
        
        Ok(())
    }
    
    async fn start_alert_monitoring(&mut self) -> Result<(), AftercareError> {
        let alert_interval = self.config.alert_evaluation_window;
        let mut interval = interval(alert_interval);
        
        // Background task for alert evaluation
        tokio::spawn(async move {
            loop {
                interval.tick().await;
                // Alert evaluation logic would go here
                debug!("🚨 Alert evaluation cycle");
            }
        });
        
        Ok(())
    }
    
    async fn initialize_runbook_procedures(&mut self) -> Result<(), AftercareError> {
        // Initialize standard runbook procedures
        self.runbook.register_standard_procedures().await?;
        
        info!("📋 Runbook procedures initialized");
        Ok(())
    }
    
    /// Execute runbook procedure for a specific symptom
    pub async fn execute_runbook(&self, symptom: &str, data: HashMap<String, f64>) -> Result<RunbookResult, AftercareError> {
        info!("📋 Executing runbook for symptom: {}", symptom);
        
        self.runbook.execute_procedure(symptom, data).await
            .map_err(|e| AftercareError::RunbookError(e.to_string()))
    }
    
    /// Get comprehensive aftercare status report
    pub async fn get_aftercare_status(&self) -> Result<AftercareStatus, AftercareError> {
        let dashboard_state = self.dashboard.get_current_state().await?;
        let active_alerts = self.alert_tuner.get_active_alerts().await?;
        let runbook_status = self.runbook.get_status().await?;
        
        Ok(AftercareStatus {
            dashboard_state,
            active_alerts,
            runbook_status,
            system_health: self.calculate_overall_health().await?,
            timestamp: SystemTime::now(),
        })
    }
    
    async fn calculate_overall_health(&self) -> Result<SystemHealthStatus, AftercareError> {
        // Simple health calculation based on current metrics
        // In production, this would be more sophisticated
        Ok(SystemHealthStatus::Healthy)
    }
}

impl ProductionDashboard {
    pub fn new(config: DashboardConfig) -> Result<Self, AftercareError> {
        let state = Arc::new(tokio::sync::RwLock::new(DashboardState::default()));
        let metrics_collectors = Self::create_default_collectors()?;
        
        Ok(Self {
            state,
            metrics_collectors,
            config,
        })
    }
    
    fn create_default_collectors() -> Result<Vec<MetricsCollector>, AftercareError> {
        // Create standard metrics collectors
        Ok(vec![])
    }
    
    pub async fn get_current_state(&self) -> Result<DashboardState, AftercareError> {
        let state = self.state.read().await;
        Ok(state.clone())
    }
    
    pub async fn update_dashboard(&self) -> Result<(), AftercareError> {
        // Update dashboard state with latest metrics
        let mut state = self.state.write().await;
        state.last_update = SystemTime::now();
        
        // Collect current metrics
        state.current_metrics = self.collect_current_metrics().await?;
        
        // Update trends
        state.trends = self.update_trends(&state.current_metrics).await?;
        
        // Update slice highlights
        state.slice_highlights = self.update_slice_highlights().await?;
        
        // Update SLA status
        state.sla_status = self.update_sla_status().await?;
        
        // Update health indicators
        state.health_indicators = self.update_health_indicators().await?;
        
        Ok(())
    }
    
    async fn collect_current_metrics(&self) -> Result<CalibrationMetrics, AftercareError> {
        // Simulate collecting current calibration metrics
        Ok(CalibrationMetrics {
            aece: 0.008,
            dece: 0.012,
            brier: 0.089,
            alpha: 0.15,
            clamp_rate_percent: 2.3,
            merged_bin_percent: 1.9,
            timestamp: SystemTime::now(),
        })
    }
    
    async fn update_trends(&self, current: &CalibrationMetrics) -> Result<TrendingData, AftercareError> {
        // Update trending data with current metrics
        Ok(TrendingData {
            aece_trend: vec![TimestampedValue { timestamp: current.timestamp, value: current.aece }],
            dece_trend: vec![TimestampedValue { timestamp: current.timestamp, value: current.dece }],
            brier_trend: vec![TimestampedValue { timestamp: current.timestamp, value: current.brier }],
            alpha_trend: vec![TimestampedValue { timestamp: current.timestamp, value: current.alpha }],
            clamp_rate_trend: vec![TimestampedValue { timestamp: current.timestamp, value: current.clamp_rate_percent }],
            merged_bin_trend: vec![TimestampedValue { timestamp: current.timestamp, value: current.merged_bin_percent }],
        })
    }
    
    async fn update_slice_highlights(&self) -> Result<HashMap<String, SliceHighlight>, AftercareError> {
        let mut highlights = HashMap::new();
        
        // Sample slice highlights
        highlights.insert("typescript_search".to_string(), SliceHighlight {
            slice_name: "typescript_search".to_string(),
            aece_tau_value: 0.009,
            threshold: 0.01,
            severity: HighlightSeverity::Normal,
            intent: Some("search".to_string()),
            language: Some("typescript".to_string()),
        });
        
        Ok(highlights)
    }
    
    async fn update_sla_status(&self) -> Result<SlaStatus, AftercareError> {
        // Update SLA status from monitoring
        Ok(SlaStatus {
            overall_compliance: 98.5,
            component_status: HashMap::new(),
            active_violations: Vec::new(),
            statistical_enforcement: StatisticalEnforcement {
                tests_performed: Vec::new(),
                confidence_intervals: HashMap::new(),
                significance_levels: HashMap::new(),
            },
        })
    }
    
    async fn update_health_indicators(&self) -> Result<HealthIndicators, AftercareError> {
        Ok(HealthIndicators {
            overall_health: SystemHealthStatus::Healthy,
            component_health: HashMap::new(),
            performance_indicators: PerformanceIndicators {
                avg_response_time_ms: 0.19,
                p95_response_time_ms: 0.45,
                p99_response_time_ms: 0.82,
                throughput_rps: 1250.0,
                error_rate_percent: 0.01,
            },
            reliability_indicators: ReliabilityIndicators {
                uptime_24h: 99.98,
                mtbf_hours: 720.0,
                mttr_minutes: 2.5,
                availability_percent: 99.95,
            },
        })
    }
}

impl OperationsRunbook {
    pub fn new(config: RunbookConfig) -> Result<Self, AftercareError> {
        let procedures = HashMap::new();
        let decision_trees = HashMap::new();
        
        Ok(Self {
            procedures,
            decision_trees,
            config,
        })
    }
    
    pub async fn register_standard_procedures(&mut self) -> Result<(), AftercareError> {
        // Register standard runbook procedures
        self.register_mask_mismatch_procedure().await?;
        self.register_alpha_drift_procedure().await?;
        self.register_merged_bin_procedure().await?;
        
        Ok(())
    }
    
    async fn register_mask_mismatch_procedure(&mut self) -> Result<(), AftercareError> {
        let procedure = RunbookProcedure {
            name: "mask_mismatch".to_string(),
            symptoms: vec![
                SymptomPattern {
                    name: "mask_count_deviation".to_string(),
                    description: "Fit/eval mask count mismatch".to_string(),
                    criteria: vec![
                        DetectionCriterion {
                            metric: "mask_count_ratio".to_string(),
                            operator: ComparisonOperator::NotEqual,
                            threshold: 1.0,
                            duration: Some(Duration::from_secs(300)),
                        }
                    ],
                }
            ],
            data_collection: vec![
                DataCollectionStep {
                    name: "collect_mask_data".to_string(),
                    data_items: vec![
                        DataItem {
                            name: "bin_table".to_string(),
                            item_type: DataItemType::BinTable,
                            collection_method: "calibration_debug".to_string(),
                        },
                        DataItem {
                            name: "mask_counts".to_string(),
                            item_type: DataItemType::MaskCounts,
                            collection_method: "mask_counter".to_string(),
                        }
                    ],
                    timeout: Duration::from_secs(30),
                }
            ],
            decision_tree: "mask_mismatch_tree".to_string(),
            revert_procedures: vec![
                RevertProcedure {
                    name: "flag_revert".to_string(),
                    steps: vec![
                        RevertStep {
                            description: "Disable CALIB_V22 flag".to_string(),
                            action: "set_flag(CALIB_V22=false)".to_string(),
                            expected_outcome: "Flag disabled globally".to_string(),
                            timeout: Duration::from_secs(30),
                        }
                    ],
                    validation: vec![
                        ValidationStep {
                            description: "Verify flag disabled".to_string(),
                            check: ValidationCheck::SystemCheck { check_type: "flag_status".to_string() },
                            pass_criteria: "CALIB_V22=false confirmed".to_string(),
                        }
                    ],
                }
            ],
        };
        
        self.procedures.insert("mask_mismatch".to_string(), procedure);
        Ok(())
    }
    
    async fn register_alpha_drift_procedure(&mut self) -> Result<(), AftercareError> {
        // Similar implementation for alpha drift procedure
        Ok(())
    }
    
    async fn register_merged_bin_procedure(&mut self) -> Result<(), AftercareError> {
        // Similar implementation for merged bin procedure
        Ok(())
    }
    
    pub async fn execute_procedure(&self, symptom: &str, data: HashMap<String, f64>) -> Result<RunbookResult, RunbookError> {
        if let Some(procedure) = self.procedures.get(symptom) {
            info!("📋 Executing runbook procedure: {}", procedure.name);
            
            // Execute data collection
            let collected_data = self.execute_data_collection(&procedure.data_collection, data).await?;
            
            // Execute decision tree
            let decision = self.execute_decision_tree(&procedure.decision_tree, &collected_data).await?;
            
            // Return runbook result
            Ok(RunbookResult {
                procedure_name: procedure.name.clone(),
                decision,
                collected_data,
                execution_time: SystemTime::now(),
                result_status: RunbookStatus::Success,
            })
        } else {
            Err(RunbookError::ProcedureNotFound(symptom.to_string()))
        }
    }
    
    async fn execute_data_collection(&self, steps: &[DataCollectionStep], initial_data: HashMap<String, f64>) -> Result<HashMap<String, f64>, RunbookError> {
        let mut collected_data = initial_data;
        
        for step in steps {
            for data_item in &step.data_items {
                match data_item.item_type {
                    DataItemType::BinTable => {
                        // Collect bin table data
                        collected_data.insert("bin_count".to_string(), 10.0);
                    }
                    DataItemType::AlphaValue => {
                        // Collect alpha value
                        collected_data.insert("alpha".to_string(), 0.15);
                    }
                    DataItemType::AeceTauValue => {
                        // Collect AECE-τ value
                        collected_data.insert("aece_tau".to_string(), 0.008);
                    }
                    _ => {
                        // Handle other data types
                    }
                }
            }
        }
        
        Ok(collected_data)
    }
    
    async fn execute_decision_tree(&self, tree_name: &str, data: &HashMap<String, f64>) -> Result<DecisionResult, RunbookError> {
        // Simulate decision tree execution
        Ok(DecisionResult {
            decision: "raise_bootstrap".to_string(),
            confidence: 0.85,
            reasoning: "Alpha drift detected, bootstrap adjustment recommended".to_string(),
            recommended_action: ActionType::RaiseBootstrap { factor: 1.1 },
        })
    }
    
    pub async fn get_status(&self) -> Result<RunbookStatus, AftercareError> {
        Ok(RunbookStatus::Ready)
    }
}

impl AlertTuningSystem {
    pub fn new(config: AlertTuningConfig) -> Result<Self, AftercareError> {
        let alert_rules = Self::create_default_alert_rules(&config)?;
        let dedup_engine = AlertDeduplicationEngine::new(config.dedup_window);
        let escalation_manager = EscalationManager::new();
        
        Ok(Self {
            alert_rules,
            dedup_engine,
            escalation_manager,
            config,
        })
    }
    
    fn create_default_alert_rules(config: &AlertTuningConfig) -> Result<HashMap<String, AlertRule>, AftercareError> {
        let mut rules = HashMap::new();
        
        // Critical alert: Mask mismatch
        rules.insert("mask_mismatch".to_string(), AlertRule {
            name: "mask_mismatch".to_string(),
            condition: AlertCondition::Threshold {
                metric: "mask_mismatch_detected".to_string(),
                operator: ComparisonOperator::GreaterThan,
                value: 0.0,
                duration: Duration::from_secs(60),
            },
            severity: AlertSeverity::Critical,
            message_template: "CRITICAL: Mask mismatch detected - fit/eval inconsistency".to_string(),
            escalation_policy: "immediate".to_string(),
        });
        
        // Critical alert: Scores out of range
        rules.insert("scores_out_of_range".to_string(), AlertRule {
            name: "scores_out_of_range".to_string(),
            condition: AlertCondition::Composite {
                conditions: vec![
                    AlertCondition::Threshold {
                        metric: "min_score".to_string(),
                        operator: ComparisonOperator::LessThan,
                        value: 0.0,
                        duration: Duration::from_secs(60),
                    },
                    AlertCondition::Threshold {
                        metric: "max_score".to_string(),
                        operator: ComparisonOperator::GreaterThan,
                        value: 1.0,
                        duration: Duration::from_secs(60),
                    },
                ],
                logic: LogicOperator::Or,
            },
            severity: AlertSeverity::Critical,
            message_template: "CRITICAL: Calibration scores outside [0,1] range".to_string(),
            escalation_policy: "immediate".to_string(),
        });
        
        // Warning alert: Alpha drift
        rules.insert("alpha_drift".to_string(), AlertRule {
            name: "alpha_drift".to_string(),
            condition: AlertCondition::Threshold {
                metric: "alpha_drift_wow".to_string(),
                operator: ComparisonOperator::GreaterThan,
                value: config.critical_thresholds.alpha_drift_wow_threshold,
                duration: Duration::from_secs(3600),
            },
            severity: AlertSeverity::Warning,
            message_template: "WARNING: Alpha drift >0.05 WoW detected".to_string(),
            escalation_policy: "standard".to_string(),
        });
        
        // Critical alert: Merged bin threshold
        rules.insert("merged_bin_critical".to_string(), AlertRule {
            name: "merged_bin_critical".to_string(),
            condition: AlertCondition::Threshold {
                metric: "merged_bin_percent".to_string(),
                operator: ComparisonOperator::GreaterThan,
                value: config.critical_thresholds.merged_bin_critical_percent,
                duration: Duration::from_secs(300),
            },
            severity: AlertSeverity::Critical,
            message_template: "CRITICAL: Merged bin percentage >20%".to_string(),
            escalation_policy: "immediate".to_string(),
        });
        
        Ok(rules)
    }
    
    pub async fn get_active_alerts(&self) -> Result<Vec<AlertSummary>, AftercareError> {
        // Return current active alerts
        Ok(vec![])
    }
    
    pub async fn process_alert(&mut self, alert_type: AlertType, message: String, severity: AlertSeverity) -> Result<String, AftercareError> {
        // Process incoming alert through deduplication and escalation
        let alert_id = format!("alert_{}_{}", 
            chrono::Utc::now().timestamp(), 
            rand::random::<u32>());
        
        // Check deduplication
        if !self.dedup_engine.should_process_alert(&alert_type, &message).await {
            return Ok(format!("Alert deduplicated: {}", alert_id));
        }
        
        // Create alert summary
        let alert_summary = AlertSummary {
            alert_id: alert_id.clone(),
            alert_type,
            severity,
            message,
            created_at: SystemTime::now(),
            status: AlertStatus::Active,
        };
        
        // Process escalation
        self.escalation_manager.process_alert(&alert_summary).await
            .map_err(|e| AftercareError::AlertError(e.to_string()))?;
        
        Ok(alert_id)
    }
}

impl AlertDeduplicationEngine {
    pub fn new(window: Duration) -> Self {
        Self {
            active_fingerprints: HashMap::new(),
            window,
        }
    }
    
    pub async fn should_process_alert(&mut self, alert_type: &AlertType, message: &str) -> bool {
        let content_hash = format!("{:?}:{}", alert_type, message);
        let now = SystemTime::now();
        
        // Clean up old fingerprints
        self.active_fingerprints.retain(|_, fingerprint| {
            now.duration_since(fingerprint.last_occurrence).unwrap_or_default() < self.window
        });
        
        // Check if this alert should be deduplicated
        if let Some(fingerprint) = self.active_fingerprints.get_mut(&content_hash) {
            fingerprint.last_occurrence = now;
            fingerprint.count += 1;
            false // Deduplicate
        } else {
            // New alert
            self.active_fingerprints.insert(content_hash.clone(), AlertFingerprint {
                alert_type: alert_type.clone(),
                content_hash,
                first_occurrence: now,
                last_occurrence: now,
                count: 1,
            });
            true // Process
        }
    }
}

impl EscalationManager {
    pub fn new() -> Self {
        Self {
            policies: HashMap::new(),
            active_escalations: HashMap::new(),
        }
    }
    
    pub async fn process_alert(&mut self, alert: &AlertSummary) -> Result<(), EscalationError> {
        // Process alert escalation based on severity and policy
        match alert.severity {
            AlertSeverity::Critical | AlertSeverity::Emergency => {
                // Immediate escalation for critical alerts
                self.create_immediate_escalation(alert).await?;
            }
            AlertSeverity::Warning => {
                // Standard escalation for warnings
                self.create_standard_escalation(alert).await?;
            }
            AlertSeverity::Info => {
                // No escalation for info alerts
            }
        }
        
        Ok(())
    }
    
    async fn create_immediate_escalation(&mut self, alert: &AlertSummary) -> Result<(), EscalationError> {
        // Create immediate escalation for critical alerts
        let escalation = ActiveEscalation {
            alert_id: alert.alert_id.clone(),
            current_level: 1,
            start_time: SystemTime::now(),
            next_escalation: SystemTime::now() + Duration::from_secs(300), // 5 minutes
            status: EscalationStatus::Active,
        };
        
        self.active_escalations.insert(alert.alert_id.clone(), escalation);
        
        info!("🚨 Immediate escalation created for alert: {}", alert.alert_id);
        Ok(())
    }
    
    async fn create_standard_escalation(&mut self, alert: &AlertSummary) -> Result<(), EscalationError> {
        // Create standard escalation for warning alerts
        let escalation = ActiveEscalation {
            alert_id: alert.alert_id.clone(),
            current_level: 1,
            start_time: SystemTime::now(),
            next_escalation: SystemTime::now() + Duration::from_secs(1800), // 30 minutes
            status: EscalationStatus::Active,
        };
        
        self.active_escalations.insert(alert.alert_id.clone(), escalation);
        
        info!("⚠️  Standard escalation created for alert: {}", alert.alert_id);
        Ok(())
    }
}

// Supporting types and implementations

impl Default for DashboardState {
    fn default() -> Self {
        let now = SystemTime::now();
        Self {
            last_update: now,
            current_metrics: CalibrationMetrics {
                aece: 0.0,
                dece: 0.0,
                brier: 0.0,
                alpha: 0.0,
                clamp_rate_percent: 0.0,
                merged_bin_percent: 0.0,
                timestamp: now,
            },
            trends: TrendingData {
                aece_trend: Vec::new(),
                dece_trend: Vec::new(),
                brier_trend: Vec::new(),
                alpha_trend: Vec::new(),
                clamp_rate_trend: Vec::new(),
                merged_bin_trend: Vec::new(),
            },
            slice_highlights: HashMap::new(),
            sla_status: SlaStatus {
                overall_compliance: 0.0,
                component_status: HashMap::new(),
                active_violations: Vec::new(),
                statistical_enforcement: StatisticalEnforcement {
                    tests_performed: Vec::new(),
                    confidence_intervals: HashMap::new(),
                    significance_levels: HashMap::new(),
                },
            },
            active_alerts: Vec::new(),
            health_indicators: HealthIndicators {
                overall_health: SystemHealthStatus::Healthy,
                component_health: HashMap::new(),
                performance_indicators: PerformanceIndicators {
                    avg_response_time_ms: 0.0,
                    p95_response_time_ms: 0.0,
                    p99_response_time_ms: 0.0,
                    throughput_rps: 0.0,
                    error_rate_percent: 0.0,
                },
                reliability_indicators: ReliabilityIndicators {
                    uptime_24h: 0.0,
                    mtbf_hours: 0.0,
                    mttr_minutes: 0.0,
                    availability_percent: 0.0,
                },
            },
        }
    }
}

impl Default for DashboardConfig {
    fn default() -> Self {
        Self {
            retention_duration: Duration::from_secs(7 * 24 * 3600), // 7 days
            trend_window: Duration::from_secs(24 * 3600), // 24 hours
            thresholds: DashboardThresholds::default(),
            refresh_rate: Duration::from_secs(30),
        }
    }
}

impl Default for DashboardThresholds {
    fn default() -> Self {
        Self {
            aece_highlight_threshold: 0.01,
            dece_warning_threshold: 0.02,
            brier_alert_threshold: 0.1,
            alpha_drift_threshold: 0.05,
            clamp_rate_warning_threshold: 10.0,
            merged_bin_warning_threshold: 5.0,
            merged_bin_critical_threshold: 20.0,
        }
    }
}

impl Default for RunbookConfig {
    fn default() -> Self {
        Self {
            procedure_timeout: Duration::from_secs(300),
            data_capture_window: Duration::from_secs(900),
            decision_timeout: Duration::from_secs(60),
        }
    }
}

impl Default for AlertTuningConfig {
    fn default() -> Self {
        Self {
            dedup_window: Duration::from_secs(300), // 5 minutes
            escalation_delays: {
                let mut delays = HashMap::new();
                delays.insert(AlertSeverity::Info, Duration::from_secs(3600));
                delays.insert(AlertSeverity::Warning, Duration::from_secs(1800));
                delays.insert(AlertSeverity::Critical, Duration::from_secs(300));
                delays.insert(AlertSeverity::Emergency, Duration::from_secs(60));
                delays
            },
            suppression_rules: Vec::new(),
            critical_thresholds: CriticalThresholds::default(),
        }
    }
}

impl Default for CriticalThresholds {
    fn default() -> Self {
        Self {
            mask_mismatch_tolerance: 0.0,
            score_range_min: 0.0,
            score_range_max: 1.0,
            alpha_drift_wow_threshold: 0.05,
            merged_bin_critical_percent: 20.0,
        }
    }
}

impl Default for AftercareConfig {
    fn default() -> Self {
        Self {
            dashboard_refresh_interval: Duration::from_secs(30),
            alert_evaluation_window: Duration::from_secs(60),
            sla_monitoring_frequency: Duration::from_secs(300),
            runbook_response_timeout: Duration::from_secs(600),
            alert_dedup_window: Duration::from_secs(300),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RunbookResult {
    pub procedure_name: String,
    pub decision: DecisionResult,
    pub collected_data: HashMap<String, f64>,
    pub execution_time: SystemTime,
    pub result_status: RunbookStatus,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DecisionResult {
    pub decision: String,
    pub confidence: f64,
    pub reasoning: String,
    pub recommended_action: ActionType,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum RunbookStatus {
    Ready,
    Executing,
    Success,
    Failed,
    Timeout,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AftercareStatus {
    pub dashboard_state: DashboardState,
    pub active_alerts: Vec<AlertSummary>,
    pub runbook_status: RunbookStatus,
    pub system_health: SystemHealthStatus,
    pub timestamp: SystemTime,
}

#[derive(Debug, Error)]
pub enum RunbookError {
    #[error("Procedure not found: {0}")]
    ProcedureNotFound(String),
    
    #[error("Data collection failed: {0}")]
    DataCollectionFailed(String),
    
    #[error("Decision tree execution failed: {0}")]
    DecisionTreeFailed(String),
    
    #[error("Timeout occurred")]
    Timeout,
    
    #[error("Configuration error: {0}")]
    ConfigurationError(String),
}

#[derive(Debug, Error)]
pub enum EscalationError {
    #[error("Escalation policy not found: {0}")]
    PolicyNotFound(String),
    
    #[error("Escalation failed: {0}")]
    EscalationFailed(String),
    
    #[error("Invalid escalation configuration: {0}")]
    InvalidConfiguration(String),
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_dashboard_config_default() {
        let config = DashboardConfig::default();
        assert_eq!(config.retention_duration, Duration::from_secs(7 * 24 * 3600));
        assert_eq!(config.thresholds.aece_highlight_threshold, 0.01);
    }
    
    #[test]
    fn test_critical_thresholds() {
        let thresholds = CriticalThresholds::default();
        assert_eq!(thresholds.mask_mismatch_tolerance, 0.0);
        assert_eq!(thresholds.merged_bin_critical_percent, 20.0);
    }
    
    #[tokio::test]
    async fn test_alert_deduplication() {
        let mut dedup_engine = AlertDeduplicationEngine::new(Duration::from_secs(300));
        
        // First alert should be processed
        let should_process1 = dedup_engine.should_process_alert(&AlertType::MaskMismatch, "Test message").await;
        assert!(should_process1);
        
        // Duplicate alert should be deduplicated
        let should_process2 = dedup_engine.should_process_alert(&AlertType::MaskMismatch, "Test message").await;
        assert!(!should_process2);
    }
    
    #[test]
    fn test_runbook_status() {
        let status = RunbookStatus::Ready;
        assert_eq!(status, RunbookStatus::Ready);
        assert_ne!(status, RunbookStatus::Executing);
    }
}