mediagit-compression 0.2.0

Content-aware compression engine for media objects
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
// MediaGit - Git for Media Files
// Copyright (C) 2025 MediaGit Contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.

//! Delta encoding implementation using bsdiff
//!
//! This module provides delta compression for incremental changes to large media files,
//! achieving up to 95% space savings when files are similar.
//!
//! # Features
//!
//! - bsdiff-based binary diff/patch operations
//! - Intelligent delta vs full compression decision logic
//! - Base reference tracking with chain depth limits
//! - Fast delta application with chain resolution (<100ms)
//! - Similarity detection (>80% threshold)
//!
//! # Example
//!
//! ```rust
//! use mediagit_compression::delta::{DeltaEncoder, DeltaMetadata};
//!
//! # fn example() -> anyhow::Result<()> {
//! let encoder = DeltaEncoder::new();
//!
//! let base = b"Hello, World!";
//! let modified = b"Hello, MediaGit World!";
//!
//! // Create delta
//! let (delta_data, should_use_delta) = encoder.encode(base, modified)?;
//!
//! if should_use_delta {
//!     // Apply delta to reconstruct
//!     let reconstructed = encoder.decode(base, &delta_data)?;
//!     assert_eq!(modified, &reconstructed[..]);
//! }
//! # Ok(())
//! # }
//! ```

use crate::error::{CompressionError, CompressionResult};
use crate::smart_compressor::{ObjectCategory, ObjectType};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Instant;
use tracing::{debug, info, trace, warn};

/// Maximum delta chain depth to prevent excessive reconstruction time
pub const MAX_CHAIN_DEPTH: u32 = 10;

/// Similarity threshold for delta encoding (80% means files must be at least 80% similar)
pub const SIMILARITY_THRESHOLD: f64 = 0.80;

/// Target space savings for using delta encoding (if savings < 10%, use full compression)
pub const MIN_SPACE_SAVINGS: f64 = 0.10;

/// Maximum reconstruction time target (100ms)
pub const MAX_RECONSTRUCTION_TIME_MS: u64 = 100;

/// Media-aware delta configuration
#[derive(Debug, Clone, Copy)]
pub struct DeltaConfig {
    /// Similarity threshold (0.0-1.0)
    pub similarity_threshold: f64,
    /// Minimum space savings (0.0-1.0)
    pub min_space_savings: f64,
    /// Maximum chain depth
    pub max_chain_depth: u32,
}

impl DeltaConfig {
    /// Get configuration for object type
    pub fn for_object_type(obj_type: ObjectType) -> Self {
        match obj_type.category() {
            // Images: higher similarity threshold, more aggressive savings
            ObjectCategory::Image => DeltaConfig {
                similarity_threshold: 0.85,
                min_space_savings: 0.15,
                max_chain_depth: 5,
            },
            // Video: very high similarity needed (metadata changes only)
            ObjectCategory::Video => DeltaConfig {
                similarity_threshold: 0.95,
                min_space_savings: 0.05,
                max_chain_depth: 3,
            },
            // Audio: moderate similarity
            ObjectCategory::Audio => DeltaConfig {
                similarity_threshold: 0.90,
                min_space_savings: 0.10,
                max_chain_depth: 5,
            },
            // Text/Code: very effective delta compression
            ObjectCategory::Text | ObjectCategory::Document => DeltaConfig {
                similarity_threshold: 0.70,
                min_space_savings: 0.10,
                max_chain_depth: MAX_CHAIN_DEPTH,
            },
            // Creative project files: HIGHEST PRIORITY for delta compression
            // These files see frequent incremental edits (layers, timelines, scenes)
            // Delta compression can achieve 80-95% space savings between versions
            ObjectCategory::CreativeProject => DeltaConfig {
                similarity_threshold: 0.70, // Lower threshold (frequent edits)
                min_space_savings: 0.10,
                max_chain_depth: 10, // Allow longer chains for project history
            },
            // Office documents: moderate delta effectiveness (XML-based)
            ObjectCategory::Office => DeltaConfig {
                similarity_threshold: 0.75,
                min_space_savings: 0.10,
                max_chain_depth: MAX_CHAIN_DEPTH,
            },
            // ML specialized: differentiate training vs inference
            // Training checkpoints benefit heavily from delta (incremental learning)
            ObjectCategory::MlSpecialized => DeltaConfig {
                similarity_threshold: 0.75, // Model weights change gradually
                min_space_savings: 0.10,
                max_chain_depth: 8, // Moderate chains for training runs
            },
            // Database: conservative approach
            ObjectCategory::Database => DeltaConfig {
                similarity_threshold: 0.80,
                min_space_savings: 0.10,
                max_chain_depth: 5,
            },
            // Git objects: optimized for source code
            ObjectCategory::GitObject => DeltaConfig {
                similarity_threshold: 0.75,
                min_space_savings: 0.10,
                max_chain_depth: MAX_CHAIN_DEPTH,
            },
            // Archives/Unknown: conservative
            ObjectCategory::Archive | ObjectCategory::Unknown => DeltaConfig {
                similarity_threshold: SIMILARITY_THRESHOLD,
                min_space_savings: MIN_SPACE_SAVINGS,
                max_chain_depth: MAX_CHAIN_DEPTH,
            },
        }
    }

