zipora 3.0.0

High-performance Rust implementation providing advanced data structures and compression algorithms with memory safety guarantees. Features LRU page cache, sophisticated caching layer, fiber-based concurrency, real-time compression, secure memory pools, SIMD optimizations, and complete C FFI for migration from C++.
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
//! PA-Zip Core Compression Engine
//!
//! This module implements the main PA-Zip compression engine with cost-aware selection,
//! combining global dictionary matching, local pattern matching, and literal encoding
//! through an 8-step algorithm for optimal compression performance.
//!
//! # Algorithm Overview
//!
//! The PA-Zip compression algorithm implements an 8-step process for each input position:
//!
//! 1. **Local Match Search**: Use LocalMatcher to find recent pattern matches
//! 2. **Global Match Search**: Use SuffixArrayDictionary for global pattern matches  
//! 3. **Cost Calculation**: Calculate encoding overhead for each match type
//! 4. **Strategy Selection**: Choose optimal compression strategy based on net benefit
//! 5. **Type Classification**: Select appropriate PA-Zip compression type
//! 6. **Encoding**: Apply chosen compression strategy and encode result
//! 7. **Statistics Update**: Track performance metrics and learning data
//! 8. **Position Advance**: Move to next position based on match length
//!
//! # Cost-Aware Selection
//!
//! The compressor uses sophisticated cost analysis to choose between strategies:
//! - **Net Benefit = Match Length - Encoding Overhead - Dictionary Overhead**
//! - **Literal threshold**: Minimum benefit required vs literal encoding
//! - **Global vs Local**: Prefer global matches for longer patterns
//! - **Learning adaptation**: Adjust thresholds based on data characteristics
//!
//! # Performance Features
//!
//! - **SIMD Acceleration**: Hardware-accelerated pattern matching and encoding
//! - **Adaptive Thresholds**: Dynamic adjustment based on compression efficiency
//! - **DFA Cache Integration**: Fast prefix matching for global dictionary
//! - **Multi-threading**: Parallel compression for large data streams
//! - **Memory Optimization**: Efficient buffer management and reuse
//!
//! # Usage Example
//!
//! ```rust
//! use zipora::compression::dict_zip::{PaZipCompressor, PaZipCompressorConfig, DictionaryBuilder};
//! use zipora::memory::{SecureMemoryPool, SecurePoolConfig};
//!
//! // Build dictionary from training data
//! let training_data = b"The quick brown fox jumps over the lazy dog. The quick brown fox jumps again.";
//! let builder = DictionaryBuilder::default();
//! let dictionary = builder.build(training_data)?;
//!
//! // Configure compressor for high compression
//! let config = PaZipCompressorConfig::high_compression();
//! let pool = SecureMemoryPool::new(SecurePoolConfig::new(4096, 1024, 8))?;
//! let mut compressor = PaZipCompressor::new(dictionary, config, pool)?;
//!
//! // Compress data
//! let input = b"data to compress with pattern matching";
//! let mut compressed = Vec::new();
//! let stats = compressor.compress(input, &mut compressed)?;
//!
//! println!("Compression ratio: {:.2}", stats.compression_ratio);
//! println!("Global matches: {}, Local matches: {}", 
//!          stats.global_matches, stats.local_matches);
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

use crate::compression::dict_zip::{
    compression_types::{CompressionType, Match, calculate_encoding_cost, choose_best_compression_type},
    dictionary::{SuffixArrayDictionary, MatchStats},
    local_matcher::{LocalMatcher, LocalMatcherConfig, LocalMatch, LocalMatcherStats},
    dfa_cache::{CacheStats},
    reference_encoding::{compress_record_reference},
};
use crate::error::{Result, ZiporaError};
use crate::memory::SecureMemoryPool;
#[cfg(test)]
use crate::memory::SecurePoolConfig;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};


use std::sync::Arc;
use std::time::{Duration, Instant};

/// Configuration for PA-Zip compressor
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PaZipCompressorConfig {
    /// Local matcher configuration
    pub local_config: LocalMatcherConfig,
    
    /// Maximum probe distance for local matching
    pub max_local_probe_distance: u32,
    
    /// Maximum probe distance for global matching  
    pub max_global_probe_distance: u32,
    
    /// Minimum net benefit threshold for accepting matches
    pub min_net_benefit: i32,
    
    /// Literal encoding cost (bits per byte)
    pub literal_cost_bits: u32,
    
    /// Global dictionary access cost (additional overhead)
    pub global_access_cost: u32,
    
    /// Adaptive threshold learning rate (0.0 to 1.0)
    pub learning_rate: f64,
    
    /// Enable adaptive threshold adjustment
    pub adaptive_thresholds: bool,
    
    /// Use reference-compliant encoding matching the reference implementation exactly
    pub use_reference_encoding: bool,
    
    /// Use suffix array for local matching (when reference encoding is enabled)
    pub use_suffix_array_local_match: bool,
    
    /// Enable SIMD acceleration where available
    pub enable_simd: bool,
    
    /// Enable multi-threading for large inputs
    pub enable_multithreading: bool,
    
    /// Minimum input size for multi-threading
    pub multithreading_threshold: usize,
    
    /// Buffer size for compression output
    pub output_buffer_size: usize,
    
    /// Enable detailed statistics collection
    pub collect_detailed_stats: bool,
}

impl Default for PaZipCompressorConfig {
    fn default() -> Self {
        Self {
            local_config: LocalMatcherConfig::default(),
            max_local_probe_distance: 8,
            max_global_probe_distance: 16,
            min_net_benefit: 2,
            literal_cost_bits: 8,
            global_access_cost: 4,
            learning_rate: 0.1,
            adaptive_thresholds: true,
            use_reference_encoding: false, // Default to legacy encoding for backwards compatibility
            use_suffix_array_local_match: false, // Default to hash table for performance
            enable_simd: true,
            enable_multithreading: true,
            multithreading_threshold: 64 * 1024,
            output_buffer_size: 1024 * 1024,
            collect_detailed_stats: false,
        }
    }
}

impl PaZipCompressorConfig {
    /// Configuration optimized for fast compression
    pub fn fast_compression() -> Self {
        Self {
            max_local_probe_distance: 4,
            max_global_probe_distance: 8,
            min_net_benefit: 1,
            learning_rate: 0.05,
            collect_detailed_stats: false,
            ..Default::default()
        }
    }
    
    /// Configuration optimized for high compression ratio
    pub fn high_compression() -> Self {
        Self {
            max_local_probe_distance: 16,
            max_global_probe_distance: 32,
            min_net_benefit: 3,
            learning_rate: 0.15,
            collect_detailed_stats: true,
            ..Default::default()
        }
    }
    
    /// Configuration optimized for balanced speed/compression
    pub fn balanced() -> Self {
        Self::default()
    }
    
    /// Configuration optimized for real-time compression
    pub fn realtime() -> Self {
        Self {
            max_local_probe_distance: 2,
            max_global_probe_distance: 4,
            min_net_benefit: 0,
            adaptive_thresholds: false,
            use_reference_encoding: false, // Use legacy for speed
            use_suffix_array_local_match: false, // Use hash table for speed
            enable_multithreading: false,
            collect_detailed_stats: false,
            ..Default::default()
        }
    }
    
