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
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
//! Context summarization module for conversational AI pipeline.
//!
//! This module provides comprehensive conversation context summarization capabilities,
//! including multiple summarization strategies, quality assessment, token management,
//! and optimization for different conversation types and requirements.
//!
//! # Features
//!
//! - **Multiple Strategies**: Extractive, abstractive, and hybrid summarization
//! - **Context Compression**: Intelligent compression while preserving key information
//! - **Quality Assessment**: Automatic summary quality scoring and validation
//! - **Token Management**: Precise token counting and context window management
//! - **Adaptive Algorithms**: Different algorithms for different conversation types
//! - **Performance Optimization**: Efficient summarization with minimal latency
//! - **Error Recovery**: Robust error handling and fallback mechanisms

use crate::error::{Result, TrustformersError};
use crate::pipeline::conversational::types::{
    ConversationRole, ConversationTurn, EngagementLevel, ReasoningType, SummarizationConfig,
    SummarizationStrategy,
};
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

// ================================================================================================
// CORE SUMMARIZATION TYPES
// ================================================================================================

// ================================================================================================
// TYPE ALIASES
// ================================================================================================

/// Summarization engine alias
pub type SummarizationEngine = ContextSummarizer;

/// Summarization metadata alias
pub type SummarizationMetadata = SummarizationResult;

/// Advanced context summarization component for conversation compression
pub struct ContextSummarizer {
    /// Summarization strategy configuration
    pub config: SummarizationConfig,
    /// Token counting function for accurate estimation
    pub token_counter: Option<Arc<dyn Fn(&str) -> usize + Send + Sync>>,
    /// Cache for frequently used regex patterns
    regex_cache: HashMap<String, Regex>,
    /// Importance scoring weights
    importance_weights: ImportanceWeights,
    /// Quality assessment thresholds
    quality_thresholds: QualityThresholds,
}

/// Result of summarization with metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SummarizationResult {
    /// Generated summary text
    pub summary: String,
    /// Original token count before summarization
    pub original_tokens: usize,
    /// Summary token count after summarization
    pub summary_tokens: usize,
    /// Compression ratio achieved
    pub compression_ratio: f32,
    /// Quality score of the summary
    pub quality_score: f32,
    /// Strategy used for summarization
    pub strategy_used: SummarizationStrategy,
    /// Key topics preserved
    pub preserved_topics: Vec<String>,
    /// Important entities preserved
    pub preserved_entities: Vec<String>,
    /// Confidence in summary quality
    pub confidence: f32,
    /// Processing time in milliseconds
    pub processing_time_ms: f64,
}

/// Weights for importance scoring
#[derive(Debug, Clone)]
struct ImportanceWeights {
    /// Weight for questions in importance calculation
    question_weight: f32,
    /// Weight for personal information
    personal_info_weight: f32,
    /// Weight for topical relevance
    topic_relevance_weight: f32,
    /// Weight for emotional content
    emotional_weight: f32,
    /// Weight for reasoning chains
    reasoning_weight: f32,
    /// Weight for engagement level
    engagement_weight: f32,
    /// Weight for recency (more recent = more important)
    recency_weight: f32,
}

impl Default for ImportanceWeights {
    fn default() -> Self {
        Self {
            question_weight: 0.3,
            personal_info_weight: 0.4,
            topic_relevance_weight: 0.25,
            emotional_weight: 0.2,
            reasoning_weight: 0.35,
            engagement_weight: 0.15,
            recency_weight: 0.1,
        }
    }
}

/// Thresholds for quality assessment
#[derive(Debug, Clone)]
struct QualityThresholds {
    /// Minimum quality score for acceptable summaries
    min_quality_score: f32,
    /// Minimum compression ratio to be worthwhile
    min_compression_ratio: f32,
    /// Maximum allowable information loss
    max_information_loss: f32,
    /// Minimum coherence score
    min_coherence_score: f32,
}

impl Default for QualityThresholds {
    fn default() -> Self {
        Self {
            min_quality_score: 0.6,
            min_compression_ratio: 0.3,
            max_information_loss: 0.4,
            min_coherence_score: 0.5,
        }
    }
}

/// Sentence importance score and metadata
#[derive(Debug, Clone)]
struct SentenceScore {
    /// The sentence text
    sentence: String,
    /// Importance score (0.0 to 1.0)
    score: f32,
    /// Position in original text
    position: usize,
    /// Turn index this sentence belongs to
    turn_index: usize,
    /// Topics this sentence covers
    topics: Vec<String>,
    /// Named entities in this sentence
    entities: Vec<String>,
    /// Role of the speaker
    speaker_role: ConversationRole,
}

/// Topic clustering result for extractive summarization
#[derive(Debug, Clone)]
struct TopicCluster {
    /// Central topic/theme
    topic: String,
    /// Sentences belonging to this cluster
    sentences: Vec<SentenceScore>,
    /// Importance score of this cluster
    cluster_score: f32,
    /// Representative sentence for this cluster
    representative_sentence: Option<String>,
}

// ================================================================================================
// LEGACY COMPATIBILITY TYPES (From original file)
// ================================================================================================

/// Hierarchical summary with segments
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HierarchicalSummary {
    pub overall_summary: String,
    pub main_topics: Vec<String>,
    pub segments: Vec<ConversationSegment>,
    pub total_turns: usize,
}

/// A segment of conversation with its summary
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversationSegment {
    pub start_turn: usize,
    pub end_turn: usize,
    pub summary: String,
    pub topics: Vec<String>,
    pub turn_count: usize,
}

/// Summary with specific constraints
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConstrainedSummary {
    pub summary: String,
    pub topics: Option<Vec<String>>,
    pub sentiment_analysis: Option<SentimentAnalysis>,
    pub original_turn_count: usize,
    pub compression_ratio: f32,
}

/// Sentiment analysis results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SentimentAnalysis {
    pub dominant_sentiment: String,
    pub positive_ratio: f32,
    pub negative_ratio: f32,
    pub neutral_ratio: f32,
    pub confidence: f32,
}

// ================================================================================================
// IMPLEMENTATION
// ================================================================================================

impl std::fmt::Debug for ContextSummarizer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ContextSummarizer")
            .field("config", &self.config)
            .field(
                "token_counter",
                &self.token_counter.as_ref().map(|_| "<function>"),
            )
            .field(
                "regex_cache",
                &format!("{} cached patterns", self.regex_cache.len()),
            )
            .field("importance_weights", &self.importance_weights)
            .field("quality_thresholds", &self.quality_thresholds)
            .finish()
    }
}

impl Clone for ContextSummarizer {
    fn clone(&self) -> Self {
        Self {
            config: self.config.clone(),
            token_counter: self.token_counter.clone(),
            regex_cache: self.regex_cache.clone(),
            importance_weights: self.importance_weights.clone(),
            quality_thresholds: self.quality_thresholds.clone(),
        }
    }
}

impl ContextSummarizer {
    /// Create a new context summarizer with configuration
    pub fn new(config: SummarizationConfig) -> Self {
        Self {
            config,
            token_counter: None,
            regex_cache: HashMap::new(),
            importance_weights: ImportanceWeights::default(),
            quality_thresholds: QualityThresholds::default(),
        }
    }

