heliosdb-nano 3.30.0

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
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
//! Conflict Detection and Resolution for HeliosDB-Lite v2.3.0
//!
//! Implements comprehensive conflict detection and resolution strategies
//! for multi-replica synchronization scenarios.
//!
//! # Features
//!
//! - Multiple conflict types (Update-Update, Update-Delete, Delete-Update, Insert-Insert)
//! - Multiple resolution strategies (Last-Write-Wins, Vector Clock Causal, Custom)
//! - Deterministic conflict resolution with tie-breaking
//! - Thread-safe conflict logging and reporting
//! - Sub-millisecond conflict detection performance

use super::{RowDelta, RowId, VectorClock};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::sync::{Arc, RwLock};
use std::time::SystemTime;
use uuid::Uuid;

/// Conflict types that can occur during synchronization
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum ConflictType {
    /// Two replicas updated the same row concurrently
    UpdateUpdate,
    /// One replica updated while another deleted
    UpdateDelete,
    /// One replica deleted while another updated
    DeleteUpdate,
    /// Same row_id inserted on multiple replicas (rare but possible with manual ID assignment)
    InsertInsert,
}

/// Represents a detected conflict between local and remote changes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Conflict {
    /// Unique conflict identifier
    pub id: Uuid,
    /// Type of conflict detected
    pub conflict_type: ConflictType,
    /// Table where conflict occurred
    pub table: String,
    /// Row identifier
    pub row_id: RowId,
    /// Local change entry
    pub local_entry: ChangeEntry,
    /// Remote change entry
    pub remote_entry: ChangeEntry,
    /// Local vector clock at time of conflict
    pub local_vector_clock: VectorClock,
    /// Remote vector clock at time of conflict
    pub remote_vector_clock: VectorClock,
    /// When the conflict was detected
    pub detected_at: DateTime<Utc>,
}

/// Represents a change entry (either local or remote)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangeEntry {
    /// The actual data (serialized row)
    pub data: Vec<u8>,
    /// Timestamp of the change
    pub timestamp: DateTime<Utc>,
    /// Node that made the change
    pub node_id: Uuid,
    /// Vector clock at time of change
    pub vector_clock: VectorClock,
    /// Operation type
    pub operation: ChangeOperation,
}

/// Operation type for a change
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ChangeOperation {
    Insert,
    Update,
    Delete,
}

/// Conflict resolution report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConflictReport {
    /// The conflict that was resolved
    pub conflict: Conflict,
    /// Resolution strategy used
    pub resolution: ConflictResolution,
    /// Winner of the resolution ("local" or "remote")
    pub winner: String,
    /// Explanation of why this resolution was chosen
    pub reason: String,
    /// When the conflict was resolved
    pub resolved_at: DateTime<Utc>,
}

/// Conflict resolution strategies
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ConflictResolution {
    /// Use timestamp-based resolution (with node_id tie-breaking)
    LastWriteWins,
    /// Use vector clock causality ordering
    VectorClockCausal,
    /// Require manual intervention
    Manual,
}

/// Conflict detector and resolver
pub struct ConflictDetector {
    /// Resolution strategy to use
    resolution_strategy: ConflictResolution,
    /// Log of all conflicts and their resolutions
    conflict_log: Arc<RwLock<Vec<ConflictReport>>>,
    /// Node ID for tie-breaking
    node_id: Uuid,
}

impl ConflictDetector {
    /// Create a new conflict detector with the specified resolution strategy
    ///
    /// # Arguments
    ///
    /// * `strategy` - The conflict resolution strategy to use
    /// * `node_id` - This node's unique identifier
    ///
    /// # Example
    ///
    /// ```no_run
    /// use heliosdb_nano::sync::conflict::{ConflictDetector, ConflictResolution};
    /// use uuid::Uuid;
    ///
    /// let detector = ConflictDetector::new(
    ///     ConflictResolution::VectorClockCausal,
    ///     Uuid::new_v4()
    /// );
    /// ```
    pub fn new(strategy: ConflictResolution, node_id: Uuid) -> Self {
        Self {
            resolution_strategy: strategy,
            conflict_log: Arc::new(RwLock::new(Vec::new())),
            node_id,
        }
    }