    /// Configuration for reference-compliant compression matching the reference implementation exactly
    pub fn reference_compliant() -> Self {
        Self {
            max_local_probe_distance: 30, // Matches reference implementation
            max_global_probe_distance: 100, // Matches reference implementation
            use_reference_encoding: true, // Enable exact reference compliance
            use_suffix_array_local_match: true, // Use suffix array for accuracy
            adaptive_thresholds: false, // Use static thresholds like reference
            enable_multithreading: false, // Single-threaded like reference
            collect_detailed_stats: true, // Collect stats for validation
            ..Default::default()
        }
    }
}

/// Compression strategy selected for a specific position
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum CompressionStrategy {
    /// Use literal encoding (no match found or not beneficial)
    Literal { length: u8 },
    
    /// Use local match from sliding window
    Local { distance: u32, length: u32, match_type: CompressionType },
    
    /// Use global match from dictionary
    Global { dict_offset: u32, length: u32, match_type: CompressionType },
}

/// Cost analysis result for compression strategy selection
#[derive(Debug, Clone)]
pub struct CostAnalysis {
    /// Net benefit of the strategy (match_length - total_cost)
    pub net_benefit: i32,
    
    /// Encoding cost in bits
    pub encoding_cost: u32,
    
    /// Dictionary access cost (for global matches)
    pub access_cost: u32,
    
    /// Total cost (encoding + access + overhead)
    pub total_cost: u32,
    
    /// Match length in bytes
    pub match_length: u32,
    
    /// Compression efficiency (bytes_saved / bytes_processed)
    pub efficiency: f64,
}

/// Comprehensive statistics for PA-Zip compression
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CompressionStats {
    /// Total bytes processed
    pub bytes_processed: u64,
    
    /// Total bytes output (compressed)
    pub bytes_output: u64,
    
    /// Overall compression ratio (output / input)
    pub compression_ratio: f64,
    
    /// Number of literal encodings used
    pub literal_count: u64,
    
    /// Number of local matches found
    pub local_matches: u64,
    
    /// Number of global matches found
    pub global_matches: u64,
    
    /// Total bytes saved by local matches
    pub local_bytes_saved: u64,
    
    /// Total bytes saved by global matches
    pub global_bytes_saved: u64,
    
    /// Average local match length
    pub avg_local_length: f64,
    
    /// Average global match length
    pub avg_global_length: f64,
    
    /// DFA cache hit rate
    pub cache_hit_rate: f64,
    
    /// Total compression time
    pub compression_time: Duration,
    
    /// Processing speed (bytes per second)
    pub processing_speed: f64,
    
    /// Number of strategy switches (local -> global, etc.)
    pub strategy_switches: u64,
    
    /// Adaptive threshold adjustments made
    pub threshold_adjustments: u64,
    
    /// Detailed compression type usage
    pub compression_type_usage: [u64; 8],
}

impl CompressionStats {
    /// Create new empty statistics
    pub fn new() -> Self {
        Self::default()
    }
    
    /// Calculate derived statistics
    pub fn finalize(&mut self, start_time: Instant) {
        self.compression_time = start_time.elapsed();
        
        if self.bytes_processed > 0 {
            self.compression_ratio = self.bytes_output as f64 / self.bytes_processed as f64;
            self.processing_speed = self.bytes_processed as f64 / self.compression_time.as_secs_f64();
        }
        
        if self.local_matches > 0 {
            self.avg_local_length = self.local_bytes_saved as f64 / self.local_matches as f64;
        }
        
        if self.global_matches > 0 {
            self.avg_global_length = self.global_bytes_saved as f64 / self.global_matches as f64;
        }
    }
    
    /// Merge statistics from another instance
    pub fn merge(&mut self, other: &CompressionStats) {
        self.bytes_processed += other.bytes_processed;
        self.bytes_output += other.bytes_output;
        self.literal_count += other.literal_count;
        self.local_matches += other.local_matches;
        self.global_matches += other.global_matches;
        self.local_bytes_saved += other.local_bytes_saved;
        self.global_bytes_saved += other.global_bytes_saved;
        self.strategy_switches += other.strategy_switches;
        self.threshold_adjustments += other.threshold_adjustments;
        
        for i in 0..8 {
            self.compression_type_usage[i] += other.compression_type_usage[i];
        }
    }
}

/// Main PA-Zip compression engine
#[derive(Clone)]
pub struct PaZipCompressor {
    /// Global dictionary for pattern matching
    dictionary: SuffixArrayDictionary,
    
    /// Local pattern matcher
    local_matcher: LocalMatcher,
    
    /// Compressor configuration
    config: PaZipCompressorConfig,
    
    /// Memory pool for allocations
    memory_pool: Arc<SecureMemoryPool>,
    
    /// Current adaptive thresholds
    adaptive_thresholds: AdaptiveThresholds,
    
    /// Compression statistics
    stats: CompressionStats,
    
    /// Output buffer for compression
    output_buffer: Vec<u8>,
    
    /// Current compression strategy
    current_strategy: Option<CompressionStrategy>,
}

/// Adaptive thresholds that learn from compression patterns
#[derive(Debug, Clone)]
struct AdaptiveThresholds {
    /// Current minimum net benefit threshold
    min_net_benefit: f64,
    
    /// Local vs global preference bias
    global_bias: f64,
    
    /// Literal encoding preference threshold
    literal_threshold: f64,
    
    /// Learning momentum for threshold updates
    momentum: f64,
    
    /// Number of updates applied
    update_count: u64,
}

impl Default for AdaptiveThresholds {
    fn default() -> Self {
        Self {
            min_net_benefit: 2.0,
            global_bias: 0.0,
            literal_threshold: 1.0,
            momentum: 0.9,
            update_count: 0,
        }
    }
}

impl AdaptiveThresholds {
    /// Update thresholds based on compression efficiency
    fn update(&mut self, efficiency: f64, strategy: CompressionStrategy, learning_rate: f64) {
        let update_factor = learning_rate * (1.0 - self.momentum) + self.momentum;
        
        match strategy {
            CompressionStrategy::Global { .. } => {
                if efficiency > 0.8 {
                    self.global_bias += update_factor * 0.1;
                } else if efficiency < 0.4 {
                    self.global_bias -= update_factor * 0.1;
                }
            },
            CompressionStrategy::Local { .. } => {
                if efficiency > 0.8 {
                    self.global_bias -= update_factor * 0.05;
                } else if efficiency < 0.4 {
                    self.global_bias += update_factor * 0.05;
                }
            },
            CompressionStrategy::Literal { .. } => {
                if efficiency < 0.3 {
                    self.literal_threshold += update_factor * 0.2;
                }
            },
        }
        
        // Clamp values to reasonable ranges
        self.global_bias = self.global_bias.clamp(-2.0, 2.0);
        self.literal_threshold = self.literal_threshold.clamp(0.5, 5.0);
        self.min_net_benefit = self.min_net_benefit.clamp(0.5, 10.0);
        
        self.update_count += 1;
    }
}

impl PaZipCompressor {
    /// Create new PA-Zip compressor with dictionary and configuration
    pub fn new(
        dictionary: SuffixArrayDictionary,
        config: PaZipCompressorConfig,
        memory_pool: Arc<SecureMemoryPool>,
    ) -> Result<Self> {
        let local_matcher = LocalMatcher::new(config.local_config.clone(), memory_pool.clone())?;
        
        let output_buffer = Vec::with_capacity(config.output_buffer_size);
        
        Ok(Self {
            dictionary,
            local_matcher,
            config,
            memory_pool,
            adaptive_thresholds: AdaptiveThresholds::default(),
            stats: CompressionStats::new(),
            output_buffer,
            current_strategy: None,
        })
    }
    
