trustformers 0.1.1

TrustformeRS - Rust port of Hugging Face Transformers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
//! Safety filtering and content moderation for conversational AI pipeline.
//!
//! This module provides comprehensive safety filtering, content moderation, and risk assessment
//! capabilities for the conversational pipeline system. It includes pattern-based detection,
//! toxicity scoring, safety policy enforcement, and detailed violation reporting.
//!
//! # Features
//!
//! - **Content Filtering**: Real-time input and output content filtering
//! - **Pattern Detection**: Advanced regex and keyword-based safety pattern detection
//! - **Risk Assessment**: Multi-dimensional safety scoring and risk evaluation
//! - **Policy Enforcement**: Configurable safety policies and violation handling
//! - **Violation Reporting**: Detailed safety violation tracking and reporting
//! - **Integration**: Seamless integration with conversation metadata and health tracking
//!
//! # Usage
//!
//! ```rust,no_run
//! use trustformers::pipeline::conversational::safety::SafetyFilter;
//!
//! // Create a safety filter with default configuration
//! let filter = SafetyFilter::new();
//!
//! // Check if content is safe
//! let is_safe = filter.is_safe("Hello, how can I help you today?");
//! assert!(is_safe);
//!
//! // Get detailed safety assessment
//! let assessment = filter.analyze_safety("Some potentially harmful content");
//! println!("Safety score: {}", assessment.toxicity_score);
//! ```

use super::types::*;
use crate::core::error::Result;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::time::Instant;
use thiserror::Error;

// ================================================================================================
// ENHANCED SAFETY CONFIGURATION TYPES
// ================================================================================================

/// Extended safety configuration with comprehensive filtering options
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtendedSafetyConfig {
    /// Base configuration
    pub base_config: SafetyFilterConfig,
    /// Maximum allowed toxicity score (0.0-1.0)
    pub max_toxicity_score: f32,
    /// Maximum allowed harm score (0.0-1.0)
    pub max_harm_score: f32,
    /// Maximum allowed bias score (0.0-1.0)
    pub max_bias_score: f32,
    /// Enable input content filtering
    pub filter_input: bool,
    /// Enable output content filtering
    pub filter_output: bool,
    /// Custom banned terms and phrases
    pub custom_banned_terms: Vec<String>,
    /// Custom allowed terms (exceptions)
    pub allowed_terms: Vec<String>,
    /// Safety patterns configuration
    pub pattern_config: PatternConfig,
    /// Content length limits for safety analysis
    pub content_limits: ContentLimits,
    /// Violation handling configuration
    pub violation_handling: ViolationHandling,
    /// Performance optimization settings
    pub performance_settings: PerformanceSettings,
}

impl Default for ExtendedSafetyConfig {
    fn default() -> Self {
        Self {
            base_config: SafetyFilterConfig::default(),
            max_toxicity_score: 0.7,
            max_harm_score: 0.6,
            max_bias_score: 0.8,
            filter_input: true,
            filter_output: true,
            custom_banned_terms: Vec::new(),
            allowed_terms: Vec::new(),
            pattern_config: PatternConfig::default(),
            content_limits: ContentLimits::default(),
            violation_handling: ViolationHandling::default(),
            performance_settings: PerformanceSettings::default(),
        }
    }
}

/// Configuration for safety pattern detection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PatternConfig {
    /// Enable regex pattern matching
    pub enable_regex_patterns: bool,
    /// Enable keyword matching
    pub enable_keyword_matching: bool,
    /// Enable context-aware analysis
    pub enable_context_analysis: bool,
    /// Case sensitivity for pattern matching
    pub case_sensitive: bool,
    /// Maximum pattern evaluation time (ms)
    pub max_pattern_eval_time_ms: u64,
}

impl Default for PatternConfig {
    fn default() -> Self {
        Self {
            enable_regex_patterns: true,
            enable_keyword_matching: true,
            enable_context_analysis: true,
            case_sensitive: false,
            max_pattern_eval_time_ms: 100,
        }
    }
}

/// Content length limits for safety analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContentLimits {
    /// Maximum content length to analyze (characters)
    pub max_analysis_length: usize,
    /// Minimum content length for detailed analysis
    pub min_detailed_analysis_length: usize,
    /// Enable content truncation for analysis
    pub enable_truncation: bool,
}

impl Default for ContentLimits {
    fn default() -> Self {
        Self {
            max_analysis_length: 10000,
            min_detailed_analysis_length: 10,
            enable_truncation: true,
        }
    }
}

/// Configuration for handling safety violations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ViolationHandling {
    /// Custom replacement message for filtered content
    pub replacement_message: Option<String>,
    /// Enable violation logging
    pub enable_logging: bool,
    /// Maximum violations before conversation reset
    pub max_violations_before_reset: usize,
    /// Cooldown period after violations (seconds)
    pub violation_cooldown_seconds: u64,
}

impl Default for ViolationHandling {
    fn default() -> Self {
        Self {
            replacement_message: Some(
                "I can't assist with that request. Let's talk about something else.".to_string(),
            ),
            enable_logging: true,
            max_violations_before_reset: 3,
            violation_cooldown_seconds: 300, // 5 minutes
        }
    }
}

/// Performance optimization settings for safety filtering
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceSettings {
    /// Enable result caching
    pub enable_caching: bool,
    /// Cache size limit
    pub cache_size_limit: usize,
    /// Cache expiry time (seconds)
    pub cache_expiry_seconds: u64,
    /// Enable parallel pattern evaluation
    pub enable_parallel_evaluation: bool,
    /// Maximum evaluation threads
    pub max_evaluation_threads: usize,
}

impl Default for PerformanceSettings {
    fn default() -> Self {
        Self {
            enable_caching: true,
            cache_size_limit: 1000,
            cache_expiry_seconds: 3600,        // 1 hour
            enable_parallel_evaluation: false, // Keep simple for now
            max_evaluation_threads: 4,
        }
    }
}

// ================================================================================================
// ENHANCED SAFETY ASSESSMENT TYPES
// ================================================================================================

/// Comprehensive safety assessment result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnhancedSafetyAssessment {
    /// Base safety analysis
    pub base_analysis: SafetyAnalysis,
    /// Detailed safety scores by category
    pub category_scores: SafetyScores,
    /// Enhanced violations with location information
    pub enhanced_violations: Vec<EnhancedSafetyViolation>,
    /// Assessment confidence (0.0-1.0)
    pub confidence: f32,
    /// Assessment timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,
    /// Processing time in milliseconds
    pub processing_time_ms: f64,
}

impl EnhancedSafetyAssessment {
    /// Check if content is considered safe based on thresholds
    pub fn is_safe(&self, config: &ExtendedSafetyConfig) -> bool {
        self.base_analysis.is_safe
            && self.category_scores.toxicity <= config.max_toxicity_score
            && self.category_scores.harm <= config.max_harm_score
            && self.category_scores.bias <= config.max_bias_score
    }

    /// Get the highest risk violation
    pub fn highest_risk_violation(&self) -> Option<&EnhancedSafetyViolation> {
        self.enhanced_violations
            .iter()
            .max_by_key(|v| v.base_violation.severity.clone() as u8)
    }

    /// Check if assessment indicates immediate action needed
    pub fn requires_immediate_action(&self) -> bool {
        self.enhanced_violations
            .iter()
            .any(|v| matches!(v.base_violation.severity, SafetySeverity::Critical))
    }
}

