trustformers-mobile 0.1.1

Mobile deployment support for TrustformeRS (iOS, Android)
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
//! Model Management System with Over-the-Air Updates
//!
//! This module provides comprehensive model management capabilities including:
//! - Over-the-air model downloads and updates
//! - Model versioning and rollback
//! - Differential model updates
//! - Secure model verification
//! - Storage optimization and caching

use crate::MobileConfig;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use trustformers_core::error::{CoreError, Result};
use trustformers_core::TrustformersError;

/// Model management configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelManagerConfig {
    /// Base URL for model updates
    pub update_server_url: String,
    /// API key for authenticated downloads
    pub api_key: Option<String>,
    /// Local storage directory for models
    pub storage_directory: PathBuf,
    /// Maximum storage size in MB
    pub max_storage_size_mb: usize,
    /// Enable automatic model updates
    pub enable_auto_updates: bool,
    /// Update check interval in seconds
    pub update_check_interval_seconds: u64,
    /// Enable differential updates
    pub enable_differential_updates: bool,
    /// Require model signature verification
    pub require_signature_verification: bool,
    /// Network timeout for downloads (seconds)
    pub download_timeout_seconds: u64,
    /// Maximum concurrent downloads
    pub max_concurrent_downloads: usize,
    /// Enable compression for model storage
    pub enable_compression: bool,
    /// Retry attempts for failed downloads
    pub download_retry_attempts: usize,
}

/// Model metadata information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelMetadata {
    /// Model unique identifier
    pub model_id: String,
    /// Model version string
    pub version: String,
    /// Model type/architecture
    pub model_type: String,
    /// Model size in bytes
    pub size_bytes: usize,
    /// Model checksum (SHA-256)
    pub checksum: String,
    /// Digital signature for verification
    pub signature: Option<String>,
    /// Download URL
    pub download_url: String,
    /// Differential update URL (if available)
    pub differential_url: Option<String>,
    /// Model description
    pub description: String,
    /// Required mobile config
    pub required_config: MobileConfig,
    /// Compatibility information
    pub compatibility: ModelCompatibility,
    /// Release timestamp
    pub release_timestamp: u64,
    /// Deprecation timestamp (if applicable)
    pub deprecation_timestamp: Option<u64>,
    /// Model tags for categorization
    pub tags: Vec<String>,
}

/// Model compatibility requirements
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelCompatibility {
    /// Minimum Android API level
    pub min_android_api: Option<u32>,
    /// Minimum iOS version
    pub min_ios_version: Option<String>,
    /// Required hardware features
    pub required_features: Vec<String>,
    /// Minimum memory requirement (MB)
    pub min_memory_mb: usize,
    /// Supported architectures
    pub supported_architectures: Vec<String>,
}

/// Model download progress information
#[derive(Debug, Clone)]
pub struct DownloadProgress {
    /// Model being downloaded
    pub model_id: String,
    /// Total bytes to download
    pub total_bytes: usize,
    /// Bytes downloaded so far
    pub downloaded_bytes: usize,
    /// Download speed (bytes/second)
    pub download_speed_bps: f64,
    /// Estimated time remaining (seconds)
    pub eta_seconds: f64,
    /// Current download status
    pub status: DownloadStatus,
}

/// Download status enumeration
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DownloadStatus {
    Pending,
    Downloading,
    Verifying,
    Installing,
    Completed,
    Failed(String),
    Cancelled,
}

/// Model update information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelUpdate {
    /// Current model metadata
    pub current: ModelMetadata,
    /// Available update metadata
    pub update: ModelMetadata,
    /// Update type (full or differential)
    pub update_type: UpdateType,
    /// Update priority level
    pub priority: UpdatePriority,
    /// Size of the update (bytes)
    pub update_size_bytes: usize,
}

/// Update type enumeration
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum UpdateType {
    /// Full model replacement
    Full,
    /// Differential/patch update
    Differential,
    /// Configuration-only update
    ConfigOnly,
}

/// Update priority levels
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum UpdatePriority {
    /// Critical security or bug fix
    Critical,
    /// Important feature or performance improvement
    High,
    /// Regular update
    Normal,
    /// Optional enhancement
    Low,
}