    /// Default configuration
    pub fn default_config() -> Self {
        DeltaConfig {
            similarity_threshold: SIMILARITY_THRESHOLD,
            min_space_savings: MIN_SPACE_SAVINGS,
            max_chain_depth: MAX_CHAIN_DEPTH,
        }
    }
}

/// Delta encoding metadata
///
/// Tracks base references, chain depth, and compression statistics
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DeltaMetadata {
    /// Hash of the base version used for this delta
    pub base_hash: String,
    /// Chain depth (0 = full version, >0 = delta)
    pub chain_depth: u32,
    /// Original size before delta compression
    pub original_size: usize,
    /// Delta size (or full compressed size if not using delta)
    pub delta_size: usize,
    /// Whether this version uses delta encoding
    pub is_delta: bool,
    /// Similarity score with base (0.0 - 1.0)
    pub similarity: f64,
    /// Space savings percentage
    pub space_savings: f64,
}

impl DeltaMetadata {
    /// Create metadata for a full (non-delta) version
    pub fn full(hash: String, size: usize) -> Self {
        DeltaMetadata {
            base_hash: hash,
            chain_depth: 0,
            original_size: size,
            delta_size: size,
            is_delta: false,
            similarity: 1.0,
            space_savings: 0.0,
        }
    }

    /// Create metadata for a delta version
    pub fn delta(
        base_hash: String,
        base_chain_depth: u32,
        original_size: usize,
        delta_size: usize,
        similarity: f64,
    ) -> Self {
        let space_savings = if original_size > 0 {
            (original_size - delta_size) as f64 / original_size as f64
        } else {
            0.0
        };

        DeltaMetadata {
            base_hash,
            chain_depth: base_chain_depth + 1,
            original_size,
            delta_size,
            is_delta: true,
            similarity,
            space_savings,
        }
    }

    /// Check if this version is at max chain depth
    pub fn is_at_max_depth(&self) -> bool {
        self.chain_depth >= MAX_CHAIN_DEPTH
    }

    /// Calculate compression ratio
    pub fn compression_ratio(&self) -> f64 {
        if self.original_size == 0 {
            1.0
        } else {
            self.delta_size as f64 / self.original_size as f64
        }
    }
}

/// Delta chain tracker
///
/// Manages base references and prevents excessive chain depth
#[derive(Debug, Clone)]
pub struct DeltaChainTracker {
    /// Map of hash -> metadata
    metadata: HashMap<String, DeltaMetadata>,
}

impl DeltaChainTracker {
    /// Create a new delta chain tracker
    pub fn new() -> Self {
        DeltaChainTracker {
            metadata: HashMap::new(),
        }
    }

    /// Register a new version
    pub fn register(&mut self, hash: String, metadata: DeltaMetadata) {
        self.metadata.insert(hash, metadata);
    }

    /// Get metadata for a version
    pub fn get(&self, hash: &str) -> Option<&DeltaMetadata> {
        self.metadata.get(hash)
    }

    /// Find the best base for a new version
    ///
    /// Returns the hash of the best base, or None if no suitable base exists
    pub fn find_best_base(&self, candidates: &[String]) -> Option<String> {
        candidates
            .iter()
            .filter_map(|hash| {
                self.metadata
                    .get(hash)
                    .map(|meta| (hash.clone(), meta.chain_depth))
            })
            .min_by_key(|(_, depth)| *depth)
            .filter(|(_, depth)| *depth < MAX_CHAIN_DEPTH)
            .map(|(hash, _)| hash)
    }

    /// Get the full chain for reconstruction
    pub fn get_chain(&self, hash: &str) -> CompressionResult<Vec<String>> {
        let mut chain = Vec::new();
        let mut current = hash.to_string();

        loop {
            chain.push(current.clone());

            if let Some(meta) = self.metadata.get(&current) {
                if meta.chain_depth == 0 {
                    // Reached base
                    break;
                }
                current = meta.base_hash.clone();
            } else {
                return Err(CompressionError::invalid_input(format!(
                    "missing metadata for hash: {}",
                    current
                )));
            }
        }

        // Reverse so base is first
        chain.reverse();
        Ok(chain)
    }
}

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