/// Detailed safety scores by category
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SafetyScores {
    /// Toxicity score (0.0-1.0)
    pub toxicity: f32,
    /// Harm potential score (0.0-1.0)
    pub harm: f32,
    /// Bias detection score (0.0-1.0)
    pub bias: f32,
    /// Personal information exposure score (0.0-1.0)
    pub privacy: f32,
    /// Inappropriate content score (0.0-1.0)
    pub inappropriate: f32,
    /// Violence indicators score (0.0-1.0)
    pub violence: f32,
    /// Harassment indicators score (0.0-1.0)
    pub harassment: f32,
}

impl Default for SafetyScores {
    fn default() -> Self {
        Self {
            toxicity: 0.0,
            harm: 0.0,
            bias: 0.0,
            privacy: 0.0,
            inappropriate: 0.0,
            violence: 0.0,
            harassment: 0.0,
        }
    }
}

/// Enhanced safety violation with location and context information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnhancedSafetyViolation {
    /// Base violation information
    pub base_violation: SafetyViolation,
    /// Location of violation in content (character positions)
    pub location: Option<ViolationLocation>,
    /// Pattern or rule that triggered the violation
    pub triggered_rule: String,
    /// Context around the violation
    pub context: Option<String>,
    /// Suggested remediation
    pub suggested_remediation: Option<String>,
}

/// Location information for violations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ViolationLocation {
    pub start_pos: usize,
    pub end_pos: usize,
    pub matched_text: String,
}

// ================================================================================================
// ENHANCED SAFETY FILTER IMPLEMENTATION
// ================================================================================================

/// Enhanced safety filter alias
pub type EnhancedSafetyFilter = SafetyFilter;

/// Safety configuration alias
pub type SafetyConfig = ExtendedSafetyConfig;

/// Safety assessment alias
pub type SafetyAssessment = EnhancedSafetyAssessment;

/// Enhanced safety filter for conversational content moderation
#[derive(Debug)]
pub struct SafetyFilter {
    /// Enhanced configuration
    extended_config: Box<ExtendedSafetyConfig>,
    /// Banned words/phrases (from base implementation)
    pub banned_terms: Vec<String>,
    /// Toxic content patterns (from base implementation)
    pub toxic_patterns: Vec<regex::Regex>,
    /// Maximum allowed toxicity score (from base implementation)
    pub max_toxicity_score: f32,
    /// Configuration for advanced filtering (from base implementation)
    pub config: SafetyFilterConfig,
    /// Additional compiled regex patterns for enhanced detection
    harm_patterns: Vec<Regex>,
    privacy_patterns: Vec<Regex>,
    violence_patterns: Vec<Regex>,
    harassment_patterns: Vec<Regex>,
    bias_keywords: HashSet<String>,
    /// Result cache for performance optimization
    assessment_cache: HashMap<String, (EnhancedSafetyAssessment, Instant)>,
    /// Violation history for tracking patterns
    violation_history: Vec<EnhancedSafetyViolation>,
}

impl SafetyFilter {
    /// Create a new safety filter with default configuration
    pub fn new() -> Self {
        let extended_config = ExtendedSafetyConfig::default();
        Self::with_extended_config(extended_config)
    }

    /// Create safety filter with extended configuration
    pub fn with_extended_config(extended_config: ExtendedSafetyConfig) -> Self {
        let mut filter = Self {
            max_toxicity_score: extended_config.max_toxicity_score,
            config: extended_config.base_config.clone(),
            extended_config: Box::new(extended_config),
            banned_terms: Self::default_banned_terms(),
            toxic_patterns: Vec::new(),
            harm_patterns: Vec::new(),
            privacy_patterns: Vec::new(),
            violence_patterns: Vec::new(),
            harassment_patterns: Vec::new(),
            bias_keywords: HashSet::new(),
            assessment_cache: HashMap::new(),
            violation_history: Vec::new(),
        };

        filter.initialize_patterns();
        filter
    }

    fn default_banned_terms() -> Vec<String> {
        vec![
            // Hate speech indicators
            "hate".to_string(),
            "racist".to_string(),
            "sexist".to_string(),
            // Violence indicators
            "violence".to_string(),
            "harm".to_string(),
            "kill".to_string(),
            "hurt".to_string(),
            "attack".to_string(),
            // Inappropriate content
            "inappropriate".to_string(),
            "offensive".to_string(),
            "abusive".to_string(),
            // Privacy concerns
            "password".to_string(),
            "ssn".to_string(),
            "social security".to_string(),
            "credit card".to_string(),
            "bank account".to_string(),
        ]
    }

    /// Initialize safety detection patterns
    fn initialize_patterns(&mut self) {
        self.initialize_toxic_patterns();
        self.initialize_harm_patterns();
        self.initialize_privacy_patterns();
        self.initialize_violence_patterns();
        self.initialize_harassment_patterns();
        self.initialize_bias_keywords();
    }

    fn initialize_toxic_patterns(&mut self) {
        let patterns = [
            r"(?i)\b(hate|despise|loathe)\s+\w+",
            r"(?i)\b(kill|murder|destroy)\s+(you|them|everyone)",
            r"(?i)\b(stupid|idiot|moron|dumb)\s+(person|people|you)",
            r"(?i)\b(go\s+die|drop\s+dead)",
            r"(?i)\b(toxic|poisonous)\s+(behavior|attitude)",
        ];

        for pattern in patterns {
            if let Ok(regex) = Regex::new(pattern) {
                self.toxic_patterns.push(regex);
            }
        }
    }

    fn initialize_harm_patterns(&mut self) {
        let patterns = [
            r"(?i)\b(self[\s\-]?harm|self[\s\-]?injury)",
            r"(?i)\b(suicide|suicidal|end\s+it\s+all)",
            r"(?i)\b(cut|cutting|burn|burning|hurt|hurting)\s+(myself|yourself)",
            r"(?i)\b(want\s+to\s+die|wish\s+I\s+was\s+dead)",
            r"(?i)\b(harmful|dangerous)\s+(advice|instructions)",
        ];

        for pattern in patterns {
            if let Ok(regex) = Regex::new(pattern) {
                self.harm_patterns.push(regex);
            }
        }
    }

    fn initialize_privacy_patterns(&mut self) {
        let patterns = [
            r"\b\d{3}-\d{2}-\d{4}\b",                               // SSN pattern
            r"\b\d{4}[\s\-]?\d{4}[\s\-]?\d{4}[\s\-]?\d{4}\b",       // Credit card pattern
            r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b", // Email pattern
            r"\b\d{3}[\s\-]?\d{3}[\s\-]?\d{4}\b",                   // Phone number pattern
            r"(?i)\b(password|passwd|pwd)[\s:=]+\S+",
            r"(?i)\b(api[\s\-]?key|secret[\s\-]?key)[\s:=]+\S+",
        ];

        for pattern in patterns {
            if let Ok(regex) = Regex::new(pattern) {
                self.privacy_patterns.push(regex);
            }
        }
    }