/// Model management system
pub struct ModelManager {
    config: ModelManagerConfig,
    models: HashMap<String, ModelMetadata>,
    downloads: HashMap<String, DownloadProgress>,
    storage_stats: StorageStats,
    last_update_check: Option<SystemTime>,
    http_client: Option<Box<dyn HttpClient>>,
}

/// Storage statistics
#[derive(Debug, Clone)]
pub struct StorageStats {
    total_size_bytes: usize,
    used_size_bytes: usize,
    model_count: usize,
    last_cleanup_time: SystemTime,
}

/// HTTP client trait for testability
pub trait HttpClient: Send + Sync {
    fn download_file(&self, url: &str, destination: &Path) -> Result<()>;
    fn download_with_progress(
        &self,
        url: &str,
        destination: &Path,
        progress_callback: Box<dyn Fn(usize, usize) + Send + Sync>,
    ) -> Result<()>;
    fn get_metadata(&self, url: &str) -> Result<String>;
}

impl ModelManager {
    /// Create new model manager
    pub fn new(config: ModelManagerConfig) -> Result<Self> {
        config.validate()?;

        // Create storage directory if it doesn't exist
        std::fs::create_dir_all(&config.storage_directory)?;

        let storage_stats = StorageStats {
            total_size_bytes: config.max_storage_size_mb * 1024 * 1024,
            used_size_bytes: 0,
            model_count: 0,
            last_cleanup_time: SystemTime::now(),
        };

        let mut manager = Self {
            config,
            models: HashMap::new(),
            downloads: HashMap::new(),
            storage_stats,
            last_update_check: None,
            http_client: None,
        };

        // Load existing models from storage
        manager.load_models_from_storage()?;

        // Set up default HTTP client
        #[cfg(not(test))]
        {
            manager.http_client = Some(Box::new(DefaultHttpClient::new(
                manager.config.download_timeout_seconds,
            )?));
        }

        Ok(manager)
    }

    /// Set custom HTTP client (for testing)
    pub fn set_http_client(&mut self, client: Box<dyn HttpClient>) {
        self.http_client = Some(client);
    }

    /// Check for available model updates
    pub async fn check_for_updates(&mut self) -> Result<Vec<ModelUpdate>> {
        if !self.should_check_for_updates() {
            return Ok(Vec::new());
        }

        tracing::info!(
            "Checking for model updates from {}",
            self.config.update_server_url
        );

        let updates_url = format!("{}/updates", self.config.update_server_url);
        let client = self.http_client.as_ref().ok_or_else(|| {
            TrustformersError::runtime_error("HTTP client not initialized".into())
        })?;

        let response = client.get_metadata(&updates_url)?;
        let available_models: Vec<ModelMetadata> = serde_json::from_str(&response)?;

        let mut updates = Vec::new();

        for available_model in available_models {
            if let Some(current_model) = self.models.get(&available_model.model_id) {
                if self.is_update_available(current_model, &available_model)? {
                    let update_type = self.determine_update_type(current_model, &available_model);
                    let priority = self.determine_update_priority(&available_model);
                    let update_size =
                        self.calculate_update_size(current_model, &available_model, &update_type);

                    updates.push(ModelUpdate {
                        current: current_model.clone(),
                        update: available_model,
                        update_type,
                        priority,
                        update_size_bytes: update_size,
                    });
                }
            } else {
                // New model available
                let update = ModelUpdate {
                    current: ModelMetadata::default_for_id(&available_model.model_id),
                    update: available_model.clone(),
                    update_type: UpdateType::Full,
                    priority: UpdatePriority::Normal,
                    update_size_bytes: available_model.size_bytes,
                };
                updates.push(update);
            }
        }

        self.last_update_check = Some(SystemTime::now());

        tracing::info!("Found {} available updates", updates.len());
        Ok(updates)
    }