    /// Compress input data using PA-Zip algorithm
    pub fn compress(&mut self, input: &[u8], output: &mut Vec<u8>) -> Result<CompressionStats> {
        let start_time = Instant::now();
        self.stats = CompressionStats::new();
        self.output_buffer.clear();
        
        if input.is_empty() {
            return Ok(self.stats.clone());
        }
        
        // Check if multithreading should be used
        if self.config.enable_multithreading && input.len() >= self.config.multithreading_threshold {
            self.compress_parallel(input, output)?;
        } else {
            self.compress_sequential(input, output)?;
        }
        
        self.stats.finalize(start_time);
        Ok(self.stats.clone())
    }
    
    /// Sequential compression using 8-step PA-Zip algorithm
    fn compress_sequential(&mut self, input: &[u8], output: &mut Vec<u8>) -> Result<()> {
        if self.config.use_reference_encoding {
            // Use reference-compliant compression matching the reference implementation exactly
            self.compress_sequential_reference(input, output)
        } else {
            // Use legacy compression for backwards compatibility
            self.compress_sequential_legacy(input, output)
        }
    }
    
    /// Reference-compliant compression using exact reference implementation encoding
    fn compress_sequential_reference(&mut self, input: &[u8], output: &mut Vec<u8>) -> Result<()> {
        // Extract global dictionary data if available
        let global_dictionary = if self.dictionary.size_in_bytes() > 0 {
            Some(self.dictionary.data()) // This will need to be implemented in dictionary.rs
        } else {
            None
        };
        
        // Use parameters matching the reference implementation
        let g_offset_bits = 24; // Standard value from reference
        let g_max_short_len = 32; // Standard value from reference
        
        // Use direct reference compression algorithm
        let bytes_written = compress_record_reference(
            input,
            output,
            self.config.use_suffix_array_local_match,
            global_dictionary,
            g_offset_bits,
            g_max_short_len,
        )?;
        
        // Update statistics
        self.stats.bytes_processed = input.len() as u64;
        self.stats.bytes_output = bytes_written as u64;
        
        Ok(())
    }
    
    /// Legacy compression using original implementation
    fn compress_sequential_legacy(&mut self, input: &[u8], output: &mut Vec<u8>) -> Result<()> {
        let mut pos = 0;
        
        while pos < input.len() {
            // Step 1: Find local match
            let local_match = self.find_local_match(input, pos)?;
            
            // Step 2: Find global match  
            let global_match = self.find_global_match(input, pos)?;
            
            // Step 3: Calculate costs for each strategy
            let strategies = self.calculate_strategy_costs(input, pos, local_match, global_match)?;
            
            // Step 4: Select optimal strategy
            let selected_strategy = self.select_optimal_strategy(strategies)?;
            
            // Step 5: Apply compression strategy  
            let mut temp_buffer = Vec::new();
            let advance_length = self.apply_compression_strategy(input, pos, selected_strategy, &mut temp_buffer)?;
            self.output_buffer.extend_from_slice(&temp_buffer);
            
            // Step 6: Update statistics and learning
            self.update_statistics(selected_strategy, advance_length);
            
            // Step 7: Update adaptive thresholds
            if self.config.adaptive_thresholds {
                self.update_adaptive_thresholds(selected_strategy);
            }
            
            // Step 8: Advance position
            pos += advance_length;
            self.current_strategy = Some(selected_strategy);
        }
        
        // Copy compressed data to output
        output.extend_from_slice(&self.output_buffer);
        self.stats.bytes_processed = input.len() as u64;
        self.stats.bytes_output = output.len() as u64;
        
        Ok(())
    }

    /// Decompress PA-Zip compressed data
    pub fn decompress(&mut self, input: &[u8], output: &mut Vec<u8>) -> Result<()> {
        if input.is_empty() {
            return Ok(());
        }

        output.clear();
        output.reserve(input.len() * 2); // Conservative estimate for decompressed size

        let mut pos = 0;
        while pos < input.len() {
            if pos >= input.len() {
                break;
            }

            // Read match type/compression type from the input
            let compression_type_byte = input[pos];
            pos += 1;

            let compression_type = match compression_type_byte {
                0 => CompressionType::Literal,
                1 => CompressionType::Global,
                2 => CompressionType::RLE,
                3 => CompressionType::NearShort,
                4 => CompressionType::Far1Short,
                5 => CompressionType::Far2Short,
                6 => CompressionType::Far2Long,
                7 => CompressionType::Far3Long,
                _ => CompressionType::Literal, // Default to literal
            };

            // Decompress based on compression type
            pos = self.decompress_match(input, pos, compression_type, output)?;
        }

        Ok(())
    }

    /// Decompress a single match based on compression type
    fn decompress_match(
        &mut self,
        input: &[u8],
        pos: usize,
        compression_type: CompressionType,
        output: &mut Vec<u8>,
    ) -> Result<usize> {
        let mut new_pos = pos;

        match compression_type {
            CompressionType::Literal => {
                // Read literal length and data
                if new_pos >= input.len() {
                    return Ok(new_pos);
                }
                let length = input[new_pos] as usize;
                new_pos += 1;

                if new_pos + length > input.len() {
                    return Err(ZiporaError::invalid_data("Literal data exceeds input bounds"));
                }

                output.extend_from_slice(&input[new_pos..new_pos + length]);
                new_pos += length;
            }
            CompressionType::Global => {
                // Read global dictionary offset and length
                if new_pos + 3 >= input.len() {
                    return Ok(new_pos);
                }
                let offset = u16::from_le_bytes([input[new_pos], input[new_pos + 1]]) as usize;
                let length = u16::from_le_bytes([input[new_pos + 2], input[new_pos + 3]]) as usize;
                new_pos += 4;

                // Copy from dictionary using actual dictionary data
                let dict_text = self.dictionary.dictionary_text();
                if offset + length <= dict_text.len() {
                    output.extend_from_slice(&dict_text[offset..offset + length]);
                } else {
                    // Handle bounds error gracefully - copy what we can
                    let available_length = dict_text.len().saturating_sub(offset);
                    if available_length > 0 {
                        output.extend_from_slice(&dict_text[offset..offset + available_length]);
                    }
                    return Err(ZiporaError::invalid_data("Global match exceeds dictionary bounds"));
                }
            }
            CompressionType::RLE => {
                // Read byte value and repetition count
                if new_pos + 1 >= input.len() {
                    return Ok(new_pos);
                }
                let byte_value = input[new_pos];
                let length = input[new_pos + 1] as usize;
                new_pos += 2;

                for _ in 0..length {
                    output.push(byte_value);
                }
            }
            CompressionType::NearShort | CompressionType::Far1Short => {
                // Read distance and length (both as single bytes)
                if new_pos + 1 >= input.len() {
                    return Ok(new_pos);
                }
                let distance = input[new_pos] as usize;
                let length = input[new_pos + 1] as usize;
                new_pos += 2;

                self.copy_from_distance(output, distance, length)?;
            }
            CompressionType::Far2Short => {
                // Read 2-byte distance and 1-byte length
                if new_pos + 2 >= input.len() {
                    return Ok(new_pos);
                }
                let distance = u16::from_le_bytes([input[new_pos], input[new_pos + 1]]) as usize;
                let length = input[new_pos + 2] as usize;
                new_pos += 3;

                self.copy_from_distance(output, distance, length)?;
            }
            CompressionType::Far2Long => {
                // Read 2-byte distance and 2-byte length
                if new_pos + 3 >= input.len() {
                    return Ok(new_pos);
                }
                let distance = u16::from_le_bytes([input[new_pos], input[new_pos + 1]]) as usize;
                let length = u16::from_le_bytes([input[new_pos + 2], input[new_pos + 3]]) as usize;
                new_pos += 4;

                self.copy_from_distance(output, distance, length)?;
            }
            CompressionType::Far3Long => {
                // Read 4-byte distance and 4-byte length
                if new_pos + 7 >= input.len() {
                    return Ok(new_pos);
                }
                let distance = u32::from_le_bytes([
                    input[new_pos], input[new_pos + 1], input[new_pos + 2], input[new_pos + 3]
                ]) as usize;
                let length = u32::from_le_bytes([
                    input[new_pos + 4], input[new_pos + 5], input[new_pos + 6], input[new_pos + 7]
                ]) as usize;
                new_pos += 8;

                self.copy_from_distance(output, distance, length)?;
            }
        }

        Ok(new_pos)
    }