    /// Create context summarizer with simple strategy and target length (legacy compatibility)
    pub fn with_strategy(strategy: SummarizationStrategy, target_length: usize) -> Self {
        let mut config = SummarizationConfig::default();
        config.strategy = strategy;
        config.target_length = target_length;
        Self::new(config)
    }

    /// Create context summarizer with custom token counter
    pub fn with_token_counter<F>(mut self, token_counter: F) -> Self
    where
        F: Fn(&str) -> usize + Send + Sync + 'static,
    {
        self.token_counter = Some(Arc::new(token_counter));
        self
    }

    /// Set custom importance weights
    pub fn with_importance_weights(mut self, weights: ImportanceWeights) -> Self {
        self.importance_weights = weights;
        self
    }

    /// Set custom quality thresholds
    pub fn with_quality_thresholds(mut self, thresholds: QualityThresholds) -> Self {
        self.quality_thresholds = thresholds;
        self
    }

    /// Summarize conversation history - legacy method for compatibility
    pub fn summarize_context(&mut self, turns: &[ConversationTurn]) -> Result<String> {
        let result = self.summarize_context_enhanced(turns)?;
        Ok(result.summary)
    }

    /// Summarize conversation history with comprehensive analysis
    pub fn summarize_context_enhanced(
        &mut self,
        turns: &[ConversationTurn],
    ) -> Result<SummarizationResult> {
        let start_time = std::time::Instant::now();

        if turns.is_empty() {
            return Ok(SummarizationResult {
                summary: String::new(),
                original_tokens: 0,
                summary_tokens: 0,
                compression_ratio: 1.0,
                quality_score: 1.0,
                strategy_used: self.config.strategy.clone(),
                preserved_topics: Vec::new(),
                preserved_entities: Vec::new(),
                confidence: 1.0,
                processing_time_ms: start_time.elapsed().as_millis() as f64,
            });
        }

        // Calculate original token count
        let original_tokens = self.calculate_total_tokens(turns);

        // Check if summarization is necessary
        if original_tokens <= self.config.target_length {
            let summary = self.build_full_context(turns);
            return Ok(SummarizationResult {
                summary: summary.clone(),
                original_tokens,
                summary_tokens: self.count_tokens(&summary),
                compression_ratio: 1.0,
                quality_score: 1.0,
                strategy_used: self.config.strategy.clone(),
                preserved_topics: self.extract_all_topics(turns),
                preserved_entities: self.extract_all_entities(turns),
                confidence: 1.0,
                processing_time_ms: start_time.elapsed().as_millis() as f64,
            });
        }

        // Perform summarization based on strategy
        let summary = match self.config.strategy {
            SummarizationStrategy::Extractive => self.extractive_summary(turns)?,
            SummarizationStrategy::Abstractive => self.abstractive_summary(turns)?,
            SummarizationStrategy::Hybrid => self.hybrid_summary(turns)?,
        };

        let summary_tokens = self.count_tokens(&summary);
        let compression_ratio = summary_tokens as f32 / original_tokens as f32;

        // Assess summary quality
        let quality_assessment = self.assess_summary_quality(&summary, turns, compression_ratio);

        // Extract preserved information
        let preserved_topics = self.extract_preserved_topics(&summary, turns);
        let preserved_entities = self.extract_preserved_entities(&summary, turns);

        let processing_time = start_time.elapsed().as_millis() as f64;

        Ok(SummarizationResult {
            summary,
            original_tokens,
            summary_tokens,
            compression_ratio,
            quality_score: quality_assessment.quality_score,
            strategy_used: self.config.strategy.clone(),
            preserved_topics,
            preserved_entities,
            confidence: quality_assessment.confidence,
            processing_time_ms: processing_time,
        })
    }

    /// Perform extractive summarization using sentence scoring and clustering
    fn extractive_summary(&mut self, turns: &[ConversationTurn]) -> Result<String> {
        // Score all sentences for importance
        let scored_sentences = self.score_sentences(turns)?;

        // Cluster sentences by topic for better coverage
        let topic_clusters = self.cluster_by_topics(&scored_sentences);

        // Select representative sentences from each cluster
        let mut selected_sentences = Vec::new();
        let mut current_tokens = 0;
        let target_tokens = self.config.target_length;

        // Sort clusters by importance
        let mut sorted_clusters = topic_clusters;
        sorted_clusters.sort_by(|a, b| {
            b.cluster_score.partial_cmp(&a.cluster_score).unwrap_or(Ordering::Equal)
        });

        // Select sentences from most important clusters first
        for cluster in sorted_clusters {
            for sentence_score in cluster.sentences {
                let sentence_tokens = self.count_tokens(&sentence_score.sentence);
                if current_tokens + sentence_tokens <= target_tokens {
                    selected_sentences.push(sentence_score);
                    current_tokens += sentence_tokens;
                } else if selected_sentences.is_empty() {
                    // Ensure we include at least one sentence even if it exceeds target
                    selected_sentences.push(sentence_score);
                    break;
                }

                if current_tokens >= target_tokens {
                    break;
                }
            }

            if current_tokens >= target_tokens {
                break;
            }
        }

        // Sort selected sentences by original position to maintain coherence
        selected_sentences.sort_by_key(|s| (s.turn_index, s.position));

        // Build coherent summary
        self.build_coherent_summary(selected_sentences)
    }

    /// Perform abstractive summarization using template-based generation
    fn abstractive_summary(&self, turns: &[ConversationTurn]) -> Result<String> {
        // Extract key information for abstraction
        let key_topics = self.extract_key_topics(turns, 5);
        let key_entities = self.extract_key_entities(turns, 10);
        let conversation_flow = self.analyze_conversation_flow(turns);
        let emotional_arc = self.analyze_emotional_arc(turns);

        // Count turns by role for context
        let user_turns = turns.iter().filter(|t| matches!(t.role, ConversationRole::User)).count();
        let assistant_turns =
            turns.iter().filter(|t| matches!(t.role, ConversationRole::Assistant)).count();

        // Build abstractive summary using templates
        let mut summary_parts = Vec::new();

        // Add conversation overview
        summary_parts.push(format!(
            "Conversation summary ({} user messages, {} assistant responses):",
            user_turns, assistant_turns
        ));

        // Add key topics
        if !key_topics.is_empty() {
            summary_parts.push(format!("Main topics discussed: {}", key_topics.join(", ")));
        }

        // Add key entities
        if !key_entities.is_empty() {
            summary_parts.push(format!(
                "Key entities mentioned: {}",
                key_entities.join(", ")
            ));
        }

        // Add conversation flow insights
        if let Some(flow_summary) = conversation_flow {
            summary_parts.push(flow_summary);
        }

        // Add emotional context if significant
        if let Some(emotional_summary) = emotional_arc {
            summary_parts.push(emotional_summary);
        }

        // Add specific important exchanges
        let important_exchanges = self.extract_important_exchanges(turns, 2);
        for exchange in important_exchanges {
            summary_parts.push(exchange);
        }

        let summary = summary_parts.join(" ");

        // Ensure summary fits within target length
        self.trim_to_target_length(summary)
    }

