romm-cli 0.32.0

Rust-based CLI and TUI for the ROMM API
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
//! Download state and management.
//!
//! `DownloadJob` holds per-download progress/status.
//! `DownloadManager` owns the shared job list and spawns background tasks.

//! ROM and save file download management.
//!
//! This module handles the logic for downloading files from the RomM server,
//! including resume support and progress tracking.

use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

use crate::client::RommClient;
use crate::core::extras::build_base_rom_file_targets;
use crate::core::extras::{DownloadAssetKind, DownloadTarget};
use crate::core::interrupt::is_cancelled_error;
use crate::core::utils;
use crate::types::Rom;
use anyhow::{anyhow, Context, Result};
use std::fs::File;
use zip::ZipArchive;

/// Directory for ROM storage (`ROMM_ROMS_DIR`, `ROMM_DOWNLOAD_DIR`, or configured path).
pub fn resolve_download_directory(configured_download_dir: Option<&str>) -> Result<PathBuf> {
    let env_override = std::env::var("ROMM_ROMS_DIR")
        .ok()
        .or_else(|| std::env::var("ROMM_DOWNLOAD_DIR").ok());
    resolve_download_directory_from_inputs(configured_download_dir, env_override.as_deref())
}

/// Validate configured download path without env override fallback.
pub fn validate_configured_download_directory(configured_download_dir: &str) -> Result<PathBuf> {
    resolve_download_directory_from_inputs(Some(configured_download_dir), None)
}

/// Backward-compatible default used by legacy CLI download code.
pub fn download_directory() -> PathBuf {
    std::env::var("ROMM_ROMS_DIR")
        .or_else(|_| std::env::var("ROMM_DOWNLOAD_DIR"))
        .map(PathBuf::from)
        .unwrap_or_else(|_| PathBuf::from("./downloads"))
}

fn resolve_download_directory_from_inputs(
    configured_download_dir: Option<&str>,
    env_override: Option<&str>,
) -> Result<PathBuf> {
    let raw = env_override
        .or(configured_download_dir)
        .map(str::trim)
        .ok_or_else(|| {
            anyhow!("ROMs directory is not configured. Run setup to set a ROMs path.")
        })?;

    if raw.is_empty() {
        return Err(anyhow!("ROMs directory cannot be empty"));
    }

    let input_path = PathBuf::from(raw);
    let normalized = if input_path.is_relative() {
        std::env::current_dir()
            .context("Could not resolve current working directory")?
            .join(input_path)
    } else {
        input_path
    };

    if normalized.exists() && !normalized.is_dir() {
        return Err(anyhow!(
            "Download path is not a directory: {}",
            normalized.display()
        ));
    }

    std::fs::create_dir_all(&normalized).with_context(|| {
        format!(
            "Could not create download directory {}",
            normalized.display()
        )
    })?;

    let probe_name = format!(
        ".romm-write-test-{}",
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos()
    );
    let probe_path = normalized.join(probe_name);
    let probe = std::fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(&probe_path)
        .with_context(|| format!("ROMs directory is not writable: {}", normalized.display()))?;
    drop(probe);
    let _ = std::fs::remove_file(&probe_path);

    Ok(normalized)
}

/// Pick `stem.zip`, then `stem__2.zip`, `stem__3.zip`, … until the path does not exist.
pub fn unique_zip_path(dir: &Path, stem: &str) -> PathBuf {
    let mut n = 1u32;
    loop {
        let name = if n == 1 {
            format!("{}.zip", stem)
        } else {
            format!("{}__{}.zip", stem, n)
        };
        let p = dir.join(name);
        if !p.exists() {
            return p;
        }
        n = n.saturating_add(1);
    }
}

/// Extract a ZIP archive into `destination_dir`.
pub fn extract_zip_archive(zip_path: &Path, destination_dir: &Path) -> Result<()> {
    let zip_path = zip_path.to_path_buf();
    let destination_dir = destination_dir.to_path_buf();
    std::fs::create_dir_all(&destination_dir).with_context(|| {
        format!(
            "Could not create extraction directory {}",
            destination_dir.display()
        )
    })?;

    let file = File::open(&zip_path)
        .with_context(|| format!("Could not open zip archive {}", zip_path.display()))?;
    let mut archive = ZipArchive::new(file)
        .with_context(|| format!("Invalid ZIP archive {}", zip_path.display()))?;
    archive.extract(&destination_dir).with_context(|| {
        format!(
            "Could not extract archive into {}",
            destination_dir.display()
        )
    })?;
    Ok(())
}

// ---------------------------------------------------------------------------
// Job status / data
// ---------------------------------------------------------------------------

/// High-level status of a single download.
#[derive(Debug, Clone)]
pub enum DownloadStatus {
    Downloading,
    Done,
    SkippedAlreadyExists,
    Cancelled,
    FinalizeFailed(String),
    Error(String),
}