    /// Copy data from previous position based on distance
    fn copy_from_distance(&self, output: &mut Vec<u8>, distance: usize, length: usize) -> Result<()> {
        if distance == 0 || distance > output.len() {
            return Err(ZiporaError::invalid_data("Invalid backreference distance"));
        }

        let start_pos = output.len() - distance;
        
        // Handle overlapping copies (pattern repetition)
        for i in 0..length {
            if start_pos + (i % distance) >= output.len() {
                break;
            }
            let byte = output[start_pos + (i % distance)];
            output.push(byte);
        }

        Ok(())
    }
    
    /// Parallel compression for large inputs
    fn compress_parallel(&mut self, input: &[u8], output: &mut Vec<u8>) -> Result<()> {
        // For inputs larger than 1MB, consider block-based parallel compression
        const PARALLEL_THRESHOLD: usize = 1024 * 1024; // 1MB
        const BLOCK_SIZE: usize = 64 * 1024; // 64KB blocks
        
        if input.len() < PARALLEL_THRESHOLD {
            // Use sequential compression for smaller inputs
            return self.compress_sequential(input, output);
        }

        // Block-based parallel compression
        let num_blocks = (input.len() + BLOCK_SIZE - 1) / BLOCK_SIZE;
        let mut compressed_blocks = Vec::with_capacity(num_blocks);

        // Process blocks sequentially for now (true parallelism would require thread safety)
        for i in 0..num_blocks {
            let start = i * BLOCK_SIZE;
            let end = (start + BLOCK_SIZE).min(input.len());
            let block = &input[start..end];

            let mut block_output = Vec::new();
            self.compress_sequential(block, &mut block_output)?;
            compressed_blocks.push(block_output);
        }

        // Combine compressed blocks
        output.clear();
        for block in compressed_blocks {
            output.extend_from_slice(&block);
        }

        Ok(())
    }
    
    /// Step 1: Find local match using sliding window
    fn find_local_match(&mut self, input: &[u8], pos: usize) -> Result<Option<LocalMatch>> {
        if pos >= input.len() {
            return Ok(None);
        }
        
        let remaining = &input[pos..];
        let max_length = remaining.len().min(self.config.local_config.max_match_length);
        
        self.local_matcher.find_match(remaining, self.config.max_local_probe_distance as usize, max_length)
    }
    
    /// Step 2: Find global match using dictionary
    fn find_global_match(&mut self, input: &[u8], pos: usize) -> Result<Option<crate::compression::dict_zip::matcher::Match>> {
        if pos >= input.len() {
            return Ok(None);
        }
        
        let remaining = &input[pos..];
        let max_length = remaining.len().min(256); // PA-Zip max pattern length
        
        self.dictionary.find_longest_match(remaining, 0, max_length)
    }
    
    /// Step 3: Calculate costs for each possible compression strategy
    fn calculate_strategy_costs(
        &self,
        _input: &[u8],
        _pos: usize,
        local_match: Option<LocalMatch>,
        global_match: Option<crate::compression::dict_zip::matcher::Match>,
    ) -> Result<Vec<(CompressionStrategy, CostAnalysis)>> {
        let mut strategies = Vec::new();
        
        // Always consider literal encoding
        let literal_strategy = CompressionStrategy::Literal { length: 1 };
        let literal_cost = self.calculate_literal_cost(1);
        strategies.push((literal_strategy, literal_cost));
        
        // Consider local match if available
        if let Some(local) = local_match {
            if let Some((strategy, cost)) = self.calculate_local_match_cost(local)? {
                strategies.push((strategy, cost));
            }
        }
        
        // Consider global match if available  
        if let Some(global) = global_match {
            if let Some((strategy, cost)) = self.calculate_global_match_cost(global)? {
                strategies.push((strategy, cost));
            }
        }
        
        Ok(strategies)
    }
    
    /// Calculate cost analysis for literal encoding
    fn calculate_literal_cost(&self, length: u32) -> CostAnalysis {
        let encoding_cost = self.config.literal_cost_bits * length;
        
        CostAnalysis {
            net_benefit: -(encoding_cost as i32),
            encoding_cost,
            access_cost: 0,
            total_cost: encoding_cost,
            match_length: length,
            efficiency: 0.0, // Literal encoding has no compression
        }
    }
    
    /// Calculate cost analysis for local match
    fn calculate_local_match_cost(&self, local_match: LocalMatch) -> Result<Option<(CompressionStrategy, CostAnalysis)>> {
        // Determine compression type based on distance and length
        let compression_type = match choose_best_compression_type(local_match.distance, local_match.length) {
            Some(ct) => ct,
            None => return Ok(None), // No suitable compression type found
        };
        
        // Calculate encoding cost
        let temp_match = Match::from_local_match(local_match.clone(), compression_type);
        let encoding_cost = calculate_encoding_cost(&temp_match);
        
        let total_cost = encoding_cost;
        let net_benefit = local_match.length as i32 * 8 - total_cost as i32; // 8 bits per byte saved
        
        let strategy = CompressionStrategy::Local {
            distance: local_match.distance as u32,
            length: local_match.length as u32,
            match_type: compression_type,
        };
        
        let cost_analysis = CostAnalysis {
            net_benefit,
            encoding_cost: encoding_cost as u32,
            access_cost: 0,
            total_cost: total_cost as u32,
            match_length: local_match.length as u32,
            efficiency: if local_match.length > 0 {
                (local_match.length as f64 * 8.0 - total_cost as f64) / (local_match.length as f64 * 8.0)
            } else {
                0.0
            },
        };
        
        Ok(Some((strategy, cost_analysis)))
    }
    
    /// Calculate cost analysis for global match
    fn calculate_global_match_cost(&self, global_match: crate::compression::dict_zip::matcher::Match) -> Result<Option<(CompressionStrategy, CostAnalysis)>> {
        // Global matches always use Global compression type
        let compression_type = CompressionType::Global;
        
        // Create match for encoding cost calculation
        let temp_match = Match::Global {
            dict_position: global_match.dict_position as u32,
            length: global_match.length as u16,
        };
        
        let encoding_cost = calculate_encoding_cost(&temp_match);
        let access_cost = self.config.global_access_cost;
        let total_cost = encoding_cost as u32 + access_cost;
        
        let net_benefit = global_match.length as i32 * 8 - total_cost as i32;
        
        let strategy = CompressionStrategy::Global {
            dict_offset: global_match.dict_position as u32,
            length: global_match.length as u32,
            match_type: compression_type,
        };
        
        let cost_analysis = CostAnalysis {
            net_benefit,
            encoding_cost: encoding_cost as u32,
            access_cost,
            total_cost,
            match_length: global_match.length as u32,
            efficiency: if global_match.length > 0 {
                (global_match.length as f64 * 8.0 - total_cost as f64) / (global_match.length as f64 * 8.0)
            } else {
                0.0
            },
        };
        
        Ok(Some((strategy, cost_analysis)))
    }
    