    /// Perform hybrid summarization combining extractive and abstractive approaches
    fn hybrid_summary(&mut self, turns: &[ConversationTurn]) -> Result<String> {
        // Use 60% of target for extractive, 40% for abstractive
        let extractive_target = (self.config.target_length as f32 * 0.6) as usize;
        let abstractive_target = self.config.target_length - extractive_target;

        // Create temporary config for extractive phase
        let mut extractive_config = self.config.clone();
        extractive_config.target_length = extractive_target;
        let original_config = std::mem::replace(&mut self.config, extractive_config);

        // Get extractive summary
        let extractive_part = self.extractive_summary(turns)?;

        // Restore original config and set for abstractive
        self.config = original_config;
        self.config.target_length = abstractive_target;

        // Get abstractive summary
        let abstractive_part = self.abstractive_summary(turns)?;

        // Restore original target length
        self.config.target_length = extractive_target + abstractive_target;

        // Combine both summaries intelligently
        let combined = if extractive_part.is_empty() {
            abstractive_part
        } else if abstractive_part.is_empty() {
            extractive_part
        } else {
            format!("{} {}", abstractive_part, extractive_part)
        };

        // Final trimming to ensure target length
        self.trim_to_target_length(combined)
    }

    /// Score individual sentences for importance
    fn score_sentences(&self, turns: &[ConversationTurn]) -> Result<Vec<SentenceScore>> {
        let mut scored_sentences = Vec::new();

        for (turn_index, turn) in turns.iter().enumerate() {
            let sentences = self.split_into_sentences(&turn.content);

            for (position, sentence) in sentences.into_iter().enumerate() {
                if sentence.trim().is_empty() {
                    continue;
                }

                let score = self.calculate_sentence_importance(&sentence, turn, turn_index);
                let topics = self.extract_sentence_topics(&sentence);
                let entities = self.extract_sentence_entities(&sentence);

                scored_sentences.push(SentenceScore {
                    sentence,
                    score,
                    position,
                    turn_index,
                    topics,
                    entities,
                    speaker_role: turn.role.clone(),
                });
            }
        }

        // Sort by importance score
        scored_sentences.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap_or(Ordering::Equal));

