trustformers 0.1.1

TrustformeRS - Rust port of Hugging Face Transformers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
use crate::error::{Result, TrustformersError};
use futures::stream::{self, StreamExt};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use reqwest::{blocking::Client, Client as AsyncClient};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::fs::{self, File, OpenOptions};
use std::io::{Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::Semaphore;
use trustformers_core::errors::TrustformersError as CoreTrustformersError;

const HF_HUB_URL: &str = "https://huggingface.co";

/// Options for downloading models from the Hugging Face Hub
#[derive(Clone, Debug)]
pub struct HubOptions {
    pub revision: Option<String>,
    pub cache_dir: Option<PathBuf>,
    pub force_download: bool,
    pub token: Option<String>,
    pub parallel_downloads: bool,
    pub max_concurrent_downloads: usize,
    pub enable_resumable_downloads: bool,
    pub enable_delta_compression: bool,
    pub chunk_size: usize,
    pub timeout_seconds: u64,
    pub retry_attempts: usize,
    pub use_cdn: bool,
    pub cdn_urls: Vec<String>,
    pub smart_caching: bool,
}

impl Default for HubOptions {
    fn default() -> Self {
        Self {
            revision: Some("main".to_string()),
            cache_dir: None,
            force_download: false,
            token: None,
            parallel_downloads: true,
            max_concurrent_downloads: 4,
            enable_resumable_downloads: true,
            enable_delta_compression: true,
            chunk_size: 8 * 1024 * 1024, // 8MB chunks
            timeout_seconds: 300,
            retry_attempts: 3,
            use_cdn: true,
            cdn_urls: vec![
                "https://cdn-lfs.huggingface.co".to_string(),
                "https://cdn.huggingface.co".to_string(),
            ],
            smart_caching: true,
        }
    }
}

/// Advanced download configuration
#[derive(Clone, Debug)]
pub struct DownloadConfig {
    pub parallel_downloads: bool,
    pub max_concurrent: usize,
    pub enable_resumable: bool,
    pub enable_compression: bool,
    pub chunk_size: usize,
    pub timeout: Duration,
    pub retry_attempts: usize,
    pub verify_checksums: bool,
    pub progress_reporting: bool,
}

impl Default for DownloadConfig {
    fn default() -> Self {
        Self {
            parallel_downloads: true,
            max_concurrent: 4,
            enable_resumable: true,
            enable_compression: true,
            chunk_size: 8 * 1024 * 1024,
            timeout: Duration::from_secs(300),
            retry_attempts: 3,
            verify_checksums: true,
            progress_reporting: true,
        }
    }
}

/// Download statistics and metrics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DownloadStats {
    pub total_files: usize,
    pub downloaded_files: usize,
    pub failed_files: usize,
    pub total_bytes: u64,
    pub downloaded_bytes: u64,
    #[serde(skip)]
    pub start_time: Option<Instant>,
    #[serde(skip)]
    pub end_time: Option<Instant>,
    pub average_speed_mbps: f64,
    pub parallel_efficiency: f64,
    pub cache_hit_rate: f64,
    pub compression_ratio: f64,
    pub resume_count: usize,
}

impl DownloadStats {
    pub fn duration(&self) -> Option<Duration> {
        if let (Some(start), Some(end)) = (self.start_time, self.end_time) {
            Some(end.duration_since(start))
        } else {
            None
        }
    }

    pub fn success_rate(&self) -> f64 {
        if self.total_files > 0 {
            self.downloaded_files as f64 / self.total_files as f64
        } else {
            0.0
        }
    }
}

/// Resume information for downloads
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResumeInfo {
    pub url: String,
    pub local_path: PathBuf,
    pub expected_size: u64,
    pub downloaded_size: u64,
    pub checksum: Option<String>,
    pub last_modified: Option<String>,
    #[serde(skip, default = "Instant::now")]
    pub created_at: Instant,
}

impl ResumeInfo {
    pub fn can_resume(&self, max_age: Duration) -> bool {
        self.created_at.elapsed() < max_age && self.downloaded_size > 0
    }
}

/// Delta compression info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeltaInfo {
    pub base_version: String,
    pub target_version: String,
    pub delta_url: String,
    pub compression_ratio: f64,
    pub delta_size: u64,
    pub full_size: u64,
}

/// CDN configuration and routing
#[derive(Debug, Clone)]
pub struct CdnConfig {
    pub primary_urls: Vec<String>,
    pub fallback_urls: Vec<String>,
    pub health_check_interval: Duration,
    pub latency_threshold: Duration,
    pub enable_geographic_routing: bool,
    pub region_preferences: Vec<String>,
}

impl Default for CdnConfig {
    fn default() -> Self {
        Self {
            primary_urls: vec![
                "https://cdn-lfs.huggingface.co".to_string(),
                "https://cdn.huggingface.co".to_string(),
            ],
            fallback_urls: vec!["https://huggingface.co".to_string()],
            health_check_interval: Duration::from_secs(300),
            latency_threshold: Duration::from_millis(1000),
            enable_geographic_routing: true,
            region_preferences: vec!["us".to_string(), "eu".to_string()],
        }
    }
}

/// Smart cache management
#[derive(Debug, Clone)]
pub struct SmartCacheConfig {
    pub max_cache_size_gb: f64,
    pub cleanup_threshold: f64,
    pub access_weight: f64,
    pub frequency_weight: f64,
    pub recency_weight: f64,
    pub size_penalty: f64,
    pub enable_predictive_caching: bool,
    pub enable_compression: bool,
}

impl Default for SmartCacheConfig {
    fn default() -> Self {
        Self {
            max_cache_size_gb: 50.0,
            cleanup_threshold: 0.9,
            access_weight: 0.4,
            frequency_weight: 0.3,
            recency_weight: 0.2,
            size_penalty: 0.1,
            enable_predictive_caching: true,
            enable_compression: true,
        }
    }
}

/// File information from the Hub API
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RepoFile {
    pub path: String,
    pub size: u64,
    #[serde(rename = "lfs")]
    pub lfs: Option<LfsInfo>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LfsInfo {
    pub sha256: String,
    pub size: u64,
    #[serde(rename = "pointerSize")]
    pub pointer_size: u64,
}

/// Model information from the Hub
#[derive(Debug, Clone)]
pub struct ModelInfo {
    pub model_id: String,
    pub sha: String,
    pub pipeline_tag: Option<String>,
    pub library_name: Option<String>,
    pub downloads: u64,
    pub likes: u64,
}

/// Model card information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelCard {
    pub license: Option<String>,
    pub language: Option<Vec<String>>,
    pub tags: Option<Vec<String>>,
    pub datasets: Option<Vec<String>>,
    pub metrics: Option<Vec<String>>,
    pub widget: Option<Vec<serde_json::Value>>,
    pub model_index: Option<Vec<serde_json::Value>>,
    pub thumbnail: Option<String>,
    pub pipeline_tag: Option<String>,
    pub inference: Option<bool>,
    #[serde(flatten)]
    pub extra: serde_json::Map<String, serde_json::Value>,
}