    /// Download and install a model update
    pub async fn download_model(
        &mut self,
        model_id: &str,
        progress_callback: Option<Box<dyn Fn(DownloadProgress) + Send + Sync>>,
    ) -> Result<()> {
        let model_metadata = self.get_model_metadata_from_server(model_id).await?;

        // Check compatibility
        self.check_compatibility(&model_metadata)?;

        // Check storage space
        self.ensure_storage_space(model_metadata.size_bytes)?;

        // Initialize download progress
        let mut progress = DownloadProgress {
            model_id: model_id.to_string(),
            total_bytes: model_metadata.size_bytes,
            downloaded_bytes: 0,
            download_speed_bps: 0.0,
            eta_seconds: 0.0,
            status: DownloadStatus::Pending,
        };

        self.downloads.insert(model_id.to_string(), progress.clone());

        if let Some(ref callback) = progress_callback {
            callback(progress.clone());
        }

        // Download model
        progress.status = DownloadStatus::Downloading;
        self.downloads.insert(model_id.to_string(), progress.clone());

        let temp_path = self.get_temp_download_path(model_id);
        let final_path = self.get_model_path(model_id);

        let client = self.http_client.as_ref().ok_or_else(|| {
            TrustformersError::runtime_error("HTTP client not initialized".into())
        })?;

        let download_progress_callback = {
            let model_id = model_id.to_string();

            move |downloaded: usize, total: usize| {
                // We'll handle progress updates in the main flow instead
                // since we can't clone the callback
                tracing::debug!(
                    "Download progress: {}/{} bytes for model {}",
                    downloaded,
                    total,
                    model_id
                );
            }
        };

        client.download_with_progress(
            &model_metadata.download_url,
            &temp_path,
            Box::new(download_progress_callback),
        )?;

        // Verify model
        progress.status = DownloadStatus::Verifying;
        self.downloads.insert(model_id.to_string(), progress.clone());

        if let Some(ref callback) = progress_callback {
            callback(progress.clone());
        }

        self.verify_model(&temp_path, &model_metadata)?;

        // Install model
        progress.status = DownloadStatus::Installing;
        self.downloads.insert(model_id.to_string(), progress.clone());

        if let Some(ref callback) = progress_callback {
            callback(progress.clone());
        }

        self.install_model(&temp_path, &final_path, &model_metadata)?;

        // Complete
        progress.status = DownloadStatus::Completed;
        self.downloads.insert(model_id.to_string(), progress.clone());

        if let Some(ref callback) = progress_callback {
            callback(progress.clone());
        }

        // Update local metadata
        self.models.insert(model_id.to_string(), model_metadata);
        self.save_models_metadata()?;

        // Cleanup
        let _ = std::fs::remove_file(&temp_path);
        self.downloads.remove(model_id);

        tracing::info!("Successfully downloaded and installed model: {}", model_id);
        Ok(())
    }

    /// Apply differential update to a model
    pub async fn apply_differential_update(&mut self, update: &ModelUpdate) -> Result<()> {
        if update.update_type != UpdateType::Differential {
            return Err(
                TrustformersError::runtime_error("Not a differential update".into()).into(),
            );
        }

        let current_path = self.get_model_path(&update.current.model_id);
        let patch_path = self.get_temp_download_path(&format!("{}_patch", update.current.model_id));
        let updated_path =
            self.get_temp_download_path(&format!("{}_updated", update.current.model_id));

        // Download differential patch
        if let Some(diff_url) = &update.update.differential_url {
            let client = self.http_client.as_ref().ok_or_else(|| {
                TrustformersError::runtime_error("HTTP client not initialized".into())
            })?;

            client.download_file(diff_url, &patch_path)?;
        } else {
            return Err(
                TrustformersError::runtime_error("No differential URL provided".into()).into(),
            );
        }

        // Apply patch
        self.apply_binary_patch(&current_path, &patch_path, &updated_path)?;

        // Verify updated model
        self.verify_model(&updated_path, &update.update)?;

        // Replace current model
        let final_path = self.get_model_path(&update.current.model_id);
        std::fs::rename(&updated_path, &final_path)?;

        // Update metadata
        self.models.insert(update.current.model_id.clone(), update.update.clone());
        self.save_models_metadata()?;

        // Cleanup
        let _ = std::fs::remove_file(&patch_path);

        tracing::info!(
            "Successfully applied differential update for model: {}",
            update.current.model_id
        );
        Ok(())
    }