/// A single background download job (for one ROM).
#[derive(Debug, Clone)]
pub struct DownloadJob {
    pub id: usize,
    pub rom_id: u64,
    pub name: String,
    pub platform: String,
    /// 0.0 ..= 1.0
    pub progress: f64,
    pub status: DownloadStatus,
}

static NEXT_JOB_ID: AtomicUsize = AtomicUsize::new(0);

impl DownloadJob {
    /// Construct a new job in the `Downloading` state.
    pub fn new(rom_id: u64, name: String, platform: String) -> Self {
        Self {
            id: NEXT_JOB_ID.fetch_add(1, Ordering::Relaxed),
            rom_id,
            name,
            platform,
            progress: 0.0,
            status: DownloadStatus::Downloading,
        }
    }

    /// Progress as percentage 0..=100.
    pub fn percent(&self) -> u16 {
        (self.progress * 100.0).round().min(100.0) as u16
    }
}

// ---------------------------------------------------------------------------
// Extras (composite) jobs
// ---------------------------------------------------------------------------

/// Outcome of one item inside an [`ExtrasJob`].
#[derive(Debug, Clone)]
pub struct ExtrasItemResult {
    pub title: String,
    pub kind: DownloadAssetKind,
    pub ok: bool,
    pub error: Option<String>,
}

/// Terminal status for a composite extras download.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExtrasJobStatus {
    Running,
    Done,
    /// Some items failed (`usize` = failure count).
    PartialFailure(usize),
    AllFailed,
}

/// One queued extras batch for a parent ROM (related files + cover + manual).
#[derive(Debug, Clone)]
pub struct ExtrasJob {
    pub id: usize,
    pub rom_id: u64,
    pub name: String,
    pub platform: String,
    pub completed_items: usize,
    pub total_items: usize,
    pub status: ExtrasJobStatus,
    pub item_results: Vec<ExtrasItemResult>,
}

static NEXT_EXTRAS_JOB_ID: AtomicUsize = AtomicUsize::new(0);

impl ExtrasJob {
    pub fn new(rom_id: u64, name: String, platform: String, total_items: usize) -> Self {
        Self {
            id: NEXT_EXTRAS_JOB_ID.fetch_add(1, Ordering::Relaxed),
            rom_id,
            name,
            platform,
            completed_items: 0,
            total_items,
            status: ExtrasJobStatus::Running,
            item_results: Vec::new(),
        }
    }

    /// Progress 0..=100 from completed item count only.
    pub fn percent(&self) -> u16 {
        if self.total_items == 0 {
            return 100;
        }
        ((self.completed_items.saturating_mul(100)) / self.total_items).min(100) as u16
    }
}

fn finalize_extras_job_status(results: &[ExtrasItemResult]) -> ExtrasJobStatus {
    let n = results.len();
    if n == 0 {
        return ExtrasJobStatus::Done;
    }
    let failures = results.iter().filter(|r| !r.ok).count();
    if failures == 0 {
        ExtrasJobStatus::Done
    } else if failures == n {
        ExtrasJobStatus::AllFailed
    } else {
        ExtrasJobStatus::PartialFailure(failures)
    }
}

// ---------------------------------------------------------------------------
// Manager
// ---------------------------------------------------------------------------

/// Owns the shared download list and spawns background download tasks.
///
/// Frontends only need an `Arc<Mutex<Vec<DownloadJob>>>` to inspect jobs.
#[derive(Clone)]
pub struct DownloadManager {
    jobs: Arc<Mutex<Vec<DownloadJob>>>,
    extras_jobs: Arc<Mutex<Vec<ExtrasJob>>>,
}

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