/// Get the cache directory for models
pub fn get_cache_dir() -> Result<PathBuf> {
    if let Ok(cache_dir) = std::env::var("TRUSTFORMERS_CACHE") {
        Ok(PathBuf::from(cache_dir))
    } else if let Some(cache_dir) = dirs::cache_dir() {
        Ok(cache_dir.join("trustformers"))
    } else if let Ok(home) = std::env::var("HOME") {
        Ok(PathBuf::from(home).join(".cache").join("trustformers"))
    } else {
        Err(TrustformersError::Core(CoreTrustformersError::other(
            "Could not determine cache directory".to_string(),
        )))
    }
}

/// Check if a model exists in the cache
pub fn is_cached(model_id: &str, revision: Option<&str>) -> Result<bool> {
    let cache_dir = get_cache_dir()?;
    let model_dir = cache_dir
        .join("models")
        .join(model_id.replace('/', "--"))
        .join(revision.unwrap_or("main"));

    Ok(model_dir.exists())
}

/// Enhanced download manager with parallel and resumable downloads
pub struct DownloadManager {
    config: DownloadConfig,
    cdn_config: CdnConfig,
    cache_config: SmartCacheConfig,
    client: AsyncClient,
    resume_db: HashMap<String, ResumeInfo>,
    stats: DownloadStats,
}

impl DownloadManager {
    pub fn new(config: DownloadConfig) -> Self {
        let client = AsyncClient::builder()
            .timeout(config.timeout)
            .build()
            .expect("Failed to build HTTP client with timeout config");

        Self {
            config,
            cdn_config: CdnConfig::default(),
            cache_config: SmartCacheConfig::default(),
            client,
            resume_db: HashMap::new(),
            stats: DownloadStats::default(),
        }
    }

    /// Download multiple files in parallel
    pub async fn download_files_parallel(
        &mut self,
        downloads: Vec<DownloadTask>,
        token: Option<&str>,
    ) -> Result<DownloadStats> {
        self.stats.start_time = Some(Instant::now());
        self.stats.total_files = downloads.len();
        self.stats.total_bytes = downloads.iter().map(|d| d.expected_size).sum();

        let multi_progress = MultiProgress::new();
        let semaphore = Arc::new(Semaphore::new(self.config.max_concurrent));

        // Create progress bars for each download
        let progress_bars: Vec<_> = downloads
            .iter()
            .map(|task| {
                let pb = multi_progress.add(ProgressBar::new(task.expected_size));
                pb.set_style(
                    ProgressStyle::default_bar()
                        .template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} {msg}")
                        .expect("Progress bar template is valid")
                        .progress_chars("#>-"),
                );
                pb.set_message(task.filename.clone());
                pb
            })
            .collect();

        // Execute downloads concurrently
        let results = stream::iter(downloads.into_iter().enumerate())
            .map(|(index, task)| {
                let semaphore = semaphore.clone();
                let client = self.client.clone();
                let config = self.config.clone();
                let pb = progress_bars[index].clone();
                let token = token.map(|s| s.to_string());

                async move {
                    let _permit =
                        semaphore.acquire().await.expect("semaphore should not be closed");
                    Self::download_single_file_async(client, task, token.as_deref(), config, pb)
                        .await
                }
            })
            .buffer_unordered(self.config.max_concurrent)
            .collect::<Vec<_>>()
            .await;

        // Process results
        for result in results {
            match result {
                Ok(_) => self.stats.downloaded_files += 1,
                Err(_) => self.stats.failed_files += 1,
            }
        }

        self.stats.end_time = Some(Instant::now());
        self.calculate_final_stats();