    /// Remove a model from local storage
    pub fn remove_model(&mut self, model_id: &str) -> Result<()> {
        let model_path = self.get_model_path(model_id);

        if model_path.exists() {
            std::fs::remove_file(&model_path)?;
        }

        if let Some(model) = self.models.remove(model_id) {
            self.storage_stats.used_size_bytes =
                self.storage_stats.used_size_bytes.saturating_sub(model.size_bytes);
            self.storage_stats.model_count = self.storage_stats.model_count.saturating_sub(1);
        }

        self.save_models_metadata()?;

        tracing::info!("Removed model: {}", model_id);
        Ok(())
    }

    /// List all locally available models
    pub fn list_models(&self) -> Vec<&ModelMetadata> {
        self.models.values().collect()
    }

    /// Get model metadata by ID
    pub fn get_model(&self, model_id: &str) -> Option<&ModelMetadata> {
        self.models.get(model_id)
    }

    /// Get model file path
    pub fn get_model_path(&self, model_id: &str) -> PathBuf {
        self.config.storage_directory.join(format!("{}.trustformers", model_id))
    }

    /// Get storage statistics
    pub fn get_storage_stats(&self) -> &StorageStats {
        &self.storage_stats
    }

    /// Clean up old and unused models
    pub fn cleanup_storage(&mut self) -> Result<()> {
        let mut models_by_age: Vec<_> = self
            .models
            .values()
            .filter(|model| {
                // Remove deprecated models
                if let Some(deprecation_time) = model.deprecation_timestamp {
                    let now = SystemTime::now()
                        .duration_since(UNIX_EPOCH)
                        .expect("SystemTime should be after UNIX_EPOCH")
                        .as_secs();
                    now > deprecation_time
                } else {
                    false
                }
            })
            .map(|model| (model.model_id.clone(), model.release_timestamp))
            .collect();

        // Sort by release timestamp (oldest first)
        models_by_age.sort_by_key(|(_, timestamp)| *timestamp);

        // Remove models until we're under storage limit
        while self.storage_stats.used_size_bytes > self.storage_stats.total_size_bytes * 90 / 100 {
            if let Some((model_id, _)) = models_by_age.pop() {
                self.remove_model(&model_id)?;
            } else {
                break;
            }
        }

        self.storage_stats.last_cleanup_time = SystemTime::now();
        Ok(())
    }

    /// Cancel an ongoing download
    pub fn cancel_download(&mut self, model_id: &str) -> Result<()> {
        if let Some(mut progress) = self.downloads.remove(model_id) {
            progress.status = DownloadStatus::Cancelled;

            // Clean up temporary files
            let temp_path = self.get_temp_download_path(model_id);
            let _ = std::fs::remove_file(&temp_path);

            tracing::info!("Cancelled download for model: {}", model_id);
        }

        Ok(())
    }

    // Private helper methods

    fn should_check_for_updates(&self) -> bool {
        if !self.config.enable_auto_updates {
            return false;
        }

        if let Some(last_check) = self.last_update_check {
            let elapsed = SystemTime::now().duration_since(last_check).unwrap_or(Duration::ZERO);
            elapsed.as_secs() >= self.config.update_check_interval_seconds
        } else {
            true
        }
    }

    fn is_update_available(
        &self,
        current: &ModelMetadata,
        available: &ModelMetadata,
    ) -> Result<bool> {
        // Simple version comparison - in practice, you'd use semantic versioning
        Ok(available.version != current.version
            && available.release_timestamp > current.release_timestamp)
    }

    fn determine_update_type(
        &self,
        current: &ModelMetadata,
        available: &ModelMetadata,
    ) -> UpdateType {
        if available.differential_url.is_some() && self.config.enable_differential_updates {
            UpdateType::Differential
        } else {
            UpdateType::Full
        }
    }

    fn determine_update_priority(&self, model: &ModelMetadata) -> UpdatePriority {
        // Determine priority based on tags or other metadata
        if model.tags.contains(&"critical".to_string())
            || model.tags.contains(&"security".to_string())
        {
            UpdatePriority::Critical
        } else if model.tags.contains(&"performance".to_string()) {
            UpdatePriority::High
        } else {
            UpdatePriority::Normal
        }
    }