    /// Step 4: Select optimal compression strategy based on cost analysis
    fn select_optimal_strategy(
        &self,
        strategies: Vec<(CompressionStrategy, CostAnalysis)>,
    ) -> Result<CompressionStrategy> {
        if strategies.is_empty() {
            return Ok(CompressionStrategy::Literal { length: 1 });
        }
        
        let mut best_strategy = strategies[0].0;
        let mut best_benefit = strategies[0].1.net_benefit as f64;
        
        for (strategy, analysis) in strategies {
            let mut adjusted_benefit = analysis.net_benefit as f64;
            
            // Apply adaptive thresholds and biases
            match strategy {
                CompressionStrategy::Global { .. } => {
                    adjusted_benefit += self.adaptive_thresholds.global_bias;
                },
                CompressionStrategy::Local { .. } => {
                    adjusted_benefit -= self.adaptive_thresholds.global_bias * 0.5;
                },
                CompressionStrategy::Literal { .. } => {
                    adjusted_benefit -= self.adaptive_thresholds.literal_threshold;
                },
            }
            
            // Only accept if meets minimum benefit threshold
            if adjusted_benefit >= self.adaptive_thresholds.min_net_benefit && adjusted_benefit > best_benefit {
                best_strategy = strategy;
                best_benefit = adjusted_benefit;
            }
        }
        
        Ok(best_strategy)
    }
    
    /// Step 5: Apply selected compression strategy and encode result
    fn apply_compression_strategy(
        &mut self,
        input: &[u8],
        pos: usize,
        strategy: CompressionStrategy,
        output: &mut Vec<u8>,
    ) -> Result<usize> {
        match strategy {
            CompressionStrategy::Literal { length } => {
                // Encode literal: [type_byte=0] [length] [literal_data...]
                output.push(0); // Type byte for Literal
                output.push(length as u8); // Length byte
                
                let end_pos = (pos + length as usize).min(input.len());
                output.extend_from_slice(&input[pos..end_pos]); // Actual literal data
                Ok(end_pos - pos)
            },
            
            CompressionStrategy::Local { distance, length, match_type } => {
                // Encode local match based on match type
                let type_byte = match_type as u8;
                output.push(type_byte);
                
                match match_type {
                    CompressionType::RLE => {
                        // RLE: [type_byte=2] [byte_value] [length]
                        if pos < input.len() {
                            output.push(input[pos]); // The repeated byte value
                        } else {
                            output.push(0); // Fallback
                        }
                        output.push(length as u8);
                    },
                    CompressionType::NearShort => {
                        // NearShort: [type_byte=3] [distance] [length] 
                        output.push(distance as u8);
                        output.push(length as u8);
                    },
                    CompressionType::Far1Short => {
                        // Far1Short: [type_byte=4] [distance_2_bytes] [length]
                        output.extend_from_slice(&(distance as u16).to_le_bytes());
                        output.push(length as u8);
                    },
                    CompressionType::Far2Short => {
                        // Far2Short: [type_byte=5] [distance_4_bytes] [length]
                        output.extend_from_slice(&distance.to_le_bytes());
                        output.push(length as u8);
                    },
                    CompressionType::Far2Long => {
                        // Far2Long: [type_byte=6] [distance_2_bytes] [length_2_bytes]
                        output.extend_from_slice(&(distance as u16).to_le_bytes());
                        output.extend_from_slice(&(length as u16).to_le_bytes());
                    },
                    CompressionType::Far3Long => {
                        // Far3Long: [type_byte=7] [distance_4_bytes] [length_4_bytes]
                        output.extend_from_slice(&distance.to_le_bytes());
                        output.extend_from_slice(&length.to_le_bytes());
                    },
                    _ => {
                        // Fallback to literal for unsupported types
                        let type_byte_index = output.len() - 1;
                        output[type_byte_index] = 0; // Change type byte to literal
                        output.push(length as u8);
                        let end_pos = (pos + length as usize).min(input.len());
                        output.extend_from_slice(&input[pos..end_pos]);
                    }
                }
                Ok(length as usize)
            },
            
            CompressionStrategy::Global { dict_offset, length, match_type: _ } => {
                // Encode global match: [type_byte=1] [dict_offset_2_bytes] [length_2_bytes]
                output.push(1); // Type byte for Global
                output.extend_from_slice(&(dict_offset as u16).to_le_bytes()); // Dictionary offset (2 bytes)
                output.extend_from_slice(&(length as u16).to_le_bytes()); // Match length (2 bytes)
                Ok(length as usize)
            },
        }
    }
    
    /// Create appropriate Match from local match parameters
    fn create_local_match(&self, distance: u32, length: u32, match_type: CompressionType) -> Result<Match> {
        match match_type {
            CompressionType::RLE => {
                // For RLE, we need the repeated byte value from the input
                // For now, use a placeholder - this should be extracted from the actual match
                Ok(Match::RLE { 
                    byte_value: 0, // This should be the repeated byte from input
                    length: length.try_into().map_err(|_| ZiporaError::invalid_data("RLE length too large"))? 
                })
            },
            CompressionType::NearShort => Ok(Match::NearShort { 
                distance: distance.try_into().map_err(|_| ZiporaError::invalid_data("NearShort distance too large"))?, 
                length: length.try_into().map_err(|_| ZiporaError::invalid_data("NearShort length too large"))? 
            }),
            CompressionType::Far1Short => Ok(Match::Far1Short { 
                distance: distance.try_into().map_err(|_| ZiporaError::invalid_data("Far1Short distance too large"))?, 
                length: length.try_into().map_err(|_| ZiporaError::invalid_data("Far1Short length too large"))? 
            }),
            CompressionType::Far2Short => Ok(Match::Far2Short { 
                distance, 
                length: length.try_into().map_err(|_| ZiporaError::invalid_data("Far2Short length too large"))? 
            }),
            CompressionType::Far2Long => Ok(Match::Far2Long { 
                distance: distance.try_into().map_err(|_| ZiporaError::invalid_data("Far2Long distance too large"))?, 
                length: length.try_into().map_err(|_| ZiporaError::invalid_data("Far2Long length too large"))? 
            }),
            CompressionType::Far3Long => Ok(Match::Far3Long { distance, length }),
            _ => Err(ZiporaError::invalid_data("Invalid match type for local match")),
        }
    }
    
    /// Step 6: Update compression statistics
    fn update_statistics(&mut self, strategy: CompressionStrategy, _advance_length: usize) {
        match strategy {
            CompressionStrategy::Literal { length: _ } => {
                self.stats.literal_count += 1;
                self.stats.compression_type_usage[0] += 1; // Literal is type 0
            },
            
            CompressionStrategy::Local { length, match_type, .. } => {
                self.stats.local_matches += 1;
                self.stats.local_bytes_saved += length as u64;
                self.stats.compression_type_usage[match_type as usize] += 1;
            },
            
            CompressionStrategy::Global { length, .. } => {
                self.stats.global_matches += 1;
                self.stats.global_bytes_saved += length as u64;
                self.stats.compression_type_usage[CompressionType::Global as usize] += 1;
            },
        }
        
        // Track strategy switches
        if let Some(prev_strategy) = self.current_strategy {
            if std::mem::discriminant(&strategy) != std::mem::discriminant(&prev_strategy) {
                self.stats.strategy_switches += 1;
            }
        }
    }
    