    /// Detect if two change entries conflict with each other
    ///
    /// Two changes conflict if they target the same row and are causally concurrent
    /// according to their vector clocks.
    ///
    /// # Performance
    ///
    /// This operation completes in <1ms for typical vector clock sizes
    ///
    /// # Arguments
    ///
    /// * `table` - Table name
    /// * `row_id` - Row identifier
    /// * `local` - Local change entry
    /// * `remote` - Remote change entry
    ///
    /// # Returns
    ///
    /// `Some(Conflict)` if a conflict is detected, `None` otherwise
    pub fn detect(
        &self,
        table: &str,
        row_id: &RowId,
        local: &ChangeEntry,
        remote: &ChangeEntry,
    ) -> Option<Conflict> {
        // Check if changes are causally concurrent
        if !self.is_concurrent(&local.vector_clock, &remote.vector_clock) {
            return None;
        }

        // Determine conflict type based on operations
        let conflict_type = match (&local.operation, &remote.operation) {
            (ChangeOperation::Update, ChangeOperation::Update) => ConflictType::UpdateUpdate,
            (ChangeOperation::Update, ChangeOperation::Delete) => ConflictType::UpdateDelete,
            (ChangeOperation::Delete, ChangeOperation::Update) => ConflictType::DeleteUpdate,
            (ChangeOperation::Insert, ChangeOperation::Insert) => ConflictType::InsertInsert,
            _ => return None, // No conflict for other combinations
        };

        Some(Conflict {
            id: Uuid::new_v4(),
            conflict_type,
            table: table.to_string(),
            row_id: row_id.clone(),
            local_entry: local.clone(),
            remote_entry: remote.clone(),
            local_vector_clock: local.vector_clock.clone(),
            remote_vector_clock: remote.vector_clock.clone(),
            detected_at: Utc::now(),
        })
    }

    /// Resolve a detected conflict according to the configured strategy
    ///
    /// # Arguments
    ///
    /// * `conflict` - The conflict to resolve
    ///
    /// # Returns
    ///
    /// `Ok(ConflictReport)` with the resolution details, or `Err` if resolution failed
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use heliosdb_nano::sync::conflict::{ConflictDetector, ConflictResolution};
    /// # use uuid::Uuid;
    /// # let detector = ConflictDetector::new(ConflictResolution::LastWriteWins, Uuid::new_v4());
    /// # let conflict = todo!();
    /// let report = detector.resolve(conflict)?;
    /// println!("Winner: {}", report.winner);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn resolve(&self, conflict: Conflict) -> Result<ConflictReport, ConflictError> {
        let (winner, reason) = match self.resolution_strategy {
            ConflictResolution::LastWriteWins => self.lww_resolve(&conflict),
            ConflictResolution::VectorClockCausal => self.vector_clock_resolve(&conflict),
            ConflictResolution::Manual => {
                return Err(ConflictError::ManualResolutionRequired(conflict.id))
            }
        };

        let report = ConflictReport {
            conflict,
            resolution: self.resolution_strategy.clone(),
            winner,
            reason,
            resolved_at: Utc::now(),
        };

        // Log the resolution
        if let Ok(mut log) = self.conflict_log.write() {
            log.push(report.clone());
        }

