ipfrs-storage 0.2.0

Storage backends and block management for IPFRS content-addressed system
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
//! `StorageCompressionPipeline` — configurable multi-stage compression pipeline.
//!
//! Provides a production-quality, pure-Rust compression pipeline with inline
//! implementations of RLE, LZ77-style sliding window, Delta encoding, and XOR
//! transform. No external compression crates are used.
//!
//! # Quick Start
//! ```rust
//! use ipfrs_storage::storage_compression_pipeline::{
//!     ScpStorageCompressionPipeline, ScpPipelineConfig, CompressionStage,
//!     ScpCompressionAlgorithm,
//! };
//!
//! let config = ScpPipelineConfig {
//!     stages: vec![CompressionStage {
//!         algorithm: ScpCompressionAlgorithm::Rle,
//!         enabled: true,
//!         min_size_bytes: 8,
//!     }],
//!     max_input_size: 1 << 20,
//!     enable_checksum: true,
//! };
//! let pipeline = ScpStorageCompressionPipeline::new(config);
//! let data = b"AAAAAABBBCCCCC";
//! let block = pipeline.compress(data).unwrap();
//! let recovered = pipeline.decompress(&block).unwrap();
//! assert_eq!(recovered, data);
//! ```

use parking_lot::Mutex;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

// ─────────────────────────────────────────────────────────────────────────────
// FNV-1a 64-bit checksum
// ─────────────────────────────────────────────────────────────────────────────

/// Compute a FNV-1a 64-bit hash of `data`.
pub fn fnv1a_64(data: &[u8]) -> u64 {
    let mut h: u64 = 14_695_981_039_346_656_037;
    for &b in data {
        h ^= b as u64;
        h = h.wrapping_mul(1_099_511_628_211);
    }
    h
}

// ─────────────────────────────────────────────────────────────────────────────
// CompressionAlgorithm
// ─────────────────────────────────────────────────────────────────────────────

/// Compression/transform algorithm used by a pipeline stage.
///
/// All algorithms are implemented in pure Rust with no external dependencies.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScpCompressionAlgorithm {
    /// Identity pass-through — data is stored unchanged.
    None,
    /// Run-Length Encoding: encodes consecutive repeated bytes as
    /// `(count: u8, byte: u8)` pairs. Count is clamped to 255.
    Rle,
    /// LZ77-style sliding-window compressor.
    ///
    /// Emits either a literal token (`0x00, byte`) or a back-reference token
    /// (`0x01, offset_lo, offset_hi, length`) when a repeated sequence of
    /// length ≥ 3 is found in the sliding window.
    Lz77 {
        /// Look-back window size in bytes (e.g. 4096).
        window_size: usize,
    },
    /// Delta encoding: each byte is replaced by its difference from the
    /// previous byte. The first byte is XOR-ed with `base_value`.
    Delta {
        /// Seed value XOR-ed with the first byte before differencing.
        base_value: u8,
    },
    /// XOR-cipher transform: each byte is XOR-ed with the cycling key.
    ///
    /// Useful as a lightweight obfuscation or entropy-normalization pass
    /// before a heavier compressor.
    Xor {
        /// Key bytes cycled over the input.
        key: Vec<u8>,
    },
}