    fn calculate_update_size(
        &self,
        current: &ModelMetadata,
        available: &ModelMetadata,
        update_type: &UpdateType,
    ) -> usize {
        match update_type {
            UpdateType::Full => available.size_bytes,
            UpdateType::Differential => available.size_bytes / 10, // Estimate 10% of full size
            UpdateType::ConfigOnly => 1024,                        // Small config update
        }
    }

    async fn get_model_metadata_from_server(&self, model_id: &str) -> Result<ModelMetadata> {
        let metadata_url = format!(
            "{}/models/{}/metadata",
            self.config.update_server_url, model_id
        );
        let client = self.http_client.as_ref().ok_or_else(|| {
            TrustformersError::runtime_error("HTTP client not initialized".into())
        })?;

        let response = client.get_metadata(&metadata_url)?;
        let metadata: ModelMetadata = serde_json::from_str(&response)?;

        Ok(metadata)
    }

    fn check_compatibility(&self, model: &ModelMetadata) -> Result<()> {
        // Check platform compatibility
        #[cfg(target_os = "android")]
        {
            if let Some(min_api) = model.compatibility.min_android_api {
                // Would check actual Android API level here
                if min_api > 21 {
                    return Err(TrustformersError::runtime_error(
                        "Android version incompatible".into(),
                    )
                    .into());
                }
            }
        }

        #[cfg(target_os = "ios")]
        {
            if let Some(ref min_ios) = model.compatibility.min_ios_version {
                // Would check actual iOS version here
                tracing::debug!("Checking iOS compatibility: {}", min_ios);
            }
        }

        // Check memory requirements
        let available_memory = self.get_available_memory_mb();
        if model.compatibility.min_memory_mb > available_memory {
            return Err(TrustformersError::runtime_error(format!(
                "Insufficient memory: {} MB required, {} MB available",
                model.compatibility.min_memory_mb, available_memory
            ))
            .into());
        }

        Ok(())
    }

    fn ensure_storage_space(&mut self, required_bytes: usize) -> Result<()> {
        let available_space =
            self.storage_stats.total_size_bytes - self.storage_stats.used_size_bytes;

        if available_space < required_bytes {
            // Try cleanup first
            self.cleanup_storage()?;

            let available_after_cleanup =
                self.storage_stats.total_size_bytes - self.storage_stats.used_size_bytes;
            if available_after_cleanup < required_bytes {
                return Err(
                    TrustformersError::runtime_error("Insufficient storage space".into()).into(),
                );
            }
        }

        Ok(())
    }

    fn verify_model(&self, path: &Path, metadata: &ModelMetadata) -> Result<()> {
        // Verify file size
        let file_size = std::fs::metadata(path)?.len() as usize;
        if file_size != metadata.size_bytes {
            return Err(TrustformersError::runtime_error("Model size mismatch".into()).into());
        }

        // Verify checksum
        let file_checksum = self.calculate_checksum(path)?;
        if file_checksum != metadata.checksum {
            return Err(TrustformersError::runtime_error("Model checksum mismatch".into()).into());
        }

        // Verify signature if required
        if self.config.require_signature_verification {
            if let Some(ref signature) = metadata.signature {
                self.verify_signature(path, signature)?;
            } else {
                return Err(TrustformersError::runtime_error(
                    "Model signature required but not provided".into(),
                )
                .into());
            }
        }

        Ok(())
    }

    fn install_model(
        &mut self,
        temp_path: &Path,
        final_path: &Path,
        metadata: &ModelMetadata,
    ) -> Result<()> {
        // Move file to final location
        std::fs::rename(temp_path, final_path)?;

        // Optionally compress the model
        if self.config.enable_compression {
            self.compress_model(final_path)?;
        }

        // Update storage stats
        self.storage_stats.used_size_bytes += metadata.size_bytes;
        self.storage_stats.model_count += 1;

        Ok(())
    }