        Ok(report)
    }

    /// Apply the resolution to determine which change should be kept
    ///
    /// # Arguments
    ///
    /// * `report` - The conflict report with resolution
    ///
    /// # Returns
    ///
    /// The winning `ChangeEntry` to be applied
    pub fn apply_resolution(&self, report: &ConflictReport) -> Result<ChangeEntry, ConflictError> {
        match report.winner.as_str() {
            "local" => Ok(report.conflict.local_entry.clone()),
            "remote" => Ok(report.conflict.remote_entry.clone()),
            _ => Err(ConflictError::InvalidWinner(report.winner.clone())),
        }
    }

    /// Get all conflicts since a specific time
    ///
    /// # Arguments
    ///
    /// * `since` - Only return conflicts detected after this time
    ///
    /// # Returns
    ///
    /// Vector of conflict reports matching the criteria
    pub fn get_conflicts(&self, since: SystemTime) -> Vec<ConflictReport> {
        let since_datetime = DateTime::<Utc>::from(since);

        match self.conflict_log.read() {
            Ok(log) => log
                .iter()
                .filter(|report| report.conflict.detected_at > since_datetime)
                .cloned()
                .collect(),
            Err(_) => Vec::new(),
        }
    }

    /// Get conflict statistics
    ///
    /// # Returns
    ///
    /// `ConflictStats` with counts and metrics
    pub fn get_stats(&self) -> ConflictStats {
        match self.conflict_log.read() {
            Ok(log) => {
                let total = log.len();
                let local_wins = log.iter().filter(|r| r.winner == "local").count();
                let remote_wins = log.iter().filter(|r| r.winner == "remote").count();

                let mut by_type = std::collections::HashMap::new();
                for report in log.iter() {
                    *by_type.entry(report.conflict.conflict_type.clone()).or_insert(0) += 1;
                }

                ConflictStats {
                    total_conflicts: total,
                    local_wins,
                    remote_wins,
                    by_type,
                }
            }
            Err(_) => ConflictStats::default(),
        }
    }

    /// Clear the conflict log
    ///
    /// Useful for testing or periodic cleanup
    pub fn clear_log(&self) {
        if let Ok(mut log) = self.conflict_log.write() {
            log.clear();
        }
    }

    // Private helper methods

    /// Check if two vector clocks are concurrent (neither happens-before the other)
    fn is_concurrent(&self, local_vc: &VectorClock, remote_vc: &VectorClock) -> bool {
        !local_vc.happens_before(remote_vc) && !remote_vc.happens_before(local_vc)
    }

    /// Last-Write-Wins resolution with deterministic tie-breaking
    ///
    /// Algorithm:
    /// 1. Compare timestamps - later timestamp wins
    /// 2. If timestamps equal, compare node_ids lexicographically
    /// 3. This ensures deterministic resolution across all replicas
    fn lww_resolve(&self, conflict: &Conflict) -> (String, String) {
        let local_ts = conflict.local_entry.timestamp;
        let remote_ts = conflict.remote_entry.timestamp;

        if remote_ts > local_ts {
            (
                "remote".to_string(),
                format!("Remote timestamp ({}) is later than local ({})",
                    remote_ts, local_ts),
            )
        } else if local_ts > remote_ts {
            (
                "local".to_string(),
                format!("Local timestamp ({}) is later than remote ({})",
                    local_ts, remote_ts),
            )
        } else {
            // Timestamps are equal - use node_id for deterministic tie-breaking
            let local_node = conflict.local_entry.node_id;
            let remote_node = conflict.remote_entry.node_id;

            if remote_node > local_node {
                (
                    "remote".to_string(),
                    format!("Timestamp tie - remote node_id ({}) > local node_id ({})",
                        remote_node, local_node),
                )
            } else {
                (
                    "local".to_string(),
                    format!("Timestamp tie - local node_id ({}) >= remote node_id ({})",
                        local_node, remote_node),
                )
            }
        }
    }

    /// Vector clock causal resolution with LWW fallback
    ///
    /// Algorithm:
    /// 1. If remote happens-before local, local wins (local is causally later)
    /// 2. If local happens-before remote, remote wins (remote is causally later)
    /// 3. If concurrent (shouldn't happen if detect() worked), fall back to LWW
    fn vector_clock_resolve(&self, conflict: &Conflict) -> (String, String) {
        let local_vc = &conflict.local_vector_clock;
        let remote_vc = &conflict.remote_vector_clock;

        if remote_vc.happens_before(local_vc) {
            (
                "local".to_string(),
                "Local change is causally later than remote".to_string(),
            )
        } else if local_vc.happens_before(remote_vc) {
            (
                "remote".to_string(),
                "Remote change is causally later than local".to_string(),
            )
        } else {
            // Concurrent - fall back to LWW
            let (winner, _) = self.lww_resolve(conflict);
            (
                winner.clone(),
                format!("Changes are concurrent - falling back to LWW, {} wins", winner),
            )
        }
    }
}