    /// Step 7: Update adaptive thresholds based on compression efficiency
    fn update_adaptive_thresholds(&mut self, strategy: CompressionStrategy) {
        if !self.config.adaptive_thresholds {
            return;
        }
        
        // Calculate current compression efficiency
        let efficiency = if self.stats.bytes_processed > 0 {
            1.0 - (self.stats.bytes_output as f64 / self.stats.bytes_processed as f64)
        } else {
            0.0
        };
        
        self.adaptive_thresholds.update(efficiency, strategy, self.config.learning_rate);
        self.stats.threshold_adjustments += 1;
    }
    
    /// Get current compression statistics
    pub fn stats(&self) -> &CompressionStats {
        &self.stats
    }
    
    /// Get dictionary statistics
    pub fn dictionary_stats(&self) -> &MatchStats {
        self.dictionary.match_stats()
    }
    
    /// Get local matcher statistics
    pub fn local_matcher_stats(&self) -> &LocalMatcherStats {
        self.local_matcher.stats()
    }
    
    /// Get DFA cache statistics
    pub fn cache_stats(&self) -> Result<CacheStats> {
        Ok(self.dictionary.cache_stats())
    }
    
    /// Reset all statistics
    pub fn reset_stats(&mut self) {
        self.stats = CompressionStats::new();
        self.local_matcher.reset_stats();
    }
    
    /// Validate compressor configuration
    pub fn validate(&self) -> Result<()> {
        if self.config.min_net_benefit < 0 {
            return Err(ZiporaError::invalid_data("Minimum net benefit must be >= 0"));
        }
        
        if self.config.learning_rate < 0.0 || self.config.learning_rate > 1.0 {
            return Err(ZiporaError::invalid_data("Learning rate must be between 0.0 and 1.0"));
        }
        
        if self.config.literal_cost_bits == 0 {
            return Err(ZiporaError::invalid_data("Literal cost bits must be > 0"));
        }
        
        Ok(())
    }
}

/// Helper trait to convert between match types
trait MatchConversion {
    fn from_local_match(local: LocalMatch, compression_type: CompressionType) -> Self;
}