        Ok(self.stats.clone())
    }

    /// Download a single file with resumable support
    async fn download_single_file_async(
        client: AsyncClient,
        task: DownloadTask,
        token: Option<&str>,
        config: DownloadConfig,
        progress_bar: ProgressBar,
    ) -> Result<()> {
        let mut resume_offset = 0u64;
        let mut file = Self::prepare_file_for_download(&task.local_path, config.enable_resumable)?;

        // Check for resumable download
        if config.enable_resumable {
            if let Ok(metadata) = std::fs::metadata(&task.local_path) {
                resume_offset = metadata.len();
                progress_bar.set_position(resume_offset);
            }
        }

        let mut attempt = 0;
        while attempt < config.retry_attempts {
            match Self::attempt_download(
                &client,
                &task,
                token,
                resume_offset,
                &mut file,
                &progress_bar,
                &config,
            )
            .await
            {
                Ok(_) => return Ok(()),
                Err(e) => {
                    attempt += 1;
                    if attempt >= config.retry_attempts {
                        progress_bar.finish_with_message("Failed");
                        return Err(e);
                    }
                    // Exponential backoff
                    tokio::time::sleep(Duration::from_secs(2u64.pow(attempt as u32))).await;
                },
            }
        }

        Err(TrustformersError::Core(CoreTrustformersError::other(
            format!("Download failed after {} attempts", config.retry_attempts),
        )))
    }

    async fn attempt_download(
        client: &AsyncClient,
        task: &DownloadTask,
        token: Option<&str>,
        resume_offset: u64,
        file: &mut File,
        progress_bar: &ProgressBar,
        config: &DownloadConfig,
    ) -> Result<()> {
        let mut request = client.get(&task.url);

        if let Some(token) = token {
            request = request.bearer_auth(token);
        }

        // Add range header for resumable downloads
        if resume_offset > 0 {
            request = request.header("Range", format!("bytes={}-", resume_offset));
        }

        let response = request.send().await.map_err(|e| {
            TrustformersError::Core(CoreTrustformersError::other(format!(
                "Failed to send request: {}",
                e
            )))
        })?;

        if !response.status().is_success() && response.status().as_u16() != 206 {
            return Err(TrustformersError::Core(CoreTrustformersError::other(
                format!("Download failed with status: {}", response.status()),
            )));
        }

        // Seek to resume position if necessary
        if resume_offset > 0 {
            file.seek(SeekFrom::Start(resume_offset)).map_err(|e| TrustformersError::Io {
                message: format!("Failed to seek file: {}", e),
                path: Some(task.local_path.to_string_lossy().to_string()),
                suggestion: Some("Check file permissions and disk space".to_string()),
            })?;
        }

        let mut hasher = Sha256::new();
        let mut bytes_stream = response.bytes_stream();

        while let Some(chunk) = bytes_stream.next().await {
            let chunk = chunk.map_err(|e| TrustformersError::Network {
                message: format!("Failed to read chunk: {}", e),
                url: Some(task.url.clone()),
                status_code: None,
                suggestion: Some("Check network connection and retry".to_string()),
                retry_recommended: true,
            })?;

            hasher.update(&chunk);
            file.write_all(&chunk).map_err(|e| TrustformersError::Io {
                message: format!("Failed to write chunk: {}", e),
                path: Some(task.local_path.to_string_lossy().to_string()),
                suggestion: Some("Check disk space and file permissions".to_string()),
            })?;

            progress_bar.inc(chunk.len() as u64);
        }

        file.flush().map_err(|e| TrustformersError::Io {
            message: format!("Failed to flush file: {}", e),
            path: Some(task.local_path.to_string_lossy().to_string()),
            suggestion: Some("Check disk space and file permissions".to_string()),
        })?;

        // Verify checksum if provided
        if let Some(expected_sha) = &task.expected_checksum {
            if config.verify_checksums {
                let calculated_sha = hex::encode(hasher.finalize());
                if &calculated_sha != expected_sha {
                    fs::remove_file(&task.local_path).ok();
                    return Err(TrustformersError::Core(CoreTrustformersError::other(
                        format!(
                            "Checksum mismatch: expected {}, got {}",
                            expected_sha, calculated_sha
                        ),
                    )));
                }
            }
        }

        progress_bar.finish_with_message("Completed");
        Ok(())
    }

    fn prepare_file_for_download(path: &Path, enable_resumable: bool) -> Result<File> {
        // Create parent directory if it doesn't exist
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).map_err(|e| TrustformersError::Io {
                message: format!("Failed to create directory: {}", e),
                path: Some(parent.to_string_lossy().to_string()),
                suggestion: Some("Check permissions and available disk space".to_string()),
            })?;
        }

        let file = if enable_resumable && path.exists() {
            OpenOptions::new().append(true).open(path).map_err(|e| TrustformersError::Io {
                message: format!("Failed to open file for resume: {}", e),
                path: Some(path.to_string_lossy().to_string()),
                suggestion: Some("Check file permissions".to_string()),
            })?
        } else {
            File::create(path).map_err(|e| TrustformersError::Io {
                message: format!("Failed to create file: {}", e),
                path: Some(path.to_string_lossy().to_string()),
                suggestion: Some("Check directory permissions and disk space".to_string()),
            })?
        };

        Ok(file)
    }

    fn calculate_final_stats(&mut self) {
        if let Some(duration) = self.stats.duration() {
            let duration_secs = duration.as_secs_f64();
            if duration_secs > 0.0 {
                let bytes_per_sec = self.stats.downloaded_bytes as f64 / duration_secs;
                self.stats.average_speed_mbps = bytes_per_sec / (1024.0 * 1024.0);
            }
        }

        // Calculate parallel efficiency (simplified)
        if self.stats.total_files > 1 {
            self.stats.parallel_efficiency =
                self.config.max_concurrent as f64 / self.stats.total_files as f64;
            if self.stats.parallel_efficiency > 1.0 {
                self.stats.parallel_efficiency = 1.0;
            }
        }
    }

    /// Apply delta compression if available
    pub async fn apply_delta_compression(
        &self,
        delta_info: &DeltaInfo,
        base_path: &Path,
        target_path: &Path,
    ) -> Result<()> {
        // Download delta file
        let delta_path = target_path.with_extension("delta");
        let task = DownloadTask {
            url: delta_info.delta_url.clone(),
            local_path: delta_path.clone(),
            filename: "delta".to_string(),
            expected_size: delta_info.delta_size,
            expected_checksum: None,
        };

        Self::download_single_file_async(
            self.client.clone(),
            task,
            None,
            self.config.clone(),
            ProgressBar::hidden(),
        )
        .await?;

        // Apply delta (simplified implementation)
        // In a real implementation, this would use a proper binary diff algorithm
        self.apply_binary_delta(&delta_path, base_path, target_path).await?;

        // Clean up delta file
        fs::remove_file(delta_path).ok();

        Ok(())
    }

    async fn apply_binary_delta(
        &self,
        delta_path: &Path,
        base_path: &Path,
        target_path: &Path,
    ) -> Result<()> {
        // Simplified delta application - in reality you'd use bsdiff/xdelta3 or similar
        let delta_data = fs::read(delta_path).map_err(|e| TrustformersError::Io {
            message: format!("Failed to read delta file: {}", e),
            path: Some(delta_path.to_string_lossy().to_string()),
            suggestion: Some("Check file existence and permissions".to_string()),
        })?;

        let base_data = fs::read(base_path).map_err(|e| TrustformersError::Io {
            message: format!("Failed to read base file: {}", e),
            path: Some(base_path.to_string_lossy().to_string()),
            suggestion: Some("Check file existence and permissions".to_string()),
        })?;

        // This is a placeholder - real delta application would reconstruct the target file
        let target_data = Self::reconstruct_from_delta(&base_data, &delta_data)?;

        fs::write(target_path, target_data).map_err(|e| TrustformersError::Io {
            message: format!("Failed to write target file: {}", e),
            path: Some(target_path.to_string_lossy().to_string()),
            suggestion: Some("Check permissions and disk space".to_string()),
        })?;

        Ok(())
    }

    fn reconstruct_from_delta(base_data: &[u8], delta_data: &[u8]) -> Result<Vec<u8>> {
        // Placeholder implementation - real implementation would use proper binary diff
        // For now, just return the base data modified by delta
        let mut result = base_data.to_vec();

        // Simple example: XOR the delta with the base
        for (i, &delta_byte) in delta_data.iter().enumerate() {
            if i < result.len() {
                result[i] ^= delta_byte;
            }
        }

        Ok(result)
    }

    /// Smart cache management
    pub fn manage_smart_cache(&mut self, cache_dir: &Path) -> Result<()> {
        let cache_usage = self.calculate_cache_usage(cache_dir)?;
        let max_size_bytes =
            (self.cache_config.max_cache_size_gb * 1024.0 * 1024.0 * 1024.0) as u64;

        if cache_usage.total_size
            > (max_size_bytes as f64 * self.cache_config.cleanup_threshold) as u64
        {
            self.cleanup_cache(cache_dir, &cache_usage, max_size_bytes)?;
        }

        Ok(())
    }

    fn calculate_cache_usage(&self, cache_dir: &Path) -> Result<CacheUsage> {
        let mut usage = CacheUsage::default();
        self.scan_cache_directory(cache_dir, &mut usage)?;
        Ok(usage)
    }

    fn scan_cache_directory(&self, dir: &Path, usage: &mut CacheUsage) -> Result<()> {
        for entry in fs::read_dir(dir).map_err(|e| TrustformersError::Io {
            message: format!("Failed to read cache directory: {}", e),
            path: Some(dir.to_string_lossy().to_string()),
            suggestion: Some("Check directory existence and permissions".to_string()),
        })? {
            let entry = entry.map_err(|e| TrustformersError::Io {
                message: format!("Failed to read directory entry: {}", e),
                path: Some(dir.to_string_lossy().to_string()),
                suggestion: Some("Check directory permissions".to_string()),
            })?;
            let path = entry.path();

            if path.is_file() {
                if let Ok(metadata) = fs::metadata(&path) {
                    usage.total_size += metadata.len();
                    usage.file_count += 1;

                    let access_time =
                        metadata.accessed().unwrap_or(std::time::SystemTime::UNIX_EPOCH);
                    let file_info = CacheFileInfo {
                        path: path.clone(),
                        size: metadata.len(),
                        access_time,
                        score: self.calculate_cache_score(&metadata),
                    };
                    usage.files.push(file_info);
                }
            } else if path.is_dir() {
                self.scan_cache_directory(&path, usage)?;
            }
        }
        Ok(())
    }

    fn calculate_cache_score(&self, metadata: &fs::Metadata) -> f64 {
        let now = std::time::SystemTime::now();
        let access_time = metadata.accessed().unwrap_or(std::time::SystemTime::UNIX_EPOCH);
        let recency = now.duration_since(access_time).unwrap_or(Duration::ZERO).as_secs() as f64;

        // Simple scoring based on recency and size
        let recency_score = 1.0 / (1.0 + recency / 86400.0); // Decay over days
        let size_penalty = (metadata.len() as f64).log10() * self.cache_config.size_penalty;

        (recency_score * self.cache_config.recency_weight) - size_penalty
    }

    fn cleanup_cache(&mut self, cache_dir: &Path, usage: &CacheUsage, max_size: u64) -> Result<()> {
        let mut files = usage.files.clone();
        files.sort_by(|a, b| a.score.partial_cmp(&b.score).unwrap_or(std::cmp::Ordering::Equal));

        let target_size = (max_size as f64 * 0.8) as u64; // Clean to 80% of max
        let mut current_size = usage.total_size;

        for file_info in files {
            if current_size <= target_size {
                break;
            }

            if fs::remove_file(&file_info.path).is_ok() {
                current_size -= file_info.size;
                tracing::info!("Removed cached file: {:?}", file_info.path);
            }
        }

        Ok(())
    }
}

