voirs-sdk 0.1.0-rc.1

Unified SDK and public API for VoiRS speech synthesis
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
use super::*;
use chrono::{DateTime, Utc};
use oxiarc_deflate::{GzipStreamDecoder, GzipStreamEncoder};
use sha2::{Digest, Sha256};
use std::collections::BTreeMap;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::fs;
use tokio::sync::Mutex;

#[cfg(feature = "cloud")]
use oxiarc_zstd;

/// Cloud storage implementation for VoiRS models
pub struct VoirsCloudStorage {
    config: CloudConfig,
    local_cache: Arc<Mutex<LocalCache>>,
    sync_manager: Arc<SyncManager>,
    backup_manager: Arc<BackupManager>,
    version_manager: Arc<VersionManager>,
}

struct LocalCache {
    models: BTreeMap<String, CachedModel>,
    cache_dir: PathBuf,
    max_size_bytes: u64,
    current_size_bytes: AtomicU64,
}

struct CachedModel {
    metadata: ModelMetadata,
    local_path: PathBuf,
    last_accessed: DateTime<Utc>,
    is_dirty: bool,
}

struct SyncManager {
    sync_queue: Arc<Mutex<Vec<SyncOperation>>>,
    sync_status: Arc<Mutex<SyncStatus>>,
}

struct BackupManager {
    backup_storage: Arc<dyn BackupStorage>,
    backup_schedule: BackupSchedule,
}

struct VersionManager {
    versions: Arc<Mutex<BTreeMap<String, Vec<ModelVersion>>>>,
    current_versions: Arc<Mutex<BTreeMap<String, String>>>,
}

/// Type of synchronization operation
#[derive(Debug, Clone)]
pub enum SyncOperation {
    /// Upload operation
    Upload(String),
    /// Download operation
    Download(String),
    /// Delete operation
    Delete(String),
    /// Verify operation
    Verify(String),
}

/// Status of cloud synchronization operations
#[derive(Debug, Clone)]
pub struct SyncStatus {
    /// Whether synchronization is currently in progress
    pub in_progress: bool,
    /// Timestamp of the last successful sync
    pub last_sync: Option<DateTime<Utc>>,
    /// Number of pending sync operations
    pub pending_operations: usize,
    /// List of errors encountered during sync
    pub errors: Vec<SyncError>,
    /// Number of models synchronized
    pub models_synced: u32,
    /// Number of models updated
    pub models_updated: u32,
    /// Number of models deleted
    pub models_deleted: u32,
}

/// Error that occurred during synchronization
#[derive(Debug, Clone)]
pub struct SyncError {
    /// The operation that failed
    pub operation: SyncOperation,
    /// Error message
    pub error: String,
    /// When the error occurred
    pub timestamp: DateTime<Utc>,
    /// Number of retry attempts
    pub retry_count: u32,
}