    fn initialize_violence_patterns(&mut self) {
        let patterns = [
            r"(?i)\b(violence|violent|attack|assault)",
            r"(?i)\b(fight|fighting|beat\s+up)",
            r"(?i)\b(weapon|gun|knife|bomb)",
            r"(?i)\b(threatening|threat|intimidate)",
            r"(?i)\b(abuse|abusive|abusing)",
        ];

        for pattern in patterns {
            if let Ok(regex) = Regex::new(pattern) {
                self.violence_patterns.push(regex);
            }
        }
    }

    fn initialize_harassment_patterns(&mut self) {
        let patterns = [
            r"(?i)\b(harass|harassment|stalking)",
            r"(?i)\b(bully|bullying|intimidate)",
            r"(?i)\b(creep|creepy|pervert)",
            r"(?i)\b(ugly|disgusting|gross)\s+(person|you)",
            r"(?i)\b(shut\s+up|get\s+lost|go\s+away)",
        ];

        for pattern in patterns {
            if let Ok(regex) = Regex::new(pattern) {
                self.harassment_patterns.push(regex);
            }
        }
    }

    fn initialize_bias_keywords(&mut self) {
        let bias_terms = [
            "racist",
            "sexist",
            "homophobic",
            "transphobic",
            "xenophobic",
            "islamophobic",
            "antisemitic",
            "stereotype",
            "prejudice",
            "discrimination",
            "bigot",
            "bigotry",
            "intolerant",
        ];

        for term in bias_terms {
            self.bias_keywords.insert(term.to_string());
        }
    }

    /// Create safety filter with custom configuration (backward compatibility)
    pub fn with_config(config: SafetyFilterConfig) -> Self {
        let mut extended_config = ExtendedSafetyConfig::default();
        extended_config.base_config = config.clone();
        extended_config.max_toxicity_score = match config.moderation_level {
            ModerationLevel::Permissive => 0.9,
            ModerationLevel::Moderate => 0.7,
            ModerationLevel::Strict => 0.4,
            ModerationLevel::VeryStrict => 0.2,
            ModerationLevel::Custom => 0.5,
        };
        Self::with_extended_config(extended_config)
    }

    /// Create a safety filter with strict configuration
    pub fn strict() -> Self {
        let mut config = ExtendedSafetyConfig::default();
        config.base_config.moderation_level = ModerationLevel::Strict;
        config.max_toxicity_score = 0.4;
        config.max_harm_score = 0.3;
        config.max_bias_score = 0.5;
        Self::with_extended_config(config)
    }

    /// Create a safety filter with permissive configuration
    pub fn permissive() -> Self {
        let mut config = ExtendedSafetyConfig::default();
        config.base_config.moderation_level = ModerationLevel::Permissive;
        config.max_toxicity_score = 0.9;
        config.max_harm_score = 0.8;
        config.max_bias_score = 0.9;
        Self::with_extended_config(config)
    }

    /// Create a safety filter for educational environments
    pub fn educational() -> Self {
        let mut config = ExtendedSafetyConfig::default();
        config.base_config.moderation_level = ModerationLevel::Moderate;
        config.max_toxicity_score = 0.6;
        config.max_harm_score = 0.5;
        config.max_bias_score = 0.7;
        Self::with_extended_config(config)
    }

    /// Check if content is safe (simplified interface)
    pub fn is_safe(&self, content: &str) -> bool {
        if !self.config.enabled {
            return true;
        }

        let assessment = self.assess_content_safety_enhanced(content);
        assessment.is_safe(&self.extended_config)
    }

    /// Enhanced comprehensive safety assessment of content
    pub fn assess_content_safety_enhanced(&self, content: &str) -> EnhancedSafetyAssessment {
        let start_time = Instant::now();

        // Check cache first if enabled
        if self.extended_config.performance_settings.enable_caching {
            if let Some((cached_assessment, cache_time)) = self.assessment_cache.get(content) {
                let cache_age = start_time.duration_since(*cache_time);
                if cache_age.as_secs()
                    < self.extended_config.performance_settings.cache_expiry_seconds
                {
                    return cached_assessment.clone();
                }
            }
        }

        // Truncate content if necessary
        let analyzed_content = if self.extended_config.content_limits.enable_truncation
            && content.len() > self.extended_config.content_limits.max_analysis_length
        {
            &content[..self.extended_config.content_limits.max_analysis_length]
        } else {
            content
        };

        // Perform base safety analysis first
        let base_analysis = self.analyze_safety(analyzed_content);

        // Perform enhanced safety assessment
        let mut category_scores = SafetyScores::default();
        let mut enhanced_violations = Vec::new();

        // Analyze different safety categories
        self.analyze_toxicity_enhanced(
            analyzed_content,
            &mut category_scores,
            &mut enhanced_violations,
        );
        self.analyze_harm_enhanced(
            analyzed_content,
            &mut category_scores,
            &mut enhanced_violations,
        );
        self.analyze_privacy_enhanced(
            analyzed_content,
            &mut category_scores,
            &mut enhanced_violations,
        );
        self.analyze_violence_enhanced(
            analyzed_content,
            &mut category_scores,
            &mut enhanced_violations,
        );
        self.analyze_harassment_enhanced(
            analyzed_content,
            &mut category_scores,
            &mut enhanced_violations,
        );
        self.analyze_bias_enhanced(
            analyzed_content,
            &mut category_scores,
            &mut enhanced_violations,
        );
        self.analyze_inappropriate_content_enhanced(
            analyzed_content,
            &mut category_scores,
            &mut enhanced_violations,
        );

        // Calculate confidence based on content length and pattern matches
        let confidence =
            self.calculate_assessment_confidence(analyzed_content, &enhanced_violations);

        let processing_time_ms = start_time.elapsed().as_secs_f64() * 1000.0;

        EnhancedSafetyAssessment {
            base_analysis,
            category_scores,
            enhanced_violations,
            confidence,
            timestamp: chrono::Utc::now(),
            processing_time_ms,
        }
    }

    /// Filter input content and return safe version
    pub fn filter_input(&self, content: &str) -> Result<String> {
        if !self.config.enabled || !self.extended_config.filter_input {
            return Ok(content.to_string());
        }

        let assessment = self.assess_content_safety_enhanced(content);

        if assessment.is_safe(&self.extended_config) {
            Ok(content.to_string())
        } else {
            self.handle_violation(&assessment, content)
        }
    }

    /// Filter output content and return safe version
    pub fn filter_output(&self, content: &str) -> Result<String> {
        if !self.config.enabled || !self.extended_config.filter_output {
            return Ok(content.to_string());
        }

        let assessment = self.assess_content_safety_enhanced(content);

        if assessment.is_safe(&self.extended_config) {
            Ok(content.to_string())
        } else {
            self.handle_violation(&assessment, content)
        }
    }

    /// Add safety metadata to conversation metadata
    pub fn enrich_conversation_metadata(&self, content: &str, metadata: &mut ConversationMetadata) {
        if !self.config.enabled {
            return;
        }

        let assessment = self.assess_content_safety_enhanced(content);

        // Add safety flags from enhanced violations
        for violation in &assessment.enhanced_violations {
            metadata.safety_flags.push(format!(
                "violation:{}",
                violation.base_violation.violation_type
            ));
        }

        // Adjust confidence based on safety assessment
        let overall_safety_score = (assessment.category_scores.toxicity
            + assessment.category_scores.harm
            + assessment.category_scores.violence)
            / 3.0;

        // Reduce confidence and quality when there are any violations
        if !assessment.enhanced_violations.is_empty() || overall_safety_score > 0.1 {
            // Apply penalty based on the higher of: number of violations or score-based penalty
            let violation_penalty = (assessment.enhanced_violations.len() as f32 * 0.05).min(0.3);
            let score_penalty = overall_safety_score * 0.3;
            let total_penalty = violation_penalty.max(score_penalty).max(0.05); // Minimum 5% penalty
            metadata.confidence *= 1.0 - total_penalty;
            metadata.quality_score *= 1.0 - total_penalty;
        } else {
            // No violations, only apply minor score-based adjustment to quality
            metadata.quality_score *= 1.0 - overall_safety_score * 0.5;
        }
    }