impl DownloadManager {
    pub fn new() -> Self {
        Self {
            jobs: Arc::new(Mutex::new(Vec::new())),
            extras_jobs: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Shared handle for observers (TUI, GUI, tests) to inspect jobs.
    pub fn shared(&self) -> Arc<Mutex<Vec<DownloadJob>>> {
        self.jobs.clone()
    }

    /// Shared extras jobs (composite batches).
    pub fn shared_extras(&self) -> Arc<Mutex<Vec<ExtrasJob>>> {
        self.extras_jobs.clone()
    }

    /// Start downloading `rom` in the background; returns immediately.
    ///
    /// Progress updates are pushed into the shared `jobs` list so that
    /// any frontend can render them.
    pub fn start_download(
        &self,
        rom: &Rom,
        client: RommClient,
        configured_download_dir: Option<&str>,
    ) -> Result<()> {
        let platform = rom
            .platform_display_name
            .as_deref()
            .or(rom.platform_custom_name.as_deref())
            .unwrap_or("—")
            .to_string();

        let job = DownloadJob::new(rom.id, rom.name.clone(), platform);
        let job_id = job.id;
        let rom_id = rom.id;
        let fs_name = rom.fs_name.clone();
        let final_console_slug = rom
            .platform_fs_slug
            .clone()
            .or_else(|| rom.platform_slug.clone())
            .unwrap_or_else(|| format!("platform-{}", rom.platform_id));
        let final_name = sanitized_final_filename(&rom.fs_name, rom.id);
        let rom_for_targets = rom.clone();
        match self.jobs.lock() {
            Ok(mut jobs) => jobs.push(job),
            Err(err) => {
                eprintln!("warning: download job list lock poisoned: {}", err);
                return Err(anyhow!("download job list lock poisoned: {err}"));
            }
        }

        let save_dir = resolve_download_directory(configured_download_dir)?;
        let jobs = self.jobs.clone();
        tokio::spawn(async move {
            let temp_root = save_dir.join(".tmp");
            if let Err(err) = tokio::fs::create_dir_all(&temp_root).await {
                if let Ok(mut list) = jobs.lock() {
                    if let Some(j) = list.iter_mut().find(|j| j.id == job_id) {
                        j.status = DownloadStatus::Error(format!(
                            "Could not create temp directory {}: {err}",
                            temp_root.display()
                        ));
                    }
                }
                return;
            }

            let console_dir = save_dir.join(utils::sanitize_filename(&final_console_slug));
            let final_path = console_dir.join(final_name.clone());
            if let Err(err) = tokio::fs::create_dir_all(&console_dir).await {
                if let Ok(mut list) = jobs.lock() {
                    if let Some(j) = list.iter_mut().find(|j| j.id == job_id) {
                        j.status = DownloadStatus::Error(format!(
                            "Could not create console directory {}: {err}",
                            console_dir.display()
                        ));
                    }
                }
                return;
            }

            let base_targets = build_base_rom_file_targets(&rom_for_targets, &save_dir);
            if !base_targets.is_empty() {
                let total_targets = base_targets.len() as f64;
                for (idx, target) in base_targets.iter().enumerate() {
                    let client = client.clone();
                    let mut progress = {
                        let jobs = jobs.clone();
                        move |received: u64, total: u64| {
                            let file_ratio = if total > 0 {
                                received as f64 / total as f64
                            } else {
                                0.0
                            };
                            let total_ratio = ((idx as f64) + file_ratio) / total_targets;
                            if let Ok(mut list) = jobs.lock() {
                                if let Some(j) = list.iter_mut().find(|j| j.id == job_id) {
                                    j.progress = total_ratio.min(1.0);
                                }
                            }
                        }
                    };
                    match prepare_download_target_destination(target).await {
                        Ok(true) => {
                            progress(
                                target.expected_size_bytes.unwrap_or(0),
                                target.expected_size_bytes.unwrap_or(0),
                            );
                            continue;
                        }
                        Ok(false) => {}
                        Err(err) => {
                            if let Ok(mut list) = jobs.lock() {
                                if let Some(j) = list.iter_mut().find(|j| j.id == job_id) {
                                    j.status = DownloadStatus::Error(err.to_string());
                                }
                            }
                            return;
                        }
                    }
                    if let Err(final_err) =
                        download_target_with_fallback(&client, target, |_, _| false, &mut progress)
                            .await
                    {
                        if let Ok(mut list) = jobs.lock() {
                            if let Some(j) = list.iter_mut().find(|j| j.id == job_id) {
                                j.status = DownloadStatus::Error(final_err.to_string());
                            }
                        }
                        return;
                    }
                }
                if let Ok(mut list) = jobs.lock() {
                    if let Some(j) = list.iter_mut().find(|j| j.id == job_id) {
                        j.status = DownloadStatus::Done;
                        j.progress = 1.0;
                    }
                }
                return;
            }

            if final_path.exists() {
                if let Ok(mut list) = jobs.lock() {
                    if let Some(j) = list.iter_mut().find(|j| j.id == job_id) {
                        j.status = DownloadStatus::SkippedAlreadyExists;
                        j.progress = 1.0;
                    }
                }
                return;
            }

            let temp_name = format!(
                "rom-{}-{}-{}.part",
                rom_id,
                utils::sanitize_filename(&fs_name),
                job_id
            );
            let temp_path = temp_root.join(temp_name);

            let on_progress = |received: u64, total: u64| {
                let p = if total > 0 {
                    received as f64 / total as f64
                } else {
                    0.0
                };

                if let Ok(mut list) = jobs.lock() {
                    if let Some(j) = list.iter_mut().find(|j| j.id == job_id) {
                        j.progress = p;
                    }
                }
            };

            let download_result = client.download_rom(rom_id, &temp_path, on_progress).await;
            if download_result.is_err() {
                let _ = tokio::fs::remove_file(&temp_path).await;
            }
            match download_result {
                Ok(()) => match finalize_download(&temp_path, &final_path).await {
                    Ok(FinalizeResult::Done) => {
                        if let Ok(mut list) = jobs.lock() {
                            if let Some(j) = list.iter_mut().find(|j| j.id == job_id) {
                                j.status = DownloadStatus::Done;
                                j.progress = 1.0;
                            }
                        }
                    }
                    Ok(FinalizeResult::SkippedAlreadyExists) => {
                        if let Ok(mut list) = jobs.lock() {
                            if let Some(j) = list.iter_mut().find(|j| j.id == job_id) {
                                j.status = DownloadStatus::SkippedAlreadyExists;
                                j.progress = 1.0;
                            }
                        }
                    }
                    Err(err) => {
                        let _ = tokio::fs::remove_file(&temp_path).await;
                        if let Ok(mut list) = jobs.lock() {
                            if let Some(j) = list.iter_mut().find(|j| j.id == job_id) {
                                j.status = DownloadStatus::FinalizeFailed(err.to_string());
                            }
                        }
                    }
                },
                Err(e) => {
                    if let Ok(mut list) = jobs.lock() {
                        if let Some(j) = list.iter_mut().find(|j| j.id == job_id) {
                            if is_cancelled_error(&e) {
                                j.status = DownloadStatus::Cancelled;
                            } else {
                                j.status = DownloadStatus::Error(e.to_string());
                            }
                        }
                    }
                }
            }
        });
        Ok(())
    }

    /// Download selected extras targets in the background as one composite job.
    ///
    /// Uses up to 4 concurrent URL downloads. Progress is item-count only (`ExtrasJob::percent`).
    pub fn start_extras_download(
        &self,
        rom: &Rom,
        selected: Vec<DownloadTarget>,
        client: RommClient,
        configured_download_dir: Option<&str>,
    ) -> Result<()> {
        if selected.is_empty() {
            return Err(anyhow!("no extras targets selected"));
        }

        let _ = resolve_download_directory(configured_download_dir)?;

        let platform = rom
            .platform_display_name
            .as_deref()
            .or(rom.platform_custom_name.as_deref())
            .unwrap_or("—")
            .to_string();

        let total_items = selected.len();
        let job = ExtrasJob::new(rom.id, rom.name.clone(), platform, total_items);
        let job_id = job.id;

        match self.extras_jobs.lock() {
            Ok(mut jobs) => jobs.push(job),
            Err(err) => {
                eprintln!("warning: extras job list lock poisoned: {}", err);
                return Err(anyhow!("extras job list lock poisoned: {err}"));
            }
        }

        let extras_jobs = self.extras_jobs.clone();
        tokio::spawn(async move {
            let semaphore = Arc::new(tokio::sync::Semaphore::new(4));
            let mut handles = Vec::new();

            for target in selected {
                let permit = match semaphore.clone().acquire_owned().await {
                    Ok(p) => p,
                    Err(_) => break,
                };
                let client = client.clone();
                let extras_jobs = extras_jobs.clone();
                handles.push(tokio::spawn(async move {
                    let mut on_progress = |_r: u64, _t: u64| {};
                    let download_result = match prepare_download_target_destination(&target).await {
                        Ok(true) => Ok(()),
                        Ok(false) => {
                            download_target_with_fallback(
                                &client,
                                &target,
                                |_, _| false,
                                &mut on_progress,
                            )
                            .await
                        }
                        Err(err) => Err(err),
                    };

                    drop(permit);

                    let (ok, err) = match download_result {
                        Ok(()) => (true, None),
                        Err(e) => (false, Some(e.to_string())),
                    };

                    let item = ExtrasItemResult {
                        title: target.title.clone(),
                        kind: target.kind,
                        ok,
                        error: err,
                    };

                    if let Ok(mut list) = extras_jobs.lock() {
                        if let Some(j) = list.iter_mut().find(|j| j.id == job_id) {
                            j.completed_items = j.completed_items.saturating_add(1);
                            j.item_results.push(item);
                            if j.completed_items >= j.total_items {
                                j.status = finalize_extras_job_status(&j.item_results);
                            }
                        }
                    }
                }));
            }

            for h in handles {
                let _ = h.await;
            }
        });

        Ok(())
    }
}

pub async fn prepare_download_target_destination(target: &DownloadTarget) -> Result<bool> {
    let Some(expected_size) = target.expected_size_bytes else {
        return Ok(false);
    };
    if expected_size == 0 {
        return Ok(false);
    }

    let Ok(metadata) = tokio::fs::metadata(&target.destination).await else {
        return Ok(false);
    };
    let current_size = metadata.len();
    if current_size == expected_size {
        return Ok(true);
    }
    if current_size > expected_size {
        tokio::fs::remove_file(&target.destination)
            .await
            .with_context(|| {
                format!(
                    "remove oversized stale download {} ({} > {} bytes)",
                    target.destination.display(),
                    current_size,
                    expected_size
                )
            })?;
    }
    Ok(false)
}

async fn download_target_with_fallback<F, C>(
    client: &RommClient,
    target: &DownloadTarget,
    mut is_cancelled: C,
    on_progress: &mut F,
) -> Result<()>
where
    F: FnMut(u64, u64) + Send,
    C: FnMut(u64, u64) -> bool + Send,
{
    let urls = candidate_download_urls(target);
    let mut last_err: Option<anyhow::Error> = None;
    for url in urls {
        match client
            .download_url_with_query_with_cancel(
                &url,
                &target.source_query,
                &target.destination,
                &mut is_cancelled,
                on_progress,
            )
            .await
        {
            Ok(()) => return Ok(()),
            Err(err) => {
                if !err.to_string().contains("404 Not Found") {
                    return Err(err);
                }
                last_err = Some(err);
            }
        }
    }
    Err(last_err.unwrap_or_else(|| anyhow!("download failed without error details")))
}

fn candidate_download_urls(target: &DownloadTarget) -> Vec<String> {
    let mut out = vec![target.source_url.clone()];
    if let Some((file_id, file_name)) = parse_current_rom_file_content_path(&target.source_url) {
        out.push(format!("/api/romsfiles/{file_id}/content/{file_name}"));
        out.push(format!("/api/roms/files/{file_id}/content/{file_name}"));
    } else if let Some((file_id, file_name)) = parse_romsfiles_path(&target.source_url) {
        out.push(format!("/api/roms/{file_id}/files/content/{file_name}"));
        out.push(format!("/api/roms/files/{file_id}/content/{file_name}"));
    } else if let Some((file_id, file_name)) = parse_legacy_roms_files_path(&target.source_url) {
        out.push(format!("/api/roms/{file_id}/files/content/{file_name}"));
        out.push(format!("/api/romsfiles/{file_id}/content/{file_name}"));
    }
    dedupe_preserve_order(out)
}

fn parse_current_rom_file_content_path(url: &str) -> Option<(String, String)> {
    let prefix = "/api/roms/";
    let marker = "/files/content/";
    let rest = url.strip_prefix(prefix)?;
    let (id, name) = rest.split_once(marker)?;
    Some((id.to_string(), name.to_string()))
}

fn parse_romsfiles_path(url: &str) -> Option<(String, String)> {
    let prefix = "/api/romsfiles/";
    let marker = "/content/";
    let rest = url.strip_prefix(prefix)?;
    let (id, name) = rest.split_once(marker)?;
    Some((id.to_string(), name.to_string()))
}

fn parse_legacy_roms_files_path(url: &str) -> Option<(String, String)> {
    let prefix = "/api/roms/files/";
    let marker = "/content/";
    let rest = url.strip_prefix(prefix)?;
    let (id, name) = rest.split_once(marker)?;
    Some((id.to_string(), name.to_string()))
}

fn dedupe_preserve_order(urls: Vec<String>) -> Vec<String> {
    let mut seen = std::collections::HashSet::new();
    let mut out = Vec::new();
    for u in urls {
        if seen.insert(u.clone()) {
            out.push(u);
        }
    }
    out
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FinalizeResult {
    Done,
    SkippedAlreadyExists,
}

async fn finalize_download(temp_path: &Path, final_path: &Path) -> Result<FinalizeResult> {
    if final_path.exists() {
        let _ = tokio::fs::remove_file(temp_path).await;
        return Ok(FinalizeResult::SkippedAlreadyExists);
    }

    match tokio::fs::rename(temp_path, final_path).await {
        Ok(()) => Ok(FinalizeResult::Done),
        Err(rename_err) if is_cross_device_rename_error(&rename_err) => {
            tokio::fs::copy(temp_path, final_path)
                .await
                .with_context(|| {
                    format!(
                        "Could not copy temp ROM {} to final destination {}",
                        temp_path.display(),
                        final_path.display()
                    )
                })?;
            let file = tokio::fs::File::open(final_path).await.with_context(|| {
                format!(
                    "Could not open finalized ROM for sync: {}",
                    final_path.display()
                )
            })?;
            file.sync_all().await.with_context(|| {
                format!(
                    "Could not sync finalized ROM to disk: {}",
                    final_path.display()
                )
            })?;
            tokio::fs::remove_file(temp_path).await.with_context(|| {
                format!(
                    "Could not remove temp ROM after copy: {}",
                    temp_path.display()
                )
            })?;
            Ok(FinalizeResult::Done)
        }
        Err(rename_err) => Err(anyhow!(
            "Could not move temp ROM {} to final destination {}: {}",
            temp_path.display(),
            final_path.display(),
            rename_err
        )),
    }
}

fn is_cross_device_rename_error(err: &std::io::Error) -> bool {
    matches!(err.raw_os_error(), Some(18) | Some(17))
}

fn sanitized_final_filename(fs_name: &str, rom_id: u64) -> String {
    let sanitized = utils::sanitize_filename(fs_name);
    if sanitized.trim().is_empty() {
        format!("rom-{rom_id}.zip")
    } else {
        sanitized
    }
}

#[cfg(test)]
fn final_download_path_for_rom(roms_dir: &Path, rom: &Rom) -> PathBuf {
    let platform_slug = rom
        .platform_fs_slug
        .clone()
        .or_else(|| rom.platform_slug.clone())
        .unwrap_or_else(|| format!("platform-{}", rom.platform_id));
    let console_dir = roms_dir.join(utils::sanitize_filename(&platform_slug));
    console_dir.join(sanitized_final_filename(&rom.fs_name, rom.id))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::Rom;
    use std::io::Write;
    use std::time::{SystemTime, UNIX_EPOCH};
    use zip::write::SimpleFileOptions;
    use zip::ZipWriter;

    fn rom_fixture_with_platform(platform_fs_slug: Option<&str>, fs_name: &str) -> Rom {
        Rom {
            id: 42,
            platform_id: 7,
            platform_slug: Some("nintendo-switch".to_string()),
            platform_fs_slug: platform_fs_slug.map(ToString::to_string),
            platform_custom_name: None,
            platform_display_name: None,
            fs_name: fs_name.to_string(),
            fs_name_no_tags: "game".to_string(),
            fs_name_no_ext: "game".to_string(),
            fs_extension: "zip".to_string(),
            fs_path: "/game.zip".to_string(),
            fs_size_bytes: 1,
            name: "Game".to_string(),
            slug: None,
            summary: None,
            path_cover_small: None,
            path_cover_large: None,
            url_cover: None,
            has_manual: false,
            path_manual: None,
            url_manual: None,
            is_unidentified: false,
            is_identified: true,
            files: Vec::new(),
        }
    }

    #[test]
    fn extras_job_percent_tracks_completed_items() {
        let mut j = ExtrasJob::new(1, "Zelda".into(), "NES".into(), 4);
        assert_eq!(j.percent(), 0);
        j.completed_items = 2;
        assert_eq!(j.percent(), 50);
        j.completed_items = 4;
        assert_eq!(j.percent(), 100);
    }

    #[test]
    fn finalize_extras_job_status_reflects_failures() {
        use crate::core::extras::DownloadAssetKind;

        let ok = ExtrasItemResult {
            title: "a".into(),
            kind: DownloadAssetKind::Cover,
            ok: true,
            error: None,
        };
        let bad = ExtrasItemResult {
            title: "b".into(),
            kind: DownloadAssetKind::Manual,
            ok: false,
            error: Some("e".into()),
        };
        assert_eq!(
            super::finalize_extras_job_status(&[ok.clone(), ok.clone()]),
            ExtrasJobStatus::Done
        );
        assert_eq!(
            super::finalize_extras_job_status(&[bad.clone(), bad.clone()]),
            ExtrasJobStatus::AllFailed
        );
        assert_eq!(
            super::finalize_extras_job_status(&[ok, bad]),
            ExtrasJobStatus::PartialFailure(1)
        );
    }

    #[test]
    fn unique_zip_path_skips_existing_files() {
        let ts = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let dir = std::env::temp_dir().join(format!("romm-dl-test-{ts}"));
        std::fs::create_dir_all(&dir).unwrap();
        let p1 = dir.join("game.zip");
        std::fs::File::create(&p1).unwrap().write_all(b"x").unwrap();
        let p2 = unique_zip_path(&dir, "game");
        assert_eq!(p2.file_name().unwrap(), "game__2.zip");
        let _ = std::fs::remove_file(&p1);
        let _ = std::fs::remove_dir(&dir);
    }

    #[test]
    fn resolve_download_directory_rejects_empty_configured_path() {
        let err = resolve_download_directory_from_inputs(Some("   "), None)
            .expect_err("empty configured path should be rejected");
        assert!(
            err.to_string().contains("cannot be empty"),
            "unexpected error: {err:#}"
        );
    }

    #[test]
    fn resolve_download_directory_creates_missing_nested_directory() {
        let ts = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let base = std::env::temp_dir().join(format!("romm-dl-resolve-{ts}"));
        let nested = base.join("a").join("b").join("c");
        let nested_str = nested.to_string_lossy().to_string();

        let resolved = resolve_download_directory_from_inputs(Some(&nested_str), None)
            .expect("expected missing directory to be created");

        assert!(resolved.is_dir(), "resolved path must be a directory");
        assert!(nested.is_dir(), "nested path should be created");
        let _ = std::fs::remove_dir_all(&base);
    }

    #[test]
    fn resolve_download_directory_fails_when_target_is_a_file() {
        let ts = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let base = std::env::temp_dir().join(format!("romm-dl-file-target-{ts}"));
        std::fs::create_dir_all(&base).expect("create base dir");
        let file_path = base.join("not-a-dir.txt");
        std::fs::write(&file_path, b"x").expect("create file");
        let input = file_path.to_string_lossy().to_string();

        let err = resolve_download_directory_from_inputs(Some(&input), None)
            .expect_err("file target must fail");
        assert!(
            err.to_string().contains("not a directory"),
            "unexpected error: {err:#}"
        );

        let _ = std::fs::remove_file(&file_path);
        let _ = std::fs::remove_dir_all(&base);
    }

    #[test]
    fn resolve_download_directory_env_override_takes_precedence() {
        let ts = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let configured = std::env::temp_dir().join(format!("romm-dl-configured-{ts}"));
        let env_dir = std::env::temp_dir().join(format!("romm-dl-env-{ts}"));
        let configured_str = configured.to_string_lossy().to_string();
        let env_str = env_dir.to_string_lossy().to_string();

        let resolved =
            resolve_download_directory_from_inputs(Some(&configured_str), Some(&env_str))
                .expect("env override should be used");

        assert_eq!(resolved, env_dir);
        assert!(env_dir.is_dir(), "env directory should be created");
        assert!(
            !configured.is_dir(),
            "configured path should be ignored when env override is set"
        );
        let _ = std::fs::remove_dir_all(&env_dir);
    }

    #[test]
    fn final_download_path_uses_console_folder_and_original_file_name() {
        let rom = rom_fixture_with_platform(Some("switch"), "Zelda (USA).xci");
        let base = PathBuf::from("/roms");
        let out = final_download_path_for_rom(&base, &rom);
        assert_eq!(out, PathBuf::from("/roms/switch/Zelda _USA_.xci"));
    }

    #[test]
    fn rom_file_download_candidates_use_official_romsfiles_endpoint() {
        let target = DownloadTarget {
            kind: DownloadAssetKind::RomFile,
            title: "Update".into(),
            source_url: "/api/roms/11/files/content/update%2Ensp".into(),
            source_query: Vec::new(),
            destination: PathBuf::from("/tmp/update.nsp"),
            expected_size_bytes: Some(11),
        };

        assert_eq!(
            candidate_download_urls(&target),
            vec![
                "/api/roms/11/files/content/update%2Ensp".to_string(),
                "/api/romsfiles/11/content/update%2Ensp".to_string(),
                "/api/roms/files/11/content/update%2Ensp".to_string()
            ]
        );
    }

    #[test]
    fn romsfiles_candidate_falls_forward_to_current_official_path() {
        let target = DownloadTarget {
            kind: DownloadAssetKind::RomFile,
            title: "Update".into(),
            source_url: "/api/romsfiles/11/content/update%2Ensp".into(),
            source_query: Vec::new(),
            destination: PathBuf::from("/tmp/update.nsp"),
            expected_size_bytes: Some(11),
        };

        assert_eq!(
            candidate_download_urls(&target),
            vec![
                "/api/romsfiles/11/content/update%2Ensp".to_string(),
                "/api/roms/11/files/content/update%2Ensp".to_string(),
                "/api/roms/files/11/content/update%2Ensp".to_string()
            ]
        );
    }

    #[test]
    fn legacy_roms_files_candidate_falls_forward_to_romsfiles() {
        let target = DownloadTarget {
            kind: DownloadAssetKind::RomFile,
            title: "Update".into(),
            source_url: "/api/roms/files/11/content/update%2Ensp".into(),
            source_query: Vec::new(),
            destination: PathBuf::from("/tmp/update.nsp"),
            expected_size_bytes: Some(11),
        };

        assert_eq!(
            candidate_download_urls(&target),
            vec![
                "/api/roms/files/11/content/update%2Ensp".to_string(),
                "/api/roms/11/files/content/update%2Ensp".to_string(),
                "/api/romsfiles/11/content/update%2Ensp".to_string()
            ]
        );
    }

    #[tokio::test]
    async fn prepare_target_removes_oversized_stale_rom_file() {
        let ts = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let path = std::env::temp_dir().join(format!("romm-oversized-target-{ts}.nsp"));
        tokio::fs::write(&path, b"too-large").await.unwrap();

        let target = DownloadTarget {
            kind: DownloadAssetKind::RomFile,
            title: "Base".into(),
            source_url: "/api/roms/1/files/content/base.nsp".into(),
            source_query: Vec::new(),
            destination: path.clone(),
            expected_size_bytes: Some(4),
        };

        let skip = prepare_download_target_destination(&target).await.unwrap();
        assert!(!skip);
        assert!(!path.exists());
    }

    #[tokio::test]
    async fn prepare_target_skips_exact_size_rom_file() {
        let ts = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let path = std::env::temp_dir().join(format!("romm-exact-target-{ts}.nsp"));
        tokio::fs::write(&path, b"done").await.unwrap();

        let target = DownloadTarget {
            kind: DownloadAssetKind::RomFile,
            title: "Base".into(),
            source_url: "/api/roms/1/files/content/base.nsp".into(),
            source_query: Vec::new(),
            destination: path.clone(),
            expected_size_bytes: Some(4),
        };

        let skip = prepare_download_target_destination(&target).await.unwrap();
        assert!(skip);
        assert_eq!(tokio::fs::read(&path).await.unwrap(), b"done");
        let _ = tokio::fs::remove_file(path).await;
    }

    #[tokio::test]
    async fn base_target_prepare_skips_exact_size_file() {
        let ts = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let base = std::env::temp_dir().join(format!("romm-base-exact-{ts}"));
        let mut rom = rom_fixture_with_platform(Some("switch"), "pack.zip");
        rom.files = vec![crate::types::RomFile {
            id: 1,
            rom_id: rom.id,
            file_name: "base.nsp".into(),
            file_path: "/base.nsp".into(),
            file_size_bytes: 4,
            category: Some(crate::types::RomFileCategory::Game),
        }];
        let target = build_base_rom_file_targets(&rom, &base).remove(0);
        tokio::fs::create_dir_all(target.destination.parent().unwrap())
            .await
            .unwrap();
        tokio::fs::write(&target.destination, b"done")
            .await
            .unwrap();

        let skip = prepare_download_target_destination(&target).await.unwrap();
        assert!(skip);
        assert_eq!(tokio::fs::read(&target.destination).await.unwrap(), b"done");
        let _ = tokio::fs::remove_dir_all(base).await;
    }

    #[tokio::test]
    async fn base_target_prepare_removes_oversized_file() {
        let ts = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let base = std::env::temp_dir().join(format!("romm-base-oversized-{ts}"));
        let mut rom = rom_fixture_with_platform(Some("switch"), "pack.zip");
        rom.files = vec![crate::types::RomFile {
            id: 1,
            rom_id: rom.id,
            file_name: "base.nsp".into(),
            file_path: "/base.nsp".into(),
            file_size_bytes: 4,
            category: Some(crate::types::RomFileCategory::Game),
        }];
        let target = build_base_rom_file_targets(&rom, &base).remove(0);
        tokio::fs::create_dir_all(target.destination.parent().unwrap())
            .await
            .unwrap();
        tokio::fs::write(&target.destination, b"too-large")
            .await
            .unwrap();

        let skip = prepare_download_target_destination(&target).await.unwrap();
        assert!(!skip);
        assert!(!target.destination.exists());
        let _ = tokio::fs::remove_dir_all(base).await;
    }

    #[tokio::test]
    async fn finalize_download_skips_when_final_exists() {
        let ts = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let base = std::env::temp_dir().join(format!("romm-finalize-skip-{ts}"));
        std::fs::create_dir_all(&base).unwrap();
        let temp = base.join("temp.part");
        let final_path = base.join("final.zip");
        std::fs::write(&temp, b"temp").unwrap();
        std::fs::write(&final_path, b"existing").unwrap();

        let result = finalize_download(&temp, &final_path).await.unwrap();
        assert_eq!(result, super::FinalizeResult::SkippedAlreadyExists);
        assert!(
            !temp.exists(),
            "temp file should be removed when final destination exists"
        );

        let _ = std::fs::remove_file(&final_path);
        let _ = std::fs::remove_dir_all(&base);
    }

    #[test]
    fn extract_zip_archive_writes_files_to_destination() {
        let ts = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let base = std::env::temp_dir().join(format!("romm-extract-{ts}"));
        let zip_path = base.join("sample.zip");
        let out_dir = base.join("out");
        std::fs::create_dir_all(&base).unwrap();

        let zip_file = std::fs::File::create(&zip_path).unwrap();
        let mut writer = ZipWriter::new(zip_file);
        writer
            .start_file("nested/game.rom", SimpleFileOptions::default())
            .unwrap();
        writer.write_all(b"rom-bytes").unwrap();
        writer.finish().unwrap();

        extract_zip_archive(&zip_path, &out_dir).unwrap();

        let extracted = out_dir.join("nested").join("game.rom");
        assert!(
            extracted.exists(),
            "expected extracted file at {:?}",
            extracted
        );
        let data = std::fs::read(&extracted).unwrap();
        assert_eq!(data, b"rom-bytes");

        let _ = std::fs::remove_dir_all(&base);
    }
}