/// Delta encoder using bsdiff
///
/// Provides delta compression and decompression with intelligent decision logic,
/// with optional media-aware configuration.
#[derive(Debug, Clone)]
pub struct DeltaEncoder {
    /// Minimum similarity threshold for using delta encoding
    similarity_threshold: f64,
    /// Minimum space savings for using delta encoding
    min_space_savings: f64,
    /// Maximum chain depth
    max_chain_depth: u32,
}

impl DeltaEncoder {
    /// Create a new delta encoder with default settings
    pub fn new() -> Self {
        DeltaEncoder {
            similarity_threshold: SIMILARITY_THRESHOLD,
            min_space_savings: MIN_SPACE_SAVINGS,
            max_chain_depth: MAX_CHAIN_DEPTH,
        }
    }

    /// Create a delta encoder with custom thresholds
    pub fn with_thresholds(similarity_threshold: f64, min_space_savings: f64) -> Self {
        DeltaEncoder {
            similarity_threshold,
            min_space_savings,
            max_chain_depth: MAX_CHAIN_DEPTH,
        }
    }

    /// Encode a delta between base and target
    ///
    /// Returns (delta_data, should_use_delta)
    ///
    /// # Arguments
    ///
    /// * `base` - Base version data
    /// * `target` - Target version data
    ///
    /// # Returns
    ///
    /// Tuple of (delta_data, should_use_delta). If should_use_delta is false,
    /// delta_data contains the full target data instead.
    pub fn encode(&self, base: &[u8], target: &[u8]) -> CompressionResult<(Vec<u8>, bool)> {
        self.encode_with_depth(base, target, 0)
    }

    /// Encode a delta between base and target with chain depth tracking
    ///
    /// Returns (delta_data, should_use_delta)
    ///
    /// # Arguments
    ///
    /// * `base` - Base version data
    /// * `target` - Target version data
    /// * `current_chain_depth` - Current depth in the delta chain
    ///
    /// # Returns
    ///
    /// Tuple of (delta_data, should_use_delta). If should_use_delta is false,
    /// delta_data contains the full target data instead.
    ///
    /// # Delta Chain Depth
    ///
    /// When the chain depth exceeds `max_chain_depth`, the encoder will return
    /// the full object instead of creating another delta. This prevents
    /// excessively long delta chains that can hurt decompression performance.
    pub fn encode_with_depth(
        &self,
        base: &[u8],
        target: &[u8],
        current_chain_depth: u32,
    ) -> CompressionResult<(Vec<u8>, bool)> {
        let start = Instant::now();

        // Check chain depth limit
        if current_chain_depth >= self.max_chain_depth {
            debug!(
                "Chain depth {} reached max depth {}, using full compression",
                current_chain_depth, self.max_chain_depth
            );
            return Ok((target.to_vec(), false));
        }

        // Calculate similarity
        let similarity = self.calculate_similarity(base, target);
        trace!(
            "Similarity between base and target: {:.2}%",
            similarity * 100.0
        );

        // Check similarity threshold
        if similarity < self.similarity_threshold {
            debug!(
                "Similarity {:.2}% below threshold {:.2}%, using full compression",
                similarity * 100.0,
                self.similarity_threshold * 100.0
            );
            return Ok((target.to_vec(), false));
        }

        // Create delta using bsdiff, then compress with zstd
        // (bsdiff v0.2 does not compress internally; raw patches are mostly zeros for
        // similar files, so zstd compresses them extremely well)
        let delta = {
            let mut raw_patch = Vec::new();
            match bsdiff::diff(base, target, &mut raw_patch) {
                Ok(()) => zstd::bulk::compress(&raw_patch, 3).unwrap_or(raw_patch),
                Err(e) => {
                    warn!("bsdiff encoding failed: {}, falling back to full", e);
                    return Ok((target.to_vec(), false));
                }
            }
        };

        // Calculate space savings
        let space_savings = if !target.is_empty() && delta.len() < target.len() {
            (target.len() - delta.len()) as f64 / target.len() as f64
        } else if !target.is_empty() && delta.len() >= target.len() {
            // Delta is larger than target, negative savings
            -((delta.len() - target.len()) as f64 / target.len() as f64)
        } else {
            0.0
        };

        // Check if delta is beneficial
        if space_savings < self.min_space_savings {
            debug!(
                "Space savings {:.2}% below threshold {:.2}%, using full compression",
                space_savings * 100.0,
                self.min_space_savings * 100.0
            );
            return Ok((target.to_vec(), false));
        }

        let elapsed = start.elapsed();
        info!(
            "Delta encoding: {} bytes -> {} bytes ({:.1}% savings) in {:?}",
            target.len(),
            delta.len(),
            space_savings * 100.0,
            elapsed
        );

        Ok((delta, true))
    }