    /// Get toxicity score (backward compatibility)
    /// Note: This method calculates toxicity directly without calling assess_content_safety_enhanced
    /// to avoid infinite recursion (assess_content_safety_enhanced -> analyze_safety -> get_toxicity_score)
    pub fn get_toxicity_score(&self, content: &str) -> f32 {
        if !self.config.enabled {
            return 0.0;
        }

        self.calculate_toxicity_score_direct(content)
    }

    /// Direct toxicity calculation without triggering recursive assessment
    fn calculate_toxicity_score_direct(&self, content: &str) -> f32 {
        let mut toxicity_score: f32 = 0.0;
        let content_lower = content.to_lowercase();

        // Check banned terms
        for term in self.banned_terms.iter() {
            if content_lower.contains(&term.to_lowercase()) {
                toxicity_score += 0.3;
            }
        }

        // Check toxic patterns
        for pattern in self.toxic_patterns.iter() {
            if pattern.is_match(content) {
                toxicity_score += 0.4;
            }
        }

        toxicity_score.min(1.0)
    }

    // ================================================================================================
    // ENHANCED ANALYSIS METHODS
    // ================================================================================================

    fn analyze_toxicity_enhanced(
        &self,
        content: &str,
        scores: &mut SafetyScores,
        violations: &mut Vec<EnhancedSafetyViolation>,
    ) {
        let mut toxicity_score: f32 = 0.0;

        // Check banned terms
        let content_lower = content.to_lowercase();
        for term in self.banned_terms.iter() {
            if content_lower.contains(&term.to_lowercase()) {
                toxicity_score += 0.3;

                violations.push(EnhancedSafetyViolation {
                    base_violation: SafetyViolation {
                        violation_type: "toxicity".to_string(),
                        severity: SafetySeverity::Medium,
                        description: format!("Banned term detected: {}", term),
                        confidence: 0.9,
                    },
                    location: self.find_term_location(content, term),
                    triggered_rule: format!("banned_term:{}", term),
                    context: self.extract_context(content, term),
                    suggested_remediation: Some(
                        "Consider rephrasing without offensive language".to_string(),
                    ),
                });
            }
        }

        // Check toxic patterns
        for (i, pattern) in self.toxic_patterns.iter().enumerate() {
            if let Some(matches) = pattern.find(content) {
                toxicity_score += 0.4;

                violations.push(EnhancedSafetyViolation {
                    base_violation: SafetyViolation {
                        violation_type: "toxicity".to_string(),
                        severity: SafetySeverity::High,
                        description: "Toxic language pattern detected".to_string(),
                        confidence: 0.8,
                    },
                    location: Some(ViolationLocation {
                        start_pos: matches.start(),
                        end_pos: matches.end(),
                        matched_text: matches.as_str().to_string(),
                    }),
                    triggered_rule: format!("toxic_pattern_{}", i),
                    context: self.extract_context_from_match(
                        content,
                        matches.start(),
                        matches.end(),
                    ),
                    suggested_remediation: Some("Please use more respectful language".to_string()),
                });
            }
        }

        scores.toxicity = toxicity_score.min(1.0);
    }

    fn analyze_harm_enhanced(
        &self,
        content: &str,
        scores: &mut SafetyScores,
        violations: &mut Vec<EnhancedSafetyViolation>,
    ) {
        let mut harm_score: f32 = 0.0;

        for (i, pattern) in self.harm_patterns.iter().enumerate() {
            if let Some(matches) = pattern.find(content) {
                harm_score += 0.6; // Harm patterns are more serious

                violations.push(EnhancedSafetyViolation {
                    base_violation: SafetyViolation {
                        violation_type: "self_harm".to_string(),
                        severity: SafetySeverity::Critical,
                        description: "Self-harm or harmful content detected".to_string(),
                        confidence: 0.9,
                    },
                    location: Some(ViolationLocation {
                        start_pos: matches.start(),
                        end_pos: matches.end(),
                        matched_text: matches.as_str().to_string(),
                    }),
                    triggered_rule: format!("harm_pattern_{}", i),
                    context: self.extract_context_from_match(content, matches.start(), matches.end()),
                    suggested_remediation: Some("If you're experiencing thoughts of self-harm, please seek help from a mental health professional".to_string()),
                });
            }
        }

        scores.harm = harm_score.min(1.0);
    }

    fn analyze_privacy_enhanced(
        &self,
        content: &str,
        scores: &mut SafetyScores,
        violations: &mut Vec<EnhancedSafetyViolation>,
    ) {
        let mut privacy_score: f32 = 0.0;

        for (i, pattern) in self.privacy_patterns.iter().enumerate() {
            if let Some(matches) = pattern.find(content) {
                privacy_score += 0.5;

                violations.push(EnhancedSafetyViolation {
                    base_violation: SafetyViolation {
                        violation_type: "personal_information".to_string(),
                        severity: SafetySeverity::High,
                        description: "Personal information detected".to_string(),
                        confidence: 0.8,
                    },
                    location: Some(ViolationLocation {
                        start_pos: matches.start(),
                        end_pos: matches.end(),
                        matched_text: "***REDACTED***".to_string(), // Don't expose PII
                    }),
                    triggered_rule: format!("privacy_pattern_{}", i),
                    context: Some("[CONTEXT REDACTED FOR PRIVACY]".to_string()),
                    suggested_remediation: Some(
                        "Avoid sharing personal information in conversations".to_string(),
                    ),
                });
            }
        }

        scores.privacy = privacy_score.min(1.0);
    }

    fn analyze_violence_enhanced(
        &self,
        content: &str,
        scores: &mut SafetyScores,
        violations: &mut Vec<EnhancedSafetyViolation>,
    ) {
        let mut violence_score: f32 = 0.0;

        for (i, pattern) in self.violence_patterns.iter().enumerate() {
            if let Some(matches) = pattern.find(content) {
                violence_score += 0.4;

                violations.push(EnhancedSafetyViolation {
                    base_violation: SafetyViolation {
                        violation_type: "violence".to_string(),
                        severity: SafetySeverity::High,
                        description: "Violence-related content detected".to_string(),
                        confidence: 0.8,
                    },
                    location: Some(ViolationLocation {
                        start_pos: matches.start(),
                        end_pos: matches.end(),
                        matched_text: matches.as_str().to_string(),
                    }),
                    triggered_rule: format!("violence_pattern_{}", i),
                    context: self.extract_context_from_match(
                        content,
                        matches.start(),
                        matches.end(),
                    ),
                    suggested_remediation: Some(
                        "Please avoid discussing violent topics".to_string(),
                    ),
                });
            }
        }

        scores.violence = violence_score.min(1.0);
    }