/// Download task definition
#[derive(Debug, Clone)]
pub struct DownloadTask {
    pub url: String,
    pub local_path: PathBuf,
    pub filename: String,
    pub expected_size: u64,
    pub expected_checksum: Option<String>,
}

/// Cache usage information
#[derive(Debug, Clone, Default)]
pub struct CacheUsage {
    pub total_size: u64,
    pub file_count: usize,
    pub files: Vec<CacheFileInfo>,
}

/// Cache file information
#[derive(Debug, Clone)]
pub struct CacheFileInfo {
    pub path: PathBuf,
    pub size: u64,
    pub access_time: std::time::SystemTime,
    pub score: f64,
}

/// Legacy synchronous download function (maintained for compatibility)
fn download_file(
    url: &str,
    path: &Path,
    token: Option<&str>,
    expected_sha: Option<&str>,
) -> Result<()> {
    // Create a basic download task and use the async implementation
    let rt = tokio::runtime::Runtime::new().map_err(|e| {
        TrustformersError::runtime_error(format!("Failed to create tokio runtime: {}", e))
    })?;
    rt.block_on(async {
        let client = AsyncClient::new();
        let config = DownloadConfig::default();
        let pb = ProgressBar::new(0);

        let task = DownloadTask {
            url: url.to_string(),
            local_path: path.to_path_buf(),
            filename: path.file_name().unwrap_or_default().to_string_lossy().to_string(),
            expected_size: 0,
            expected_checksum: expected_sha.map(|s| s.to_string()),
        };

        DownloadManager::download_single_file_async(client, task, token, config, pb).await
    })
}

/// List files in a repository
fn list_repo_files(model_id: &str, revision: &str, token: Option<&str>) -> Result<Vec<RepoFile>> {
    let client = Client::new();
    let url = format!("{}/api/models/{}/tree/{}", HF_HUB_URL, model_id, revision);

    let mut request = client.get(&url);
    if let Some(token) = token {
        request = request.bearer_auth(token);
    }

    let response = request.send().map_err(|e| TrustformersError::Hub {
        message: format!("Failed to list repo files: {}", e),
        model_id: model_id.to_string(),
        endpoint: Some(url.clone()),
        suggestion: Some("Check network connection and model ID".to_string()),
        recovery_actions: vec![],
    })?;

    if !response.status().is_success() {
        return Err(TrustformersError::Core(CoreTrustformersError::other(
            format!("Failed to list repo files: HTTP {}", response.status()),
        )));
    }

    let files: Vec<RepoFile> = response.json().map_err(|e| {
        TrustformersError::invalid_input(
            format!("Failed to parse repo files response: {}", e),
            Some("api_response"),
            Some("valid JSON array of RepoFile objects"),
            Some("invalid JSON format"),
        )
    })?;

    Ok(files)
}