    /// Decode a delta to reconstruct the target
    ///
    /// # Arguments
    ///
    /// * `base` - Base version data
    /// * `delta` - Delta data
    ///
    /// # Returns
    ///
    /// Reconstructed target data
    pub fn decode(&self, base: &[u8], delta: &[u8]) -> CompressionResult<Vec<u8>> {
        let start = Instant::now();

        let result = {
            // Decompress zstd wrapper, then apply bsdiff patch
            let raw_patch = zstd::bulk::decompress(delta, 128 * 1024 * 1024).map_err(|e| {
                CompressionError::decompression_failed(format!(
                    "bsdiff zstd decompression failed: {}",
                    e
                ))
            })?;
            let mut output = Vec::new();
            bsdiff::patch(base, &mut raw_patch.as_slice(), &mut output).map_err(|e| {
                CompressionError::decompression_failed(format!("bsdiff patch failed: {}", e))
            })?;
            output
        };

        let elapsed = start.elapsed();
        if elapsed.as_millis() > MAX_RECONSTRUCTION_TIME_MS as u128 {
            warn!(
                "Delta reconstruction took {:?}, exceeds target of {}ms",
                elapsed, MAX_RECONSTRUCTION_TIME_MS
            );
        }

        trace!(
            "Delta decoding: {} bytes delta + {} bytes base -> {} bytes target in {:?}",
            delta.len(),
            base.len(),
            result.len(),
            elapsed
        );

        Ok(result)
    }

    /// Apply a delta chain to reconstruct the final version
    ///
    /// # Arguments
    ///
    /// * `versions` - Ordered list of (data, is_delta) tuples, base first
    ///
    /// # Returns
    ///
    /// Reconstructed final version
    pub fn decode_chain(&self, versions: &[(Vec<u8>, bool)]) -> CompressionResult<Vec<u8>> {
        if versions.is_empty() {
            return Err(CompressionError::invalid_input("empty version chain"));
        }

        let start = Instant::now();
        let mut current = versions[0].0.clone();

        for (i, (data, is_delta)) in versions.iter().enumerate().skip(1) {
            if *is_delta {
                trace!("Applying delta {} in chain", i);
                current = self.decode(&current, data)?;
            } else {
                // Full version, replace current
                current = data.clone();
            }
        }

        let elapsed = start.elapsed();
        if elapsed.as_millis() > MAX_RECONSTRUCTION_TIME_MS as u128 {
            warn!(
                "Delta chain reconstruction took {:?}, exceeds target of {}ms",
                elapsed, MAX_RECONSTRUCTION_TIME_MS
            );
        }

        info!(
            "Delta chain reconstruction: {} versions -> {} bytes in {:?}",
            versions.len(),
            current.len(),
            elapsed
        );

        Ok(current)
    }

    /// Calculate similarity between two byte arrays
    ///
    /// Uses a simple byte-level similarity metric based on longest common subsequence
    /// approximation. For production use, consider more sophisticated algorithms.
    fn calculate_similarity(&self, base: &[u8], target: &[u8]) -> f64 {
        if base.is_empty() && target.is_empty() {
            return 1.0;
        }

        if base.is_empty() || target.is_empty() {
            return 0.0;
        }

        // For performance, use a sampling-based similarity metric
        // This is a fast approximation that works well for large files
        let sample_size = 1024.min(base.len().min(target.len()));
        let mut matches = 0;

        // Sample at regular intervals
        let base_step = if base.len() > sample_size {
            base.len() / sample_size
        } else {
            1
        };
        let target_step = if target.len() > sample_size {
            target.len() / sample_size
        } else {
            1
        };

        for i in 0..sample_size {
            let base_idx = (i * base_step).min(base.len() - 1);
            let target_idx = (i * target_step).min(target.len() - 1);

            if base[base_idx] == target[target_idx] {
                matches += 1;
            }
        }

        matches as f64 / sample_size as f64
    }

    /// Calculate hash for data (for tracking purposes)
    pub fn hash_data(data: &[u8]) -> String {
        let hash = blake3::hash(data);
        hash.to_hex().to_string()
    }

    /// Get the maximum chain depth setting
    pub fn max_chain_depth(&self) -> u32 {
        self.max_chain_depth
    }