    fn analyze_harassment_enhanced(
        &self,
        content: &str,
        scores: &mut SafetyScores,
        violations: &mut Vec<EnhancedSafetyViolation>,
    ) {
        let mut harassment_score: f32 = 0.0;

        for (i, pattern) in self.harassment_patterns.iter().enumerate() {
            if let Some(matches) = pattern.find(content) {
                harassment_score += 0.4;

                violations.push(EnhancedSafetyViolation {
                    base_violation: SafetyViolation {
                        violation_type: "harassment".to_string(),
                        severity: SafetySeverity::Medium,
                        description: "Harassment-related content detected".to_string(),
                        confidence: 0.7,
                    },
                    location: Some(ViolationLocation {
                        start_pos: matches.start(),
                        end_pos: matches.end(),
                        matched_text: matches.as_str().to_string(),
                    }),
                    triggered_rule: format!("harassment_pattern_{}", i),
                    context: self.extract_context_from_match(
                        content,
                        matches.start(),
                        matches.end(),
                    ),
                    suggested_remediation: Some(
                        "Please maintain respectful communication".to_string(),
                    ),
                });
            }
        }

        scores.harassment = harassment_score.min(1.0);
    }

    fn analyze_bias_enhanced(
        &self,
        content: &str,
        scores: &mut SafetyScores,
        violations: &mut Vec<EnhancedSafetyViolation>,
    ) {
        let content_lower = content.to_lowercase();
        let mut bias_score: f32 = 0.0;

        for keyword in self.bias_keywords.iter() {
            if content_lower.contains(keyword) {
                bias_score += 0.3;

                violations.push(EnhancedSafetyViolation {
                    base_violation: SafetyViolation {
                        violation_type: "bias".to_string(),
                        severity: SafetySeverity::Medium,
                        description: format!("Bias-related keyword detected: {}", keyword),
                        confidence: 0.6,
                    },
                    location: self.find_term_location(content, keyword),
                    triggered_rule: format!("bias_keyword:{}", keyword),
                    context: self.extract_context(content, keyword),
                    suggested_remediation: Some(
                        "Consider using more inclusive language".to_string(),
                    ),
                });
            }
        }

        scores.bias = bias_score.min(1.0);
    }

    fn analyze_inappropriate_content_enhanced(
        &self,
        content: &str,
        scores: &mut SafetyScores,
        violations: &mut Vec<EnhancedSafetyViolation>,
    ) {
        let inappropriate_keywords = ["inappropriate", "offensive", "rude", "vulgar", "profanity"];
        let content_lower = content.to_lowercase();
        let mut inappropriate_score: f32 = 0.0;

        for keyword in &inappropriate_keywords {
            if content_lower.contains(keyword) {
                inappropriate_score += 0.2;

                violations.push(EnhancedSafetyViolation {
                    base_violation: SafetyViolation {
                        violation_type: "inappropriate".to_string(),
                        severity: SafetySeverity::Low,
                        description: format!("Inappropriate content indicator: {}", keyword),
                        confidence: 0.5,
                    },
                    location: self.find_term_location(content, keyword),
                    triggered_rule: format!("inappropriate_keyword:{}", keyword),
                    context: self.extract_context(content, keyword),
                    suggested_remediation: Some(
                        "Please keep the conversation appropriate".to_string(),
                    ),
                });
            }
        }

        scores.inappropriate = inappropriate_score.min(1.0);
    }

    // ================================================================================================
    // HELPER METHODS
    // ================================================================================================

    fn calculate_assessment_confidence(
        &self,
        content: &str,
        violations: &[EnhancedSafetyViolation],
    ) -> f32 {
        let mut confidence = 0.7; // Base confidence

        // Adjust based on content length
        if content.len() > self.extended_config.content_limits.min_detailed_analysis_length {
            confidence += 0.1;
        }

        // Adjust based on violation confidence
        if !violations.is_empty() {
            let avg_violation_confidence: f32 =
                violations.iter().map(|v| v.base_violation.confidence).sum::<f32>()
                    / violations.len() as f32;
            confidence = (confidence + avg_violation_confidence) / 2.0;
        }

        confidence.min(1.0)
    }

    fn find_term_location(&self, content: &str, term: &str) -> Option<ViolationLocation> {
        let content_lower = content.to_lowercase();
        let term_lower = term.to_lowercase();

        content_lower.find(&term_lower).map(|start_pos| ViolationLocation {
            start_pos,
            end_pos: start_pos + term.len(),
            matched_text: content[start_pos..start_pos + term.len()].to_string(),
        })
    }

    fn extract_context(&self, content: &str, term: &str) -> Option<String> {
        if let Some(start_pos) = content.to_lowercase().find(&term.to_lowercase()) {
            let context_start = start_pos.saturating_sub(20);
            let context_end = (start_pos + term.len() + 20).min(content.len());
            Some(content[context_start..context_end].to_string())
        } else {
            None
        }
    }

    fn extract_context_from_match(
        &self,
        content: &str,
        start_pos: usize,
        end_pos: usize,
    ) -> Option<String> {
        let context_start = start_pos.saturating_sub(20);
        let context_end = (end_pos + 20).min(content.len());
        Some(content[context_start..context_end].to_string())
    }

    fn handle_violation(
        &self,
        assessment: &EnhancedSafetyAssessment,
        content: &str,
    ) -> Result<String> {
        // Use the base analysis recommended action
        match assessment.base_analysis.recommended_action {
            SafetyAction::Block => {
                let violations: Vec<String> = assessment
                    .enhanced_violations
                    .iter()
                    .map(|v| v.base_violation.description.clone())
                    .collect();
                Err(crate::error::TrustformersError::InvalidInput {
                    message: format!("Content blocked due to safety violations: {:?}", violations),
                    parameter: Some("content".to_string()),
                    expected: Some("safe content".to_string()),
                    received: Some("content with safety violations".to_string()),
                    suggestion: Some("Remove or modify unsafe content".to_string()),
                }
                .into())
            },
            SafetyAction::Modify => Ok(self
                .extended_config
                .violation_handling
                .replacement_message
                .clone()
                .unwrap_or_else(|| "Content filtered for safety.".to_string())),
            SafetyAction::Warn => {
                // Log violation but allow content
                Ok(format!("[FLAGGED] {}", content))
            },
            SafetyAction::Log => {
                // Just log, no modification
                Ok(content.to_string())
            },
            SafetyAction::Clarify => {
                // Request clarification from user
                Ok(format!(
                    "{}\n[System: This content requires clarification for safety verification]",
                    content
                ))
            },
        }
    }

    /// Evaluate a custom safety rule
    fn evaluate_safety_rule(&self, content: &str, rule: &SafetyRule) -> bool {
        let content_lower = content.to_lowercase();

        // Simple pattern matching - in a real implementation this would be more sophisticated
        if let Ok(regex) = regex::Regex::new(&rule.pattern) {
            regex.is_match(&content_lower)
        } else {
            // Fallback to simple string matching
            content_lower.contains(&rule.pattern.to_lowercase())
        }
    }

    /// Update extended configuration
    pub fn update_extended_config(&mut self, config: ExtendedSafetyConfig) {
        *self.extended_config = config;
        self.config = self.extended_config.base_config.clone();
        self.max_toxicity_score = self.extended_config.max_toxicity_score;
        self.initialize_patterns();

        // Clear cache when configuration changes
        self.assessment_cache.clear();
    }