/// Download a model from the Hugging Face Hub (legacy implementation)
pub fn download_model(model_id: &str, options: Option<HubOptions>) -> Result<PathBuf> {
    let opts = options.unwrap_or_default();
    let revision = opts.revision.as_deref().unwrap_or("main");

    // Get cache directory
    let cache_dir = opts.cache_dir.unwrap_or_else(|| {
        get_cache_dir().expect("Failed to get cache directory from environment")
    });
    let model_dir = cache_dir.join("models").join(model_id.replace('/', "--")).join(revision);

    // Check if already cached and not forcing download
    if !opts.force_download && model_dir.exists() {
        tracing::info!("Model {} already cached at {:?}", model_id, model_dir);
        return Ok(model_dir);
    }

    // Create model directory
    fs::create_dir_all(&model_dir).map_err(|e| TrustformersError::Io {
        message: format!("Failed to create model directory: {}", e),
        path: Some(model_dir.to_string_lossy().to_string()),
        suggestion: Some("Check cache directory permissions and disk space".to_string()),
    })?;

    // List files in the repository
    let files = list_repo_files(model_id, revision, opts.token.as_deref())?;

    // Download essential files
    let essential_files = [
        "config.json",
        "pytorch_model.bin",
        "model.safetensors",
        "tokenizer_config.json",
        "tokenizer.json",
        "vocab.txt",
        "vocab.json",
        "merges.txt",
    ];

    for file in files.iter() {
        if essential_files.contains(&file.path.as_str()) || file.path.ends_with(".safetensors") {
            let file_path = model_dir.join(&file.path);

            // Skip if file already exists and not forcing download
            if !opts.force_download && file_path.exists() {
                tracing::info!("File {} already exists, skipping", file.path);
                continue;
            }

            let download_url = if file.lfs.is_some() {
                format!(
                    "{}/{}/resolve/{}/{}",
                    HF_HUB_URL, model_id, revision, file.path
                )
            } else {
                format!("{}/{}/raw/{}/{}", HF_HUB_URL, model_id, revision, file.path)
            };

            tracing::info!("Downloading {} from {}", file.path, download_url);

            let expected_sha = file.lfs.as_ref().map(|lfs| lfs.sha256.as_str());
            download_file(
                &download_url,
                &file_path,
                opts.token.as_deref(),
                expected_sha,
            )?;
        }
    }

    Ok(model_dir)
}

/// Enhanced model download with parallel downloads and advanced features
pub async fn download_model_enhanced(
    model_id: &str,
    options: Option<HubOptions>,
) -> Result<(PathBuf, DownloadStats)> {
    let opts = options.unwrap_or_default();
    let revision = opts.revision.as_deref().unwrap_or("main");

    // Get cache directory
    let cache_dir = opts.cache_dir.unwrap_or_else(|| {
        get_cache_dir().expect("Failed to get cache directory from environment")
    });
    let model_dir = cache_dir.join("models").join(model_id.replace('/', "--")).join(revision);

    // Check if already cached and not forcing download
    if !opts.force_download && model_dir.exists() {
        tracing::info!("Model {} already cached at {:?}", model_id, model_dir);
        return Ok((model_dir, DownloadStats::default()));
    }

    // Create model directory
    fs::create_dir_all(&model_dir).map_err(|e| TrustformersError::Io {
        message: format!("Failed to create model directory: {}", e),
        path: Some(model_dir.to_string_lossy().to_string()),
        suggestion: Some("Check cache directory permissions and disk space".to_string()),
    })?;

    // Create download configuration
    let download_config = DownloadConfig {
        parallel_downloads: opts.parallel_downloads,
        max_concurrent: opts.max_concurrent_downloads,
        enable_resumable: opts.enable_resumable_downloads,
        enable_compression: opts.enable_delta_compression,
        chunk_size: opts.chunk_size,
        timeout: Duration::from_secs(opts.timeout_seconds),
        retry_attempts: opts.retry_attempts,
        verify_checksums: true,
        progress_reporting: true,
    };

    let mut download_manager = DownloadManager::new(download_config);

    // Enable smart caching if requested
    if opts.smart_caching {
        download_manager.manage_smart_cache(&cache_dir)?;
    }

    // List files in the repository
    let files = list_repo_files(model_id, revision, opts.token.as_deref())?;

    // Filter and prepare download tasks
    let essential_files = [
        "config.json",
        "pytorch_model.bin",
        "model.safetensors",
        "tokenizer_config.json",
        "tokenizer.json",
        "vocab.txt",
        "vocab.json",
        "merges.txt",
    ];

    let mut download_tasks = Vec::new();

    for file in files.iter() {
        if essential_files.contains(&file.path.as_str()) || file.path.ends_with(".safetensors") {
            let file_path = model_dir.join(&file.path);

            // Skip if file already exists and not forcing download
            if !opts.force_download && file_path.exists() {
                tracing::info!("File {} already exists, skipping", file.path);
                continue;
            }

            // Choose optimal download URL
            let download_url = if opts.use_cdn && !opts.cdn_urls.is_empty() {
                // Try CDN first
                if file.lfs.is_some() {
                    format!(
                        "{}/{}/resolve/{}/{}",
                        opts.cdn_urls[0], model_id, revision, file.path
                    )
                } else {
                    format!(
                        "{}/{}/raw/{}/{}",
                        opts.cdn_urls[0], model_id, revision, file.path
                    )
                }
            } else {
                // Use main hub URL
                if file.lfs.is_some() {
                    format!(
                        "{}/{}/resolve/{}/{}",
                        HF_HUB_URL, model_id, revision, file.path
                    )
                } else {
                    format!("{}/{}/raw/{}/{}", HF_HUB_URL, model_id, revision, file.path)
                }
            };

            let expected_checksum = file.lfs.as_ref().map(|lfs| lfs.sha256.clone());

            download_tasks.push(DownloadTask {
                url: download_url,
                local_path: file_path,
                filename: file.path.clone(),
                expected_size: file.size,
                expected_checksum,
            });
        }
    }

    // Execute downloads
    let stats = if opts.parallel_downloads && download_tasks.len() > 1 {
        tracing::info!(
            "Starting parallel download of {} files",
            download_tasks.len()
        );
        download_manager
            .download_files_parallel(download_tasks, opts.token.as_deref())
            .await?
    } else {
        tracing::info!(
            "Starting sequential download of {} files",
            download_tasks.len()
        );
        let mut sequential_stats = DownloadStats::default();
        sequential_stats.start_time = Some(Instant::now());
        sequential_stats.total_files = download_tasks.len();

        for task in download_tasks {
            let pb = ProgressBar::new(task.expected_size);
            match DownloadManager::download_single_file_async(
                download_manager.client.clone(),
                task,
                opts.token.as_deref(),
                download_manager.config.clone(),
                pb,
            )
            .await
            {
                Ok(_) => sequential_stats.downloaded_files += 1,
                Err(_) => sequential_stats.failed_files += 1,
            }
        }

        sequential_stats.end_time = Some(Instant::now());
        sequential_stats
    };

    tracing::info!("Download completed. Stats: {:#?}", stats);

    Ok((model_dir, stats))
}