#[derive(Debug, Clone)]
struct BackupSchedule {
    enabled: bool,
    interval_hours: u32,
    retention_days: u32,
    incremental: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct ModelVersion {
    id: String,
    version: String,
    checksum: String,
    size_bytes: u64,
    created_at: DateTime<Utc>,
    changes: Vec<String>,
    parent_version: Option<String>,
}

#[async_trait::async_trait]
trait BackupStorage: Send + Sync {
    async fn store_backup(&self, backup: &BackupData) -> Result<String>;
    async fn retrieve_backup(&self, backup_id: &str) -> Result<BackupData>;
    async fn list_backups(&self) -> Result<Vec<BackupInfo>>;
    async fn delete_backup(&self, backup_id: &str) -> Result<()>;
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct BackupData {
    id: String,
    models: Vec<ModelMetadata>,
    data: Vec<u8>,
    compression: CompressionType,
    encryption: Option<EncryptionInfo>,
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
enum CompressionType {
    None,
    Gzip,
    Zstd,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct EncryptionInfo {
    algorithm: String,
    key_id: String,
    nonce: Vec<u8>,
}

impl VoirsCloudStorage {
    pub async fn new(config: CloudConfig, cache_dir: PathBuf) -> Result<Self> {
        // Ensure cache directory exists
        fs::create_dir_all(&cache_dir).await.map_err(|e| {
            VoirsError::config_error(format!("Failed to create cache directory: {}", e))
        })?;

        let local_cache = Arc::new(Mutex::new(LocalCache {
            models: BTreeMap::new(),
            cache_dir: cache_dir.clone(),
            max_size_bytes: 10_000_000_000, // 10GB default
            current_size_bytes: AtomicU64::new(0),
        }));

        let sync_manager = Arc::new(SyncManager {
            sync_queue: Arc::new(Mutex::new(Vec::new())),
            sync_status: Arc::new(Mutex::new(SyncStatus {
                in_progress: false,
                last_sync: None,
                pending_operations: 0,
                errors: Vec::new(),
                models_synced: 0,
                models_updated: 0,
                models_deleted: 0,
            })),
        });

        let backup_manager = Arc::new(BackupManager {
            backup_storage: Arc::new(LocalBackupStorage::new(cache_dir.join("backups"))),
            backup_schedule: BackupSchedule {
                enabled: config.storage_config.backup_retention_days > 0,
                interval_hours: 24,
                retention_days: config.storage_config.backup_retention_days,
                incremental: true,
            },
        });

        let version_manager = Arc::new(VersionManager {
            versions: Arc::new(Mutex::new(BTreeMap::new())),
            current_versions: Arc::new(Mutex::new(BTreeMap::new())),
        });

        let storage = Self {
            config,
            local_cache,
            sync_manager,
            backup_manager,
            version_manager,
        };

        // Initialize cache from existing files
        storage.initialize_cache().await?;

        Ok(storage)
    }

    async fn initialize_cache(&self) -> Result<()> {
        let cache_dir = {
            let cache = self.local_cache.lock().await;
            cache.cache_dir.clone()
        };

        if !cache_dir.exists() {
            return Ok(());
        }

        let mut total_size = 0u64;
        let mut models = BTreeMap::new();

        let mut entries = fs::read_dir(&cache_dir).await.map_err(|e| {
            VoirsError::config_error(format!("Failed to read cache directory: {}", e))
        })?;

        while let Some(entry) = entries.next_entry().await.map_err(|e| {
            VoirsError::config_error(format!("Failed to read directory entry: {}", e))
        })? {
            let path = entry.path();
            if path.extension().is_some_and(|ext| ext == "model") {
                if let Ok(metadata) = self.load_model_metadata(&path).await {
                    let file_size = entry
                        .metadata()
                        .await
                        .map_err(|e| {
                            VoirsError::config_error(format!("Failed to get file metadata: {}", e))
                        })?
                        .len();

                    total_size += file_size;

                    models.insert(
                        metadata.id.clone(),
                        CachedModel {
                            metadata,
                            local_path: path,
                            last_accessed: Utc::now(),
                            is_dirty: false,
                        },
                    );
                }
            }
        }

        let mut cache = self.local_cache.lock().await;
        cache.models = models;
        cache
            .current_size_bytes
            .store(total_size, Ordering::Relaxed);

        Ok(())
    }

    async fn load_model_metadata(&self, path: &Path) -> Result<ModelMetadata> {
        let metadata_path = path.with_extension("metadata");
        let metadata_content = fs::read_to_string(&metadata_path)
            .await
            .map_err(|e| VoirsError::config_error(format!("Failed to read metadata: {}", e)))?;

        serde_json::from_str(&metadata_content)
            .map_err(|e| VoirsError::config_error(format!("Failed to parse metadata: {}", e)))
    }

    async fn save_model_metadata(&self, model: &CachedModel) -> Result<()> {
        let metadata_path = model.local_path.with_extension("metadata");
        let metadata_content = serde_json::to_string_pretty(&model.metadata).map_err(|e| {
            VoirsError::config_error(format!("Failed to serialize metadata: {}", e))
        })?;

        fs::write(&metadata_path, metadata_content)
            .await
            .map_err(|e| VoirsError::config_error(format!("Failed to write metadata: {}", e)))
    }

    fn calculate_checksum(data: &[u8]) -> String {
        let mut hasher = Sha256::new();
        hasher.update(data);
        format!("{:x}", hasher.finalize())
    }

    /// Get preferred compression type based on configuration and features
    fn get_compression_type(&self) -> CompressionType {
        if !self.config.storage_config.compression {
            return CompressionType::None;
        }

        // Prefer Zstd if available, otherwise fall back to Gzip
        if cfg!(feature = "cloud") {
            CompressionType::Zstd
        } else {
            CompressionType::Gzip
        }
    }

    fn compress_data(data: &[u8]) -> Result<Vec<u8>> {
        Self::compress_data_with_type(data, CompressionType::Gzip)
    }

    fn decompress_data(compressed_data: &[u8]) -> Result<Vec<u8>> {
        Self::decompress_data_with_type(compressed_data, CompressionType::Gzip)
    }

    fn compress_data_with_type(data: &[u8], compression_type: CompressionType) -> Result<Vec<u8>> {
        match compression_type {
            CompressionType::None => Ok(data.to_vec()),
            CompressionType::Gzip => {
                let mut encoder = GzipStreamEncoder::new(Vec::new(), 6);
                encoder.write_all(data).map_err(|e| {
                    VoirsError::config_error(format!("Failed to compress data with Gzip: {}", e))
                })?;
                encoder.finish().map_err(|e| {
                    VoirsError::config_error(format!("Failed to finish Gzip compression: {}", e))
                })
            }
            #[cfg(feature = "cloud")]
            CompressionType::Zstd => {
                oxiarc_zstd::encode_all(data, 3) // Compression level 3 (balanced)
                    .map_err(|e| {
                        VoirsError::config_error(format!(
                            "Failed to compress data with Zstd: {}",
                            e
                        ))
                    })
            }
            #[cfg(not(feature = "cloud"))]
            CompressionType::Zstd => Err(VoirsError::config_error(
                "Zstd compression not available - compile with 'cloud' feature".to_string(),
            )),
        }
    }

    fn decompress_data_with_type(
        compressed_data: &[u8],
        compression_type: CompressionType,
    ) -> Result<Vec<u8>> {
        match compression_type {
            CompressionType::None => Ok(compressed_data.to_vec()),
            CompressionType::Gzip => {
                let mut decoder = GzipStreamDecoder::new(compressed_data);
                let mut decompressed = Vec::new();
                decoder.read_to_end(&mut decompressed).map_err(|e| {
                    VoirsError::config_error(format!("Failed to decompress Gzip data: {}", e))
                })?;
                Ok(decompressed)
            }
            #[cfg(feature = "cloud")]
            CompressionType::Zstd => oxiarc_zstd::decode_all(compressed_data).map_err(|e| {
                VoirsError::config_error(format!("Failed to decompress Zstd data: {}", e))
            }),
            #[cfg(not(feature = "cloud"))]
            CompressionType::Zstd => Err(VoirsError::config_error(
                "Zstd decompression not available - compile with 'cloud' feature".to_string(),
            )),
        }
    }

    async fn ensure_cache_space(&self, required_bytes: u64) -> Result<()> {
        let mut cache = self.local_cache.lock().await;
        let current_size = cache.current_size_bytes.load(Ordering::Relaxed);

        if current_size + required_bytes <= cache.max_size_bytes {
            return Ok(());
        }

        // Sort models by last accessed time and remove oldest ones
        let mut models_by_access: Vec<_> = cache
            .models
            .iter()
            .map(|(id, model)| (id.clone(), model.last_accessed))
            .collect();

        models_by_access.sort_by_key(|a| a.1);

        let mut freed_bytes = 0u64;
        let mut to_remove = Vec::new();

        for (model_id, _) in models_by_access {
            if current_size - freed_bytes + required_bytes <= cache.max_size_bytes {
                break;
            }

            if let Some(model) = cache.models.get(&model_id) {
                if let Ok(metadata) = fs::metadata(&model.local_path).await {
                    freed_bytes += metadata.len();
                    to_remove.push(model_id);
                }
            }
        }

        // Remove selected models
        for model_id in to_remove {
            if let Some(model) = cache.models.remove(&model_id) {
                let _ = fs::remove_file(&model.local_path).await;
                let _ = fs::remove_file(model.local_path.with_extension("metadata")).await;
            }
        }

        cache
            .current_size_bytes
            .store(current_size - freed_bytes, Ordering::Relaxed);
        Ok(())
    }

    pub async fn get_sync_status(&self) -> Result<SyncStatus> {
        let status = self.sync_manager.sync_status.lock().await;
        Ok(status.clone())
    }

    pub async fn start_sync(&self) -> Result<()> {
        let mut status = self.sync_manager.sync_status.lock().await;
        if status.in_progress {
            return Ok(());
        }

        status.in_progress = true;
        drop(status);

        // Start background sync task
        let sync_manager = self.sync_manager.clone();
        let local_cache = self.local_cache.clone();

        tokio::spawn(async move {
            let _ = Self::run_sync_process(sync_manager, local_cache).await;
        });

        Ok(())
    }

    async fn run_sync_process(
        sync_manager: Arc<SyncManager>,
        local_cache: Arc<Mutex<LocalCache>>,
    ) -> Result<()> {
        let operations = {
            let mut queue = sync_manager.sync_queue.lock().await;
            let ops = queue.clone();
            queue.clear();
            ops
        };

        let mut errors = Vec::new();

        for operation in operations {
            match Self::execute_sync_operation(&operation, &local_cache).await {
                Ok(_) => {}
                Err(e) => {
                    errors.push(SyncError {
                        operation,
                        error: e.to_string(),
                        timestamp: Utc::now(),
                        retry_count: 0,
                    });
                }
            }
        }

        let mut status = sync_manager.sync_status.lock().await;
        status.in_progress = false;
        status.last_sync = Some(Utc::now());
        status.pending_operations = 0;
        status.errors = errors;

        Ok(())
    }

    async fn execute_sync_operation(
        operation: &SyncOperation,
        local_cache: &Arc<Mutex<LocalCache>>,
    ) -> Result<()> {
        match operation {
            SyncOperation::Upload(model_id) => {
                tracing::info!("Uploading model: {}", model_id);
                Self::upload_model_to_cloud(model_id, local_cache).await
            }
            SyncOperation::Download(model_id) => {
                tracing::info!("Downloading model: {}", model_id);
                Self::download_model_from_cloud(model_id, local_cache).await
            }
            SyncOperation::Delete(model_id) => {
                tracing::info!("Deleting model: {}", model_id);
                Self::delete_model_from_cloud(model_id).await
            }
            SyncOperation::Verify(model_id) => {
                tracing::info!("Verifying model: {}", model_id);
                Self::verify_model_checksum(model_id, local_cache).await
            }
        }
    }

    async fn upload_model_to_cloud(
        model_id: &str,
        local_cache: &Arc<Mutex<LocalCache>>,
    ) -> Result<()> {
        let cache = local_cache.lock().await;
        if let Some(model) = cache.models.get(model_id) {
            let data = fs::read(&model.local_path).await.map_err(|e| {
                VoirsError::config_error(format!("Failed to read model file: {}", e))
            })?;

            // Calculate checksum for verification
            let checksum = Self::calculate_checksum(&data);

            // Compress data if enabled
            let compressed_data = Self::compress_data(&data)?;

            // Simulate cloud upload with local storage for now
            let cloud_path = cache
                .cache_dir
                .join("cloud_mirror")
                .join(format!("{}.cloud", model_id));

            // Create parent directory
            if let Some(parent_dir) = cloud_path.parent() {
                fs::create_dir_all(parent_dir).await.map_err(|e| {
                    VoirsError::config_error(format!(
                        "Failed to create cloud mirror directory: {}",
                        e
                    ))
                })?;
            }

            // Write compressed data and metadata
            fs::write(&cloud_path, &compressed_data)
                .await
                .map_err(|e| {
                    VoirsError::config_error(format!("Failed to write cloud data: {}", e))
                })?;

            let metadata_path = cloud_path.with_extension("metadata");
            let metadata_json = serde_json::to_string_pretty(&model.metadata).map_err(|e| {
                VoirsError::config_error(format!("Failed to serialize metadata: {}", e))
            })?;
            fs::write(&metadata_path, metadata_json)
                .await
                .map_err(|e| {
                    VoirsError::config_error(format!("Failed to write metadata: {}", e))
                })?;

            tracing::info!(
                "Successfully uploaded model {} to cloud (checksum: {})",
                model_id,
                checksum
            );
            Ok(())
        } else {
            Err(VoirsError::config_error(format!(
                "Model {} not found in local cache",
                model_id
            )))
        }
    }

    async fn download_model_from_cloud(
        model_id: &str,
        local_cache: &Arc<Mutex<LocalCache>>,
    ) -> Result<()> {
        let cache_dir = {
            let cache = local_cache.lock().await;
            cache.cache_dir.clone()
        };

        // Simulate cloud download from local mirror
        let cloud_path = cache_dir
            .join("cloud_mirror")
            .join(format!("{}.cloud", model_id));
        let metadata_path = cloud_path.with_extension("metadata");

        if !cloud_path.exists() {
            return Err(VoirsError::config_error(format!(
                "Model {} not found in cloud storage",
                model_id
            )));
        }

        // Read compressed data and metadata
        let compressed_data = fs::read(&cloud_path)
            .await
            .map_err(|e| VoirsError::config_error(format!("Failed to read cloud data: {}", e)))?;

        let metadata_json = fs::read_to_string(&metadata_path)
            .await
            .map_err(|e| VoirsError::config_error(format!("Failed to read metadata: {}", e)))?;

        let metadata: ModelMetadata = serde_json::from_str(&metadata_json)
            .map_err(|e| VoirsError::config_error(format!("Failed to parse metadata: {}", e)))?;

        // Decompress data
        let data = Self::decompress_data(&compressed_data)?;

        // Verify checksum
        let calculated_checksum = Self::calculate_checksum(&data);
        if calculated_checksum != metadata.checksum {
            return Err(VoirsError::config_error(format!(
                "Checksum verification failed for model {}: expected {}, got {}",
                model_id, metadata.checksum, calculated_checksum
            )));
        }

        // Save to local cache
        let local_path = cache_dir.join(format!("{}.model", model_id));
        fs::write(&local_path, &data)
            .await
            .map_err(|e| VoirsError::config_error(format!("Failed to write local model: {}", e)))?;

        // Update cache
        let mut cache = local_cache.lock().await;
        cache.models.insert(
            model_id.to_string(),
            CachedModel {
                metadata,
                local_path,
                last_accessed: Utc::now(),
                is_dirty: false,
            },
        );

        tracing::info!(
            "Successfully downloaded model {} from cloud (checksum: {})",
            model_id,
            calculated_checksum
        );
        Ok(())
    }

    async fn delete_model_from_cloud(model_id: &str) -> Result<()> {
        // For the local mirror simulation, we would delete the cloud files
        // In a real implementation, this would call the cloud provider's delete API

        tracing::info!("Marking model {} for deletion from cloud", model_id);

        // Simulate deletion by marking as deleted (in real implementation, call cloud API)
        // For now, we'll just log the operation since it's a placeholder
        tracing::warn!(
            "Cloud deletion is simulated - model {} marked for removal",
            model_id
        );

        Ok(())
    }

    async fn verify_model_checksum(
        model_id: &str,
        local_cache: &Arc<Mutex<LocalCache>>,
    ) -> Result<()> {
        let cache = local_cache.lock().await;
        if let Some(model) = cache.models.get(model_id) {
            if model.local_path.exists() {
                let data = fs::read(&model.local_path).await.map_err(|e| {
                    VoirsError::config_error(format!("Failed to read model file: {}", e))
                })?;

                let calculated_checksum = Self::calculate_checksum(&data);
                if calculated_checksum == model.metadata.checksum {
                    tracing::info!("Checksum verification passed for model {}", model_id);
                    Ok(())
                } else {
                    Err(VoirsError::config_error(format!(
                        "Checksum verification failed for model {}: expected {}, got {}",
                        model_id, model.metadata.checksum, calculated_checksum
                    )))
                }
            } else {
                Err(VoirsError::config_error(format!(
                    "Model file {} not found locally",
                    model_id
                )))
            }
        } else {
            Err(VoirsError::config_error(format!(
                "Model {} not found in cache",
                model_id
            )))
        }
    }
}

#[async_trait::async_trait]
impl CloudStorage for VoirsCloudStorage {
    async fn upload_model(&self, model_id: &str, data: &[u8]) -> Result<String> {
        self.ensure_cache_space(data.len() as u64).await?;

        let checksum = Self::calculate_checksum(data);
        let compression_type = self.get_compression_type();
        let compressed_data = Self::compress_data_with_type(data, compression_type)?;

        let metadata = ModelMetadata {
            id: model_id.to_string(),
            name: model_id.to_string(),
            version: "1.0.0".to_string(),
            size_bytes: data.len() as u64,
            checksum: checksum.clone(),
            created_at: Utc::now(),
            updated_at: Utc::now(),
            tags: HashMap::new(),
        };

        let cache_dir = {
            let cache = self.local_cache.lock().await;
            cache.cache_dir.clone()
        };

        let file_path = cache_dir.join(format!("{}.model", model_id));
        fs::write(&file_path, &compressed_data)
            .await
            .map_err(|e| VoirsError::config_error(format!("Failed to write model file: {}", e)))?;

        let cached_model = CachedModel {
            metadata: metadata.clone(),
            local_path: file_path,
            last_accessed: Utc::now(),
            is_dirty: true,
        };

        self.save_model_metadata(&cached_model).await?;

        let mut cache = self.local_cache.lock().await;
        cache
            .current_size_bytes
            .fetch_add(compressed_data.len() as u64, Ordering::Relaxed);
        cache.models.insert(model_id.to_string(), cached_model);

        // Queue for cloud upload
        let mut queue = self.sync_manager.sync_queue.lock().await;
        queue.push(SyncOperation::Upload(model_id.to_string()));

        Ok(checksum)
    }

    async fn download_model(&self, model_id: &str) -> Result<Vec<u8>> {
        // Check local cache first
        let cache = self.local_cache.lock().await;
        if let Some(model) = cache.models.get(model_id) {
            let compressed_data = fs::read(&model.local_path).await.map_err(|e| {
                VoirsError::config_error(format!("Failed to read cached model: {}", e))
            })?;

            let compression_type = self.get_compression_type();
            let data = Self::decompress_data_with_type(&compressed_data, compression_type)?;

            // Update access time
            drop(cache);
            let mut cache = self.local_cache.lock().await;
            if let Some(model) = cache.models.get_mut(model_id) {
                model.last_accessed = Utc::now();
            }

            return Ok(data);
        }
        drop(cache);

        // Try to download from cloud storage
        tracing::info!(
            "Model {} not in local cache, attempting cloud download",
            model_id
        );

        // Attempt cloud download
        match Self::download_model_from_cloud(model_id, &self.local_cache).await {
            Ok(()) => {
                // Successfully downloaded, now retrieve from cache
                let cache = self.local_cache.lock().await;
                if let Some(model) = cache.models.get(model_id) {
                    let data = fs::read(&model.local_path).await.map_err(|e| {
                        VoirsError::config_error(format!("Failed to read downloaded model: {}", e))
                    })?;

                    let compression_type = self.get_compression_type();
                    Self::decompress_data_with_type(&data, compression_type)
                } else {
                    Err(VoirsError::config_error(format!(
                        "Model {} not found after download",
                        model_id
                    )))
                }
            }
            Err(_) => {
                // Cloud download failed, model not available
                Err(VoirsError::config_error(format!(
                    "Model {} not found in cache or cloud",
                    model_id
                )))
            }
        }
    }

    async fn list_models(&self) -> Result<Vec<ModelMetadata>> {
        let cache = self.local_cache.lock().await;
        Ok(cache
            .models
            .values()
            .map(|model| model.metadata.clone())
            .collect())
    }

    async fn delete_model(&self, model_id: &str) -> Result<()> {
        let mut cache = self.local_cache.lock().await;
        if let Some(model) = cache.models.remove(model_id) {
            let _ = fs::remove_file(&model.local_path).await;
            let _ = fs::remove_file(model.local_path.with_extension("metadata")).await;

            if let Ok(metadata) = fs::metadata(&model.local_path).await {
                cache
                    .current_size_bytes
                    .fetch_sub(metadata.len(), Ordering::Relaxed);
            }
        }

        // Queue for cloud deletion
        let mut queue = self.sync_manager.sync_queue.lock().await;
        queue.push(SyncOperation::Delete(model_id.to_string()));

        Ok(())
    }

    async fn sync_models(&self) -> Result<SyncReport> {
        let start_time = Utc::now();
        self.start_sync().await?;

        // Wait for sync to complete (with timeout)
        let timeout = tokio::time::Duration::from_secs(300); // 5 minutes
        let deadline = tokio::time::Instant::now() + timeout;

        while tokio::time::Instant::now() < deadline {
            let status = self.get_sync_status().await?;
            if !status.in_progress {
                let end_time = Utc::now();
                return Ok(SyncReport {
                    models_synced: status.models_synced,
                    models_updated: status.models_updated,
                    models_deleted: status.models_deleted,
                    sync_duration: end_time - start_time,
                    errors: status.errors.into_iter().map(|e| e.error).collect(),
                });
            }
            tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
        }

        Err(VoirsError::config_error(
            "Sync operation timed out".to_string(),
        ))
    }

    async fn create_backup(&self, backup_id: &str) -> Result<BackupInfo> {
        let models = self.list_models().await?;
        let mut backup_data = Vec::new();

        // Collect all model data
        for model in &models {
            if let Ok(data) = self.download_model(&model.id).await {
                backup_data.extend_from_slice(&data);
            }
        }

        // Use preferred compression type based on configuration
        let compression_type = self.get_compression_type();
        let compressed_backup = Self::compress_data_with_type(&backup_data, compression_type)?;
        let checksum = Self::calculate_checksum(&compressed_backup);

        let backup = BackupData {
            id: backup_id.to_string(),
            models: models.clone(),
            data: compressed_backup.clone(),
            compression: compression_type,
            encryption: None,
        };

        self.backup_manager
            .backup_storage
            .store_backup(&backup)
            .await?;

        Ok(BackupInfo {
            id: backup_id.to_string(),
            name: format!("Backup {}", backup_id),
            size_bytes: compressed_backup.len() as u64,
            created_at: Utc::now(),
            models_count: models.len() as u32,
            checksum,
        })
    }

    async fn restore_backup(&self, backup_id: &str) -> Result<()> {
        let backup = self
            .backup_manager
            .backup_storage
            .retrieve_backup(backup_id)
            .await?;

        let decompressed_data = Self::decompress_data_with_type(&backup.data, backup.compression)?;

        // Implement proper restoration logic
        let mut restored_count = 0;

        for model_metadata in &backup.models {
            // Extract model data from backup
            let model_start = restored_count * (decompressed_data.len() / backup.models.len());
            let model_end = (restored_count + 1) * (decompressed_data.len() / backup.models.len());

            if model_end <= decompressed_data.len() {
                let model_data = &decompressed_data[model_start..model_end];

                // Save model to cache
                let model_path = {
                    let cache = self.local_cache.lock().await;
                    cache.cache_dir.join(format!("{}.model", model_metadata.id))
                };

                fs::write(&model_path, model_data).await.map_err(|e| {
                    VoirsError::config_error(format!(
                        "Failed to restore model {}: {}",
                        model_metadata.id, e
                    ))
                })?;

                // Add to cache
                let mut cache = self.local_cache.lock().await;
                cache.models.insert(
                    model_metadata.id.clone(),
                    CachedModel {
                        metadata: model_metadata.clone(),
                        local_path: model_path,
                        last_accessed: Utc::now(),
                        is_dirty: false,
                    },
                );

                restored_count += 1;
                tracing::debug!("Restored model: {}", model_metadata.id);
            }
        }

        tracing::info!(
            "Successfully restored backup {} with {} models",
            backup_id,
            restored_count
        );
        Ok(())
    }
}

struct LocalBackupStorage {
    backup_dir: PathBuf,
}

impl LocalBackupStorage {
    fn new(backup_dir: PathBuf) -> Self {
        Self { backup_dir }
    }
}

#[async_trait::async_trait]
impl BackupStorage for LocalBackupStorage {
    async fn store_backup(&self, backup: &BackupData) -> Result<String> {
        fs::create_dir_all(&self.backup_dir).await.map_err(|e| {
            VoirsError::config_error(format!("Failed to create backup directory: {}", e))
        })?;

        let backup_path = self.backup_dir.join(format!("{}.backup", backup.id));
        let backup_content = serde_json::to_vec(backup)
            .map_err(|e| VoirsError::config_error(format!("Failed to serialize backup: {}", e)))?;

        fs::write(&backup_path, backup_content)
            .await
            .map_err(|e| VoirsError::config_error(format!("Failed to write backup: {}", e)))?;

        Ok(backup.id.clone())
    }

    async fn retrieve_backup(&self, backup_id: &str) -> Result<BackupData> {
        let backup_path = self.backup_dir.join(format!("{}.backup", backup_id));
        let backup_content = fs::read(&backup_path)
            .await
            .map_err(|e| VoirsError::config_error(format!("Failed to read backup: {}", e)))?;

        serde_json::from_slice(&backup_content)
            .map_err(|e| VoirsError::config_error(format!("Failed to deserialize backup: {}", e)))
    }

    async fn list_backups(&self) -> Result<Vec<BackupInfo>> {
        let mut backups = Vec::new();

        if !self.backup_dir.exists() {
            return Ok(backups);
        }

        let mut entries = fs::read_dir(&self.backup_dir).await.map_err(|e| {
            VoirsError::config_error(format!("Failed to read backup directory: {}", e))
        })?;

        while let Some(entry) = entries.next_entry().await.map_err(|e| {
            VoirsError::config_error(format!("Failed to read directory entry: {}", e))
        })? {
            let path = entry.path();
            if path.extension().is_some_and(|ext| ext == "backup") {
                // Get file stem as string
                let backup_id = path
                    .file_stem()
                    .and_then(|s| s.to_str())
                    .map(|s| s.to_string());

                if let Some(id) = backup_id {
                    if let Ok(backup) = self.retrieve_backup(&id).await {
                        // Get creation time from file metadata if available
                        let created_at = if let Ok(metadata) = fs::metadata(&path).await {
                            if let Ok(created) = metadata.created() {
                                DateTime::<Utc>::from(created)
                            } else {
                                Utc::now()
                            }
                        } else {
                            Utc::now()
                        };

                        backups.push(BackupInfo {
                            id: backup.id,
                            name: "Backup".to_string(),
                            size_bytes: backup.data.len() as u64,
                            created_at,
                            models_count: backup.models.len() as u32,
                            checksum: Self::calculate_backup_checksum(&backup.data),
                        });
                    }
                }
            }
        }

        Ok(backups)
    }

    async fn delete_backup(&self, backup_id: &str) -> Result<()> {
        let backup_path = self.backup_dir.join(format!("{}.backup", backup_id));
        fs::remove_file(&backup_path)
            .await
            .map_err(|e| VoirsError::config_error(format!("Failed to delete backup: {}", e)))
    }
}

impl LocalBackupStorage {
    fn calculate_backup_checksum(data: &[u8]) -> String {
        let mut hasher = Sha256::new();
        hasher.update(data);
        format!("{:x}", hasher.finalize())
    }
}

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

    #[tokio::test]
    async fn test_cloud_storage_creation() {
        let temp_dir = TempDir::new().unwrap();
        let config = CloudConfig::default();

        let storage = VoirsCloudStorage::new(config, temp_dir.path().to_path_buf()).await;
        assert!(storage.is_ok());
    }

    #[tokio::test]
    async fn test_model_upload_download() {
        let temp_dir = TempDir::new().unwrap();
        let config = CloudConfig::default();
        let storage = VoirsCloudStorage::new(config, temp_dir.path().to_path_buf())
            .await
            .unwrap();

        let test_data = b"test model data";
        let model_id = "test_model";

        // Upload model
        let checksum = storage.upload_model(model_id, test_data).await.unwrap();
        assert!(!checksum.is_empty());

        // Download model
        let downloaded_data = storage.download_model(model_id).await.unwrap();
        assert_eq!(test_data, downloaded_data.as_slice());
    }

    #[tokio::test]
    async fn test_model_listing() {
        let temp_dir = TempDir::new().unwrap();
        let config = CloudConfig::default();
        let storage = VoirsCloudStorage::new(config, temp_dir.path().to_path_buf())
            .await
            .unwrap();

        // Upload multiple models
        storage.upload_model("model1", b"data1").await.unwrap();
        storage.upload_model("model2", b"data2").await.unwrap();

        // List models
        let models = storage.list_models().await.unwrap();
        assert_eq!(models.len(), 2);

        let model_ids: Vec<String> = models.iter().map(|m| m.id.clone()).collect();
        assert!(model_ids.contains(&"model1".to_string()));
        assert!(model_ids.contains(&"model2".to_string()));
    }

    #[tokio::test]
    async fn test_backup_creation() {
        let temp_dir = TempDir::new().unwrap();
        let config = CloudConfig::default();
        let storage = VoirsCloudStorage::new(config, temp_dir.path().to_path_buf())
            .await
            .unwrap();

        // Upload a model
        storage
            .upload_model("test_model", b"test data")
            .await
            .unwrap();

        // Create backup
        let backup_info = storage.create_backup("test_backup").await.unwrap();
        assert_eq!(backup_info.id, "test_backup");
        assert_eq!(backup_info.models_count, 1);
    }

    #[test]
    fn test_checksum_calculation() {
        let data = b"test data";
        let checksum = VoirsCloudStorage::calculate_checksum(data);
        assert!(!checksum.is_empty());
        assert_eq!(checksum.len(), 64); // SHA256 produces 64-char hex string
    }

    #[test]
    fn test_compression_decompression() {
        let data = b"test data for compression";
        let compressed = VoirsCloudStorage::compress_data(data).unwrap();
        let decompressed = VoirsCloudStorage::decompress_data(&compressed).unwrap();
        assert_eq!(data, decompressed.as_slice());
    }

    #[test]
    fn test_gzip_compression_decompression() {
        let data = b"test data for gzip compression";
        let compressed =
            VoirsCloudStorage::compress_data_with_type(data, CompressionType::Gzip).unwrap();
        let decompressed =
            VoirsCloudStorage::decompress_data_with_type(&compressed, CompressionType::Gzip)
                .unwrap();
        assert_eq!(data, decompressed.as_slice());
    }

    #[cfg(feature = "cloud")]
    #[test]
    fn test_zstd_compression_decompression() {
        // Create larger, repetitive data that compresses well
        let data_str =
            "test data for zstd compression - this should compress well with zstd. ".repeat(100);
        let data = data_str.as_bytes();
        let compressed =
            VoirsCloudStorage::compress_data_with_type(data, CompressionType::Zstd).unwrap();
        let decompressed =
            VoirsCloudStorage::decompress_data_with_type(&compressed, CompressionType::Zstd)
                .unwrap();
        assert_eq!(data, decompressed.as_slice());

        // Zstd should achieve significant compression on repetitive data
        assert!(compressed.len() < data.len());
    }

    #[test]
    fn test_no_compression() {
        let data = b"test data without compression";
        let compressed =
            VoirsCloudStorage::compress_data_with_type(data, CompressionType::None).unwrap();
        let decompressed =
            VoirsCloudStorage::decompress_data_with_type(&compressed, CompressionType::None)
                .unwrap();
        assert_eq!(data, decompressed.as_slice());
        assert_eq!(data.len(), compressed.len());
    }

    #[tokio::test]
    async fn test_compression_type_selection() {
        let temp_dir = TempDir::new().unwrap();
        let mut config = CloudConfig::default();

        // Test with compression disabled
        config.storage_config.compression = false;
        let storage = VoirsCloudStorage::new(config.clone(), temp_dir.path().to_path_buf())
            .await
            .unwrap();
        assert_eq!(storage.get_compression_type(), CompressionType::None);

        // Test with compression enabled
        config.storage_config.compression = true;
        let storage = VoirsCloudStorage::new(config, temp_dir.path().to_path_buf())
            .await
            .unwrap();

        #[cfg(feature = "cloud")]
        assert_eq!(storage.get_compression_type(), CompressionType::Zstd);

        #[cfg(not(feature = "cloud"))]
        assert_eq!(storage.get_compression_type(), CompressionType::Gzip);
    }
}