    /// Get current extended configuration
    pub fn get_extended_config(&self) -> &ExtendedSafetyConfig {
        &self.extended_config
    }

    /// Clear assessment cache
    pub fn clear_cache(&mut self) {
        self.assessment_cache.clear();
    }

    /// Get violation history
    pub fn get_violation_history(&self) -> &[EnhancedSafetyViolation] {
        &self.violation_history
    }

    /// Clear violation history
    pub fn clear_violation_history(&mut self) {
        self.violation_history.clear();
    }

    /// Add custom safety pattern
    pub fn add_custom_pattern(&mut self, pattern: &str, violation_type: &str) -> Result<()> {
        let regex =
            Regex::new(pattern).map_err(|e| crate::error::TrustformersError::InvalidInput {
                message: format!("Invalid safety pattern: {}", e),
                parameter: Some("pattern".to_string()),
                expected: Some("valid regex pattern".to_string()),
                received: Some(pattern.to_string()),
                suggestion: Some("Check regex syntax".to_string()),
            })?;

        match violation_type {
            "toxicity" => self.toxic_patterns.push(regex),
            "violence" => self.violence_patterns.push(regex),
            "harassment" => self.harassment_patterns.push(regex),
            "privacy" => self.privacy_patterns.push(regex),
            "harm" => self.harm_patterns.push(regex),
            _ => {
                // For other types, add to toxic patterns as default
                self.toxic_patterns.push(regex);
            },
        }

        Ok(())
    }

    /// Perform comprehensive safety analysis
    pub fn analyze_safety(&self, content: &str) -> SafetyAnalysis {
        if !self.config.enabled {
            return SafetyAnalysis {
                is_safe: true,
                toxicity_score: 0.0,
                violations: Vec::new(),
                risk_level: RiskLevel::None,
                recommended_action: SafetyAction::Log,
            };
        }

        let mut violations = Vec::new();
        let toxicity_score = self.get_toxicity_score(content);

        // Check for various types of violations
        if self.config.toxicity_detection && toxicity_score > 0.5 {
            violations.push(SafetyViolation {
                violation_type: "toxicity".to_string(),
                severity: SafetySeverity::Medium,
                description: "High toxicity score detected".to_string(),
                confidence: toxicity_score,
            });
        }

        if self.config.harmful_content_detection {
            violations.extend(self.detect_harmful_content(content));
        }

        if self.config.bias_detection {
            violations.extend(self.detect_bias(content));
        }

        // Check custom rules
        for rule in &self.config.custom_rules {
            if self.evaluate_safety_rule(content, rule) {
                violations.push(SafetyViolation {
                    violation_type: rule.name.clone(),
                    severity: rule.severity.clone(),
                    description: format!("Custom rule violation: {}", rule.pattern),
                    confidence: 0.8,
                });
            }
        }

        let risk_level = self.calculate_risk_level(&violations, toxicity_score);
        let is_safe = violations.is_empty() && toxicity_score <= self.max_toxicity_score;
        let recommended_action = self.recommend_action(&violations, risk_level);

        SafetyAnalysis {
            is_safe,
            toxicity_score,
            violations,
            risk_level,
            recommended_action,
        }
    }

    /// Detect harmful content patterns
    fn detect_harmful_content(&self, content: &str) -> Vec<SafetyViolation> {
        let mut violations = Vec::new();
        let content_lower = content.to_lowercase();

        let harmful_patterns = [
            (
                "violence",
                &["kill", "murder", "attack", "hurt", "harm"] as &[&str],
            ),
            (
                "self_harm",
                &["suicide", "kill myself", "end it all", "hurt myself"],
            ),
            ("harassment", &["stalking", "threatening", "intimidating"]),
            ("illegal_activity", &["drug dealing", "illegal", "criminal"]),
        ];

        for (category, patterns) in &harmful_patterns {
            for pattern in *patterns {
                if content_lower.contains(pattern) {
                    violations.push(SafetyViolation {
                        violation_type: category.to_string(),
                        severity: SafetySeverity::High,
                        description: format!("Detected harmful content: {}", pattern),
                        confidence: 0.7,
                    });
                    break; // Only report one violation per category
                }
            }
        }

        violations
    }

    /// Detect bias patterns
    fn detect_bias(&self, content: &str) -> Vec<SafetyViolation> {
        let mut violations = Vec::new();
        let content_lower = content.to_lowercase();

        let bias_patterns = [
            (
                "gender_bias",
                &["women are", "men are", "girls can't", "boys don't"],
            ),
            (
                "racial_bias",
                &["people of", "race is", "ethnicity", "racial"],
            ),
            (
                "age_bias",
                &[
                    "old people",
                    "young people",
                    "millennials are",
                    "boomers are",
                ],
            ),
        ];

        for (category, patterns) in &bias_patterns {
            for pattern in *patterns {
                if content_lower.contains(pattern) {
                    violations.push(SafetyViolation {
                        violation_type: category.to_string(),
                        severity: SafetySeverity::Medium,
                        description: format!("Potential bias detected: {}", pattern),
                        confidence: 0.6,
                    });
                    break;
                }
            }
        }

        violations
    }

    /// Calculate overall risk level
    fn calculate_risk_level(
        &self,
        violations: &[SafetyViolation],
        toxicity_score: f32,
    ) -> RiskLevel {
        if violations.is_empty() && toxicity_score < 0.3 {
            return RiskLevel::None;
        }

        let high_severity_count = violations
            .iter()
            .filter(|v| matches!(v.severity, SafetySeverity::High | SafetySeverity::Critical))
            .count();

        if high_severity_count > 0 || toxicity_score > 0.8 {
            RiskLevel::High
        } else if violations.len() > 2 || toxicity_score > 0.6 {
            RiskLevel::Medium
        } else {
            RiskLevel::Low
        }
    }

    /// Recommend action based on analysis
    fn recommend_action(
        &self,
        violations: &[SafetyViolation],
        risk_level: RiskLevel,
    ) -> SafetyAction {
        match risk_level {
            RiskLevel::None => SafetyAction::Log,
            RiskLevel::Low => SafetyAction::Warn,
            RiskLevel::Medium => SafetyAction::Modify,
            RiskLevel::High => SafetyAction::Block,
        }
    }

    /// Sanitize content by removing/replacing problematic parts
    pub fn sanitize_content(&self, content: &str) -> String {
        let mut sanitized = content.to_string();

        // Replace banned terms with asterisks
        for term in self.banned_terms.iter() {
            let replacement = "*".repeat(term.len());
            sanitized = sanitized.replace(term, &replacement);
        }

        // Apply regex replacements
        for pattern in self.toxic_patterns.iter() {
            sanitized = pattern.replace_all(&sanitized, "[FILTERED]").to_string();
        }

        sanitized
    }

    /// Update configuration
    pub fn update_config(&mut self, config: SafetyFilterConfig) {
        self.config = config;
    }

    /// Add custom safety rule
    pub fn add_custom_rule(&mut self, rule: SafetyRule) {
        self.config.custom_rules.push(rule);
    }