/// Create optimized download configuration for different scenarios
pub fn create_download_config_for_scenario(scenario: DownloadScenario) -> DownloadConfig {
    match scenario {
        DownloadScenario::FastDevelopment => DownloadConfig {
            parallel_downloads: true,
            max_concurrent: 8,
            enable_resumable: true,
            enable_compression: false,    // Skip compression for speed
            chunk_size: 16 * 1024 * 1024, // 16MB chunks
            timeout: Duration::from_secs(120),
            retry_attempts: 2,
            verify_checksums: false, // Skip verification for speed
            progress_reporting: true,
        },
        DownloadScenario::Production => DownloadConfig {
            parallel_downloads: true,
            max_concurrent: 4,
            enable_resumable: true,
            enable_compression: true,
            chunk_size: 8 * 1024 * 1024,
            timeout: Duration::from_secs(600),
            retry_attempts: 5,
            verify_checksums: true,
            progress_reporting: false, // Reduce overhead in production
        },
        DownloadScenario::BandwidthLimited => DownloadConfig {
            parallel_downloads: false, // Sequential to reduce bandwidth usage
            max_concurrent: 1,
            enable_resumable: true,
            enable_compression: true,
            chunk_size: 1024 * 1024, // 1MB chunks
            timeout: Duration::from_secs(1200),
            retry_attempts: 10,
            verify_checksums: true,
            progress_reporting: true,
        },
        DownloadScenario::Reliable => DownloadConfig {
            parallel_downloads: true,
            max_concurrent: 2,
            enable_resumable: true,
            enable_compression: true,
            chunk_size: 4 * 1024 * 1024,
            timeout: Duration::from_secs(900),
            retry_attempts: 8,
            verify_checksums: true,
            progress_reporting: true,
        },
    }
}

/// Download scenarios for optimized configurations
#[derive(Debug, Clone, Copy)]
pub enum DownloadScenario {
    FastDevelopment,
    Production,
    BandwidthLimited,
    Reliable,
}

/// Get download statistics for a model
pub async fn get_download_stats(
    model_id: &str,
    revision: Option<&str>,
) -> Result<ModelDownloadInfo> {
    let client = AsyncClient::new();
    let revision = revision.unwrap_or("main");
    let url = format!("{}/api/models/{}", HF_HUB_URL, model_id);

    let response = client.get(&url).send().await.map_err(|e| TrustformersError::Hub {
        message: format!("Failed to get model info: {}", e),
        model_id: model_id.to_string(),
        endpoint: Some(url.clone()),
        suggestion: Some("Check network connection and model ID".to_string()),
        recovery_actions: vec![],
    })?;

    if !response.status().is_success() {
        return Err(TrustformersError::Core(CoreTrustformersError::other(
            format!("Failed to get model info: HTTP {}", response.status()),
        )));
    }

    let model_info: serde_json::Value = response.json().await.map_err(|e| {
        TrustformersError::invalid_input(
            format!("Failed to parse model info response: {}", e),
            Some("api_response"),
            Some("valid JSON model info object"),
            Some("invalid JSON format"),
        )
    })?;

    // Extract relevant information
    let downloads = model_info.get("downloads").and_then(|d| d.as_u64()).unwrap_or(0);
    let likes = model_info.get("likes").and_then(|l| l.as_u64()).unwrap_or(0);
    let pipeline_tag =
        model_info.get("pipeline_tag").and_then(|p| p.as_str()).map(|s| s.to_string());

    // Get file information for size calculation
    let files = list_repo_files(model_id, revision, None)?;
    let total_size: u64 = files.iter().map(|f| f.size).sum();
    let file_count = files.len();

    Ok(ModelDownloadInfo {
        model_id: model_id.to_string(),
        revision: revision.to_string(),
        total_size,
        file_count,
        downloads,
        likes,
        pipeline_tag,
        essential_files: files.iter().filter(|f| is_essential_file(&f.path)).cloned().collect(),
        estimated_download_time: estimate_download_time(total_size),
    })
}

/// Check if a file is essential for model operation
fn is_essential_file(filename: &str) -> bool {
    let essential_files = [
        "config.json",
        "pytorch_model.bin",
        "model.safetensors",
        "tokenizer_config.json",
        "tokenizer.json",
        "vocab.txt",
        "vocab.json",
        "merges.txt",
    ];

    essential_files.contains(&filename) || filename.ends_with(".safetensors")
}

/// Estimate download time based on file size
fn estimate_download_time(total_size: u64) -> Duration {
    // Assume average download speed of 10 MB/s
    let average_speed_mbps = 10.0 * 1024.0 * 1024.0;
    let estimated_seconds = total_size as f64 / average_speed_mbps;
    Duration::from_secs(estimated_seconds as u64)
}

/// Model download information
#[derive(Debug, Clone)]
pub struct ModelDownloadInfo {
    pub model_id: String,
    pub revision: String,
    pub total_size: u64,
    pub file_count: usize,
    pub downloads: u64,
    pub likes: u64,
    pub pipeline_tag: Option<String>,
    pub essential_files: Vec<RepoFile>,
    pub estimated_download_time: Duration,
}

impl ModelDownloadInfo {
    pub fn size_mb(&self) -> f64 {
        self.total_size as f64 / (1024.0 * 1024.0)
    }

    pub fn size_gb(&self) -> f64 {
        self.total_size as f64 / (1024.0 * 1024.0 * 1024.0)
    }
}

/// Check if delta compression is available for a model update
pub async fn check_delta_availability(
    model_id: &str,
    from_revision: &str,
    to_revision: &str,
) -> Result<Option<DeltaInfo>> {
    // This is a placeholder implementation
    // In reality, you would check the hub API for delta information
    let delta_url = format!(
        "{}/api/models/{}/deltas/{}/{}",
        HF_HUB_URL, model_id, from_revision, to_revision
    );

    let client = AsyncClient::new();
    let response = client.get(&delta_url).send().await;

    match response {
        Ok(resp) if resp.status().is_success() => {
            let delta_info: DeltaInfo = resp.json().await.map_err(|e| {
                TrustformersError::invalid_input(
                    format!("Failed to parse delta info: {}", e),
                    Some("delta_response"),
                    Some("valid DeltaInfo JSON object"),
                    Some("invalid JSON format"),
                )
            })?;
            Ok(Some(delta_info))
        },
        _ => Ok(None), // Delta not available
    }
}

/// Download a specific file from the Hub
pub fn download_file_from_hub(
    model_id: &str,
    filename: &str,
    options: Option<HubOptions>,
) -> Result<PathBuf> {
    let opts = options.unwrap_or_default();
    let revision = opts.revision.as_deref().unwrap_or("main");

    // Get cache directory
    let cache_dir = opts.cache_dir.unwrap_or_else(|| {
        get_cache_dir().expect("Failed to get cache directory from environment")
    });
    let model_dir = cache_dir.join("models").join(model_id.replace('/', "--")).join(revision);

    let file_path = model_dir.join(filename);

    // Check if already cached and not forcing download
    if !opts.force_download && file_path.exists() {
        return Ok(file_path);
    }

    // Create model directory
    fs::create_dir_all(&model_dir).map_err(|e| TrustformersError::Io {
        message: format!("Failed to create model directory: {}", e),
        path: Some(model_dir.to_string_lossy().to_string()),
        suggestion: Some("Check cache directory permissions and disk space".to_string()),
    })?;

    // Download the file
    let download_url = format!(
        "{}/{}/resolve/{}/{}",
        HF_HUB_URL, model_id, revision, filename
    );

    download_file(&download_url, &file_path, opts.token.as_deref(), None)?;

    Ok(file_path)
}