        Ok(scored_sentences)
    }

    /// Calculate importance score for a sentence
    fn calculate_sentence_importance(
        &self,
        sentence: &str,
        turn: &ConversationTurn,
        turn_index: usize,
    ) -> f32 {
        let mut importance = 0.0;
        let sentence_lower = sentence.to_lowercase();

        // Question bonus
        if sentence.contains('?') {
            importance += self.importance_weights.question_weight;
        }

        // Personal information bonus
        if self.contains_personal_info(&sentence_lower) {
            importance += self.importance_weights.personal_info_weight;
        }

        // Topic relevance (based on metadata)
        if let Some(metadata) = &turn.metadata {
            if !metadata.topics.is_empty() {
                importance += self.importance_weights.topic_relevance_weight;
            }

            // Engagement level bonus
            let engagement_bonus = match metadata.engagement_level {
                EngagementLevel::VeryHigh => 0.4,
                EngagementLevel::High => 0.3,
                EngagementLevel::Medium => 0.1,
                EngagementLevel::Low => 0.0,
            };
            importance += engagement_bonus * self.importance_weights.engagement_weight;

            // Reasoning type bonus
            if let Some(reasoning_type) = &metadata.reasoning_type {
                let reasoning_bonus = match reasoning_type {
                    ReasoningType::Logical | ReasoningType::Mathematical => 0.3,
                    ReasoningType::Causal | ReasoningType::Analogical => 0.25,
                    ReasoningType::Creative | ReasoningType::Emotional => 0.2,
                };
                importance += reasoning_bonus * self.importance_weights.reasoning_weight;
            }
        }

        // Emotional content bonus
        if self.contains_emotional_content(&sentence_lower) {
            importance += self.importance_weights.emotional_weight;
        }

        // Length factor (not too short, not too long)
        let length_factor = self.calculate_length_factor(sentence);
        importance *= length_factor;

        // Recency factor (more recent turns are slightly more important)
        let recency_factor = 1.0 - (turn_index as f32 * 0.05).min(0.5);
        importance += recency_factor * self.importance_weights.recency_weight;

        importance.min(1.0).max(0.0)
    }

    /// Check if sentence contains personal information
    fn contains_personal_info(&self, sentence: &str) -> bool {
        let personal_patterns = [
            "i am",
            "my name",
            "i like",
            "i prefer",
            "i want",
            "i need",
            "i work",
            "i live",
            "my job",
            "my family",
            "my hobby",
        ];

        personal_patterns.iter().any(|&pattern| sentence.contains(pattern))
    }

    /// Check if sentence contains emotional content
    fn contains_emotional_content(&self, sentence: &str) -> bool {
        let emotional_words = [
            "love",
            "hate",
            "happy",
            "sad",
            "angry",
            "excited",
            "frustrated",
            "disappointed",
            "pleased",
            "worried",
            "nervous",
            "confident",
            "feel",
            "feeling",
            "emotion",
            "heart",
            "soul",
        ];

        emotional_words.iter().any(|&word| sentence.contains(word))
    }

    /// Calculate length factor for sentence scoring
    fn calculate_length_factor(&self, sentence: &str) -> f32 {
        let word_count = sentence.split_whitespace().count();

        match word_count {
            0..=3 => 0.3,   // Too short
            4..=8 => 0.8,   // Short but meaningful
            9..=20 => 1.0,  // Good length
            21..=30 => 0.9, // A bit long
            31..=50 => 0.7, // Long
            _ => 0.5,       // Very long
        }
    }

    /// Cluster sentences by topics for better coverage
    fn cluster_by_topics(&self, sentences: &[SentenceScore]) -> Vec<TopicCluster> {
        let mut topic_map: HashMap<String, Vec<SentenceScore>> = HashMap::new();
        let mut uncategorized = Vec::new();

        // Group sentences by topics
        for sentence in sentences {
            if sentence.topics.is_empty() {
                uncategorized.push(sentence.clone());
            } else {
                for topic in &sentence.topics {
                    topic_map.entry(topic.clone()).or_default().push(sentence.clone());
                }
            }
        }

        let mut clusters = Vec::new();

        // Create clusters for each topic
        for (topic, mut topic_sentences) in topic_map {
            // Sort sentences within topic by importance
            topic_sentences
                .sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap_or(Ordering::Equal));

            // Calculate cluster score as average of top sentences
            let cluster_score = if topic_sentences.is_empty() {
                0.0
            } else {
                let top_count = (topic_sentences.len() / 2).max(1).min(3);
                topic_sentences.iter().take(top_count).map(|s| s.score).sum::<f32>()
                    / top_count as f32
            };

            // Find representative sentence
            let representative_sentence = topic_sentences.first().map(|s| s.sentence.clone());

            clusters.push(TopicCluster {
                topic,
                sentences: topic_sentences,
                cluster_score,
                representative_sentence,
            });
        }

        // Add uncategorized sentences as a general cluster
        if !uncategorized.is_empty() {
            uncategorized.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap_or(Ordering::Equal));
            let cluster_score = uncategorized.iter().take(3).map(|s| s.score).sum::<f32>() / 3.0;

            clusters.push(TopicCluster {
                topic: "general".to_string(),
                sentences: uncategorized,
                cluster_score,
                representative_sentence: None,
            });
        }

        clusters
    }

    /// Build coherent summary from selected sentences
    fn build_coherent_summary(&self, sentences: Vec<SentenceScore>) -> Result<String> {
        if sentences.is_empty() {
            return Ok(String::new());
        }

        let mut summary_parts = Vec::new();
        let mut current_role: Option<ConversationRole> = None;

        for sentence in sentences {
            // Add role marker if role changes
            if current_role.as_ref() != Some(&sentence.speaker_role) {
                let role_marker = match sentence.speaker_role {
                    ConversationRole::User => "User:",
                    ConversationRole::Assistant => "Assistant:",
                    ConversationRole::System => "System:",
                };

                if !summary_parts.is_empty() {
                    summary_parts.push(" ".to_string());
                }
                summary_parts.push(format!("{} ", role_marker));
                current_role = Some(sentence.speaker_role);
            }

            summary_parts.push(sentence.sentence);
            summary_parts.push(" ".to_string());
        }

        Ok(summary_parts.concat().trim().to_string())
    }

    /// Extract key topics from conversation
    fn extract_key_topics(&self, turns: &[ConversationTurn], limit: usize) -> Vec<String> {
        let mut topic_counts: HashMap<String, usize> = HashMap::new();

        for turn in turns {
            if let Some(metadata) = &turn.metadata {
                for topic in &metadata.topics {
                    *topic_counts.entry(topic.clone()).or_insert(0) += 1;
                }
            }
        }

        let mut topics: Vec<_> = topic_counts.into_iter().collect();
        topics.sort_by_key(|item| std::cmp::Reverse(item.1));

        topics.into_iter().take(limit).map(|(topic, _)| topic).collect()
    }

    /// Extract key entities from conversation
    fn extract_key_entities(&self, turns: &[ConversationTurn], limit: usize) -> Vec<String> {
        let mut entity_counts: HashMap<String, usize> = HashMap::new();

        for turn in turns {
            if let Some(metadata) = &turn.metadata {
                for entity in &metadata.entities {
                    *entity_counts.entry(entity.text.clone()).or_insert(0) += 1;
                }
            }
        }

        let mut entities: Vec<_> = entity_counts.into_iter().collect();
        entities.sort_by_key(|item| std::cmp::Reverse(item.1));

        entities.into_iter().take(limit).map(|(entity, _)| entity).collect()
    }

    /// Analyze conversation flow for abstractive summary
    fn analyze_conversation_flow(&self, turns: &[ConversationTurn]) -> Option<String> {
        if turns.len() < 3 {
            return None;
        }

        let question_count = turns.iter().filter(|t| t.content.contains('?')).count();
        let total_turns = turns.len();
        let question_ratio = question_count as f32 / total_turns as f32;

        let flow_type = if question_ratio > 0.4 {
            "inquiry-heavy discussion"
        } else if question_ratio > 0.2 {
            "interactive conversation"
        } else {
            "informational exchange"
        };

        Some(format!("The conversation followed a {} pattern", flow_type))
    }

    /// Analyze emotional arc for abstractive summary
    fn analyze_emotional_arc(&self, turns: &[ConversationTurn]) -> Option<String> {
        let mut sentiment_progression = Vec::new();

        for turn in turns {
            if let Some(metadata) = &turn.metadata {
                if let Some(sentiment) = &metadata.sentiment {
                    sentiment_progression.push(sentiment.clone());
                }
            }
        }

        if sentiment_progression.len() < 2 {
            return None;
        }

        let initial_sentiment = &sentiment_progression[0];
        let final_sentiment = sentiment_progression.last().expect("len >= 2 checked above");

        if initial_sentiment != final_sentiment {
            Some(format!(
                "The emotional tone shifted from {} to {} throughout the conversation",
                initial_sentiment, final_sentiment
            ))
        } else {
            Some(format!(
                "The conversation maintained a {} tone",
                initial_sentiment
            ))
        }
    }

    /// Extract important exchanges for abstractive summary
    fn extract_important_exchanges(&self, turns: &[ConversationTurn], limit: usize) -> Vec<String> {
        let mut exchanges = Vec::new();

        for i in 0..turns.len().saturating_sub(1) {
            let current_turn = &turns[i];
            let next_turn = &turns[i + 1];

            // Look for question-answer pairs
            if current_turn.content.contains('?')
                && matches!(current_turn.role, ConversationRole::User)
                && matches!(next_turn.role, ConversationRole::Assistant)
            {
                let exchange = format!(
                    "User asked about {}, Assistant responded with {}",
                    self.extract_question_topic(&current_turn.content),
                    self.extract_response_summary(&next_turn.content)
                );
                exchanges.push(exchange);
            }
        }

        exchanges.into_iter().take(limit).collect()
    }

    /// Extract topic from a question
    fn extract_question_topic(&self, content: &str) -> String {
        // Simple keyword extraction for question topics
        let keywords = ["what", "how", "why", "when", "where", "who"];
        let content_lower = content.to_lowercase();

        for keyword in keywords {
            if let Some(start) = content_lower.find(keyword) {
                let rest = &content[start..];
                if let Some(end) = rest.find('?') {
                    let question_part = &rest[..end + 1];
                    return question_part.trim().to_string();
                }
            }
        }

        "a topic".to_string()
    }

    /// Extract summary from a response
    fn extract_response_summary(&self, content: &str) -> String {
        let words: Vec<&str> = content.split_whitespace().take(10).collect();
        if words.len() < 10 {
            content.to_string()
        } else {
            format!("{}...", words.join(" "))
        }
    }

    /// Split text into sentences
    fn split_into_sentences(&self, text: &str) -> Vec<String> {
        // Simple sentence splitting on common punctuation
        let sentences: Vec<String> = text
            .split(&['.', '!', '?'])
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty() && s.len() > 5)
            .collect();

        if sentences.is_empty() {
            vec![text.to_string()]
        } else {
            sentences
        }
    }

    /// Extract topics from a sentence
    fn extract_sentence_topics(&self, sentence: &str) -> Vec<String> {
        let mut topics = Vec::new();
        let sentence_lower = sentence.to_lowercase();

        let topic_keywords = [
            (
                "technology",
                &["computer", "software", "tech", "ai", "programming", "code"] as &[&str],
            ),
            (
                "sports",
                &[
                    "football",
                    "basketball",
                    "soccer",
                    "tennis",
                    "game",
                    "sport",
                ],
            ),
            (
                "food",
                &["restaurant", "cooking", "recipe", "eat", "meal", "food"],
            ),
            (
                "travel",
                &["trip", "vacation", "visit", "country", "hotel", "travel"],
            ),
            (
                "work",
                &["job", "career", "office", "meeting", "project", "work"],
            ),
            (
                "health",
                &[
                    "doctor", "medicine", "exercise", "wellness", "fitness", "health",
                ],
            ),
            (
                "education",
                &[
                    "school",
                    "university",
                    "learn",
                    "study",
                    "education",
                    "teacher",
                ],
            ),
            (
                "family",
                &["family", "parents", "children", "kids", "relatives", "home"],
            ),
        ];

        for (topic, keywords) in topic_keywords {
            if keywords.iter().any(|keyword| sentence_lower.contains(keyword)) {
                topics.push(topic.to_string());
            }
        }

        topics
    }

    /// Extract entities from a sentence (simplified)
    fn extract_sentence_entities(&self, sentence: &str) -> Vec<String> {
        let mut entities = Vec::new();

        // Simple patterns for common entity types
        let patterns = [
            (r"\b[A-Z][a-z]+ [A-Z][a-z]+\b", "PERSON"),
            (r"\b\d{1,2}/\d{1,2}/\d{4}\b", "DATE"),
            (r"\b\d{4}-\d{2}-\d{2}\b", "DATE"),
            (r"\$\d+(?:\.\d{2})?\b", "MONEY"),
        ];

        for (pattern, _entity_type) in patterns {
            if let Ok(regex) = Regex::new(pattern) {
                for mat in regex.find_iter(sentence) {
                    entities.push(mat.as_str().to_string());
                }
            }
        }

        entities
    }

    /// Build full context without summarization
    fn build_full_context(&self, turns: &[ConversationTurn]) -> String {
        turns
            .iter()
            .map(|turn| {
                let role_str = match turn.role {
                    ConversationRole::User => "User",
                    ConversationRole::Assistant => "Assistant",
                    ConversationRole::System => "System",
                };
                format!("{}: {}", role_str, turn.content)
            })
            .collect::<Vec<_>>()
            .join(" ")
    }

    /// Calculate total tokens across all turns
    fn calculate_total_tokens(&self, turns: &[ConversationTurn]) -> usize {
        turns.iter().map(|turn| self.count_tokens(&turn.content)).sum()
    }

    /// Count tokens in text
    fn count_tokens(&self, text: &str) -> usize {
        if let Some(ref counter) = self.token_counter {
            counter(text)
        } else {
            // Fallback estimation: ~4 characters per token
            text.len() / 4
        }
    }

    /// Trim summary to target length
    fn trim_to_target_length(&self, summary: String) -> Result<String> {
        let current_tokens = self.count_tokens(&summary);

        if current_tokens <= self.config.target_length {
            return Ok(summary);
        }

        // Calculate target character count
        let target_chars = (summary.len() as f32 * self.config.target_length as f32
            / current_tokens as f32) as usize;

        // Trim at word boundary
        if let Some(truncated) = self.truncate_at_word_boundary(&summary, target_chars) {
            Ok(truncated)
        } else {
            Ok(summary)
        }
    }

    /// Truncate text at word boundary
    fn truncate_at_word_boundary(&self, text: &str, max_chars: usize) -> Option<String> {
        if text.len() <= max_chars {
            return Some(text.to_string());
        }

        let truncated = &text[..max_chars];
        truncated
            .rfind(' ')
            .map(|last_space| format!("{}...", &truncated[..last_space]))
    }

    /// Extract all topics from conversation
    fn extract_all_topics(&self, turns: &[ConversationTurn]) -> Vec<String> {
        let mut topics = HashSet::new();

        for turn in turns {
            if let Some(metadata) = &turn.metadata {
                for topic in &metadata.topics {
                    topics.insert(topic.clone());
                }
            }
        }

        topics.into_iter().collect()
    }

    /// Extract all entities from conversation
    fn extract_all_entities(&self, turns: &[ConversationTurn]) -> Vec<String> {
        let mut entities = HashSet::new();

        for turn in turns {
            if let Some(metadata) = &turn.metadata {
                for entity in &metadata.entities {
                    entities.insert(entity.text.clone());
                }
            }
        }

        entities.into_iter().collect()
    }

    /// Extract topics preserved in summary
    fn extract_preserved_topics(
        &self,
        summary: &str,
        original_turns: &[ConversationTurn],
    ) -> Vec<String> {
        let original_topics = self.extract_all_topics(original_turns);
        let summary_lower = summary.to_lowercase();

        original_topics
            .into_iter()
            .filter(|topic| summary_lower.contains(&topic.to_lowercase()))
            .collect()
    }

    /// Extract entities preserved in summary
    fn extract_preserved_entities(
        &self,
        summary: &str,
        original_turns: &[ConversationTurn],
    ) -> Vec<String> {
        let original_entities = self.extract_all_entities(original_turns);
        let summary_lower = summary.to_lowercase();

        original_entities
            .into_iter()
            .filter(|entity| summary_lower.contains(&entity.to_lowercase()))
            .collect()
    }

    /// Assess summary quality
    fn assess_summary_quality(
        &self,
        summary: &str,
        original_turns: &[ConversationTurn],
        compression_ratio: f32,
    ) -> QualityAssessment {
        let mut quality_score = 0.0;
        let mut confidence: f32 = 1.0;

        // Length appropriateness (0.2 weight)
        let length_score = if summary.trim().is_empty() {
            0.0
        } else if compression_ratio > 0.8 {
            0.5 // Little compression
        } else if compression_ratio < 0.1 {
            0.3 // Too much compression
        } else {
            1.0 // Good compression
        };
        quality_score += length_score * 0.2;

        // Topic preservation (0.3 weight)
        let original_topics = self.extract_all_topics(original_turns);
        let preserved_topics = self.extract_preserved_topics(summary, original_turns);
        let topic_preservation = if original_topics.is_empty() {
            1.0
        } else {
            preserved_topics.len() as f32 / original_topics.len() as f32
        };
        quality_score += topic_preservation * 0.3;

        // Entity preservation (0.2 weight)
        let original_entities = self.extract_all_entities(original_turns);
        let preserved_entities = self.extract_preserved_entities(summary, original_turns);
        let entity_preservation = if original_entities.is_empty() {
            1.0
        } else {
            preserved_entities.len() as f32 / original_entities.len() as f32
        };
        quality_score += entity_preservation * 0.2;

        // Coherence (0.2 weight) - simplified heuristic
        let coherence_score = self.assess_coherence(summary);
        quality_score += coherence_score * 0.2;

        // Readability (0.1 weight)
        let readability_score = self.assess_readability(summary);
        quality_score += readability_score * 0.1;

        // Adjust confidence based on various factors
        if compression_ratio < 0.2 {
            confidence *= 0.8; // Less confident with high compression
        }
        if original_turns.len() < 3 {
            confidence *= 0.9; // Less confident with few turns
        }

        QualityAssessment {
            quality_score: quality_score.min(1.0).max(0.0),
            confidence: confidence.min(1.0).max(0.0),
        }
    }

    /// Assess summary coherence
    fn assess_coherence(&self, summary: &str) -> f32 {
        if summary.trim().is_empty() {
            return 0.0;
        }

        let mut coherence_score: f32 = 0.5; // Base score

        // Check for proper sentence structure
        let sentence_endings = summary.matches(&['.', '!', '?']).count();
        let sentences = self.split_into_sentences(summary).len();
        if sentences > 0 && sentence_endings > 0 {
            coherence_score += 0.2;
        }

        // Check for role markers (indicates conversation structure preserved)
        if summary.contains("User:") || summary.contains("Assistant:") {
            coherence_score += 0.2;
        }

        // Check for transition words
        let transitions = [
            "however",
            "therefore",
            "meanwhile",
            "additionally",
            "furthermore",
        ];
        if transitions.iter().any(|&word| summary.to_lowercase().contains(word)) {
            coherence_score += 0.1;
        }

        coherence_score.min(1.0)
    }

    /// Assess summary readability
    fn assess_readability(&self, summary: &str) -> f32 {
        if summary.trim().is_empty() {
            return 0.0;
        }

        let word_count = summary.split_whitespace().count();
        let sentence_count = summary.matches(&['.', '!', '?']).count().max(1);
        let avg_sentence_length = word_count as f32 / sentence_count as f32;

        // Optimal sentence length is around 15-20 words

        if avg_sentence_length < 5.0 {
            0.6 // Too short
        } else if avg_sentence_length <= 25.0 {
            1.0 // Good length
        } else if avg_sentence_length <= 35.0 {
            0.8 // A bit long
        } else {
            0.5 // Too long
        }
    }

    // ================================================================================================
    // LEGACY COMPATIBILITY METHODS (From original file)
    // ================================================================================================

    /// Generate topic-focused summary (legacy compatibility)
    pub fn summarize_by_topic(
        &self,
        turns: &[ConversationTurn],
        target_topic: &str,
    ) -> Result<String> {
        let relevant_turns: Vec<_> = turns
            .iter()
            .filter(|turn| {
                if let Some(metadata) = &turn.metadata {
                    metadata.topics.iter().any(|topic| topic.contains(target_topic))
                } else {
                    turn.content.to_lowercase().contains(&target_topic.to_lowercase())
                }
            })
            .collect();

        if relevant_turns.is_empty() {
            return Ok(format!("No discussion found about topic: {}", target_topic));
        }

        let cloned_turns = relevant_turns.into_iter().cloned().collect::<Vec<_>>();
        let mut cloned_summarizer = self.clone();
        cloned_summarizer.summarize_context(&cloned_turns)
    }

    /// Generate time-based summary (legacy compatibility)
    pub fn summarize_time_window(
        &self,
        turns: &[ConversationTurn],
        start_time: chrono::DateTime<chrono::Utc>,
        end_time: chrono::DateTime<chrono::Utc>,
    ) -> Result<String> {
        let windowed_turns: Vec<_> = turns
            .iter()
            .filter(|turn| turn.timestamp >= start_time && turn.timestamp <= end_time)
            .cloned()
            .collect();

        if windowed_turns.is_empty() {
            return Ok("No conversation activity in the specified time window.".to_string());
        }

        let mut cloned_summarizer = self.clone();
        cloned_summarizer.summarize_context(&windowed_turns)
    }

    /// Generate hierarchical summary (legacy compatibility)
    pub fn hierarchical_summary(&self, turns: &[ConversationTurn]) -> Result<HierarchicalSummary> {
        let total_turns = turns.len();

        // Divide into segments
        let segment_size = (total_turns / 3).max(1);
        let mut segments = Vec::new();

        for i in (0..total_turns).step_by(segment_size) {
            let end = (i + segment_size).min(total_turns);
            let segment_turns = &turns[i..end];

            if !segment_turns.is_empty() {
                let mut cloned_summarizer = self.clone();
                let segment_summary = cloned_summarizer.summarize_context(segment_turns)?;
                let segment_topics = self.extract_segment_topics(segment_turns);

                segments.push(ConversationSegment {
                    start_turn: i,
                    end_turn: end - 1,
                    summary: segment_summary,
                    topics: segment_topics,
                    turn_count: segment_turns.len(),
                });
            }
        }

        // Generate overall summary
        let mut cloned_summarizer = self.clone();
        let overall_summary = cloned_summarizer.summarize_context(turns)?;
        let main_topics = self.extract_main_topics(turns);

        Ok(HierarchicalSummary {
            overall_summary,
            main_topics,
            segments,
            total_turns,
        })
    }

    /// Extract main topics from conversation (legacy compatibility)
    fn extract_main_topics(&self, turns: &[ConversationTurn]) -> Vec<String> {
        let mut topic_counts = HashMap::new();

        for turn in turns {
            if let Some(metadata) = &turn.metadata {
                for topic in &metadata.topics {
                    *topic_counts.entry(topic.clone()).or_insert(0) += 1;
                }
            }
        }

        let mut topics: Vec<_> = topic_counts.into_iter().collect();
        topics.sort_by_key(|item| std::cmp::Reverse(item.1));

        topics.into_iter()
            .take(5) // Top 5 topics
            .map(|(topic, _)| topic)
            .collect()
    }

    /// Extract topics from a conversation segment (legacy compatibility)
    fn extract_segment_topics(&self, turns: &[ConversationTurn]) -> Vec<String> {
        let mut topics = HashSet::new();

        for turn in turns {
            if let Some(metadata) = &turn.metadata {
                topics.extend(metadata.topics.iter().cloned());
            }
        }

        topics.into_iter().collect()
    }

    /// Generate summary with specified constraints (legacy compatibility)
    pub fn constrained_summary(
        &self,
        turns: &[ConversationTurn],
        max_length: usize,
        include_topics: bool,
        include_sentiment: bool,
    ) -> Result<ConstrainedSummary> {
        let mut cloned_summarizer = self.clone();
        let base_summary = cloned_summarizer.summarize_context(turns)?;

        let mut final_summary = base_summary;
        if final_summary.len() > max_length {
            final_summary.truncate(max_length - 3);
            final_summary.push_str("...");
        }

        let topics = if include_topics { Some(self.extract_main_topics(turns)) } else { None };

        let sentiment_analysis = if include_sentiment {
            Some(self.analyze_overall_sentiment(turns))
        } else {
            None
        };

        Ok(ConstrainedSummary {
            summary: final_summary.clone(),
            topics,
            sentiment_analysis,
            original_turn_count: turns.len(),
            compression_ratio: turns.iter().map(|t| t.content.len()).sum::<usize>() as f32
                / final_summary.len() as f32,
        })
    }

    /// Analyze overall sentiment of conversation (legacy compatibility)
    fn analyze_overall_sentiment(&self, turns: &[ConversationTurn]) -> SentimentAnalysis {
        let mut positive_count = 0;
        let mut negative_count = 0;
        let mut neutral_count = 0;
        let mut total_confidence = 0.0;

        for turn in turns {
            if let Some(metadata) = &turn.metadata {
                total_confidence += metadata.confidence;

                if let Some(sentiment) = &metadata.sentiment {
                    match sentiment.as_str() {
                        "positive" => positive_count += 1,
                        "negative" => negative_count += 1,
                        _ => neutral_count += 1,
                    }
                }
            }
        }

        let total_turns = turns.len();
        let avg_confidence =
            if total_turns > 0 { total_confidence / total_turns as f32 } else { 0.0 };

        let dominant_sentiment =
            if positive_count > negative_count && positive_count > neutral_count {
                "positive".to_string()
            } else if negative_count > positive_count && negative_count > neutral_count {
                "negative".to_string()
            } else {
                "neutral".to_string()
            };

        SentimentAnalysis {
            dominant_sentiment,
            positive_ratio: positive_count as f32 / total_turns as f32,
            negative_ratio: negative_count as f32 / total_turns as f32,
            neutral_ratio: neutral_count as f32 / total_turns as f32,
            confidence: avg_confidence,
        }
    }
}