    /// Get safety statistics
    pub fn get_safety_stats(&self, content_samples: &[String]) -> SafetyStats {
        if content_samples.is_empty() {
            return SafetyStats::default();
        }

        let mut total_violations = 0;
        let mut total_toxicity_score = 0.0;
        let mut safe_count = 0;
        let mut risk_distribution = std::collections::HashMap::new();

        for content in content_samples {
            let analysis = self.analyze_safety(content);

            if analysis.is_safe {
                safe_count += 1;
            }

            total_violations += analysis.violations.len();
            total_toxicity_score += analysis.toxicity_score;

            *risk_distribution.entry(analysis.risk_level).or_insert(0) += 1;
        }

        let total_samples = content_samples.len();
        SafetyStats {
            total_samples,
            safe_percentage: (safe_count as f32 / total_samples as f32) * 100.0,
            avg_toxicity_score: total_toxicity_score / total_samples as f32,
            total_violations,
            avg_violations_per_sample: total_violations as f32 / total_samples as f32,
            risk_distribution,
        }
    }
}

impl Default for SafetyFilter {
    fn default() -> Self {
        Self::new()
    }
}

/// Result of safety analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SafetyAnalysis {
    pub is_safe: bool,
    pub toxicity_score: f32,
    pub violations: Vec<SafetyViolation>,
    pub risk_level: RiskLevel,
    pub recommended_action: SafetyAction,
}

/// Individual safety violation
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SafetyViolation {
    pub violation_type: String,
    pub severity: SafetySeverity,
    pub description: String,
    pub confidence: f32,
}

/// Risk level classification
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum RiskLevel {
    None,
    Low,
    Medium,
    High,
}

/// Safety statistics
#[derive(Debug, Clone, Default)]
pub struct SafetyStats {
    pub total_samples: usize,
    pub safe_percentage: f32,
    pub avg_toxicity_score: f32,
    pub total_violations: usize,
    pub avg_violations_per_sample: f32,
    pub risk_distribution: std::collections::HashMap<RiskLevel, usize>,
}

// ================================================================================================
// ERROR TYPES
// ================================================================================================

/// Safety-related errors
#[derive(Error, Debug)]
pub enum SafetyError {
    #[error("Content blocked due to safety violations: {0:?}")]
    ContentBlocked(Vec<String>),

    #[error("Conversation ended due to safety violation: {0}")]
    ConversationEnded(String),

    #[error("Invalid safety pattern: {0}")]
    InvalidPattern(String),

    #[error("Safety assessment failed: {0}")]
    AssessmentFailed(String),

    #[error("Configuration error: {0}")]
    ConfigurationError(String),
}

impl From<SafetyError> for crate::error::TrustformersError {
    fn from(err: SafetyError) -> Self {
        crate::error::TrustformersError::InvalidInput {
            message: format!("Safety error: {}", err),
            parameter: Some("safety_check".to_string()),
            expected: Some("safe content".to_string()),
            received: None,
            suggestion: Some("Check safety filtering configuration".to_string()),
        }
    }
}