/// Load a model configuration from the Hub
pub fn load_config_from_hub(
    model_id: &str,
    options: Option<HubOptions>,
) -> Result<serde_json::Value> {
    let config_path = download_file_from_hub(model_id, "config.json", options)?;
    let config_str = std::fs::read_to_string(&config_path).map_err(|e| TrustformersError::Io {
        message: format!("Failed to read config file: {}", e),
        path: Some(config_path.to_string_lossy().to_string()),
        suggestion: Some("Check file existence and permissions".to_string()),
    })?;
    serde_json::from_str(&config_str).map_err(|e| {
        TrustformersError::invalid_input(
            format!("Failed to parse config: {}", e),
            Some("config_json"),
            Some("valid JSON format"),
            Some("invalid JSON"),
        )
    })
}

/// Load model weights from the Hub (supports SafeTensors format)
pub fn load_weights_from_hub(
    model_id: &str,
    options: Option<HubOptions>,
) -> Result<Box<dyn crate::core::traits::WeightReader>> {
    // Try SafeTensors first
    let safetensors_path = download_file_from_hub(model_id, "model.safetensors", options.clone());

    if let Ok(path) = safetensors_path {
        let reader = crate::core::utils::weight_loading::SafeTensorsReader::from_file(&path)?;
        return Ok(Box::new(reader));
    }

    // Fall back to PyTorch format if SafeTensors not available
    let pytorch_formats = ["pytorch_model.bin", "model.pt", "pytorch_model.pt"];

    for pytorch_file in &pytorch_formats {
        if let Ok(path) = download_file_from_hub(model_id, pytorch_file, options.clone()) {
            let reader = crate::core::utils::weight_loading::PyTorchReader::from_file(&path)?;
            return Ok(Box::new(reader));
        }
    }

    // If neither SafeTensors nor PyTorch formats are found
    Err(TrustformersError::Core(CoreTrustformersError::other(
        format!("No supported weight format found for model {}: Tried SafeTensors (.safetensors), PyTorch (.bin, .pt)", model_id)
    )))
}

/// Parse model card from README.md
fn parse_model_card_from_readme(content: &str) -> Result<ModelCard> {
    // Look for YAML frontmatter in the README
    if let Some(yaml_start) = content.find("---\n") {
        if let Some(yaml_end) = content[yaml_start + 4..].find("\n---") {
            let yaml_content = &content[yaml_start + 4..yaml_start + 4 + yaml_end];

            // Parse YAML frontmatter
            let yaml_value: serde_yaml::Value =
                serde_yaml::from_str(yaml_content).map_err(|e| {
                    TrustformersError::invalid_input(
                        format!("Failed to parse YAML frontmatter: {}", e),
                        Some("yaml_frontmatter"),
                        Some("valid YAML format"),
                        Some("invalid YAML"),
                    )
                })?;

            // Convert YAML to JSON for easier handling with serde_json
            let json_value = serde_json::to_value(yaml_value).map_err(|e| {
                TrustformersError::invalid_input(
                    format!("Failed to convert YAML to JSON: {}", e),
                    Some("yaml_content"),
                    Some("YAML convertible to JSON"),
                    Some("incompatible YAML structure"),
                )
            })?;

            // Parse as ModelCard
            let model_card: ModelCard = serde_json::from_value(json_value).map_err(|e| {
                TrustformersError::invalid_input(
                    format!("Failed to parse model card: {}", e),
                    Some("model_card_json"),
                    Some("valid ModelCard structure"),
                    Some("invalid model card format"),
                )
            })?;

            return Ok(model_card);
        }
    }

    // If no YAML frontmatter found, return empty model card
    Ok(ModelCard {
        license: None,
        language: None,
        tags: None,
        datasets: None,
        metrics: None,
        widget: None,
        model_index: None,
        thumbnail: None,
        pipeline_tag: None,
        inference: None,
        extra: serde_json::Map::new(),
    })
}