impl Default for ConflictDetector {
    fn default() -> Self {
        Self::new(ConflictResolution::VectorClockCausal, Uuid::new_v4())
    }
}

/// Conflict statistics
#[derive(Debug, Clone, Default)]
pub struct ConflictStats {
    pub total_conflicts: usize,
    pub local_wins: usize,
    pub remote_wins: usize,
    pub by_type: std::collections::HashMap<ConflictType, usize>,
}

/// Conflict detection and resolution errors
#[derive(Debug, thiserror::Error)]
pub enum ConflictError {
    #[error("Manual resolution required for conflict {0}")]
    ManualResolutionRequired(Uuid),

    #[error("Invalid winner specified: {0}")]
    InvalidWinner(String),

    #[error("Failed to log conflict: {0}")]
    LoggingFailed(String),

    #[error("Conflict resolution failed: {0}")]
    ResolutionFailed(String),
}

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

    fn create_test_entry(
        node_id: Uuid,
        timestamp: DateTime<Utc>,
        operation: ChangeOperation,
    ) -> ChangeEntry {
        let mut vc = VectorClock::new();
        vc.increment(node_id);

        ChangeEntry {
            data: vec![1, 2, 3],
            timestamp,
            node_id,
            vector_clock: vc,
            operation,
        }
    }

    #[test]
    fn test_detect_update_update_conflict() {
        let detector = ConflictDetector::default();
        let node1 = Uuid::new_v4();
        let node2 = Uuid::new_v4();

        let mut local = create_test_entry(node1, Utc::now(), ChangeOperation::Update);
        let mut remote = create_test_entry(node2, Utc::now(), ChangeOperation::Update);

        // Make them concurrent
        local.vector_clock.increment(node1);
        remote.vector_clock.increment(node2);

        let conflict = detector.detect("users", &vec![1], &local, &remote);

        assert!(conflict.is_some());
        let conflict = conflict.expect("Expected conflict");
        assert_eq!(conflict.conflict_type, ConflictType::UpdateUpdate);
        assert_eq!(conflict.table, "users");
    }

    #[test]
    fn test_detect_update_delete_conflict() {
        let detector = ConflictDetector::default();
        let node1 = Uuid::new_v4();
        let node2 = Uuid::new_v4();

        let mut local = create_test_entry(node1, Utc::now(), ChangeOperation::Update);
        let mut remote = create_test_entry(node2, Utc::now(), ChangeOperation::Delete);

        local.vector_clock.increment(node1);
        remote.vector_clock.increment(node2);

        let conflict = detector.detect("orders", &vec![42], &local, &remote);

        assert!(conflict.is_some());
        let conflict = conflict.expect("Expected conflict");
        assert_eq!(conflict.conflict_type, ConflictType::UpdateDelete);
    }

    #[test]
    fn test_detect_delete_update_conflict() {
        let detector = ConflictDetector::default();
        let node1 = Uuid::new_v4();
        let node2 = Uuid::new_v4();

        let mut local = create_test_entry(node1, Utc::now(), ChangeOperation::Delete);
        let mut remote = create_test_entry(node2, Utc::now(), ChangeOperation::Update);

        local.vector_clock.increment(node1);
        remote.vector_clock.increment(node2);

        let conflict = detector.detect("products", &vec![99], &local, &remote);

        assert!(conflict.is_some());
        let conflict = conflict.expect("Expected conflict");
        assert_eq!(conflict.conflict_type, ConflictType::DeleteUpdate);
    }

    #[test]
    fn test_detect_insert_insert_conflict() {
        let detector = ConflictDetector::default();
        let node1 = Uuid::new_v4();
        let node2 = Uuid::new_v4();

        let mut local = create_test_entry(node1, Utc::now(), ChangeOperation::Insert);
        let mut remote = create_test_entry(node2, Utc::now(), ChangeOperation::Insert);

        local.vector_clock.increment(node1);
        remote.vector_clock.increment(node2);

        let conflict = detector.detect("items", &vec![7], &local, &remote);

        assert!(conflict.is_some());
        let conflict = conflict.expect("Expected conflict");
        assert_eq!(conflict.conflict_type, ConflictType::InsertInsert);
    }

    #[test]
    fn test_no_conflict_when_not_concurrent() {
        let detector = ConflictDetector::default();
        let node1 = Uuid::new_v4();

        let mut local = create_test_entry(node1, Utc::now(), ChangeOperation::Update);
        let mut remote = create_test_entry(node1, Utc::now(), ChangeOperation::Update);

        // Make local happen-after remote
        remote.vector_clock.increment(node1);
        local.vector_clock = remote.vector_clock.clone();
        local.vector_clock.increment(node1);

        let conflict = detector.detect("users", &vec![1], &local, &remote);

        assert!(conflict.is_none(), "Should not detect conflict when not concurrent");
    }

    #[test]
    fn test_lww_resolve_later_timestamp() {
        let detector = ConflictDetector::new(ConflictResolution::LastWriteWins, Uuid::new_v4());
        let node1 = Uuid::new_v4();
        let node2 = Uuid::new_v4();

        let now = Utc::now();
        let later = now + chrono::Duration::seconds(10);

        let local = create_test_entry(node1, now, ChangeOperation::Update);
        let remote = create_test_entry(node2, later, ChangeOperation::Update);

        let conflict = Conflict {
            id: Uuid::new_v4(),
            conflict_type: ConflictType::UpdateUpdate,
            table: "users".to_string(),
            row_id: vec![1],
            local_entry: local.clone(),
            remote_entry: remote.clone(),
            local_vector_clock: local.vector_clock.clone(),
            remote_vector_clock: remote.vector_clock.clone(),
            detected_at: Utc::now(),
        };

        let report = detector.resolve(conflict).expect("Resolution failed");
        assert_eq!(report.winner, "remote", "Remote should win with later timestamp");
    }

    #[test]
    fn test_lww_resolve_earlier_timestamp() {
        let detector = ConflictDetector::new(ConflictResolution::LastWriteWins, Uuid::new_v4());
        let node1 = Uuid::new_v4();
        let node2 = Uuid::new_v4();

        let now = Utc::now();
        let earlier = now - chrono::Duration::seconds(10);

        let local = create_test_entry(node1, now, ChangeOperation::Update);
        let remote = create_test_entry(node2, earlier, ChangeOperation::Update);

        let conflict = Conflict {
            id: Uuid::new_v4(),
            conflict_type: ConflictType::UpdateUpdate,
            table: "users".to_string(),
            row_id: vec![1],
            local_entry: local.clone(),
            remote_entry: remote.clone(),
            local_vector_clock: local.vector_clock.clone(),
            remote_vector_clock: remote.vector_clock.clone(),
            detected_at: Utc::now(),
        };

        let report = detector.resolve(conflict).expect("Resolution failed");
        assert_eq!(report.winner, "local", "Local should win with later timestamp");
    }

    #[test]
    fn test_lww_resolve_tie_breaking() {
        let detector = ConflictDetector::new(ConflictResolution::LastWriteWins, Uuid::new_v4());
        let node1 = Uuid::new_v4();
        let node2 = Uuid::new_v4();

        let now = Utc::now();

        let local = create_test_entry(node1, now, ChangeOperation::Update);
        let remote = create_test_entry(node2, now, ChangeOperation::Update);

        let conflict = Conflict {
            id: Uuid::new_v4(),
            conflict_type: ConflictType::UpdateUpdate,
            table: "users".to_string(),
            row_id: vec![1],
            local_entry: local.clone(),
            remote_entry: remote.clone(),
            local_vector_clock: local.vector_clock.clone(),
            remote_vector_clock: remote.vector_clock.clone(),
            detected_at: Utc::now(),
        };

        let report = detector.resolve(conflict).expect("Resolution failed");

        // Winner should be deterministic based on node_id comparison
        if node2 > node1 {
            assert_eq!(report.winner, "remote");
        } else {
            assert_eq!(report.winner, "local");
        }
    }

    #[test]
    fn test_vector_clock_resolve_local_later() {
        let detector = ConflictDetector::new(
            ConflictResolution::VectorClockCausal,
            Uuid::new_v4(),
        );
        let node1 = Uuid::new_v4();
        let node2 = Uuid::new_v4();

        let mut local = create_test_entry(node1, Utc::now(), ChangeOperation::Update);
        let remote = create_test_entry(node2, Utc::now(), ChangeOperation::Update);

        // Make local causally after remote
        local.vector_clock.merge(&remote.vector_clock);
        local.vector_clock.increment(node1);

        // For this test, we manually create a conflict even though they're not concurrent
        let conflict = Conflict {
            id: Uuid::new_v4(),
            conflict_type: ConflictType::UpdateUpdate,
            table: "users".to_string(),
            row_id: vec![1],
            local_entry: local.clone(),
            remote_entry: remote.clone(),
            local_vector_clock: local.vector_clock.clone(),
            remote_vector_clock: remote.vector_clock.clone(),
            detected_at: Utc::now(),
        };

        let report = detector.resolve(conflict).expect("Resolution failed");
        assert_eq!(report.winner, "local", "Local should win as it's causally later");
    }

    #[test]
    fn test_vector_clock_resolve_remote_later() {
        let detector = ConflictDetector::new(
            ConflictResolution::VectorClockCausal,
            Uuid::new_v4(),
        );
        let node1 = Uuid::new_v4();
        let node2 = Uuid::new_v4();

        let local = create_test_entry(node1, Utc::now(), ChangeOperation::Update);
        let mut remote = create_test_entry(node2, Utc::now(), ChangeOperation::Update);

        // Make remote causally after local
        remote.vector_clock.merge(&local.vector_clock);
        remote.vector_clock.increment(node2);

        let conflict = Conflict {
            id: Uuid::new_v4(),
            conflict_type: ConflictType::UpdateUpdate,
            table: "users".to_string(),
            row_id: vec![1],
            local_entry: local.clone(),
            remote_entry: remote.clone(),
            local_vector_clock: local.vector_clock.clone(),
            remote_vector_clock: remote.vector_clock.clone(),
            detected_at: Utc::now(),
        };

        let report = detector.resolve(conflict).expect("Resolution failed");
        assert_eq!(report.winner, "remote", "Remote should win as it's causally later");
    }

    #[test]
    fn test_manual_resolution_error() {
        let detector = ConflictDetector::new(ConflictResolution::Manual, Uuid::new_v4());
        let node1 = Uuid::new_v4();
        let node2 = Uuid::new_v4();

        let local = create_test_entry(node1, Utc::now(), ChangeOperation::Update);
        let remote = create_test_entry(node2, Utc::now(), ChangeOperation::Update);

        let conflict = Conflict {
            id: Uuid::new_v4(),
            conflict_type: ConflictType::UpdateUpdate,
            table: "users".to_string(),
            row_id: vec![1],
            local_entry: local.clone(),
            remote_entry: remote.clone(),
            local_vector_clock: local.vector_clock.clone(),
            remote_vector_clock: remote.vector_clock.clone(),
            detected_at: Utc::now(),
        };

        let result = detector.resolve(conflict);
        assert!(result.is_err(), "Manual resolution should return error");
    }

    #[test]
    fn test_apply_resolution_local_winner() {
        let detector = ConflictDetector::default();
        let node1 = Uuid::new_v4();
        let node2 = Uuid::new_v4();

        let local = create_test_entry(node1, Utc::now(), ChangeOperation::Update);
        let remote = create_test_entry(node2, Utc::now(), ChangeOperation::Update);

        let conflict = Conflict {
            id: Uuid::new_v4(),
            conflict_type: ConflictType::UpdateUpdate,
            table: "users".to_string(),
            row_id: vec![1],
            local_entry: local.clone(),
            remote_entry: remote.clone(),
            local_vector_clock: local.vector_clock.clone(),
            remote_vector_clock: remote.vector_clock.clone(),
            detected_at: Utc::now(),
        };

        let report = ConflictReport {
            conflict: conflict.clone(),
            resolution: ConflictResolution::LastWriteWins,
            winner: "local".to_string(),
            reason: "Test".to_string(),
            resolved_at: Utc::now(),
        };

        let result = detector.apply_resolution(&report).expect("Apply failed");
        assert_eq!(result.node_id, local.node_id);
    }

    #[test]
    fn test_apply_resolution_remote_winner() {
        let detector = ConflictDetector::default();
        let node1 = Uuid::new_v4();
        let node2 = Uuid::new_v4();

        let local = create_test_entry(node1, Utc::now(), ChangeOperation::Update);
        let remote = create_test_entry(node2, Utc::now(), ChangeOperation::Update);

        let conflict = Conflict {
            id: Uuid::new_v4(),
            conflict_type: ConflictType::UpdateUpdate,
            table: "users".to_string(),
            row_id: vec![1],
            local_entry: local.clone(),
            remote_entry: remote.clone(),
            local_vector_clock: local.vector_clock.clone(),
            remote_vector_clock: remote.vector_clock.clone(),
            detected_at: Utc::now(),
        };

        let report = ConflictReport {
            conflict: conflict.clone(),
            resolution: ConflictResolution::LastWriteWins,
            winner: "remote".to_string(),
            reason: "Test".to_string(),
            resolved_at: Utc::now(),
        };

        let result = detector.apply_resolution(&report).expect("Apply failed");
        assert_eq!(result.node_id, remote.node_id);
    }

    #[test]
    fn test_get_conflicts_filtering() {
        let detector = ConflictDetector::new(ConflictResolution::LastWriteWins, Uuid::new_v4());
        let node1 = Uuid::new_v4();
        let node2 = Uuid::new_v4();

        let local = create_test_entry(node1, Utc::now(), ChangeOperation::Update);
        let remote = create_test_entry(node2, Utc::now(), ChangeOperation::Update);

        // Create and resolve a conflict
        let conflict = Conflict {
            id: Uuid::new_v4(),
            conflict_type: ConflictType::UpdateUpdate,
            table: "users".to_string(),
            row_id: vec![1],
            local_entry: local.clone(),
            remote_entry: remote.clone(),
            local_vector_clock: local.vector_clock.clone(),
            remote_vector_clock: remote.vector_clock.clone(),
            detected_at: Utc::now(),
        };

        detector.resolve(conflict).expect("Resolution failed");

        // Get conflicts from before the conflict was created
        let past = SystemTime::now() - std::time::Duration::from_secs(10);
        let conflicts = detector.get_conflicts(past);
        assert_eq!(conflicts.len(), 1);

        // Get conflicts from the future
        let future = SystemTime::now() + std::time::Duration::from_secs(10);
        let conflicts = detector.get_conflicts(future);
        assert_eq!(conflicts.len(), 0);
    }

    #[test]
    fn test_conflict_stats() {
        let detector = ConflictDetector::new(ConflictResolution::LastWriteWins, Uuid::new_v4());
        let node1 = Uuid::new_v4();
        let node2 = Uuid::new_v4();

        // Create multiple conflicts with known outcomes
        for i in 0..5 {
            let now = Utc::now();
            let local = create_test_entry(node1, now, ChangeOperation::Update);
            let remote = create_test_entry(
                node2,
                now + chrono::Duration::seconds(i),
                ChangeOperation::Update,
            );

            let conflict = Conflict {
                id: Uuid::new_v4(),
                conflict_type: ConflictType::UpdateUpdate,
                table: "users".to_string(),
                row_id: vec![i as u8],
                local_entry: local.clone(),
                remote_entry: remote.clone(),
                local_vector_clock: local.vector_clock.clone(),
                remote_vector_clock: remote.vector_clock.clone(),
                detected_at: Utc::now(),
            };

            detector.resolve(conflict).expect("Resolution failed");
        }

        let stats = detector.get_stats();
        assert_eq!(stats.total_conflicts, 5);
        assert!(stats.remote_wins > 0, "Should have some remote wins");
    }

    #[test]
    fn test_deterministic_resolution() {
        // Test that same inputs always produce same output
        let node1 = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000")
            .expect("Valid UUID");
        let node2 = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440001")
            .expect("Valid UUID");

        let detector1 = ConflictDetector::new(ConflictResolution::LastWriteWins, node1);
        let detector2 = ConflictDetector::new(ConflictResolution::LastWriteWins, node2);

        let timestamp = DateTime::parse_from_rfc3339("2025-01-01T00:00:00Z")
            .expect("Valid timestamp")
            .with_timezone(&Utc);

        let local = create_test_entry(node1, timestamp, ChangeOperation::Update);
        let remote = create_test_entry(node2, timestamp, ChangeOperation::Update);

        let conflict1 = Conflict {
            id: Uuid::new_v4(),
            conflict_type: ConflictType::UpdateUpdate,
            table: "users".to_string(),
            row_id: vec![1],
            local_entry: local.clone(),
            remote_entry: remote.clone(),
            local_vector_clock: local.vector_clock.clone(),
            remote_vector_clock: remote.vector_clock.clone(),
            detected_at: timestamp,
        };

        let conflict2 = conflict1.clone();

        let report1 = detector1.resolve(conflict1).expect("Resolution failed");
        let report2 = detector2.resolve(conflict2).expect("Resolution failed");

        // Both detectors should resolve to the same winner
        assert_eq!(
            report1.winner, report2.winner,
            "Deterministic resolution failed"
        );
    }

    #[test]
    fn test_clear_log() {
        let detector = ConflictDetector::default();
        let node1 = Uuid::new_v4();
        let node2 = Uuid::new_v4();

        let local = create_test_entry(node1, Utc::now(), ChangeOperation::Update);
        let remote = create_test_entry(node2, Utc::now(), ChangeOperation::Update);

        let conflict = Conflict {
            id: Uuid::new_v4(),
            conflict_type: ConflictType::UpdateUpdate,
            table: "users".to_string(),
            row_id: vec![1],
            local_entry: local.clone(),
            remote_entry: remote.clone(),
            local_vector_clock: local.vector_clock.clone(),
            remote_vector_clock: remote.vector_clock.clone(),
            detected_at: Utc::now(),
        };

        detector.resolve(conflict).expect("Resolution failed");

        let stats_before = detector.get_stats();
        assert_eq!(stats_before.total_conflicts, 1);

        detector.clear_log();

        let stats_after = detector.get_stats();
        assert_eq!(stats_after.total_conflicts, 0);
    }
}