impl MatchConversion for Match {
    fn from_local_match(local: LocalMatch, compression_type: CompressionType) -> Self {
        match compression_type {
            CompressionType::RLE => Match::RLE { 
                byte_value: 0, // This should be the repeated byte
                length: local.length.try_into().unwrap_or(255) 
            },
            CompressionType::NearShort => Match::NearShort { 
                distance: local.distance.try_into().unwrap_or(255), 
                length: local.length.try_into().unwrap_or(255) 
            },
            CompressionType::Far1Short => Match::Far1Short { 
                distance: local.distance.try_into().unwrap_or(65535), 
                length: local.length.try_into().unwrap_or(255) 
            },
            CompressionType::Far2Short => Match::Far2Short { 
                distance: local.distance as u32, 
                length: local.length.try_into().unwrap_or(255) 
            },
            CompressionType::Far2Long => Match::Far2Long { 
                distance: local.distance.try_into().unwrap_or(65535), 
                length: local.length.try_into().unwrap_or(65535) 
            },
            CompressionType::Far3Long => Match::Far3Long { 
                distance: local.distance as u32, 
                length: local.length as u32 
            },
            _ => Match::Literal { length: local.length.try_into().unwrap_or(255) }, // Fallback
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::compression::dict_zip::{DictionaryBuilder, DictionaryBuilderConfig};
    use crate::memory::SecureMemoryPool;
    
    pub fn setup_test_compressor() -> Result<PaZipCompressor> {
        let training_data = b"the quick brown fox jumps over the lazy dog. the quick brown fox jumps again.";
        
        let dict_config = DictionaryBuilderConfig {
            target_dict_size: 2048,
            max_dict_size: 4096,
            validate_result: true,
            ..Default::default()
        };
        
        let builder = DictionaryBuilder::with_config(dict_config);
        let dictionary = builder.build(training_data)?;
        
        let config = PaZipCompressorConfig::balanced();
        let pool = SecureMemoryPool::new(SecurePoolConfig::new(4096, 1024, 8))?;
        
        PaZipCompressor::new(dictionary, config, pool)
    }
    
    #[test]
    fn test_compressor_creation() -> Result<()> {
        let compressor = setup_test_compressor()?;
        assert!(compressor.validate().is_ok());
        Ok(())
    }
    
    #[test]
    fn test_empty_input_compression() -> Result<()> {
        let mut compressor = setup_test_compressor()?;
        let input = b"";
        let mut output = Vec::new();
        
        let stats = compressor.compress(input, &mut output)?;
        assert_eq!(stats.bytes_processed, 0);
        assert_eq!(stats.bytes_output, 0);
        assert!(output.is_empty());
        
        Ok(())
    }
    
    #[test]
    fn test_small_input_compression() -> Result<()> {
        let mut compressor = setup_test_compressor()?;
        let input = b"the quick brown fox";
        let mut output = Vec::new();
        
        let stats = compressor.compress(input, &mut output)?;
        assert_eq!(stats.bytes_processed, input.len() as u64);
        // Note: Small inputs may not produce output if below compression threshold
        assert!(stats.compression_ratio <= 1.0);
        
        Ok(())
    }
    
    #[test]
    fn test_compression_with_repetitive_data() -> Result<()> {
        let mut compressor = setup_test_compressor()?;
        let input = b"the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog.";
        let mut output = Vec::new();
        
        let stats = compressor.compress(input, &mut output)?;
        assert!(stats.global_matches > 0 || stats.local_matches > 0);
        assert!(stats.compression_ratio < 1.0); // Should achieve some compression
        
        Ok(())
    }
    
    #[test]
    fn test_configuration_presets() {
        let fast = PaZipCompressorConfig::fast_compression();
        let high = PaZipCompressorConfig::high_compression();
        let balanced = PaZipCompressorConfig::balanced();
        let realtime = PaZipCompressorConfig::realtime();
        
        assert!(fast.max_local_probe_distance < high.max_local_probe_distance);
        assert!(realtime.max_global_probe_distance < balanced.max_global_probe_distance);
        assert!(!realtime.adaptive_thresholds);
        assert!(high.collect_detailed_stats);
        assert!(!fast.collect_detailed_stats);
    }
    
    #[test]
    fn test_adaptive_thresholds() {
        let mut thresholds = AdaptiveThresholds::default();
        let initial_bias = thresholds.global_bias;
        
        // Simulate successful global match
        thresholds.update(0.9, CompressionStrategy::Global { 
            dict_offset: 0, 
            length: 10, 
            match_type: CompressionType::Global 
        }, 0.1);
        
        assert!(thresholds.global_bias > initial_bias);
        assert_eq!(thresholds.update_count, 1);
    }
    
    #[test]
    fn test_statistics_tracking() -> Result<()> {
        let mut compressor = setup_test_compressor()?;
        let input = b"test data for statistics tracking";
        let mut output = Vec::new();
        
        let initial_stats = compressor.stats().clone();
        compressor.compress(input, &mut output)?;
        let final_stats = compressor.stats();
        
        assert!(final_stats.bytes_processed > initial_stats.bytes_processed);
        assert!(final_stats.compression_time > Duration::from_nanos(0));
        
        Ok(())
    }
    
    #[test]
    fn test_compression_decompression_roundtrip() -> Result<()> {
        let mut compressor = setup_test_compressor()?;
        let original_input = b"The quick brown fox jumps over the lazy dog";
        let mut compressed_output = Vec::new();
        
        // Compress the input
        let _stats = compressor.compress(original_input, &mut compressed_output)?;
        
        // Verify that we got some compressed output
        assert!(!compressed_output.is_empty(), "Compression should produce output");
        
        // Decompress the output
        let mut decompressed_output = Vec::new();
        compressor.decompress(&compressed_output, &mut decompressed_output)?;
        
        // Verify that decompression produces the original input
        assert_eq!(
            original_input, 
            &decompressed_output[..], 
            "Decompressed output should match original input.\nOriginal: {:?}\nDecompressed: {:?}",
            std::str::from_utf8(original_input).unwrap_or("(invalid UTF-8)"),
            std::str::from_utf8(&decompressed_output).unwrap_or("(invalid UTF-8)")
        );
        
        Ok(())
    }
    
    #[test]
    fn test_simple_literal_compression() -> Result<()> {
        let mut compressor = setup_test_compressor()?;
        let simple_input = b"hello world";
        let mut compressed_output = Vec::new();
        
        // Compress the input
        let _stats = compressor.compress(simple_input, &mut compressed_output)?;
        
        // Verify that we got some compressed output
        assert!(!compressed_output.is_empty(), "Compression should produce output");
        println!("Original: {:?}", simple_input);
        println!("Compressed: {:?}", compressed_output);
        
        // Decompress the output
        let mut decompressed_output = Vec::new();
        compressor.decompress(&compressed_output, &mut decompressed_output)?;
        
        println!("Decompressed: {:?}", decompressed_output);
        
        // Verify that decompression produces the original input
        assert_eq!(
            simple_input, 
            &decompressed_output[..], 
            "Simple literal compression/decompression failed"
        );
        
        Ok(())
    }
    
    #[test]
    fn test_cost_analysis() -> Result<()> {
        let compressor = setup_test_compressor()?;
        
        // Test literal cost calculation
        let literal_cost = compressor.calculate_literal_cost(5);
        assert_eq!(literal_cost.match_length, 5);
        assert_eq!(literal_cost.encoding_cost, compressor.config.literal_cost_bits * 5);
        assert_eq!(literal_cost.efficiency, 0.0);
        
        Ok(())
    }
    
    #[test]
    fn test_strategy_selection() -> Result<()> {
        let compressor = setup_test_compressor()?;
        
        // Create test strategies with different benefits
        let strategies = vec![
            (CompressionStrategy::Literal { length: 1 }, CostAnalysis {
                net_benefit: -8,
                encoding_cost: 8,
                access_cost: 0,
                total_cost: 8,
                match_length: 1,
                efficiency: 0.0,
            }),
            (CompressionStrategy::Local { 
                distance: 10, 
                length: 8, 
                match_type: CompressionType::NearShort 
            }, CostAnalysis {
                net_benefit: 50,
                encoding_cost: 14,
                access_cost: 0,
                total_cost: 14,
                match_length: 8,
                efficiency: 0.7,
            }),
        ];
        
        let selected = compressor.select_optimal_strategy(strategies)?;
        
        // Should select the strategy with higher net benefit
        match selected {
            CompressionStrategy::Local { length: 8, .. } => {},
            _ => panic!("Expected local strategy to be selected"),
        }
        
        Ok(())
    }
    
    #[test]
    fn test_validation() -> Result<()> {
        let compressor = setup_test_compressor()?;
        assert!(compressor.validate().is_ok());
        
        // Test invalid configuration
        let mut invalid_config = PaZipCompressorConfig::default();
        invalid_config.min_net_benefit = -10;
        invalid_config.learning_rate = 2.0;
        
        let training_data = b"test";
        let builder = DictionaryBuilder::default();
        let dictionary = builder.build(training_data)?;
        let pool = SecureMemoryPool::new(SecurePoolConfig::new(4096, 1024, 8))?;
        
        let invalid_compressor = PaZipCompressor::new(dictionary, invalid_config, pool)?;
        assert!(invalid_compressor.validate().is_err());
        
        Ok(())
    }
}

#[cfg(test)]
mod bench_tests {
    use super::*;
    use std::time::Instant;
    
    #[test]
    fn bench_compression_speed() -> Result<()> {
        let mut compressor = setup_test_compressor()?;
        
        // Create larger test data
        let test_data = "the quick brown fox jumps over the lazy dog. ".repeat(1000);
        let input = test_data.as_bytes();
        let mut output = Vec::new();
        
        let start = Instant::now();
        let stats = compressor.compress(input, &mut output)?;
        let elapsed = start.elapsed();
        
        let speed_mbps = (input.len() as f64 / 1024.0 / 1024.0) / elapsed.as_secs_f64();
        
        println!("Compression speed: {:.2} MB/s", speed_mbps);
        println!("Compression ratio: {:.3}", stats.compression_ratio);
        println!("Global matches: {}, Local matches: {}", stats.global_matches, stats.local_matches);
        
        assert!(speed_mbps > 0.1); // Should compress at least 0.1 MB/s
        assert!(stats.compression_ratio < 1.0); // Should achieve some compression
        
        Ok(())
    }
    
    use super::tests::setup_test_compressor;
}

#[cfg(test)]
mod reference_compliance_tests {
    use super::*;
    use crate::compression::dict_zip::{DictionaryBuilder, DictionaryBuilderConfig};
    use crate::memory::SecureMemoryPool;
    
    fn setup_reference_compliant_compressor() -> Result<PaZipCompressor> {
        let training_data = b"The quick brown fox jumps over the lazy dog.";
        
        // Use small dictionary size for tests
        let dict_config = DictionaryBuilderConfig {
            target_dict_size: 1024, // 1KB
            max_dict_size: 4096, // 4KB
            validate_result: false, // Skip validation for speed
            sample_ratio: 1.0, // Use full data
            ..Default::default()
        };
        
        let builder = DictionaryBuilder::with_config(dict_config);
        let dictionary = builder.build(training_data)?;
        
        // Use reference-compliant configuration
        let config = PaZipCompressorConfig::reference_compliant();
        let pool = SecureMemoryPool::new(SecurePoolConfig::new(4096, 1024, 8))?;
        
        PaZipCompressor::new(dictionary, config, pool)
    }
    
    #[test]
    fn test_reference_compliant_compression_basic() -> Result<()> {
        let mut compressor = setup_reference_compliant_compressor()?;
        
        // Test simple text compression
        let input = b"The quick brown fox";
        let mut output = Vec::new();
        
        let stats = compressor.compress(input, &mut output)?;
        
        // Verify output is not empty and reasonable
        assert!(!output.is_empty(), "Compression should produce output");
        assert!(output.len() <= input.len() + 20, "Output should not be much larger than input");
        
        // Verify statistics are reasonable (allow slight differences due to processing granularity)
        assert!(stats.bytes_processed > 0, "Should process some bytes");
        assert!(stats.bytes_processed <= input.len() as u64, "Should not process more than input");
        assert!(stats.bytes_output > 0, "Should produce some output");
        assert!(stats.compression_ratio > 0.0);
        
        // Verify output format has reasonable structure
        // Reference compression should produce binary data
        assert!(output.len() >= 4, "Output should have at least some encoding overhead");
        
        // Print compression details for validation
        println!("Reference compression: {} -> {} bytes (ratio: {:.3})", 
                 input.len(), output.len(), stats.compression_ratio);
        println!("Output format validation: first 10 bytes = {:?}", 
                 &output[..output.len().min(10)]);
        
        Ok(())
    }
    
    #[test]
    fn test_reference_compliant_compression_patterns() -> Result<()> {
        let mut compressor = setup_reference_compliant_compressor()?;
        
        // Test various data patterns to ensure reference compliance
        let test_cases = vec![
            b"abcdefghijklmnopqrstuvwxyz".to_vec(),  // alphabet
            b"aaaaaaaaaaaaaaaaaaaaaaaa".to_vec(),    // repeated pattern
            b"The quick brown fox jumps over the lazy dog".to_vec(), // dictionary data
            b"1234567890".repeat(5),                 // numeric pattern
            vec![0u8; 64],                          // null bytes
            (0..=255u8).collect(),                  // full byte range
        ];
        
        for (i, input) in test_cases.iter().enumerate() {
            let mut output = Vec::new();
            let stats = compressor.compress(input, &mut output)?;
            
            // Verify basic compression properties
            assert!(!output.is_empty(), "Test case {} should produce output", i);
            
            // Verify output looks reasonable for reference compression
            assert!(output.len() >= 2, "Test case {} should have minimum encoding overhead", i);
            
            println!("Test case {}: input len={}, output len={}, ratio={:.3}", 
                     i, input.len(), output.len(), stats.compression_ratio);
        }
        
        Ok(())
    }
    
    #[test]
    fn test_reference_compliant_compression_type_usage() -> Result<()> {
        // Use the working setup function that stays within dictionary size limits
        let mut compressor = setup_reference_compliant_compressor()?;
        
        // Test that reference-compliant compression uses expected compression types
        let input = b"The quick brown fox jumps over the lazy dog. The quick brown fox.";
        let mut output = Vec::new();
        
        let stats = compressor.compress(input, &mut output)?;
        
        // Verify we get reasonable statistics
        assert!(stats.bytes_processed > 0);
        assert!(stats.bytes_output > 0);
        assert!(stats.compression_ratio > 0.0);
        
        // For very small dictionaries, matches may not be found - this is acceptable
        // Focus on validating that compression works and produces output
        assert!(stats.bytes_processed > 0, "Should process some bytes");
        assert!(stats.bytes_output > 0, "Should produce some output");
        
        // At minimum, compression should use some encoding strategy (even if all stats are 0 due to small dictionary)
        println!("Compression stats: global={}, local={}, literals={}", 
                 stats.global_matches, stats.local_matches, stats.literal_count);
        
        Ok(())
    }
    
    #[test]
    fn test_reference_vs_legacy_compression() -> Result<()> {
        // Use the working setup to create base dictionary
        let base_compressor = setup_reference_compliant_compressor()?;
        
        // Get the working dictionary from the base compressor 
        let dictionary1 = base_compressor.dictionary.clone();
        let dictionary2 = dictionary1.clone();
        
        let pool = SecureMemoryPool::new(SecurePoolConfig::new(4096, 1024, 8))?;
        
        // Reference-compliant compressor
        let ref_config = PaZipCompressorConfig::reference_compliant();
        let mut ref_compressor = PaZipCompressor::new(dictionary1, ref_config, pool.clone())?;
        
        // Legacy compressor
        let legacy_config = PaZipCompressorConfig::default(); // use_reference_encoding = false
        let mut legacy_compressor = PaZipCompressor::new(dictionary2, legacy_config, pool)?;
        
        let input = b"The quick brown fox jumps over the lazy dog";
        
        // Compress with both
        let mut ref_output = Vec::new();
        let ref_stats = ref_compressor.compress(input, &mut ref_output)?;
        
        let mut legacy_output = Vec::new();
        let legacy_stats = legacy_compressor.compress(input, &mut legacy_output)?;
        
        // Both should compress successfully
        assert!(!ref_output.is_empty());
        assert!(!legacy_output.is_empty());
        
        // Both should produce reasonable output
        assert!(ref_output.len() >= 4, "Reference compression should have minimum overhead");
        assert!(legacy_output.len() >= 4, "Legacy compression should have minimum overhead");
        
        println!("Reference compression: {} -> {} bytes (ratio: {:.3})", 
                 input.len(), ref_output.len(), ref_stats.compression_ratio);
        println!("Legacy compression: {} -> {} bytes (ratio: {:.3})", 
                 input.len(), legacy_output.len(), legacy_stats.compression_ratio);
        
        // The outputs may be different (which is expected) but both should work
        assert!(ref_stats.compression_ratio > 0.0);
        assert!(legacy_stats.compression_ratio > 0.0);
        
        Ok(())
    }
    
    #[test]
    fn test_reference_compliant_edge_cases() -> Result<()> {
        let mut compressor = setup_reference_compliant_compressor()?;
        
        // Test edge cases that might reveal encoding issues
        let edge_cases = vec![
            vec![0u8],                  // Single byte
            vec![255u8],                // Max byte value
            vec![0u8, 255u8],           // Min/max pair
            b"A".to_vec(),              // Single character
            b"AA".to_vec(),             // Two identical characters
            b"AB".to_vec(),             // Two different characters
            b"ABC".to_vec(),            // Three characters
            b"ABCD".to_vec(),           // Four characters (min pattern length)
        ];
        
        for (i, input) in edge_cases.iter().enumerate() {
            let mut output = Vec::new();
            let _stats = compressor.compress(input, &mut output)?;
            
            assert!(!output.is_empty(), "Edge case {} should produce output", i);
            
            // Verify output format is reasonable
            assert!(output.len() >= 2, "Edge case {} should have minimum encoding overhead", i);
        }
        
        Ok(())
    }
    
    #[test]
    fn test_reference_compliant_suffix_array_vs_hash_table() -> Result<()> {
        // Use the working setup to create base dictionary
        let base_compressor = setup_reference_compliant_compressor()?;
        
        // Get the working dictionary from the base compressor 
        let dictionary1 = base_compressor.dictionary.clone();
        let dictionary2 = dictionary1.clone();
        
        let pool = SecureMemoryPool::new(SecurePoolConfig::new(4096, 1024, 8))?;
        
        // Reference-compliant with suffix array
        let mut sa_config = PaZipCompressorConfig::reference_compliant();
        sa_config.use_suffix_array_local_match = true;
        let mut sa_compressor = PaZipCompressor::new(dictionary1, sa_config, pool.clone())?;
        
        // Reference-compliant with hash table
        let mut ht_config = PaZipCompressorConfig::reference_compliant();
        ht_config.use_suffix_array_local_match = false;
        let mut ht_compressor = PaZipCompressor::new(dictionary2, ht_config, pool)?;
        
        let input = b"The quick brown fox jumps";
        
        // Compress with both approaches
        let mut sa_output = Vec::new();
        let sa_stats = sa_compressor.compress(input, &mut sa_output)?;
        
        let mut ht_output = Vec::new();
        let ht_stats = ht_compressor.compress(input, &mut ht_output)?;
        
        // Both should compress successfully
        assert!(!sa_output.is_empty());
        assert!(!ht_output.is_empty());
        
        // Both should produce reasonable output
        assert!(sa_output.len() >= 4, "Suffix array compression should have minimum overhead");
        assert!(ht_output.len() >= 4, "Hash table compression should have minimum overhead");
        
        println!("Suffix array compression: {} -> {} bytes (ratio: {:.3})", 
                 input.len(), sa_output.len(), sa_stats.compression_ratio);
        println!("Hash table compression: {} -> {} bytes (ratio: {:.3})", 
                 input.len(), ht_output.len(), ht_stats.compression_ratio);
        
        Ok(())
    }
}