/// Load a model card from the Hub
pub fn load_model_card_from_hub(model_id: &str, options: Option<HubOptions>) -> Result<ModelCard> {
    // Try to download README.md
    let readme_path = download_file_from_hub(model_id, "README.md", options);

    if let Ok(path) = readme_path {
        let readme_content = std::fs::read_to_string(&path).map_err(|e| TrustformersError::Io {
            message: format!("Failed to read README.md: {}", e),
            path: Some(path.to_string_lossy().to_string()),
            suggestion: Some("Check file existence and permissions".to_string()),
        })?;

        parse_model_card_from_readme(&readme_content)
    } else {
        // If README.md not found, return empty model card
        Ok(ModelCard {
            license: None,
            language: None,
            tags: None,
            datasets: None,
            metrics: None,
            widget: None,
            model_index: None,
            thumbnail: None,
            pipeline_tag: None,
            inference: None,
            extra: serde_json::Map::new(),
        })
    }
}

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

    #[test]
    fn test_cache_dir() {
        let cache_dir = get_cache_dir();
        assert!(cache_dir.is_ok());
    }

    #[test]
    fn test_is_cached() {
        let result = is_cached("bert-base-uncased", None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_hub_options_default() {
        let opts = HubOptions::default();
        assert_eq!(opts.revision, Some("main".to_string()));
        assert!(opts.cache_dir.is_none());
        assert!(!opts.force_download);
        assert!(opts.token.is_none());
        assert!(opts.parallel_downloads);
        assert_eq!(opts.max_concurrent_downloads, 4);
        assert!(opts.enable_resumable_downloads);
        assert!(opts.enable_delta_compression);
        assert_eq!(opts.chunk_size, 8 * 1024 * 1024);
        assert_eq!(opts.timeout_seconds, 300);
        assert_eq!(opts.retry_attempts, 3);
        assert!(opts.use_cdn);
        assert!(opts.smart_caching);
    }

    #[test]
    fn test_download_config_default() {
        let config = DownloadConfig::default();
        assert!(config.parallel_downloads);
        assert_eq!(config.max_concurrent, 4);
        assert!(config.enable_resumable);
        assert!(config.enable_compression);
        assert_eq!(config.chunk_size, 8 * 1024 * 1024);
        assert_eq!(config.timeout, Duration::from_secs(300));
        assert_eq!(config.retry_attempts, 3);
        assert!(config.verify_checksums);
        assert!(config.progress_reporting);
    }

    #[test]
    fn test_download_stats_default() {
        let stats = DownloadStats::default();
        assert_eq!(stats.total_files, 0);
        assert_eq!(stats.downloaded_files, 0);
        assert_eq!(stats.failed_files, 0);
        assert_eq!(stats.total_bytes, 0);
        assert_eq!(stats.downloaded_bytes, 0);
    }

    #[test]
    fn test_download_stats_success_rate_empty() {
        let stats = DownloadStats::default();
        assert!((stats.success_rate() - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_download_stats_success_rate_partial() {
        let stats = DownloadStats {
            total_files: 10,
            downloaded_files: 7,
            failed_files: 3,
            ..DownloadStats::default()
        };
        assert!((stats.success_rate() - 0.7).abs() < f64::EPSILON);
    }

    #[test]
    fn test_download_stats_success_rate_all() {
        let stats = DownloadStats {
            total_files: 5,
            downloaded_files: 5,
            failed_files: 0,
            ..DownloadStats::default()
        };
        assert!((stats.success_rate() - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_download_stats_duration_none() {
        let stats = DownloadStats::default();
        assert!(stats.duration().is_none());
    }

    #[test]
    fn test_download_stats_duration_with_times() {
        let start = Instant::now();
        let stats = DownloadStats {
            start_time: Some(start),
            end_time: Some(start),
            ..DownloadStats::default()
        };
        let dur = stats.duration();
        assert!(dur.is_some());
    }

    #[test]
    fn test_cdn_config_default() {
        let config = CdnConfig::default();
        assert!(!config.primary_urls.is_empty());
        assert!(!config.fallback_urls.is_empty());
        assert_eq!(config.health_check_interval, Duration::from_secs(300));
        assert_eq!(config.latency_threshold, Duration::from_millis(1000));
        assert!(config.enable_geographic_routing);
        assert!(!config.region_preferences.is_empty());
    }

    #[test]
    fn test_smart_cache_config_default() {
        let config = SmartCacheConfig::default();
        assert!((config.max_cache_size_gb - 50.0).abs() < f64::EPSILON);
        assert!((config.cleanup_threshold - 0.9).abs() < f64::EPSILON);
        // Weights should sum to approximately 1.0
        let total = config.access_weight
            + config.frequency_weight
            + config.recency_weight
            + config.size_penalty;
        assert!((total - 1.0).abs() < f64::EPSILON);
        assert!(config.enable_predictive_caching);
        assert!(config.enable_compression);
    }

    #[test]
    fn test_resume_info_can_resume_recent() {
        let info = ResumeInfo {
            url: "https://example.com/file".to_string(),
            local_path: PathBuf::from("/tmp/file"),
            expected_size: 1000,
            downloaded_size: 500,
            checksum: None,
            last_modified: None,
            created_at: Instant::now(),
        };
        assert!(info.can_resume(Duration::from_secs(3600)));
    }

    #[test]
    fn test_resume_info_cannot_resume_zero_downloaded() {
        let info = ResumeInfo {
            url: "https://example.com/file".to_string(),
            local_path: PathBuf::from("/tmp/file"),
            expected_size: 1000,
            downloaded_size: 0,
            checksum: None,
            last_modified: None,
            created_at: Instant::now(),
        };
        assert!(!info.can_resume(Duration::from_secs(3600)));
    }

    #[test]
    fn test_delta_info_creation() {
        let delta = DeltaInfo {
            base_version: "v1".to_string(),
            target_version: "v2".to_string(),
            delta_url: "https://example.com/delta".to_string(),
            compression_ratio: 0.3,
            delta_size: 30_000_000,
            full_size: 100_000_000,
        };
        assert!(delta.delta_size < delta.full_size);
        assert!((delta.compression_ratio - 0.3).abs() < f64::EPSILON);
    }

    #[test]
    fn test_model_info_creation() {
        let info = ModelInfo {
            model_id: "bert-base".to_string(),
            sha: "abc123".to_string(),
            pipeline_tag: Some("text-classification".to_string()),
            library_name: Some("trustformers".to_string()),
            downloads: 10000,
            likes: 500,
        };
        assert_eq!(info.model_id, "bert-base");
        assert!(info.pipeline_tag.is_some());
        assert!(info.downloads > 0);
    }

    #[test]
    fn test_hub_options_custom() {
        let opts = HubOptions {
            revision: Some("develop".to_string()),
            cache_dir: Some(PathBuf::from("/custom/cache")),
            force_download: true,
            token: Some("hf_token".to_string()),
            parallel_downloads: false,
            max_concurrent_downloads: 1,
            enable_resumable_downloads: false,
            enable_delta_compression: false,
            chunk_size: 1024 * 1024,
            timeout_seconds: 60,
            retry_attempts: 1,
            use_cdn: false,
            cdn_urls: vec![],
            smart_caching: false,
        };
        assert!(opts.force_download);
        assert!(!opts.parallel_downloads);
        assert_eq!(opts.max_concurrent_downloads, 1);
    }

    #[test]
    fn test_download_config_custom() {
        let config = DownloadConfig {
            parallel_downloads: false,
            max_concurrent: 1,
            enable_resumable: false,
            enable_compression: false,
            chunk_size: 1024,
            timeout: Duration::from_secs(10),
            retry_attempts: 0,
            verify_checksums: false,
            progress_reporting: false,
        };
        assert!(!config.parallel_downloads);
        assert_eq!(config.retry_attempts, 0);
    }

    #[test]
    fn test_is_cached_nonexistent_model() {
        let result = is_cached("nonexistent-model-xyz-123", None);
        assert!(result.is_ok());
        assert!(!result.expect("Operation failed"));
    }

    #[test]
    fn test_is_cached_with_revision() {
        let result = is_cached("bert-base", Some("v1.0"));
        assert!(result.is_ok());
    }

    #[test]
    fn test_cdn_config_primary_url_count() {
        let config = CdnConfig::default();
        assert_eq!(config.primary_urls.len(), 2);
    }

    #[test]
    fn test_hub_options_cdn_urls_default() {
        let opts = HubOptions::default();
        assert_eq!(opts.cdn_urls.len(), 2);
    }

    #[test]
    fn test_download_manager_creation() {
        let config = DownloadConfig::default();
        let manager = DownloadManager::new(config);
        assert_eq!(manager.stats.total_files, 0);
    }
}