/// Quality assessment result
#[derive(Debug, Clone)]
struct QualityAssessment {
    quality_score: f32,
    confidence: f32,
}

// ================================================================================================
// ADDITIONAL HELPER FUNCTIONS
// ================================================================================================

/// Validate summarization configuration
pub fn validate_summarization_config(config: &SummarizationConfig) -> Result<()> {
    if config.target_length == 0 {
        return Err(TrustformersError::invalid_input_simple(
            "Target length must be greater than 0".to_string(),
        ));
    }

    if config.trigger_threshold <= config.target_length {
        return Err(TrustformersError::invalid_input_simple(
            "Trigger threshold must be greater than target length".to_string(),
        ));
    }

    Ok(())
}

/// Create a default context summarizer
pub fn create_default_summarizer() -> ContextSummarizer {
    ContextSummarizer::new(SummarizationConfig::default())
}

/// Create a high-compression summarizer for memory-constrained environments
pub fn create_high_compression_summarizer() -> ContextSummarizer {
    let mut config = SummarizationConfig::default();
    config.target_length = 100;
    config.trigger_threshold = 500;
    config.strategy = SummarizationStrategy::Hybrid;

    ContextSummarizer::new(config)
}

/// Create a topic-focused extractive summarizer
pub fn create_extractive_summarizer() -> ContextSummarizer {
    let mut config = SummarizationConfig::default();
    config.strategy = SummarizationStrategy::Extractive;
    config.target_length = 300;

    ContextSummarizer::new(config)
}