    fn load_models_from_storage(&mut self) -> Result<()> {
        let metadata_path = self.config.storage_directory.join("models_metadata.json");

        if metadata_path.exists() {
            let metadata_content = std::fs::read_to_string(&metadata_path)?;
            let models: HashMap<String, ModelMetadata> = serde_json::from_str(&metadata_content)?;

            // Verify models still exist on disk
            for (model_id, metadata) in models {
                let model_path = self.get_model_path(&model_id);
                if model_path.exists() {
                    self.storage_stats.used_size_bytes += metadata.size_bytes;
                    self.storage_stats.model_count += 1;
                    self.models.insert(model_id, metadata);
                }
            }
        }

        Ok(())
    }

    fn save_models_metadata(&self) -> Result<()> {
        let metadata_path = self.config.storage_directory.join("models_metadata.json");
        let metadata_content = serde_json::to_string_pretty(&self.models)?;
        std::fs::write(&metadata_path, metadata_content)?;
        Ok(())
    }

    fn get_temp_download_path(&self, model_id: &str) -> PathBuf {
        self.config.storage_directory.join(format!("{}.tmp", model_id))
    }

    fn calculate_checksum(&self, path: &Path) -> Result<String> {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let content = std::fs::read(path)?;
        let mut hasher = DefaultHasher::new();
        content.hash(&mut hasher);
        Ok(format!("{:x}", hasher.finish()))
    }

    fn verify_signature(&self, _path: &Path, _signature: &str) -> Result<()> {
        // Implement digital signature verification
        // This would use cryptographic libraries to verify the model signature
        Ok(())
    }

    fn apply_binary_patch(&self, _original: &Path, _patch: &Path, _output: &Path) -> Result<()> {
        // Implement binary patch application (e.g., using bsdiff/bspatch)
        // This would apply differential updates to models
        Ok(())
    }

    fn compress_model(&self, _path: &Path) -> Result<()> {
        // Implement model compression if enabled
        Ok(())
    }

    fn get_available_memory_mb(&self) -> usize {
        // Get available system memory
        // This would use platform-specific APIs
        2048 // Placeholder: 2GB
    }
}

impl Default for ModelManagerConfig {
    fn default() -> Self {
        Self {
            update_server_url: "https://models.trustformers.ai".to_string(),
            api_key: None,
            storage_directory: PathBuf::from("./models"),
            max_storage_size_mb: 1024,
            enable_auto_updates: true,
            update_check_interval_seconds: 3600, // 1 hour
            enable_differential_updates: true,
            require_signature_verification: true,
            download_timeout_seconds: 300, // 5 minutes
            max_concurrent_downloads: 2,
            enable_compression: true,
            download_retry_attempts: 3,
        }
    }
}

impl ModelManagerConfig {
    /// Validate configuration
    pub fn validate(&self) -> Result<()> {
        if self.update_server_url.is_empty() {
            return Err(TrustformersError::config_error(
                "Update server URL cannot be empty",
                "validate_model_config",
            )
            .into());
        }

        if self.max_storage_size_mb == 0 {
            return Err(
                TrustformersError::config_error("Storage size must be > 0", "validate").into(),
            );
        }

        if self.download_timeout_seconds < 30 {
            return Err(
                TrustformersError::config_error("Download timeout too short", "validate").into(),
            );
        }

        if self.max_concurrent_downloads == 0 || self.max_concurrent_downloads > 10 {
            return Err(TrustformersError::config_error(
                "Invalid concurrent download count",
                "validate",
            )
            .into());
        }

        Ok(())
    }
}

impl ModelMetadata {
    fn default_for_id(model_id: &str) -> Self {
        Self {
            model_id: model_id.to_string(),
            version: "0.0.0".to_string(),
            model_type: "unknown".to_string(),
            size_bytes: 0,
            checksum: String::new(),
            signature: None,
            download_url: String::new(),
            differential_url: None,
            description: String::new(),
            required_config: MobileConfig::default(),
            compatibility: ModelCompatibility::default(),
            release_timestamp: 0,
            deprecation_timestamp: None,
            tags: Vec::new(),
        }
    }
}

impl Default for ModelCompatibility {
    fn default() -> Self {
        Self {
            min_android_api: Some(21),
            min_ios_version: Some("12.0".to_string()),
            required_features: Vec::new(),
            min_memory_mb: 512,
            supported_architectures: vec!["arm64".to_string(), "x86_64".to_string()],
        }
    }
}