    /// Set the maximum chain depth
    ///
    /// When encoding deltas, if the current chain depth reaches this value,
    /// the encoder will store the full object instead of creating another delta.
    ///
    /// # Arguments
    ///
    /// * `depth` - Maximum allowed chain depth (typically 50-100)
    ///
    /// # Example
    ///
    /// ```
    /// use mediagit_compression::delta::DeltaEncoder;
    ///
    /// let mut encoder = DeltaEncoder::new();
    /// encoder.set_max_chain_depth(75);
    /// assert_eq!(encoder.max_chain_depth(), 75);
    /// ```
    pub fn set_max_chain_depth(&mut self, depth: u32) {
        self.max_chain_depth = depth;
    }

    /// Create a delta encoder with custom chain depth
    ///
    /// # Arguments
    ///
    /// * `max_chain_depth` - Maximum allowed chain depth
    ///
    /// # Example
    ///
    /// ```
    /// use mediagit_compression::delta::DeltaEncoder;
    ///
    /// let encoder = DeltaEncoder::with_max_chain_depth(100);
    /// assert_eq!(encoder.max_chain_depth(), 100);
    /// ```
    pub fn with_max_chain_depth(max_chain_depth: u32) -> Self {
        DeltaEncoder {
            similarity_threshold: SIMILARITY_THRESHOLD,
            min_space_savings: MIN_SPACE_SAVINGS,
            max_chain_depth,
        }
    }
}

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

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

    #[test]
    fn test_delta_metadata_full() {
        let meta = DeltaMetadata::full("abc123".to_string(), 1000);
        assert_eq!(meta.chain_depth, 0);
        assert!(!meta.is_delta);
        assert_eq!(meta.similarity, 1.0);
        assert_eq!(meta.compression_ratio(), 1.0);
    }

    #[test]
    fn test_delta_metadata_delta() {
        let meta = DeltaMetadata::delta("abc123".to_string(), 0, 1000, 100, 0.95);
        assert_eq!(meta.chain_depth, 1);
        assert!(meta.is_delta);
        assert_eq!(meta.similarity, 0.95);
        assert_eq!(meta.space_savings, 0.9);
        assert_eq!(meta.compression_ratio(), 0.1);
    }

    #[test]
    fn test_delta_metadata_max_depth() {
        let meta = DeltaMetadata::delta("abc".to_string(), MAX_CHAIN_DEPTH - 1, 1000, 100, 0.9);
        assert!(meta.is_at_max_depth());

        let meta2 = DeltaMetadata::delta("abc".to_string(), 5, 1000, 100, 0.9);
        assert!(!meta2.is_at_max_depth());
    }

    #[test]
    fn test_delta_chain_tracker() {
        let mut tracker = DeltaChainTracker::new();

        let meta1 = DeltaMetadata::full("base".to_string(), 1000);
        tracker.register("hash1".to_string(), meta1.clone());

        let meta2 = DeltaMetadata::delta("hash1".to_string(), 0, 900, 100, 0.95);
        tracker.register("hash2".to_string(), meta2);

        assert_eq!(tracker.get("hash1"), Some(&meta1));
        assert!(tracker.get("hash2").is_some());
        assert!(tracker.get("nonexistent").is_none());
    }

    #[test]
    fn test_find_best_base() {
        let mut tracker = DeltaChainTracker::new();

        tracker.register(
            "base".to_string(),
            DeltaMetadata::full("".to_string(), 1000),
        );
        tracker.register(
            "v1".to_string(),
            DeltaMetadata::delta("base".to_string(), 0, 900, 100, 0.9),
        );
        tracker.register(
            "v2".to_string(),
            DeltaMetadata::delta("v1".to_string(), 1, 850, 50, 0.85),
        );

        let candidates = vec!["base".to_string(), "v1".to_string(), "v2".to_string()];
        let best = tracker.find_best_base(&candidates);

        assert_eq!(best, Some("base".to_string())); // base has lowest depth (0)
    }

    #[test]
    fn test_get_chain() {
        let mut tracker = DeltaChainTracker::new();

        tracker.register(
            "base".to_string(),
            DeltaMetadata::full("".to_string(), 1000),
        );
        tracker.register(
            "v1".to_string(),
            DeltaMetadata::delta("base".to_string(), 0, 900, 100, 0.9),
        );
        tracker.register(
            "v2".to_string(),
            DeltaMetadata::delta("v1".to_string(), 1, 850, 50, 0.85),
        );

        let chain = tracker.get_chain("v2").unwrap();
        assert_eq!(chain, vec!["base", "v1", "v2"]);
    }

    #[test]
    fn test_delta_encode_decode_simple() {
        let encoder = DeltaEncoder::new();

        let base = b"Hello, World!";
        let target = b"Hello, MediaGit World!";

        let (delta, should_use_delta) = encoder.encode(base, target).unwrap();

        if should_use_delta {
            let reconstructed = encoder.decode(base, &delta).unwrap();
            assert_eq!(target, &reconstructed[..]);
        }
    }

    #[test]
    fn test_delta_encode_high_similarity() {
        let encoder = DeltaEncoder::new();

        let base = b"a".repeat(1000);
        let mut target = base.clone();
        target[500] = b'b'; // Only one byte different

        let (data, should_use_delta) = encoder.encode(&base, &target).unwrap();

        // For highly similar data, delta encoding should be beneficial
        if should_use_delta {
            assert!(
                data.len() < target.len() / 2,
                "Delta should be much smaller"
            );
            let reconstructed = encoder.decode(&base, &data).unwrap();
            assert_eq!(target, reconstructed);
        } else {
            // If delta encoding wasn't beneficial, should return full data
            assert_eq!(data, target);
        }
    }

    #[test]
    fn test_delta_encode_low_similarity() {
        let encoder = DeltaEncoder::new();

        let base: Vec<u8> = (0..1000).map(|i| (i % 256) as u8).collect();
        let target: Vec<u8> = (0..1000).map(|i| ((i + 128) % 256) as u8).collect();

        let (data, should_use_delta) = encoder.encode(&base, &target).unwrap();

        if !should_use_delta {
            // Low similarity, should return full data
            assert_eq!(data, target);
        }
    }

    #[test]
    fn test_delta_encode_identical() {
        let encoder = DeltaEncoder::new();

        let base = b"Hello, World!";
        let target = b"Hello, World!";

        let (data, should_use_delta) = encoder.encode(base, target).unwrap();

        // For very small identical data, bsdiff may not generate a delta
        // The system should fall back to returning the full data
        if should_use_delta {
            // If delta was used, verify it can be decoded
            assert!(data.len() < 50, "Delta for identical files should be tiny");
            let reconstructed = encoder.decode(base, &data).unwrap();
            assert_eq!(target, &reconstructed[..]);
        } else {
            // If delta wasn't used, should return full target data
            assert_eq!(data, target.to_vec());
        }
    }

    #[test]
    fn test_delta_decode_chain() {
        let encoder = DeltaEncoder::new();

        let v1 = b"Hello, World!";
        let v2 = b"Hello, MediaGit World!";
        let v3 = b"Hello, MediaGit Rust World!";

        let (delta2, should_use_delta2) = encoder.encode(v1, v2).unwrap();
        let (delta3, should_use_delta3) = encoder.encode(v2, v3).unwrap();

        // Build version chain based on what encoding actually produced
        let versions = vec![
            (v1.to_vec(), false),
            (delta2, should_use_delta2),
            (delta3, should_use_delta3),
        ];

        let reconstructed = encoder.decode_chain(&versions).unwrap();
        assert_eq!(v3, &reconstructed[..]);
    }

    #[test]
    fn test_similarity_calculation() {
        let encoder = DeltaEncoder::new();

        // Identical data
        let data = b"Hello, World!";
        assert_eq!(encoder.calculate_similarity(data, data), 1.0);

        // Empty data
        assert_eq!(encoder.calculate_similarity(b"", b""), 1.0);
        assert_eq!(encoder.calculate_similarity(b"test", b""), 0.0);
        assert_eq!(encoder.calculate_similarity(b"", b"test"), 0.0);

        // High similarity
        let base = b"a".repeat(1000);
        let mut similar = base.clone();
        similar[500] = b'b';
        let sim = encoder.calculate_similarity(&base, &similar);
        assert!(sim > 0.99, "Similarity should be very high: {}", sim);
    }

    #[test]
    fn test_hash_data() {
        let data1 = b"Hello, World!";
        let data2 = b"Hello, World!";
        let data3 = b"Hello, MediaGit!";

        let hash1 = DeltaEncoder::hash_data(data1);
        let hash2 = DeltaEncoder::hash_data(data2);
        let hash3 = DeltaEncoder::hash_data(data3);

        assert_eq!(hash1, hash2); // Same data should produce same hash
        assert_ne!(hash1, hash3); // Different data should produce different hash
        assert_eq!(hash1.len(), 64); // Blake3 produces 32-byte hash = 64 hex chars
    }

    #[test]
    fn test_custom_thresholds() {
        let encoder = DeltaEncoder::with_thresholds(0.5, 0.05);

        let base = b"Hello, World!";
        let target = b"Goodbye, World!";

        let (_, should_use_delta) = encoder.encode(base, target).unwrap();

        // With lower thresholds, more likely to use delta
        // (actual result depends on data characteristics)
        let _ = should_use_delta; // Just verify it doesn't panic
    }

    #[test]
    fn test_large_data() {
        let encoder = DeltaEncoder::new();

        let base = vec![0x42u8; 1024 * 100]; // 100KB
        let mut target = base.clone();
        // Modify 1% of the data
        for i in (0..target.len()).step_by(100) {
            target[i] = 0x43;
        }

        let (data, should_use_delta) = encoder.encode(&base, &target).unwrap();

        // For large data with small changes, delta should be beneficial
        if should_use_delta {
            assert!(
                data.len() < target.len() / 2,
                "Delta should be much smaller than target"
            );
            let reconstructed = encoder.decode(&base, &data).unwrap();
            assert_eq!(target, reconstructed);
        } else {
            // If delta wasn't beneficial, should return full data
            assert_eq!(data, target);
        }
    }

    #[test]
    fn test_chain_depth_enforcement() {
        let encoder = DeltaEncoder::with_max_chain_depth(3);
        assert_eq!(encoder.max_chain_depth(), 3);

        // Use larger data that will definitely benefit from delta encoding
        let base = vec![0x42u8; 1000];
        let mut target = base.clone();
        target[500] = 0x43; // Small change to make delta beneficial

        // Depth 0 - should create delta
        let (data0, should_use0) = encoder.encode_with_depth(&base, &target, 0).unwrap();
        if should_use0 {
            assert!(
                data0.len() < target.len(),
                "Delta should be smaller than full data"
            );
        }

        // Depth 2 - should still create delta
        let (_data2, should_use2) = encoder.encode_with_depth(&base, &target, 2).unwrap();
        // Should use delta as long as we're below max depth
        assert_eq!(
            should_use2, should_use0,
            "Should make same decision at depth 2"
        );

        // Depth 3 - should NOT create delta (at max depth)
        let (data3, should_use3) = encoder.encode_with_depth(&base, &target, 3).unwrap();
        assert!(!should_use3, "Should NOT use delta at max depth");
        assert_eq!(data3, target, "Should return full data at max depth");

        // Depth 4 - should NOT create delta (exceeds max depth)
        let (data4, should_use4) = encoder.encode_with_depth(&base, &target, 4).unwrap();
        assert!(!should_use4, "Should NOT use delta beyond max depth");
        assert_eq!(data4, target, "Should return full data beyond max depth");
    }

    #[test]
    fn test_max_chain_depth_setter() {
        let mut encoder = DeltaEncoder::new();

        // Default should be MAX_CHAIN_DEPTH (10)
        assert_eq!(encoder.max_chain_depth(), MAX_CHAIN_DEPTH);
        assert_eq!(encoder.max_chain_depth(), 10);

        // Set to custom value
        encoder.set_max_chain_depth(75);
        assert_eq!(encoder.max_chain_depth(), 75);

        // Set to another value
        encoder.set_max_chain_depth(25);
        assert_eq!(encoder.max_chain_depth(), 25);
    }

    #[test]
    fn test_chain_depth_with_constructor() {
        let encoder = DeltaEncoder::with_max_chain_depth(100);
        assert_eq!(encoder.max_chain_depth(), 100);

        // Verify it actually enforces the limit
        // Use structured data (repeating text pattern) that bsdiff can compress well
        let pattern = b"The quick brown fox jumps over the lazy dog. ";
        let base: Vec<u8> = pattern.iter().cycle().take(100_000).copied().collect();
        let mut target = base.clone();
        // Modify a small region
        for b in &mut target[50_000..50_100] {
            *b = b'X';
        }

        // At depth 99, should create delta
        let (_, should_use99) = encoder.encode_with_depth(&base, &target, 99).unwrap();
        assert!(should_use99, "Should use delta just below max depth");

        // At depth 100, should NOT create delta
        let (_, should_use100) = encoder.encode_with_depth(&base, &target, 100).unwrap();
        assert!(!should_use100, "Should NOT use delta at max depth");
    }

    #[test]
    fn test_encode_delegates_to_encode_with_depth() {
        let encoder = DeltaEncoder::new();

        let base = b"Test data for delta encoding";
        let target = b"Test data for delta encoding with changes";

        // encode() should delegate to encode_with_depth(0)
        let (data1, should1) = encoder.encode(base, target).unwrap();
        let (data2, should2) = encoder.encode_with_depth(base, target, 0).unwrap();

        assert_eq!(should1, should2, "Both methods should return same decision");
        assert_eq!(data1, data2, "Both methods should return same data");
    }

    #[test]
    fn test_new_category_delta_configurations() {
        use crate::smart_compressor::{ObjectCategory, ObjectType};

        // Test Creative Project configuration
        let creative_config = DeltaConfig::for_object_type(ObjectType::AdobePhotoshop);
        assert_eq!(
            creative_config.similarity_threshold, 0.70,
            "Creative projects should have 70% threshold"
        );
        assert_eq!(
            creative_config.max_chain_depth, 10,
            "Creative projects should allow depth 10"
        );
        assert_eq!(
            creative_config.min_space_savings, 0.10,
            "Creative projects should require 10% savings"
        );

        // Verify all creative project types use same config
        assert_eq!(
            ObjectType::AdobePhotoshop.category(),
            ObjectCategory::CreativeProject
        );
        assert_eq!(
            ObjectType::Blender.category(),
            ObjectCategory::CreativeProject
        );
        assert_eq!(
            ObjectType::DavinciResolve.category(),
            ObjectCategory::CreativeProject
        );

        // Test Office configuration
        let office_config = DeltaConfig::for_object_type(ObjectType::WordDocument);
        assert_eq!(
            office_config.similarity_threshold, 0.75,
            "Office docs should have 75% threshold"
        );
        assert_eq!(
            office_config.max_chain_depth, 10,
            "Office docs should allow depth 10"
        );
        assert_eq!(ObjectType::WordDocument.category(), ObjectCategory::Office);

        // Test ML Specialized configuration
        let ml_config = DeltaConfig::for_object_type(ObjectType::MlCheckpoint);
        assert_eq!(
            ml_config.similarity_threshold, 0.75,
            "ML models should have 75% threshold"
        );
        assert_eq!(
            ml_config.max_chain_depth, 8,
            "ML models should allow depth 8"
        );
        assert_eq!(
            ObjectType::MlCheckpoint.category(),
            ObjectCategory::MlSpecialized
        );
        assert_eq!(
            ObjectType::MlInference.category(),
            ObjectCategory::MlSpecialized
        );

        // Test Database configuration
        let db_config = DeltaConfig::for_object_type(ObjectType::SqliteDatabase);
        assert_eq!(
            db_config.similarity_threshold, 0.80,
            "Databases should have 80% threshold"
        );
        assert_eq!(
            db_config.max_chain_depth, 5,
            "Databases should allow depth 5"
        );
        assert_eq!(
            ObjectType::SqliteDatabase.category(),
            ObjectCategory::Database
        );
    }

    #[test]
    fn test_creative_project_delta_priority() {
        use crate::smart_compressor::ObjectType;

        // Creative projects have the most aggressive delta configuration
        let creative_config = DeltaConfig::for_object_type(ObjectType::AdobePhotoshop);
        let image_config = DeltaConfig::for_object_type(ObjectType::Tiff);
        let video_config = DeltaConfig::for_object_type(ObjectType::Mp4);

        // Creative projects should have lower threshold than images
        assert!(
            creative_config.similarity_threshold < image_config.similarity_threshold,
            "Creative projects (0.70) should have lower threshold than images (0.85)"
        );

        // Creative projects should allow longer chains than most types
        assert!(
            creative_config.max_chain_depth >= image_config.max_chain_depth,
            "Creative projects should allow equal or longer chains"
        );

        // Video has highest threshold (metadata changes only)
        assert!(
            video_config.similarity_threshold > creative_config.similarity_threshold,
            "Video (0.95) should have higher threshold than creative projects (0.70)"
        );
    }

    #[test]
    fn test_delta_roundtrip_creative_project() {
        use crate::smart_compressor::ObjectType;

        let encoder = DeltaEncoder::new();

        // Use a more realistic test - text-based content like .blend or XML-based creative files
        // Simulate creative project data with high similarity
        let base_content = b"<CreativeProjectLayer id='1' opacity='100%'>".repeat(100);
        let base_data = base_content.clone();

        // Modified version (small change - like adjusting opacity in one layer)
        let mut modified = base_data.clone();
        // Change a few bytes to simulate minor edits
        for i in (0..modified.len()).step_by(500) {
            if i < modified.len() {
                modified[i] = b'X';
            }
        }

        // Calculate similarity
        let similarity = encoder.calculate_similarity(&base_data, &modified);

        // Get config for creative projects
        let config = DeltaConfig::for_object_type(ObjectType::AdobePhotoshop);

        // Verify similarity threshold
        // Creative projects have 0.70 threshold, should be met with minor changes
        assert!(
            similarity >= config.similarity_threshold,
            "Similarity {:.2} should meet creative project threshold {:.2}",
            similarity,
            config.similarity_threshold
        );

        // Test that config is correctly applied
        assert_eq!(
            config.max_chain_depth, 10,
            "Creative projects should allow depth 10"
        );
        assert_eq!(
            config.similarity_threshold, 0.70,
            "Creative projects should have 70% threshold"
        );

        // Verify the category
        assert_eq!(
            ObjectType::AdobePhotoshop.category(),
            crate::smart_compressor::ObjectCategory::CreativeProject,
            "AdobePhotoshop should be in CreativeProject category"
        );
    }
}