/// Create an abstractive summarizer for detailed overviews
pub fn create_abstractive_summarizer() -> ContextSummarizer {
    let mut config = SummarizationConfig::default();
    config.strategy = SummarizationStrategy::Abstractive;
    config.target_length = 250;

    ContextSummarizer::new(config)
}

#[cfg(test)]
mod tests {
    use super::super::types::ConversationMetadata;
    use super::*;
    use chrono::Utc;

    fn create_test_turn(role: ConversationRole, content: &str) -> ConversationTurn {
        ConversationTurn {
            role,
            content: content.to_string(),
            timestamp: Utc::now(),
            metadata: None,
            token_count: content.len() / 4, // Simple estimation
        }
    }

    fn create_test_turn_with_metadata(
        role: ConversationRole,
        content: &str,
        topics: Vec<String>,
    ) -> ConversationTurn {
        ConversationTurn {
            role,
            content: content.to_string(),
            timestamp: Utc::now(),
            metadata: Some(ConversationMetadata {
                sentiment: Some("neutral".to_string()),
                intent: Some("statement".to_string()),
                confidence: 0.8,
                topics,
                safety_flags: Vec::new(),
                entities: Vec::new(),
                quality_score: 0.8,
                engagement_level: EngagementLevel::Medium,
                reasoning_type: None,
            }),
            token_count: content.len() / 4,
        }
    }