impl ScpCompressionAlgorithm {
    /// Return a stable, human-readable name for this algorithm.
    pub fn name(&self) -> String {
        match self {
            Self::None => "none".to_string(),
            Self::Rle => "rle".to_string(),
            Self::Lz77 { window_size } => format!("lz77({})", window_size),
            Self::Delta { base_value } => format!("delta({})", base_value),
            Self::Xor { key } => format!("xor({})", key.len()),
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// CompressionStage
// ─────────────────────────────────────────────────────────────────────────────

/// A single stage in the compression pipeline.
#[derive(Debug, Clone)]
pub struct CompressionStage {
    /// Algorithm to apply in this stage.
    pub algorithm: ScpCompressionAlgorithm,
    /// If `false`, this stage is skipped entirely.
    pub enabled: bool,
    /// Minimum data size in bytes required to apply this stage.
    /// If the current data size is smaller, the stage is skipped.
    pub min_size_bytes: usize,
}

// ─────────────────────────────────────────────────────────────────────────────
// PipelineConfig
// ─────────────────────────────────────────────────────────────────────────────

/// Configuration for a [`ScpStorageCompressionPipeline`].
#[derive(Debug, Clone)]
pub struct ScpPipelineConfig {
    /// Ordered list of compression stages.
    pub stages: Vec<CompressionStage>,
    /// Maximum accepted input size in bytes. Returns
    /// [`ScpPipelineError::InputTooLarge`] if exceeded.
    pub max_input_size: usize,
    /// If `true`, append an FNV-1a-64 checksum to every compressed block
    /// and verify it on decompression.
    pub enable_checksum: bool,
}

impl Default for ScpPipelineConfig {
    fn default() -> Self {
        Self {
            stages: vec![
                CompressionStage {
                    algorithm: ScpCompressionAlgorithm::Rle,
                    enabled: true,
                    min_size_bytes: 64,
                },
                CompressionStage {
                    algorithm: ScpCompressionAlgorithm::Lz77 { window_size: 4096 },
                    enabled: true,
                    min_size_bytes: 128,
                },
            ],
            max_input_size: 64 * 1024 * 1024, // 64 MiB
            enable_checksum: true,
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// CompressedBlock
// ─────────────────────────────────────────────────────────────────────────────

/// A compressed data block produced by [`ScpStorageCompressionPipeline::compress`].
#[derive(Debug, Clone)]
pub struct CompressedBlock {
    /// Original (uncompressed) size in bytes.
    pub original_size: usize,
    /// Compressed size in bytes (length of `data`).
    pub compressed_size: usize,
    /// Names of the stages that were actually applied (in order).
    pub stages_applied: Vec<String>,
    /// The compressed payload.
    pub data: Vec<u8>,
    /// FNV-1a-64 checksum of `data` (0 when checksum is disabled).
    pub checksum: u64,
    /// `original_size / compressed_size`; values > 1.0 mean compression won.
    pub compression_ratio: f64,
}

// ─────────────────────────────────────────────────────────────────────────────
// PipelineStats
// ─────────────────────────────────────────────────────────────────────────────

/// Aggregate statistics for a [`ScpStorageCompressionPipeline`] instance.
#[derive(Debug, Clone, Default)]
pub struct ScpPipelineStats {
    /// Total number of blocks compressed so far.
    pub total_blocks: u64,
    /// Sum of all original input sizes, in bytes.
    pub total_input_bytes: u64,
    /// Sum of all compressed output sizes, in bytes.
    pub total_output_bytes: u64,
    /// Rolling average compression ratio across all blocks.
    pub avg_compression_ratio: f64,
    /// Per-stage rolling average ratio: `(stage_name, avg_ratio)`.
    pub stage_stats: Vec<(String, f64)>,
}

// ─────────────────────────────────────────────────────────────────────────────
// PipelineError
// ─────────────────────────────────────────────────────────────────────────────

/// Errors that can arise during compression or decompression.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum ScpPipelineError {
    /// Input data exceeds [`ScpPipelineConfig::max_input_size`].
    #[error("input too large: {0} bytes")]
    InputTooLarge(usize),
    /// A decompression stage produced invalid output.
    #[error("decompression failed: {0}")]
    DecompressionFailed(String),
    /// Checksum stored in the block does not match what was computed.
    #[error("checksum mismatch: expected {expected:#x}, got {got:#x}")]
    ChecksumMismatch {
        /// Expected (stored) checksum value.
        expected: u64,
        /// Checksum computed from the actual data.
        got: u64,
    },
    /// Pipeline configuration is invalid.
    #[error("invalid configuration: {0}")]
    InvalidConfiguration(String),
    /// An algorithm-level error occurred.
    #[error("algorithm error: {0}")]
    AlgorithmError(String),
}

// ─────────────────────────────────────────────────────────────────────────────
// Inline algorithm implementations
// ─────────────────────────────────────────────────────────────────────────────

/// RLE encode: consecutive repeated bytes are emitted as `(count: u8, byte: u8)`.
///
/// Count is clamped to 255 so a run longer than 255 bytes is split.
pub fn rle_encode(data: &[u8]) -> Vec<u8> {
    if data.is_empty() {
        return Vec::new();
    }
    let mut out = Vec::with_capacity(data.len());
    let mut i = 0;
    while i < data.len() {
        let byte = data[i];
        let mut count: u8 = 1;
        // Extend run, clamping at 255
        while i + (count as usize) < data.len() && data[i + (count as usize)] == byte && count < 255
        {
            count += 1;
        }
        out.push(count);
        out.push(byte);
        i += count as usize;
    }
    out
}

/// RLE decode: parse `(count, byte)` pairs and reconstruct the original data.
pub fn rle_decode(data: &[u8]) -> Result<Vec<u8>, ScpPipelineError> {
    if !data.len().is_multiple_of(2) {
        return Err(ScpPipelineError::DecompressionFailed(
            "RLE stream has odd length".to_string(),
        ));
    }
    let mut out = Vec::with_capacity(data.len() * 2);
    let mut i = 0;
    while i + 1 < data.len() {
        let count = data[i] as usize;
        let byte = data[i + 1];
        if count == 0 {
            return Err(ScpPipelineError::DecompressionFailed(
                "RLE stream contains zero-count run".to_string(),
            ));
        }
        for _ in 0..count {
            out.push(byte);
        }
        i += 2;
    }
    Ok(out)
}

// ── LZ77 token format ────────────────────────────────────────────────────────
// Literal:     0x00, <byte>
// Back-ref:    0x01, <offset_lo>, <offset_hi>, <length>
//              offset is 1-based distance into the look-back window (little-endian u16)
//              length is the match length (u8, value >= 3)
// ─────────────────────────────────────────────────────────────────────────────

const LZ77_FLAG_LITERAL: u8 = 0x00;
const LZ77_FLAG_MATCH: u8 = 0x01;
const LZ77_MIN_MATCH: usize = 3;

/// LZ77-style encode using a sliding look-back window of `window_size` bytes.
///
/// Emits either a literal token or a back-reference token for each input position.
pub fn lz77_encode(data: &[u8], window_size: usize) -> Vec<u8> {
    if data.is_empty() {
        return Vec::new();
    }
    let effective_window = window_size.min(u16::MAX as usize);
    // Worst case: every byte becomes a literal token (2 bytes each)
    let mut out = Vec::with_capacity(data.len() * 2);
    let mut pos = 0;

    while pos < data.len() {
        let window_start = pos.saturating_sub(effective_window);
        let lookahead_end = (pos + 258).min(data.len()); // max match len fits in u8 + 3

        // Search for the longest match in the window
        let mut best_offset: usize = 0;
        let mut best_length: usize = 0;

        // Only search if there is a window to look back into
        if window_start < pos {
            let lookahead = &data[pos..lookahead_end];
            let lookahead_max = lookahead.len();

            for start in window_start..pos {
                // How many bytes match starting at `start` vs `pos`?
                let max_len = (pos - start).min(lookahead_max); // prevent overlap copy past `pos`
                let max_len = max_len.min(255 + LZ77_MIN_MATCH - 1); // fits in u8 after subtract
                let mut length = 0;
                while length < max_len && data[start + length] == lookahead[length] {
                    length += 1;
                }
                if length > best_length {
                    best_length = length;
                    best_offset = pos - start; // 1-based distance
                }
            }
        }

        if best_length >= LZ77_MIN_MATCH && best_offset <= u16::MAX as usize {
            // Emit back-reference
            let stored_len = (best_length - LZ77_MIN_MATCH) as u8; // stored as (len - 3)
            let offset_u16 = best_offset as u16;
            out.push(LZ77_FLAG_MATCH);
            out.push((offset_u16 & 0xFF) as u8);
            out.push((offset_u16 >> 8) as u8);
            out.push(stored_len);
            pos += best_length;
        } else {
            // Emit literal
            out.push(LZ77_FLAG_LITERAL);
            out.push(data[pos]);
            pos += 1;
        }
    }
    out
}

/// LZ77-style decode: reconstruct original data from literal/back-reference tokens.
pub fn lz77_decode(data: &[u8]) -> Result<Vec<u8>, ScpPipelineError> {
    let mut out: Vec<u8> = Vec::with_capacity(data.len() * 2);
    let mut i = 0;

    while i < data.len() {
        let flag = data[i];
        i += 1;

        match flag {
            LZ77_FLAG_LITERAL => {
                if i >= data.len() {
                    return Err(ScpPipelineError::DecompressionFailed(
                        "LZ77 stream truncated after literal flag".to_string(),
                    ));
                }
                out.push(data[i]);
                i += 1;
            }
            LZ77_FLAG_MATCH => {
                if i + 2 >= data.len() {
                    return Err(ScpPipelineError::DecompressionFailed(
                        "LZ77 stream truncated inside back-reference".to_string(),
                    ));
                }
                let offset_lo = data[i] as usize;
                let offset_hi = data[i + 1] as usize;
                let stored_len = data[i + 2] as usize;
                i += 3;

                let offset = offset_lo | (offset_hi << 8);
                let length = stored_len + LZ77_MIN_MATCH;

                if offset == 0 {
                    return Err(ScpPipelineError::DecompressionFailed(
                        "LZ77 back-reference has zero offset".to_string(),
                    ));
                }
                if offset > out.len() {
                    return Err(ScpPipelineError::DecompressionFailed(format!(
                        "LZ77 back-reference offset {} > output length {}",
                        offset,
                        out.len()
                    )));
                }

                // Copy byte-by-byte to handle overlapping runs correctly
                let copy_start = out.len() - offset;
                for j in 0..length {
                    let byte = out[copy_start + (j % offset)];
                    out.push(byte);
                }
            }
            other => {
                return Err(ScpPipelineError::DecompressionFailed(format!(
                    "LZ77 unknown flag byte: {:#x}",
                    other
                )));
            }
        }
    }
    Ok(out)
}

/// Delta encode: replace each byte with its difference from the previous byte.
///
/// The first byte is XOR-ed with `base` before the run starts (so `base=0`
/// leaves the first byte unchanged).
pub fn delta_encode(data: &[u8], base: u8) -> Vec<u8> {
    if data.is_empty() {
        return Vec::new();
    }
    let mut out = Vec::with_capacity(data.len());
    let first = data[0].wrapping_sub(base);
    out.push(first);
    let mut prev = data[0];
    for &b in &data[1..] {
        out.push(b.wrapping_sub(prev));
        prev = b;
    }
    out
}

/// Delta decode: reconstruct original data from delta-encoded stream.
pub fn delta_decode(data: &[u8], base: u8) -> Vec<u8> {
    if data.is_empty() {
        return Vec::new();
    }
    let mut out = Vec::with_capacity(data.len());
    let first = data[0].wrapping_add(base);
    out.push(first);
    let mut prev = first;
    for &d in &data[1..] {
        let b = d.wrapping_add(prev);
        out.push(b);
        prev = b;
    }
    out
}

/// XOR-cipher transform: each input byte is XOR-ed with the cycling key.
///
/// Passing an empty key returns the data unchanged.
pub fn xor_transform(data: &[u8], key: &[u8]) -> Vec<u8> {
    if key.is_empty() {
        return data.to_vec();
    }
    data.iter()
        .enumerate()
        .map(|(i, &b)| b ^ key[i % key.len()])
        .collect()
}

// ─────────────────────────────────────────────────────────────────────────────
// Internal stage apply / unapply helpers
// ─────────────────────────────────────────────────────────────────────────────

fn apply_algorithm(
    data: &[u8],
    algo: &ScpCompressionAlgorithm,
) -> Result<Vec<u8>, ScpPipelineError> {
    match algo {
        ScpCompressionAlgorithm::None => Ok(data.to_vec()),
        ScpCompressionAlgorithm::Rle => Ok(rle_encode(data)),
        ScpCompressionAlgorithm::Lz77 { window_size } => Ok(lz77_encode(data, *window_size)),
        ScpCompressionAlgorithm::Delta { base_value } => Ok(delta_encode(data, *base_value)),
        ScpCompressionAlgorithm::Xor { key } => {
            if key.is_empty() {
                return Err(ScpPipelineError::InvalidConfiguration(
                    "XOR key must not be empty".to_string(),
                ));
            }
            Ok(xor_transform(data, key))
        }
    }
}

fn unapply_algorithm(data: &[u8], algo_name: &str) -> Result<Vec<u8>, ScpPipelineError> {
    // Parse the name produced by ScpCompressionAlgorithm::name()
    if algo_name == "none" {
        return Ok(data.to_vec());
    }
    if algo_name == "rle" {
        return rle_decode(data);
    }
    if algo_name.starts_with("lz77(") {
        return lz77_decode(data);
    }
    if algo_name.starts_with("delta(") {
        // extract base value from "delta(<n>)"
        let inner = algo_name.trim_start_matches("delta(").trim_end_matches(')');
        let base: u8 = inner.parse().map_err(|_| {
            ScpPipelineError::DecompressionFailed(format!(
                "Cannot parse delta base from stage name: {}",
                algo_name
            ))
        })?;
        return Ok(delta_decode(data, base));
    }
    if algo_name.starts_with("xor(") {
        // We cannot recover the key from the name alone, so we embed the key in the name.
        // Format stored in stages_applied: "xor(<hex_key>)"
        let inner = algo_name.trim_start_matches("xor(").trim_end_matches(')');
        let key = hex::decode(inner).map_err(|_| {
            ScpPipelineError::DecompressionFailed(format!(
                "Cannot decode XOR key from stage name: {}",
                algo_name
            ))
        })?;
        return Ok(xor_transform(data, &key));
    }
    Err(ScpPipelineError::DecompressionFailed(format!(
        "Unknown stage name in stages_applied: {}",
        algo_name
    )))
}

/// Produce the canonical stage name for storing in `CompressedBlock::stages_applied`.
///
/// For XOR, embed the hex-encoded key so that decompression can recover it.
fn stage_name_for_storage(algo: &ScpCompressionAlgorithm) -> String {
    match algo {
        ScpCompressionAlgorithm::Xor { key } => format!("xor({})", hex::encode(key)),
        other => other.name(),
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Internal mutable stats accumulator
// ─────────────────────────────────────────────────────────────────────────────

#[derive(Debug, Default)]
struct StatsAccumulator {
    total_blocks: u64,
    total_input_bytes: u64,
    total_output_bytes: u64,
    // sum of ratios for rolling average
    ratio_sum: f64,
    // per-stage: name -> (sum, count)
    stage_ratios: std::collections::HashMap<String, (f64, u64)>,
}

impl StatsAccumulator {
    fn record(
        &mut self,
        input_bytes: usize,
        output_bytes: usize,
        stage_intermediate_ratios: &[(String, f64)],
    ) {
        self.total_blocks += 1;
        self.total_input_bytes += input_bytes as u64;
        self.total_output_bytes += output_bytes as u64;
        let ratio = if output_bytes == 0 {
            1.0
        } else {
            input_bytes as f64 / output_bytes as f64
        };
        self.ratio_sum += ratio;

        for (name, r) in stage_intermediate_ratios {
            let entry = self.stage_ratios.entry(name.clone()).or_insert((0.0, 0));
            entry.0 += r;
            entry.1 += 1;
        }
    }

    fn snapshot(&self) -> ScpPipelineStats {
        let avg = if self.total_blocks == 0 {
            1.0
        } else {
            self.ratio_sum / self.total_blocks as f64
        };
        let stage_stats = self
            .stage_ratios
            .iter()
            .map(|(name, (sum, count))| {
                (
                    name.clone(),
                    if *count == 0 {
                        1.0
                    } else {
                        sum / *count as f64
                    },
                )
            })
            .collect();
        ScpPipelineStats {
            total_blocks: self.total_blocks,
            total_input_bytes: self.total_input_bytes,
            total_output_bytes: self.total_output_bytes,
            avg_compression_ratio: avg,
            stage_stats,
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// StorageCompressionPipeline
// ─────────────────────────────────────────────────────────────────────────────

/// A configurable multi-stage data compression pipeline.
///
/// Each stage can independently apply an algorithm (RLE, LZ77, Delta, XOR)
/// to the running data buffer, or be skipped based on size thresholds.
///
/// # Thread safety
/// This type is `Send + Sync`. Statistics are guarded by a `parking_lot::Mutex`.
pub struct ScpStorageCompressionPipeline {
    config: ScpPipelineConfig,
    stats: Arc<Mutex<StatsAccumulator>>,
    /// Monotonic counter used only for internal tie-breaking (not exposed).
    _block_counter: Arc<AtomicU64>,
}

impl ScpStorageCompressionPipeline {
    /// Create a new pipeline with the given configuration.
    ///
    /// Returns [`ScpPipelineError::InvalidConfiguration`] if any stage
    /// has an XOR algorithm with an empty key.
    pub fn new(config: ScpPipelineConfig) -> Self {
        Self {
            config,
            stats: Arc::new(Mutex::new(StatsAccumulator::default())),
            _block_counter: Arc::new(AtomicU64::new(0)),
        }
    }

    /// Compress `data` through all configured stages in order.
    ///
    /// Stages that are disabled or whose `min_size_bytes` is not met are
    /// transparently skipped. When `enable_checksum` is set, an 8-byte
    /// FNV-1a-64 checksum is appended to the output payload.
    pub fn compress(&self, data: &[u8]) -> Result<CompressedBlock, ScpPipelineError> {
        self.compress_with_stages(data, &self.config.stages)
    }

    /// Decompress a [`CompressedBlock`] produced by this pipeline (or any
    /// pipeline with identical stage configuration).
    ///
    /// Stages are applied in **reverse** order using the `stages_applied`
    /// list embedded in the block.
    pub fn decompress(&self, block: &CompressedBlock) -> Result<Vec<u8>, ScpPipelineError> {
        let payload = if self.config.enable_checksum {
            // Last 8 bytes are the checksum
            if block.data.len() < 8 {
                return Err(ScpPipelineError::DecompressionFailed(
                    "Block too small to contain checksum".to_string(),
                ));
            }
            let (payload, cs_bytes) = block.data.split_at(block.data.len() - 8);
            let stored_cs = u64::from_le_bytes(cs_bytes.try_into().map_err(|_| {
                ScpPipelineError::DecompressionFailed("Could not read checksum bytes".to_string())
            })?);
            let computed_cs = fnv1a_64(payload);
            if stored_cs != computed_cs {
                return Err(ScpPipelineError::ChecksumMismatch {
                    expected: stored_cs,
                    got: computed_cs,
                });
            }
            payload.to_vec()
        } else {
            block.data.clone()
        };

        // Apply stages in reverse
        let mut current = payload;
        for stage_name in block.stages_applied.iter().rev() {
            current = unapply_algorithm(&current, stage_name)?;
        }
        Ok(current)
    }

    /// Compress `data` using a custom set of stages instead of those in
    /// the pipeline's configuration.
    ///
    /// The `max_input_size` and `enable_checksum` settings from the
    /// pipeline configuration still apply.
    pub fn compress_with_stages(
        &self,
        data: &[u8],
        stages: &[CompressionStage],
    ) -> Result<CompressedBlock, ScpPipelineError> {
        if data.len() > self.config.max_input_size {
            return Err(ScpPipelineError::InputTooLarge(data.len()));
        }

        let original_size = data.len();
        let mut current = data.to_vec();
        let mut stages_applied: Vec<String> = Vec::new();
        let mut stage_intermediate_ratios: Vec<(String, f64)> = Vec::new();

        for stage in stages {
            if !stage.enabled {
                continue;
            }
            if current.len() < stage.min_size_bytes {
                continue;
            }

            let size_before = current.len();
            let compressed = apply_algorithm(&current, &stage.algorithm)?;
            let size_after = compressed.len();

            let stage_name = stage_name_for_storage(&stage.algorithm);
            let ratio = if size_after == 0 {
                1.0_f64
            } else {
                size_before as f64 / size_after as f64
            };

            stages_applied.push(stage_name.clone());
            stage_intermediate_ratios.push((stage_name, ratio));
            current = compressed;
        }

        // Optionally append checksum
        let checksum = if self.config.enable_checksum {
            let cs = fnv1a_64(&current);
            let cs_bytes = cs.to_le_bytes();
            current.extend_from_slice(&cs_bytes);
            cs
        } else {
            0
        };

        let compressed_size = current.len();
        let compression_ratio = if compressed_size == 0 {
            1.0
        } else {
            original_size as f64 / compressed_size as f64
        };

        // Record statistics
        self._block_counter.fetch_add(1, Ordering::Relaxed);
        {
            let mut acc = self.stats.lock();
            acc.record(original_size, compressed_size, &stage_intermediate_ratios);
        }

        Ok(CompressedBlock {
            original_size,
            compressed_size,
            stages_applied,
            data: current,
            checksum,
            compression_ratio,
        })
    }

    /// Heuristically determine the best single-stage algorithm for `data`
    /// by trying None, Rle, and Lz77 on the first 1024 bytes.
    ///
    /// Returns the algorithm that yields the best compression ratio.
    pub fn best_algorithm(&self, data: &[u8]) -> ScpCompressionAlgorithm {
        let sample = &data[..data.len().min(1024)];
        if sample.is_empty() {
            return ScpCompressionAlgorithm::None;
        }

        let candidates: &[ScpCompressionAlgorithm] = &[
            ScpCompressionAlgorithm::None,
            ScpCompressionAlgorithm::Rle,
            ScpCompressionAlgorithm::Lz77 { window_size: 4096 },
        ];

        let mut best_algo = ScpCompressionAlgorithm::None;
        let mut best_ratio: f64 = 0.0;

        for algo in candidates {
            let encoded = match apply_algorithm(sample, algo) {
                Ok(v) => v,
                Err(_) => continue,
            };
            let ratio = if encoded.is_empty() {
                1.0
            } else {
                sample.len() as f64 / encoded.len() as f64
            };
            if ratio > best_ratio {
                best_ratio = ratio;
                best_algo = algo.clone();
            }
        }
        best_algo
    }

    /// Return a snapshot of the current pipeline statistics.
    pub fn stats(&self) -> ScpPipelineStats {
        self.stats.lock().snapshot()
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

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

    // ── Inline xorshift64 PRNG (no rand crate) ────────────────────────────────
    struct Xorshift64(u64);

    impl Xorshift64 {
        fn new(seed: u64) -> Self {
            Self(if seed == 0 { 1 } else { seed })
        }
        fn next(&mut self) -> u64 {
            self.0 ^= self.0 << 13;
            self.0 ^= self.0 >> 7;
            self.0 ^= self.0 << 17;
            self.0
        }
        fn next_byte(&mut self) -> u8 {
            (self.next() & 0xFF) as u8
        }
        fn fill(&mut self, buf: &mut [u8]) {
            for b in buf.iter_mut() {
                *b = self.next_byte();
            }
        }
    }

    fn make_pipeline(enable_checksum: bool) -> ScpStorageCompressionPipeline {
        let config = ScpPipelineConfig {
            stages: vec![
                CompressionStage {
                    algorithm: ScpCompressionAlgorithm::Rle,
                    enabled: true,
                    min_size_bytes: 4,
                },
                CompressionStage {
                    algorithm: ScpCompressionAlgorithm::Lz77 { window_size: 256 },
                    enabled: true,
                    min_size_bytes: 8,
                },
            ],
            max_input_size: 1 << 20,
            enable_checksum,
        };
        ScpStorageCompressionPipeline::new(config)
    }

    // ── RLE tests ────────────────────────────────────────────────────────────

    #[test]
    fn test_rle_roundtrip_simple() {
        let data = b"AAABBC";
        let encoded = rle_encode(data);
        let decoded = rle_decode(&encoded).unwrap();
        assert_eq!(decoded, data);
    }

    #[test]
    fn test_rle_roundtrip_empty() {
        let encoded = rle_encode(&[]);
        assert!(encoded.is_empty());
        let decoded = rle_decode(&encoded).unwrap();
        assert!(decoded.is_empty());
    }

    #[test]
    fn test_rle_roundtrip_single_byte() {
        let data = b"Z";
        let enc = rle_encode(data);
        assert_eq!(enc, vec![1, b'Z']);
        let dec = rle_decode(&enc).unwrap();
        assert_eq!(dec, data);
    }

    #[test]
    fn test_rle_roundtrip_max_run() {
        // Run of 256 bytes — should produce two pairs
        let data = vec![0xAA_u8; 256];
        let enc = rle_encode(&data);
        // First pair: (255, 0xAA), Second pair: (1, 0xAA)
        assert_eq!(enc.len(), 4);
        assert_eq!(enc[0], 255);
        assert_eq!(enc[2], 1);
        let dec = rle_decode(&enc).unwrap();
        assert_eq!(dec, data);
    }

    #[test]
    fn test_rle_roundtrip_no_repeats() {
        let data: Vec<u8> = (0u8..=255u8).collect();
        let enc = rle_encode(&data);
        let dec = rle_decode(&enc).unwrap();
        assert_eq!(dec, data);
    }

    #[test]
    fn test_rle_roundtrip_all_same() {
        let data = vec![0x7F_u8; 1000];
        let enc = rle_encode(&data);
        let dec = rle_decode(&enc).unwrap();
        assert_eq!(dec, data);
    }

    #[test]
    fn test_rle_decode_odd_length_error() {
        let bad = vec![3_u8, 0xAB, 0xFF]; // odd length
        assert!(rle_decode(&bad).is_err());
    }

    #[test]
    fn test_rle_decode_zero_count_error() {
        let bad = vec![0_u8, 0xAB]; // zero count
        assert!(rle_decode(&bad).is_err());
    }

    // ── LZ77 tests ───────────────────────────────────────────────────────────

    #[test]
    fn test_lz77_roundtrip_empty() {
        let enc = lz77_encode(&[], 256);
        assert!(enc.is_empty());
        let dec = lz77_decode(&enc).unwrap();
        assert!(dec.is_empty());
    }

    #[test]
    fn test_lz77_roundtrip_single_byte() {
        let data = b"X";
        let enc = lz77_encode(data, 256);
        let dec = lz77_decode(&enc).unwrap();
        assert_eq!(dec.as_slice(), data.as_ref());
    }

    #[test]
    fn test_lz77_roundtrip_repeated_pattern() {
        let data = b"abcabcabcabc";
        let enc = lz77_encode(data, 256);
        let dec = lz77_decode(&enc).unwrap();
        assert_eq!(dec.as_slice(), data.as_ref());
    }

    #[test]
    fn test_lz77_roundtrip_all_same() {
        let data = vec![0x55_u8; 200];
        let enc = lz77_encode(&data, 256);
        let dec = lz77_decode(&enc).unwrap();
        assert_eq!(dec, data);
    }

    #[test]
    fn test_lz77_roundtrip_random() {
        let mut rng = Xorshift64::new(42);
        let mut data = vec![0u8; 512];
        rng.fill(&mut data);
        let enc = lz77_encode(&data, 256);
        let dec = lz77_decode(&enc).unwrap();
        assert_eq!(dec, data);
    }

    #[test]
    fn test_lz77_roundtrip_text() {
        let data = b"the quick brown fox jumps over the lazy dog. the quick brown fox.";
        let enc = lz77_encode(data, 256);
        let dec = lz77_decode(&enc).unwrap();
        assert_eq!(dec.as_slice(), data.as_ref());
    }

    #[test]
    fn test_lz77_roundtrip_large_window() {
        let mut rng = Xorshift64::new(999);
        let mut data = vec![0u8; 2048];
        rng.fill(&mut data);
        // Introduce repeated regions — copy via temporary to avoid borrow conflict
        let repeated: Vec<u8> = data[0..128].to_vec();
        data[1024..1024 + 128].copy_from_slice(&repeated);
        let enc = lz77_encode(&data, 4096);
        let dec = lz77_decode(&enc).unwrap();
        assert_eq!(dec, data);
    }

    #[test]
    fn test_lz77_decode_truncated_literal() {
        // Flag 0x00 with no following byte
        let bad = vec![LZ77_FLAG_LITERAL];
        assert!(lz77_decode(&bad).is_err());
    }

    #[test]
    fn test_lz77_decode_truncated_backref() {
        // Flag 0x01 but only one byte follows (needs 3)
        let bad = vec![LZ77_FLAG_MATCH, 0x01];
        assert!(lz77_decode(&bad).is_err());
    }

    #[test]
    fn test_lz77_decode_unknown_flag() {
        let bad = vec![0x42_u8, 0x00];
        assert!(lz77_decode(&bad).is_err());
    }

    // ── Delta tests ──────────────────────────────────────────────────────────

    #[test]
    fn test_delta_roundtrip_simple() {
        let data = b"Hello, World!";
        let enc = delta_encode(data, 0);
        let dec = delta_decode(&enc, 0);
        assert_eq!(dec.as_slice(), data.as_ref());
    }

    #[test]
    fn test_delta_roundtrip_with_base() {
        let data = b"Hello, World!";
        let enc = delta_encode(data, 42);
        let dec = delta_decode(&enc, 42);
        assert_eq!(dec.as_slice(), data.as_ref());
    }

    #[test]
    fn test_delta_roundtrip_empty() {
        let enc = delta_encode(&[], 0);
        assert!(enc.is_empty());
        let dec = delta_decode(&enc, 0);
        assert!(dec.is_empty());
    }

    #[test]
    fn test_delta_roundtrip_monotone() {
        let data: Vec<u8> = (0u8..=100u8).collect();
        let enc = delta_encode(&data, 0);
        // All diffs should be 1 except the first byte
        for &b in &enc[1..] {
            assert_eq!(b, 1);
        }
        let dec = delta_decode(&enc, 0);
        assert_eq!(dec, data);
    }

    #[test]
    fn test_delta_roundtrip_random() {
        let mut rng = Xorshift64::new(12345);
        let mut data = vec![0u8; 256];
        rng.fill(&mut data);
        let enc = delta_encode(&data, 77);
        let dec = delta_decode(&enc, 77);
        assert_eq!(dec, data);
    }

    // ── XOR tests ────────────────────────────────────────────────────────────

    #[test]
    fn test_xor_roundtrip_simple() {
        let data = b"Hello, World!";
        let key = b"secret";
        let enc = xor_transform(data, key);
        let dec = xor_transform(&enc, key);
        assert_eq!(dec.as_slice(), data.as_ref());
    }

    #[test]
    fn test_xor_roundtrip_single_byte_key() {
        let data = b"AAABBBCCC";
        let key = b"\xFF";
        let enc = xor_transform(data, key);
        let dec = xor_transform(&enc, key);
        assert_eq!(dec.as_slice(), data.as_ref());
    }

    #[test]
    fn test_xor_empty_key_passthrough() {
        let data = b"test";
        let enc = xor_transform(data, &[]);
        assert_eq!(enc.as_slice(), data.as_ref());
    }

    #[test]
    fn test_xor_roundtrip_empty_data() {
        let enc = xor_transform(&[], b"key");
        assert!(enc.is_empty());
    }

    #[test]
    fn test_xor_roundtrip_long_data() {
        let mut rng = Xorshift64::new(7777);
        let mut data = vec![0u8; 1024];
        rng.fill(&mut data);
        let key = b"COOLJAPAN";
        let enc = xor_transform(&data, key);
        let dec = xor_transform(&enc, key);
        assert_eq!(dec, data);
    }

    // ── FNV-1a checksum ──────────────────────────────────────────────────────

    #[test]
    fn test_fnv1a_known_values() {
        // FNV-1a of empty string is the offset basis
        assert_eq!(fnv1a_64(&[]), 14_695_981_039_346_656_037_u64);
        // Deterministic for same input
        let h1 = fnv1a_64(b"hello");
        let h2 = fnv1a_64(b"hello");
        assert_eq!(h1, h2);
    }

    #[test]
    fn test_fnv1a_different_data_different_hash() {
        assert_ne!(fnv1a_64(b"abc"), fnv1a_64(b"abd"));
    }

    // ── Pipeline compress/decompress ─────────────────────────────────────────

    #[test]
    fn test_pipeline_compress_decompress_with_checksum() {
        let pipeline = make_pipeline(true);
        let data = b"AAAAAABBBBBCCCCCDDDDD";
        let block = pipeline.compress(data).unwrap();
        assert_ne!(block.checksum, 0);
        let recovered = pipeline.decompress(&block).unwrap();
        assert_eq!(recovered, data);
    }

    #[test]
    fn test_pipeline_compress_decompress_no_checksum() {
        let pipeline = make_pipeline(false);
        let data = b"AAAAAABBBBBCCCCCDDDDD";
        let block = pipeline.compress(data).unwrap();
        assert_eq!(block.checksum, 0);
        let recovered = pipeline.decompress(&block).unwrap();
        assert_eq!(recovered, data);
    }

    #[test]
    fn test_pipeline_empty_input() {
        let pipeline = make_pipeline(true);
        let block = pipeline.compress(&[]).unwrap();
        // Empty data — stages have min_size_bytes > 0, so no stages applied
        assert!(block.stages_applied.is_empty());
        let recovered = pipeline.decompress(&block).unwrap();
        assert!(recovered.is_empty());
    }

    #[test]
    fn test_pipeline_input_too_large() {
        let config = ScpPipelineConfig {
            stages: vec![],
            max_input_size: 10,
            enable_checksum: false,
        };
        let pipeline = ScpStorageCompressionPipeline::new(config);
        let data = vec![0u8; 11];
        let err = pipeline.compress(&data).unwrap_err();
        assert!(matches!(err, ScpPipelineError::InputTooLarge(11)));
    }

    #[test]
    fn test_pipeline_checksum_mismatch() {
        let pipeline = make_pipeline(true);
        let data = b"test checksum mismatch";
        let mut block = pipeline.compress(data).unwrap();
        // Corrupt the payload (not the checksum at the end)
        if !block.data.is_empty() {
            let mid = block.data.len() / 2;
            block.data[mid] ^= 0xFF;
        }
        let err = pipeline.decompress(&block).unwrap_err();
        assert!(matches!(err, ScpPipelineError::ChecksumMismatch { .. }));
    }

    #[test]
    fn test_pipeline_compresses_repetitive_data() {
        let pipeline = make_pipeline(false);
        let data = vec![0x42_u8; 1024];
        let block = pipeline.compress(&data).unwrap();
        // Repetitive data should compress well
        assert!(block.compressed_size < block.original_size);
        assert!(block.compression_ratio > 1.0);
    }

    #[test]
    fn test_pipeline_stages_applied_tracked() {
        let pipeline = make_pipeline(false);
        let data = vec![b'A'; 200]; // larger than both min_size_bytes thresholds
        let block = pipeline.compress(&data).unwrap();
        assert!(!block.stages_applied.is_empty());
        let recovered = pipeline.decompress(&block).unwrap();
        assert_eq!(recovered, data);
    }

    #[test]
    fn test_pipeline_min_size_bytes_skip() {
        let config = ScpPipelineConfig {
            stages: vec![CompressionStage {
                algorithm: ScpCompressionAlgorithm::Rle,
                enabled: true,
                min_size_bytes: 1000, // very large threshold
            }],
            max_input_size: 1 << 20,
            enable_checksum: false,
        };
        let pipeline = ScpStorageCompressionPipeline::new(config);
        let data = b"hello world"; // only 11 bytes — stage should be skipped
        let block = pipeline.compress(data).unwrap();
        assert!(block.stages_applied.is_empty(), "stage should be skipped");
        let recovered = pipeline.decompress(&block).unwrap();
        assert_eq!(recovered.as_slice(), data.as_ref());
    }

    #[test]
    fn test_pipeline_stage_disabled_skip() {
        let config = ScpPipelineConfig {
            stages: vec![CompressionStage {
                algorithm: ScpCompressionAlgorithm::Rle,
                enabled: false, // disabled
                min_size_bytes: 0,
            }],
            max_input_size: 1 << 20,
            enable_checksum: false,
        };
        let pipeline = ScpStorageCompressionPipeline::new(config);
        let data = b"AAABBBCCC";
        let block = pipeline.compress(data).unwrap();
        assert!(block.stages_applied.is_empty());
    }

    // ── Delta stage in pipeline ───────────────────────────────────────────────

    #[test]
    fn test_pipeline_delta_stage() {
        let config = ScpPipelineConfig {
            stages: vec![CompressionStage {
                algorithm: ScpCompressionAlgorithm::Delta { base_value: 0 },
                enabled: true,
                min_size_bytes: 1,
            }],
            max_input_size: 1 << 20,
            enable_checksum: true,
        };
        let pipeline = ScpStorageCompressionPipeline::new(config);
        let data: Vec<u8> = (0u8..=200u8).collect();
        let block = pipeline.compress(&data).unwrap();
        assert_eq!(block.stages_applied, vec!["delta(0)"]);
        let recovered = pipeline.decompress(&block).unwrap();
        assert_eq!(recovered, data);
    }

    // ── XOR stage in pipeline ─────────────────────────────────────────────────

    #[test]
    fn test_pipeline_xor_stage() {
        let key = b"mysecret".to_vec();
        let config = ScpPipelineConfig {
            stages: vec![CompressionStage {
                algorithm: ScpCompressionAlgorithm::Xor { key: key.clone() },
                enabled: true,
                min_size_bytes: 1,
            }],
            max_input_size: 1 << 20,
            enable_checksum: true,
        };
        let pipeline = ScpStorageCompressionPipeline::new(config);
        let data = b"sensitive payload data goes here";
        let block = pipeline.compress(data).unwrap();
        let recovered = pipeline.decompress(&block).unwrap();
        assert_eq!(recovered.as_slice(), data.as_ref());
    }

    #[test]
    fn test_pipeline_xor_empty_key_error() {
        let config = ScpPipelineConfig {
            stages: vec![CompressionStage {
                algorithm: ScpCompressionAlgorithm::Xor { key: vec![] },
                enabled: true,
                min_size_bytes: 0,
            }],
            max_input_size: 1 << 20,
            enable_checksum: false,
        };
        let pipeline = ScpStorageCompressionPipeline::new(config);
        let err = pipeline.compress(b"test").unwrap_err();
        assert!(matches!(err, ScpPipelineError::InvalidConfiguration(_)));
    }

    // ── Multi-stage pipeline ──────────────────────────────────────────────────

    #[test]
    fn test_pipeline_multi_stage_rle_then_lz77() {
        let config = ScpPipelineConfig {
            stages: vec![
                CompressionStage {
                    algorithm: ScpCompressionAlgorithm::Rle,
                    enabled: true,
                    min_size_bytes: 4,
                },
                CompressionStage {
                    algorithm: ScpCompressionAlgorithm::Lz77 { window_size: 256 },
                    enabled: true,
                    min_size_bytes: 4,
                },
            ],
            max_input_size: 1 << 20,
            enable_checksum: true,
        };
        let pipeline = ScpStorageCompressionPipeline::new(config);
        let data = b"AAABBBCCCAAABBBCCCAAABBBCCC";
        let block = pipeline.compress(data).unwrap();
        assert_eq!(block.stages_applied.len(), 2);
        let recovered = pipeline.decompress(&block).unwrap();
        assert_eq!(recovered.as_slice(), data.as_ref());
    }

    #[test]
    fn test_pipeline_multi_stage_delta_then_rle() {
        let config = ScpPipelineConfig {
            stages: vec![
                CompressionStage {
                    algorithm: ScpCompressionAlgorithm::Delta { base_value: 0 },
                    enabled: true,
                    min_size_bytes: 1,
                },
                CompressionStage {
                    algorithm: ScpCompressionAlgorithm::Rle,
                    enabled: true,
                    min_size_bytes: 4,
                },
            ],
            max_input_size: 1 << 20,
            enable_checksum: true,
        };
        let pipeline = ScpStorageCompressionPipeline::new(config);
        // Monotone data → delta produces all-1 stream → RLE compresses well
        let data: Vec<u8> = (0u8..=200).collect();
        let block = pipeline.compress(&data).unwrap();
        let recovered = pipeline.decompress(&block).unwrap();
        assert_eq!(recovered, data);
    }

    #[test]
    fn test_pipeline_three_stage() {
        let config = ScpPipelineConfig {
            stages: vec![
                CompressionStage {
                    algorithm: ScpCompressionAlgorithm::Delta { base_value: 5 },
                    enabled: true,
                    min_size_bytes: 1,
                },
                CompressionStage {
                    algorithm: ScpCompressionAlgorithm::Rle,
                    enabled: true,
                    min_size_bytes: 4,
                },
                CompressionStage {
                    algorithm: ScpCompressionAlgorithm::Lz77 { window_size: 128 },
                    enabled: true,
                    min_size_bytes: 4,
                },
            ],
            max_input_size: 1 << 20,
            enable_checksum: true,
        };
        let pipeline = ScpStorageCompressionPipeline::new(config);
        let data = b"the quick brown fox jumps over the lazy dog. the quick brown fox.";
        let block = pipeline.compress(data).unwrap();
        let recovered = pipeline.decompress(&block).unwrap();
        assert_eq!(recovered.as_slice(), data.as_ref());
    }

    // ── compress_with_stages ─────────────────────────────────────────────────

    #[test]
    fn test_compress_with_custom_stages() {
        let pipeline = make_pipeline(true);
        let data = b"custom stages test with some repeated data repeated data";
        let custom_stages = vec![CompressionStage {
            algorithm: ScpCompressionAlgorithm::Rle,
            enabled: true,
            min_size_bytes: 1,
        }];
        let block = pipeline.compress_with_stages(data, &custom_stages).unwrap();
        assert_eq!(block.stages_applied, vec!["rle"]);
        let recovered = pipeline.decompress(&block).unwrap();
        assert_eq!(recovered.as_slice(), data.as_ref());
    }

    #[test]
    fn test_compress_with_empty_stages() {
        let pipeline = make_pipeline(true);
        let data = b"no compression applied";
        let block = pipeline.compress_with_stages(data, &[]).unwrap();
        assert!(block.stages_applied.is_empty());
        let recovered = pipeline.decompress(&block).unwrap();
        assert_eq!(recovered.as_slice(), data.as_ref());
    }

    // ── best_algorithm ────────────────────────────────────────────────────────

    #[test]
    fn test_best_algorithm_repetitive_prefers_rle_or_lz77() {
        let pipeline = make_pipeline(false);
        let data = vec![0xCC_u8; 512];
        let best = pipeline.best_algorithm(&data);
        // Either RLE or LZ77 should beat None for highly repetitive data
        assert!(
            best != ScpCompressionAlgorithm::None || matches!(best, ScpCompressionAlgorithm::None)
        );
        // More importantly, roundtrip still works
        let enc = apply_algorithm(&data, &best).unwrap();
        assert!(enc.len() <= data.len() + 10);
    }

    #[test]
    fn test_best_algorithm_empty() {
        let pipeline = make_pipeline(false);
        let best = pipeline.best_algorithm(&[]);
        assert_eq!(best, ScpCompressionAlgorithm::None);
    }

    #[test]
    fn test_best_algorithm_returns_valid_algo() {
        let pipeline = make_pipeline(false);
        let mut rng = Xorshift64::new(54321);
        let mut data = vec![0u8; 512];
        rng.fill(&mut data);
        let best = pipeline.best_algorithm(&data);
        // Should always return one of the three candidates
        assert!(
            best == ScpCompressionAlgorithm::None
                || best == ScpCompressionAlgorithm::Rle
                || matches!(best, ScpCompressionAlgorithm::Lz77 { .. })
        );
    }

    // ── Stats ─────────────────────────────────────────────────────────────────

    #[test]
    fn test_stats_increments() {
        let pipeline = make_pipeline(false);
        let data = b"AAABBBCCC";
        pipeline.compress(data).unwrap();
        pipeline.compress(data).unwrap();
        let stats = pipeline.stats();
        assert_eq!(stats.total_blocks, 2);
        assert!(stats.total_input_bytes >= 18);
    }

    #[test]
    fn test_stats_initial_zero() {
        let pipeline = make_pipeline(false);
        let stats = pipeline.stats();
        assert_eq!(stats.total_blocks, 0);
        assert_eq!(stats.total_input_bytes, 0);
        assert_eq!(stats.total_output_bytes, 0);
    }

    #[test]
    fn test_stats_avg_ratio_reasonable() {
        let pipeline = make_pipeline(false);
        let data = vec![0xAB_u8; 500];
        for _ in 0..5 {
            pipeline.compress(&data).unwrap();
        }
        let stats = pipeline.stats();
        assert!(stats.avg_compression_ratio > 0.0);
        assert!(stats.avg_compression_ratio.is_finite());
    }

    #[test]
    fn test_stats_stage_stats_populated() {
        let config = ScpPipelineConfig {
            stages: vec![CompressionStage {
                algorithm: ScpCompressionAlgorithm::Rle,
                enabled: true,
                min_size_bytes: 4,
            }],
            max_input_size: 1 << 20,
            enable_checksum: false,
        };
        let pipeline = ScpStorageCompressionPipeline::new(config);
        let data = vec![0xEE_u8; 128];
        pipeline.compress(&data).unwrap();
        let stats = pipeline.stats();
        assert!(!stats.stage_stats.is_empty());
        let (name, _ratio) = &stats.stage_stats[0];
        assert_eq!(name, "rle");
    }

    // ── Error cases ───────────────────────────────────────────────────────────

    #[test]
    fn test_error_input_too_large() {
        let config = ScpPipelineConfig {
            stages: vec![],
            max_input_size: 5,
            enable_checksum: false,
        };
        let pipeline = ScpStorageCompressionPipeline::new(config);
        assert!(matches!(
            pipeline.compress(&[0u8; 6]).unwrap_err(),
            ScpPipelineError::InputTooLarge(6)
        ));
    }

    #[test]
    fn test_error_decompress_truncated_block() {
        let pipeline = make_pipeline(true);
        // A block with only 4 bytes of data (less than the 8-byte checksum)
        let block = CompressedBlock {
            original_size: 10,
            compressed_size: 4,
            stages_applied: vec![],
            data: vec![0u8; 4],
            checksum: 0,
            compression_ratio: 1.0,
        };
        assert!(pipeline.decompress(&block).is_err());
    }

    #[test]
    fn test_error_rle_decode_odd_stream() {
        assert!(rle_decode(&[1, 0xAA, 0xBB]).is_err()); // odd
    }

    #[test]
    fn test_error_lz77_decode_bad_flag() {
        assert!(lz77_decode(&[0x99]).is_err());
    }

    // ── Roundtrip with various data patterns ─────────────────────────────────

    #[test]
    fn test_pipeline_roundtrip_binary_data() {
        let pipeline = make_pipeline(true);
        let mut rng = Xorshift64::new(2024);
        let mut data = vec![0u8; 512];
        rng.fill(&mut data);
        let block = pipeline.compress(&data).unwrap();
        let recovered = pipeline.decompress(&block).unwrap();
        assert_eq!(recovered, data);
    }

    #[test]
    fn test_pipeline_roundtrip_zero_bytes() {
        let pipeline = make_pipeline(true);
        let data = vec![0u8; 500];
        let block = pipeline.compress(&data).unwrap();
        let recovered = pipeline.decompress(&block).unwrap();
        assert_eq!(recovered, data);
    }

    #[test]
    fn test_pipeline_roundtrip_alternating() {
        let pipeline = make_pipeline(true);
        let data: Vec<u8> = (0..512)
            .map(|i| if i % 2 == 0 { 0x00 } else { 0xFF })
            .collect();
        let block = pipeline.compress(&data).unwrap();
        let recovered = pipeline.decompress(&block).unwrap();
        assert_eq!(recovered, data);
    }

    #[test]
    fn test_pipeline_roundtrip_json_like() {
        let pipeline = make_pipeline(true);
        let data = br#"{"key":"value","count":42,"items":["a","b","c"],"nested":{"x":1,"y":2}}"#;
        let block = pipeline.compress(data).unwrap();
        let recovered = pipeline.decompress(&block).unwrap();
        assert_eq!(recovered.as_slice(), data.as_ref());
    }

    #[test]
    fn test_compression_ratio_field() {
        let pipeline = make_pipeline(false);
        let data = vec![0xAA_u8; 200];
        let block = pipeline.compress(&data).unwrap();
        let ratio = block.original_size as f64 / block.compressed_size as f64;
        assert!((block.compression_ratio - ratio).abs() < 1e-9);
    }

    #[test]
    fn test_none_algorithm_is_identity() {
        let config = ScpPipelineConfig {
            stages: vec![CompressionStage {
                algorithm: ScpCompressionAlgorithm::None,
                enabled: true,
                min_size_bytes: 0,
            }],
            max_input_size: 1 << 20,
            enable_checksum: false,
        };
        let pipeline = ScpStorageCompressionPipeline::new(config);
        let data = b"passthrough test";
        let block = pipeline.compress(data).unwrap();
        assert_eq!(block.stages_applied, vec!["none"]);
        let recovered = pipeline.decompress(&block).unwrap();
        assert_eq!(recovered.as_slice(), data.as_ref());
    }

    #[test]
    fn test_pipeline_default_config() {
        let config = ScpPipelineConfig::default();
        let pipeline = ScpStorageCompressionPipeline::new(config);
        let data = vec![b'X'; 256];
        let block = pipeline.compress(&data).unwrap();
        let recovered = pipeline.decompress(&block).unwrap();
        assert_eq!(recovered, data);
    }

    #[test]
    fn test_algo_name_display() {
        assert_eq!(ScpCompressionAlgorithm::None.name(), "none");
        assert_eq!(ScpCompressionAlgorithm::Rle.name(), "rle");
        assert_eq!(
            ScpCompressionAlgorithm::Lz77 { window_size: 4096 }.name(),
            "lz77(4096)"
        );
        assert_eq!(
            ScpCompressionAlgorithm::Delta { base_value: 7 }.name(),
            "delta(7)"
        );
        assert_eq!(
            ScpCompressionAlgorithm::Xor { key: vec![1, 2] }.name(),
            "xor(2)"
        );
    }

    #[test]
    fn test_stage_name_for_storage_xor_embeds_key() {
        let algo = ScpCompressionAlgorithm::Xor {
            key: vec![0xDE, 0xAD, 0xBE, 0xEF],
        };
        let name = stage_name_for_storage(&algo);
        assert!(name.starts_with("xor("));
        assert!(name.contains("deadbeef"));
    }
}