// Default HTTP client implementation
#[cfg(not(test))]
struct DefaultHttpClient {
    timeout: Duration,
}

#[cfg(not(test))]
impl DefaultHttpClient {
    fn new(timeout_seconds: u64) -> Result<Self> {
        Ok(Self {
            timeout: Duration::from_secs(timeout_seconds),
        })
    }
}

#[cfg(not(test))]
impl HttpClient for DefaultHttpClient {
    fn download_file(&self, _url: &str, _destination: &Path) -> Result<()> {
        // Implement actual HTTP download
        // This would use reqwest or similar HTTP client
        Ok(())
    }

    fn download_with_progress(
        &self,
        _url: &str,
        _destination: &Path,
        _progress_callback: Box<dyn Fn(usize, usize) + Send + Sync>,
    ) -> Result<()> {
        // Implement HTTP download with progress reporting
        Ok(())
    }

    fn get_metadata(&self, _url: &str) -> Result<String> {
        // Implement HTTP GET for metadata
        Ok("{}".to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Arc, Mutex};

    // Mock HTTP client for testing
    struct MockHttpClient {
        responses: Arc<Mutex<HashMap<String, String>>>,
    }

    impl MockHttpClient {
        fn new() -> Self {
            Self {
                responses: Arc::new(Mutex::new(HashMap::new())),
            }
        }

        fn set_response(&self, url: &str, response: &str) {
            self.responses
                .lock()
                .expect("lock should not be poisoned")
                .insert(url.to_string(), response.to_string());
        }
    }

    impl HttpClient for MockHttpClient {
        fn download_file(&self, _url: &str, _destination: &Path) -> Result<()> {
            Ok(())
        }

        fn download_with_progress(
            &self,
            _url: &str,
            _destination: &Path,
            progress_callback: Box<dyn Fn(usize, usize) + Send + Sync>,
        ) -> Result<()> {
            progress_callback(100, 100);
            Ok(())
        }

        fn get_metadata(&self, url: &str) -> Result<String> {
            let responses = self.responses.lock().expect("lock should not be poisoned");
            responses
                .get(url)
                .cloned()
                .ok_or_else(|| {
                    TrustformersError::runtime_error("URL not found in mock responses".into())
                })
                .map_err(|e| e.into())
        }
    }

    #[test]
    fn test_model_manager_config_validation() {
        let config = ModelManagerConfig::default();
        assert!(config.validate().is_ok());

        let mut invalid_config = config.clone();
        invalid_config.update_server_url = String::new();
        assert!(invalid_config.validate().is_err());

        invalid_config.update_server_url = "https://example.com".to_string();
        invalid_config.max_storage_size_mb = 0;
        assert!(invalid_config.validate().is_err());
    }

    #[test]
    fn test_model_metadata_creation() {
        let metadata = ModelMetadata::default_for_id("test_model");
        assert_eq!(metadata.model_id, "test_model");
        assert_eq!(metadata.version, "0.0.0");
    }

    #[tokio::test]
    async fn test_model_manager_creation() {
        let config = ModelManagerConfig::default();
        let result = ModelManager::new(config);
        assert!(result.is_ok());
    }

    #[test]
    fn test_model_manager_config_defaults() {
        let config = ModelManagerConfig::default();
        assert!(!config.update_server_url.is_empty());
        assert!(config.api_key.is_none());
        assert!(config.max_storage_size_mb > 0);
        assert!(config.enable_auto_updates);
        assert!(config.enable_differential_updates);
        assert!(config.require_signature_verification);
        assert!(config.download_timeout_seconds > 0);
        assert!(config.max_concurrent_downloads > 0);
        assert!(config.download_retry_attempts > 0);
    }

    #[test]
    fn test_model_manager_config_invalid_empty_url() {
        let mut config = ModelManagerConfig::default();
        config.update_server_url = String::new();
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_model_manager_config_invalid_storage() {
        let mut config = ModelManagerConfig::default();
        config.max_storage_size_mb = 0;
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_model_metadata_default_for_id() {
        let metadata = ModelMetadata::default_for_id("my_model");
        assert_eq!(metadata.model_id, "my_model");
        assert_eq!(metadata.version, "0.0.0");
        assert!(metadata.signature.is_none());
    }

    #[test]
    fn test_model_compatibility_creation() {
        let compat = ModelCompatibility {
            min_android_api: Some(30),
            min_ios_version: Some("16.0".to_string()),
            required_features: vec!["gpu".to_string()],
            min_memory_mb: 256,
            supported_architectures: vec!["arm64".to_string(), "x86_64".to_string()],
        };
        assert_eq!(compat.min_android_api, Some(30));
        assert_eq!(compat.supported_architectures.len(), 2);
    }

    #[test]
    fn test_download_progress_creation() {
        let progress = DownloadProgress {
            model_id: "test_model".to_string(),
            total_bytes: 1000000,
            downloaded_bytes: 500000,
            download_speed_bps: 100000.0,
            eta_seconds: 5.0,
            status: DownloadStatus::Downloading,
        };
        assert_eq!(progress.downloaded_bytes * 2, progress.total_bytes);
        assert!(progress.download_speed_bps > 0.0);
    }

    #[test]
    fn test_model_metadata_with_all_fields() {
        let metadata = ModelMetadata {
            model_id: "bert_tiny".to_string(),
            version: "1.2.0".to_string(),
            model_type: "transformer".to_string(),
            size_bytes: 50_000_000,
            checksum: "abc123def456".to_string(),
            signature: Some("sig_data".to_string()),
            download_url: "https://models.example.com/bert_tiny.bin".to_string(),
            differential_url: Some("https://models.example.com/bert_tiny.diff".to_string()),
            description: "Tiny BERT model".to_string(),
            required_config: MobileConfig::default(),
            compatibility: ModelCompatibility {
                min_android_api: None,
                min_ios_version: None,
                required_features: vec![],
                min_memory_mb: 64,
                supported_architectures: vec![],
            },
            release_timestamp: 1700000000,
            deprecation_timestamp: None,
            tags: vec!["nlp".to_string(), "bert".to_string()],
        };
        assert_eq!(metadata.model_id, "bert_tiny");
        assert!(metadata.signature.is_some());
        assert!(metadata.differential_url.is_some());
        assert!(metadata.deprecation_timestamp.is_none());
        assert_eq!(metadata.tags.len(), 2);
    }

    #[test]
    fn test_model_manager_config_with_api_key() {
        let mut config = ModelManagerConfig::default();
        config.api_key = Some("test_key_123".to_string());
        assert!(config.api_key.is_some());
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_model_manager_config_compression_disabled() {
        let mut config = ModelManagerConfig::default();
        config.enable_compression = false;
        assert!(!config.enable_compression);
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_model_manager_config_auto_updates_disabled() {
        let mut config = ModelManagerConfig::default();
        config.enable_auto_updates = false;
        assert!(!config.enable_auto_updates);
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_model_compatibility_minimal() {
        let compat = ModelCompatibility {
            min_android_api: None,
            min_ios_version: None,
            required_features: vec![],
            min_memory_mb: 0,
            supported_architectures: vec![],
        };
        assert!(compat.required_features.is_empty());
    }

    #[test]
    fn test_mock_http_client() {
        let client = MockHttpClient::new();
        client.set_response("https://example.com/api", "{\"status\": \"ok\"}");
        let result = client.get_metadata("https://example.com/api");
        assert!(result.is_ok());
    }

    #[test]
    fn test_mock_http_client_missing_url() {
        let client = MockHttpClient::new();
        let result = client.get_metadata("https://nonexistent.com");
        assert!(result.is_err());
    }

    #[test]
    fn test_mock_http_client_download() {
        let client = MockHttpClient::new();
        let tmp_path = std::env::temp_dir().join("test_download");
        let result = client.download_file("https://example.com/model", &tmp_path);
        assert!(result.is_ok());
    }

    #[test]
    fn test_mock_http_client_download_with_progress() {
        let client = MockHttpClient::new();
        let tmp_path = std::env::temp_dir().join("test_download_progress");
        let result = client.download_with_progress(
            "https://example.com/model",
            &tmp_path,
            Box::new(|downloaded, total| {
                let _ = (downloaded, total);
            }),
        );
        assert!(result.is_ok());
    }
}