    #[test]
    fn test_context_summarizer_creation() {
        let config = SummarizationConfig::default();
        let summarizer = ContextSummarizer::new(config.clone());

        assert_eq!(summarizer.config.strategy, config.strategy);
        assert_eq!(summarizer.config.target_length, config.target_length);
    }

    #[test]
    fn test_legacy_constructor() {
        let summarizer = ContextSummarizer::with_strategy(SummarizationStrategy::Extractive, 200);
        assert_eq!(
            summarizer.config.strategy,
            SummarizationStrategy::Extractive
        );
        assert_eq!(summarizer.config.target_length, 200);
    }

    #[test]
    fn test_empty_conversation_summarization() {
        let mut summarizer = create_default_summarizer();
        let result = summarizer.summarize_context_enhanced(&[]).expect("operation failed in test");

        assert!(result.summary.is_empty());
        assert_eq!(result.original_tokens, 0);
        assert_eq!(result.summary_tokens, 0);
        assert_eq!(result.compression_ratio, 1.0);
    }

    #[test]
    fn test_legacy_summarization() {
        let mut summarizer = create_default_summarizer();
        let turns = vec![
            create_test_turn(ConversationRole::User, "Hello!"),
            create_test_turn(ConversationRole::Assistant, "Hi there!"),
        ];

        let result = summarizer.summarize_context(&turns).expect("operation failed in test");
        assert!(!result.is_empty());
    }

    #[test]
    fn test_short_conversation_no_summarization() {
        let mut summarizer = create_default_summarizer();
        let turns = vec![
            create_test_turn(ConversationRole::User, "Hello!"),
            create_test_turn(ConversationRole::Assistant, "Hi there!"),
        ];

        let result =
            summarizer.summarize_context_enhanced(&turns).expect("operation failed in test");

        // Should not summarize if under target length
        assert!(result.summary.contains("Hello"));
        assert!(result.summary.contains("Hi there"));
        assert_eq!(result.compression_ratio, 1.0);
    }

    #[test]
    fn test_extractive_summarization() {
        let mut config = SummarizationConfig::default();
        config.strategy = SummarizationStrategy::Extractive;
        config.target_length = 20; // Force summarization
        config.trigger_threshold = 10;

        let mut summarizer = ContextSummarizer::new(config);

        let turns = vec![
            create_test_turn_with_metadata(
                ConversationRole::User,
                "I really need help with my Python programming project. It's about machine learning algorithms.",
                vec!["technology".to_string(), "programming".to_string()]
            ),
            create_test_turn(
                ConversationRole::Assistant,
                "I'd be happy to help you with your Python machine learning project. What specific aspect are you working on?"
            ),
            create_test_turn(
                ConversationRole::User,
                "I'm trying to implement a neural network from scratch but I'm getting confused about backpropagation."
            ),
        ];

        let result =
            summarizer.summarize_context_enhanced(&turns).expect("operation failed in test");

        assert!(!result.summary.is_empty());
        assert!(result.compression_ratio < 1.0);
        assert!(result.quality_score > 0.0);
        assert_eq!(result.strategy_used, SummarizationStrategy::Extractive);
    }

    #[test]
    fn test_abstractive_summarization() {
        let mut config = SummarizationConfig::default();
        config.strategy = SummarizationStrategy::Abstractive;
        config.target_length = 30;
        config.trigger_threshold = 10;

        let mut summarizer = ContextSummarizer::new(config);

        let turns = vec![
            create_test_turn_with_metadata(
                ConversationRole::User,
                "What's the weather like today?",
                vec!["weather".to_string()]
            ),
            create_test_turn(
                ConversationRole::Assistant,
                "I don't have access to current weather data, but I can help you find weather information."
            ),
            create_test_turn_with_metadata(
                ConversationRole::User,
                "How can I check the weather?",
                vec!["weather".to_string()]
            ),
        ];

        let result =
            summarizer.summarize_context_enhanced(&turns).expect("operation failed in test");

        assert!(!result.summary.is_empty());
        assert!(result.summary.contains("Conversation summary"));
        assert_eq!(result.strategy_used, SummarizationStrategy::Abstractive);
    }

    #[test]
    fn test_hybrid_summarization() {
        let mut config = SummarizationConfig::default();
        config.strategy = SummarizationStrategy::Hybrid;
        config.target_length = 40;
        config.trigger_threshold = 10;

        let mut summarizer = ContextSummarizer::new(config);

        let turns = vec![
            create_test_turn(
                ConversationRole::User,
                "I'm interested in learning about artificial intelligence and machine learning.",
            ),
            create_test_turn(
                ConversationRole::Assistant,
                "AI and ML are fascinating fields! What specific area interests you most?",
            ),
            create_test_turn(
                ConversationRole::User,
                "I'd like to understand neural networks and deep learning applications.",
            ),
        ];

        let result =
            summarizer.summarize_context_enhanced(&turns).expect("operation failed in test");

        assert!(!result.summary.is_empty());
        assert!(result.compression_ratio < 1.0);
        assert_eq!(result.strategy_used, SummarizationStrategy::Hybrid);
    }