// ================================================================================================
// TESTS
// ================================================================================================

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

    #[test]
    fn test_safety_filter_creation() {
        let filter = SafetyFilter::new();
        assert!(filter.config.enabled);
        assert_eq!(filter.max_toxicity_score, 0.7);
        assert!(filter.extended_config.filter_input);
        assert!(filter.extended_config.filter_output);
    }

    #[test]
    fn test_safety_filter_strict_mode() {
        let filter = SafetyFilter::strict();
        assert_eq!(filter.extended_config.max_toxicity_score, 0.4);
        assert_eq!(filter.extended_config.max_harm_score, 0.3);
        assert_eq!(filter.extended_config.max_bias_score, 0.5);
    }

    #[test]
    fn test_safety_filter_permissive_mode() {
        let filter = SafetyFilter::permissive();
        assert_eq!(filter.extended_config.max_toxicity_score, 0.9);
        assert_eq!(filter.extended_config.max_harm_score, 0.8);
        assert_eq!(filter.extended_config.max_bias_score, 0.9);
    }

    #[test]
    fn test_safety_filter_educational_mode() {
        let filter = SafetyFilter::educational();
        assert_eq!(filter.extended_config.max_toxicity_score, 0.6);
        assert_eq!(filter.extended_config.max_harm_score, 0.5);
        assert_eq!(filter.extended_config.max_bias_score, 0.7);
    }

    #[test]
    fn test_safe_content() {
        let filter = SafetyFilter::new();
        assert!(filter.is_safe("Hello, how are you today?"));
        assert!(filter.is_safe("I would like to learn about programming."));
        assert!(filter.is_safe("Thank you for your help!"));
        assert!(filter.is_safe("What's the weather like?"));
    }

    #[test]
    fn test_toxic_content_detection() {
        let filter = SafetyFilter::new();
        assert!(!filter.is_safe("I hate everyone"));
        assert!(!filter.is_safe("I will kill you"));

        let assessment = filter.assess_content_safety_enhanced("I hate everyone");
        assert!(assessment.category_scores.toxicity > 0.0);
        assert!(!assessment.enhanced_violations.is_empty());
        assert!(assessment
            .enhanced_violations
            .iter()
            .any(|v| v.base_violation.violation_type == "toxicity"));
    }

    #[test]
    fn test_personal_information_detection() {
        let filter = SafetyFilter::new();
        let assessment = filter.assess_content_safety_enhanced("My SSN is 123-45-6789");

        assert!(assessment.category_scores.privacy > 0.0);
        assert!(assessment
            .enhanced_violations
            .iter()
            .any(|v| v.base_violation.violation_type == "personal_information"));
    }

    #[test]
    fn test_violence_detection() {
        let filter = SafetyFilter::new();
        let assessment =
            filter.assess_content_safety_enhanced("I want to attack someone with violence");

        assert!(assessment.category_scores.violence > 0.0);
        assert!(assessment
            .enhanced_violations
            .iter()
            .any(|v| v.base_violation.violation_type == "violence"));
    }

    #[test]
    fn test_self_harm_detection() {
        let filter = SafetyFilter::new();
        let assessment = filter.assess_content_safety_enhanced("I want to hurt myself");

        assert!(assessment.category_scores.harm > 0.0);
        assert!(assessment
            .enhanced_violations
            .iter()
            .any(|v| v.base_violation.violation_type == "self_harm"));
        assert!(assessment
            .enhanced_violations
            .iter()
            .any(|v| matches!(v.base_violation.severity, SafetySeverity::Critical)));
    }

    #[test]
    fn test_harassment_detection() {
        let filter = SafetyFilter::new();
        let assessment = filter.assess_content_safety_enhanced("I will harass you constantly");

        assert!(assessment.category_scores.harassment > 0.0);
        assert!(assessment
            .enhanced_violations
            .iter()
            .any(|v| v.base_violation.violation_type == "harassment"));
    }

    #[test]
    fn test_bias_detection() {
        let filter = SafetyFilter::new();
        let assessment = filter.assess_content_safety_enhanced("That's so racist and sexist");

        assert!(assessment.category_scores.bias > 0.0);
        assert!(assessment
            .enhanced_violations
            .iter()
            .any(|v| v.base_violation.violation_type == "bias"));
    }

    #[test]
    fn test_content_filtering() {
        let filter = SafetyFilter::new();

        // Safe content should pass through
        let result =
            filter.filter_input("Hello, how can I help?").expect("operation failed in test");
        assert_eq!(result, "Hello, how can I help?");

        // Unsafe content should be filtered
        let result = filter.filter_input("I hate you").expect("operation failed in test");
        assert_ne!(result, "I hate you");
        assert!(result.contains("can't assist") || result.contains("something else"));
    }

    #[test]
    fn test_metadata_enrichment() {
        let filter = SafetyFilter::new();
        let mut metadata = ConversationMetadata {
            sentiment: Some("neutral".to_string()),
            intent: Some("question".to_string()),
            confidence: 0.9,
            topics: vec!["general".to_string()],
            safety_flags: Vec::new(),
            entities: Vec::new(),
            quality_score: 0.8,
            engagement_level: EngagementLevel::Medium,
            reasoning_type: Some(ReasoningType::Logical),
        };

        filter.enrich_conversation_metadata("I hate everyone", &mut metadata);

        assert!(!metadata.safety_flags.is_empty());
        assert!(metadata.confidence < 0.9); // Should be reduced due to safety issues
        assert!(metadata.quality_score < 0.8); // Should be reduced due to safety issues
    }

    #[test]
    fn test_violation_location_tracking() {
        let filter = SafetyFilter::new();
        let content = "Hello I hate you goodbye";
        let assessment = filter.assess_content_safety_enhanced(content);

        // Find the hate-related violation
        let hate_violation = assessment
            .enhanced_violations
            .iter()
            .find(|v| v.base_violation.violation_type == "toxicity");

        if let Some(violation) = hate_violation {
            if let Some(location) = &violation.location {
                assert!(content[location.start_pos..location.end_pos].contains("hate"));
            }
        }
    }

    #[test]
    fn test_custom_pattern_addition() {
        let mut filter = SafetyFilter::new();

        // Add custom toxic pattern
        filter
            .add_custom_pattern(r"(?i)\bcustom_bad_word\b", "toxicity")
            .expect("add operation failed");

        // Test that the custom pattern is detected
        let assessment = filter.assess_content_safety_enhanced("This contains custom_bad_word");
        assert!(assessment.category_scores.toxicity > 0.0);
    }

    #[test]
    fn test_configuration_modes() {
        let strict_filter = SafetyFilter::strict();
        let permissive_filter = SafetyFilter::permissive();
        let educational_filter = SafetyFilter::educational();

        // Test different thresholds
        assert!(
            strict_filter.extended_config.max_toxicity_score
                < permissive_filter.extended_config.max_toxicity_score
        );
        assert!(
            educational_filter.extended_config.max_toxicity_score
                < permissive_filter.extended_config.max_toxicity_score
        );
        assert!(
            educational_filter.extended_config.max_toxicity_score
                > strict_filter.extended_config.max_toxicity_score
        );
    }

    #[test]
    fn test_assessment_caching() {
        let filter = SafetyFilter::new();
        let content = "Hello world";

        // First assessment
        let start1 = Instant::now();
        let assessment1 = filter.assess_content_safety_enhanced(content);
        let duration1 = start1.elapsed();

        // Second assessment (should use cache if enabled)
        let start2 = Instant::now();
        let assessment2 = filter.assess_content_safety_enhanced(content);
        let duration2 = start2.elapsed();

        // Both assessments should complete without error
        // (durations are always >= 0 for std::time::Duration)

        // Assessments should be consistent
        assert_eq!(
            assessment1.category_scores.toxicity,
            assessment2.category_scores.toxicity
        );
    }

    #[test]
    fn test_disabled_filter() {
        let mut config = ExtendedSafetyConfig::default();
        config.base_config.enabled = false;
        let filter = SafetyFilter::with_extended_config(config);

        // Should allow all content when disabled
        assert!(filter.is_safe("I hate everyone"));
        assert_eq!(filter.get_toxicity_score("I hate everyone"), 0.0);

        let result = filter.filter_input("I hate everyone").expect("operation failed in test");
        assert_eq!(result, "I hate everyone");
    }

    #[test]
    fn test_safety_analysis_backward_compatibility() {
        let filter = SafetyFilter::new();

        // Test backward compatibility with original analyze_safety method
        let analysis = filter.analyze_safety("I hate you");
        assert!(!analysis.is_safe);
        assert!(analysis.toxicity_score > 0.0);
        assert!(!analysis.violations.is_empty());
        assert!(matches!(
            analysis.risk_level,
            RiskLevel::Low | RiskLevel::Medium | RiskLevel::High
        ));
    }

    #[test]
    fn test_content_sanitization() {
        let filter = SafetyFilter::new();
        let toxic_content = "You are hate and violence";
        let sanitized = filter.sanitize_content(toxic_content);

        // Should replace banned terms
        assert!(sanitized.contains("****")); // hate replaced
        assert!(sanitized.contains("********")); // violence replaced
    }

    #[test]
    fn test_safety_statistics() {
        let filter = SafetyFilter::new();
        let content_samples = vec![
            "Hello world".to_string(),
            "I hate you".to_string(),
            "Nice weather today".to_string(),
            "Violence is bad".to_string(),
        ];

        let stats = filter.get_safety_stats(&content_samples);

        assert_eq!(stats.total_samples, 4);
        assert!(stats.safe_percentage >= 0.0 && stats.safe_percentage <= 100.0);
        assert!(stats.avg_toxicity_score >= 0.0 && stats.avg_toxicity_score <= 1.0);
        // total_violations is usize which is always >= 0
        assert!(!stats.risk_distribution.is_empty());
    }

    #[test]
    fn test_enhanced_assessment_confidence() {
        let filter = SafetyFilter::new();

        // Long content should have higher confidence
        let long_content = "This is a very long piece of content that should have higher confidence in its assessment because there is more text to analyze and detect patterns in.";
        let short_content = "Hi";

        let long_assessment = filter.assess_content_safety_enhanced(long_content);
        let short_assessment = filter.assess_content_safety_enhanced(short_content);

        assert!(long_assessment.confidence >= short_assessment.confidence);
    }

    #[test]
    fn test_violation_context_extraction() {
        let filter = SafetyFilter::new();
        let content = "This is some text with hate in the middle of it";
        let assessment = filter.assess_content_safety_enhanced(content);

        if let Some(violation) = assessment.enhanced_violations.first() {
            assert!(violation.context.is_some());
            let context = violation.context.as_ref().expect("operation failed in test");
            assert!(context.contains("hate"));
        }
    }

    #[test]
    fn test_enhanced_safety_assessment_immediate_action() {
        let filter = SafetyFilter::new();
        let critical_content = "I want to hurt myself badly";
        let assessment = filter.assess_content_safety_enhanced(critical_content);

        // Critical self-harm content should require immediate action
        assert!(assessment.requires_immediate_action());
    }

    #[test]
    fn test_privacy_redaction() {
        let filter = SafetyFilter::new();
        let content_with_ssn = "My social security number is 123-45-6789";
        let assessment = filter.assess_content_safety_enhanced(content_with_ssn);

        if let Some(privacy_violation) = assessment
            .enhanced_violations
            .iter()
            .find(|v| v.base_violation.violation_type == "personal_information")
        {
            if let Some(location) = &privacy_violation.location {
                // Should be redacted for privacy
                assert_eq!(location.matched_text, "***REDACTED***");
            }
        }
    }
}