    #[test]
    fn test_sentence_importance_scoring() {
        let summarizer = create_default_summarizer();
        let turn = create_test_turn(
            ConversationRole::User,
            "I really need help with this important question.",
        );

        let score = summarizer.calculate_sentence_importance(
            "I really need help with this important question.",
            &turn,
            0,
        );

        assert!(score > 0.0);
        assert!(score <= 1.0);
    }

    #[test]
    fn test_personal_info_detection() {
        let summarizer = create_default_summarizer();

        assert!(summarizer.contains_personal_info("i am john and i work as a developer"));
        assert!(summarizer.contains_personal_info("my name is alice"));
        assert!(!summarizer.contains_personal_info("the weather is nice today"));
    }

    #[test]
    fn test_emotional_content_detection() {
        let summarizer = create_default_summarizer();

        assert!(summarizer.contains_emotional_content("i love this amazing product"));
        assert!(summarizer.contains_emotional_content("i feel frustrated about this"));
        assert!(!summarizer.contains_emotional_content("the technical specifications are correct"));
    }

    #[test]
    fn test_token_counting() {
        let summarizer = create_default_summarizer();

        let short_text = "Hello world";
        let long_text = "This is a much longer text with many more words and characters";

        let short_tokens = summarizer.count_tokens(short_text);
        let long_tokens = summarizer.count_tokens(long_text);

        assert!(long_tokens > short_tokens);
        assert!(short_tokens > 0);
    }

    #[test]
    fn test_topic_extraction() {
        let summarizer = create_default_summarizer();

        let tech_sentence = "I need help with programming and software development";
        let food_sentence = "Let's go to a restaurant for dinner";
        let mixed_sentence = "I work in tech but love cooking food";

        let tech_topics = summarizer.extract_sentence_topics(tech_sentence);
        let food_topics = summarizer.extract_sentence_topics(food_sentence);
        let mixed_topics = summarizer.extract_sentence_topics(mixed_sentence);

        assert!(tech_topics.contains(&"technology".to_string()));
        assert!(food_topics.contains(&"food".to_string()));
        assert!(mixed_topics.len() >= 2);
    }

    #[test]
    fn test_quality_assessment() {
        let summarizer = create_default_summarizer();
        let turns = vec![create_test_turn_with_metadata(
            ConversationRole::User,
            "What is machine learning?",
            vec!["technology".to_string()],
        )];

        let good_summary = "User asked about machine learning technology";
        let assessment = summarizer.assess_summary_quality(good_summary, &turns, 0.5);

        assert!(assessment.quality_score > 0.0);
        assert!(assessment.confidence > 0.0);
    }

    #[test]
    fn test_configuration_validation() {
        let mut config = SummarizationConfig::default();
        assert!(validate_summarization_config(&config).is_ok());

        config.target_length = 0;
        assert!(validate_summarization_config(&config).is_err());

        config.target_length = 100;
        config.trigger_threshold = 50;
        assert!(validate_summarization_config(&config).is_err());
    }

    #[test]
    fn test_specialized_summarizers() {
        let high_compression = create_high_compression_summarizer();
        let extractive = create_extractive_summarizer();
        let abstractive = create_abstractive_summarizer();

        assert_eq!(high_compression.config.target_length, 100);
        assert_eq!(
            extractive.config.strategy,
            SummarizationStrategy::Extractive
        );
        assert_eq!(
            abstractive.config.strategy,
            SummarizationStrategy::Abstractive
        );
    }

    #[test]
    fn test_topic_clustering() {
        let summarizer = create_default_summarizer();

        let sentences = vec![
            SentenceScore {
                sentence: "I love programming in Python".to_string(),
                score: 0.8,
                position: 0,
                turn_index: 0,
                topics: vec!["technology".to_string()],
                entities: vec![],
                speaker_role: ConversationRole::User,
            },
            SentenceScore {
                sentence: "Let's discuss machine learning algorithms".to_string(),
                score: 0.9,
                position: 1,
                turn_index: 0,
                topics: vec!["technology".to_string()],
                entities: vec![],
                speaker_role: ConversationRole::User,
            },
            SentenceScore {
                sentence: "I had pizza for dinner".to_string(),
                score: 0.3,
                position: 2,
                turn_index: 1,
                topics: vec!["food".to_string()],
                entities: vec![],
                speaker_role: ConversationRole::User,
            },
        ];

        let clusters = summarizer.cluster_by_topics(&sentences);

        assert!(clusters.len() >= 2); // Should have at least technology and food clusters

        let tech_cluster = clusters.iter().find(|c| c.topic == "technology");
        assert!(tech_cluster.is_some());
        assert_eq!(
            tech_cluster.expect("operation failed in test").sentences.len(),
            2
        );
    }

    #[test]
    fn test_conversation_flow_analysis() {
        let summarizer = create_default_summarizer();

        let question_heavy_turns = vec![
            create_test_turn(ConversationRole::User, "What is AI?"),
            create_test_turn(
                ConversationRole::Assistant,
                "AI is artificial intelligence.",
            ),
            create_test_turn(ConversationRole::User, "How does it work?"),
            create_test_turn(ConversationRole::Assistant, "It uses algorithms."),
            create_test_turn(ConversationRole::User, "Can you give examples?"),
        ];

        let flow_analysis = summarizer.analyze_conversation_flow(&question_heavy_turns);
        assert!(flow_analysis.is_some());
        assert!(flow_analysis.expect("operation failed in test").contains("inquiry-heavy"));

        let statement_heavy_turns = vec![
            create_test_turn(ConversationRole::User, "I work in tech."),
            create_test_turn(ConversationRole::Assistant, "That's interesting."),
            create_test_turn(ConversationRole::User, "I develop software applications."),
        ];

        let flow_analysis2 = summarizer.analyze_conversation_flow(&statement_heavy_turns);
        assert!(flow_analysis2.is_some());
        assert!(flow_analysis2.expect("operation failed in test").contains("informational"));
    }

    #[test]
    fn test_legacy_compatibility() {
        let summarizer = create_default_summarizer();
        let turns = vec![
            create_test_turn_with_metadata(
                ConversationRole::User,
                "Let's talk about technology and programming",
                vec!["technology".to_string()],
            ),
            create_test_turn(
                ConversationRole::Assistant,
                "Sure, what would you like to know?",
            ),
        ];

        // Test topic-focused summary
        let topic_summary = summarizer
            .summarize_by_topic(&turns, "technology")
            .expect("operation failed in test");
        assert!(!topic_summary.is_empty());

        // Test hierarchical summary
        let hierarchical =
            summarizer.hierarchical_summary(&turns).expect("operation failed in test");
        assert!(!hierarchical.overall_summary.is_empty());
        assert_eq!(hierarchical.total_turns, 2);

        // Test constrained summary
        let constrained = summarizer
            .constrained_summary(&turns, 100, true, true)
            .expect("operation failed in test");
        assert!(!constrained.summary.is_empty());
        assert!(constrained.topics.is_some());
        assert!(constrained.sentiment_analysis.is_some());
    }
}