firecloud-cli 0.2.0

Command-line interface for FireCloud P2P messaging and file sharing
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
//! Download command - Download files from the network
//!
//! Supports two modes:
//! 1. Chunk mode: Download a single chunk by hash (--hash)
//! 2. File mode: Download a complete file by file_id (--file)
//!
//! Features:
//! - Parallel chunk downloads with configurable concurrency
//! - Progress bar with speed and ETA
//! - Local cache check before network fetch

use anyhow::{anyhow, Result};
use crate::format::{format_bytes, format_speed, format_duration};
use crate::password::{prompt_password, derive_kek};
use firecloud_core::ChunkHash;
use firecloud_crypto::decrypt;
use firecloud_net::{
    FireCloudNode, NodeConfig, NodeEvent, OutboundRequestId, PeerId, TransferRequest,
    TransferResponse,
};
use firecloud_storage::{decompress, ChunkStore, ManifestStore};
use indicatif::{ProgressBar, ProgressStyle};
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::time::{Duration, Instant};
use tracing::{debug, info, warn};

/// Maximum concurrent chunk downloads (pipelined requests)
const MAX_PARALLEL_DOWNLOADS: usize = 8;

/// Statistics for a download operation
struct DownloadStats {
    start_time: Instant,
    bytes_downloaded: u64,
    chunks_downloaded: usize,
    chunks_local: usize,
    #[allow(dead_code)]
    total_chunks: usize,
    #[allow(dead_code)]
    total_size: u64,
}

impl DownloadStats {
    fn new(total_chunks: usize, total_size: u64) -> Self {
        Self {
            start_time: Instant::now(),
            bytes_downloaded: 0,
            chunks_downloaded: 0,
            chunks_local: 0,
            total_chunks,
            total_size,
        }
    }

    fn add_downloaded(&mut self, bytes: u64) {
        self.bytes_downloaded += bytes;
        self.chunks_downloaded += 1;
    }

    fn add_local(&mut self) {
        self.chunks_local += 1;
    }

    fn elapsed_secs(&self) -> f64 {
        self.start_time.elapsed().as_secs_f64()
    }

    fn display_summary(&self) {
        println!();
        println!("📊 Transfer Statistics:");
        println!("   Chunks from network: {}", self.chunks_downloaded);
        println!("   Chunks from cache:   {}", self.chunks_local);
        println!("   Data transferred:    {}", format_bytes(self.bytes_downloaded));
        println!("   Time elapsed:        {}", format_duration(self.elapsed_secs()));
        if self.bytes_downloaded > 0 {
            println!("   Transfer speed:      {}", format_speed(self.bytes_downloaded as f64 / self.elapsed_secs()));
        }
    }
}

pub async fn run(
    data_dir: PathBuf,
    hash: Option<String>,
    file_id: Option<String>,
    output: Option<PathBuf>,
) -> Result<()> {
    match (hash, file_id) {
        (Some(h), None) => download_chunk(data_dir, h, output).await,
        (None, Some(f)) => download_file(data_dir, f, output).await,
        (None, None) => Err(anyhow!("Either --hash or --file must be specified")),
        (Some(_), Some(_)) => Err(anyhow!("Cannot specify both --hash and --file")),
    }
}

/// Download a single chunk by hash
async fn download_chunk(data_dir: PathBuf, hash: String, output: Option<PathBuf>) -> Result<()> {
    info!("Downloading chunk: {}", hash);

    let store_path = data_dir.join("chunks");
    let store = ChunkStore::open(&store_path)?;

    let chunk_hash =
        ChunkHash::from_hex(&hash).map_err(|e| anyhow!("Invalid chunk hash: {}", e))?;

    if let Ok(Some(chunk)) = store.get(&chunk_hash) {
        info!("✅ Chunk found locally ({} bytes)", chunk.data.len());

        if let Some(output_path) = output {
            let mut file = File::create(&output_path)?;
            file.write_all(&chunk.data)?;
            info!("Written to: {}", output_path.display());
        } else {
            info!("Chunk data: {} bytes (use --output to save)", chunk.data.len());
        }
        return Ok(());
    }

    info!("Chunk not found locally, searching peers...");

    let (chunk_data, original_size) = fetch_chunk_from_network(&hash).await?;

    if let Some(output_path) = output {
        let mut file = File::create(&output_path)?;
        file.write_all(&chunk_data)?;
        info!("Written to: {}", output_path.display());
    } else {
        info!("Chunk data: {} bytes (use --output to save)", chunk_data.len());
    }

    let metadata = firecloud_core::ChunkMetadata {
        hash: chunk_hash,
        size: chunk_data.len() as u64,
        original_size,
        compression: firecloud_core::CompressionType::None,
        encrypted: true,
    };
    let chunk = firecloud_core::Chunk {
        metadata,
        data: chunk_data.into(),
    };
    store.put(&chunk)?;
    info!("Cached chunk locally");

    Ok(())
}

/// Download a complete file by file_id
async fn download_file(
    data_dir: PathBuf,
    file_id_str: String,
    output: Option<PathBuf>,
) -> Result<()> {
    info!("Downloading file: {}", file_id_str);

    let chunk_store_path = data_dir.join("chunks");
    let chunk_store = ChunkStore::open(&chunk_store_path)?;

    let manifest_store_path = data_dir.join("manifests");
    let manifest_store = ManifestStore::open(&manifest_store_path)?;

    let manifest = match manifest_store.get_by_id_str(&file_id_str)? {
        Some(m) => {
            info!("✅ Manifest found locally");
            m
        }
        None => {
            info!("Manifest not found locally, searching DHT for providers...");
            fetch_manifest_from_dht(&file_id_str, &manifest_store).await?
        }
    };

    println!();
    println!("📁 File: {} ({} bytes)", manifest.metadata.name, manifest.metadata.size);
    println!("📦 Chunks: {}", manifest.chunks.len());

    // Decrypt DEK if encrypted with KEK
    let dek: [u8; 32] = if let Some(encrypted_dek_bytes) = &manifest.encrypted_dek {
        if let Some(salt_bytes) = &manifest.salt {
            // File is encrypted with password, need to decrypt DEK
            println!();
            let password = prompt_password("🔐 Enter password to decrypt file: ")?;
            
            println!("⏳ Deriving decryption key...");
            let kek = derive_kek(&password, salt_bytes)?;
            
            let dek_vec = kek.decrypt_dek(encrypted_dek_bytes)
                .map_err(|e| anyhow!("Failed to decrypt DEK (wrong password?): {}", e))?;
            
            println!("✅ Password verified\n");
            
            dek_vec.try_into()
                .map_err(|_| anyhow!("Invalid DEK size after decryption"))?
        } else {
            // Old format: DEK not encrypted (backward compatibility)
            println!("⚠️  Warning: File uses legacy unencrypted DEK format");
            encrypted_dek_bytes.clone().try_into()
                .map_err(|_| anyhow!("Invalid DEK size in manifest"))?
        }
    } else {
        return Err(anyhow!("No DEK in manifest - file cannot be decrypted"));
    };

    let output_path = output.unwrap_or_else(|| PathBuf::from(&manifest.metadata.name));

    let mut local_chunks: HashMap<ChunkHash, Vec<u8>> = HashMap::new();
    let mut missing_hashes: Vec<String> = Vec::new();

    let mut stats = DownloadStats::new(manifest.chunks.len(), manifest.metadata.size);

    println!("🔍 Checking for local chunks...");
    for chunk_hash in &manifest.chunks {
        match chunk_store.get(chunk_hash)? {
            Some(chunk) => {
                debug!("  ✅ {} (local)", &chunk_hash.to_hex()[..16]);
                local_chunks.insert(chunk_hash.clone(), chunk.data.to_vec());
                stats.add_local();
            }
            None => {
                debug!("  ❌ {} (need to fetch)", &chunk_hash.to_hex()[..16]);
                missing_hashes.push(chunk_hash.to_hex());
            }
        }
    }

    println!("   Found {} chunks locally, need to fetch {}", local_chunks.len(), missing_hashes.len());

    if !missing_hashes.is_empty() {
        let fetched = fetch_chunks_parallel(&chunk_store, &missing_hashes, &mut stats).await?;
        for (hash_str, data) in fetched {
            let chunk_hash = ChunkHash::from_hex(&hash_str)?;
            local_chunks.insert(chunk_hash, data);
        }
    }

    println!();
    println!("📝 Reassembling file...");
    let reassemble_bar = ProgressBar::new(manifest.chunks.len() as u64);
    reassemble_bar.set_style(
        ProgressStyle::default_bar()
            .template("{spinner:.green} [{bar:40.cyan/blue}] {pos}/{len} chunks")
            .unwrap()
            .progress_chars("█▓░"),
    );

    let mut output_file = File::create(&output_path)?;
    let mut total_written = 0u64;

    for chunk_hash in &manifest.chunks {
        let encrypted_data = local_chunks
            .get(chunk_hash)
            .ok_or_else(|| anyhow!("Missing chunk: {}", chunk_hash.to_hex()))?;

        let compressed = decrypt(&dek, encrypted_data)
            .map_err(|e| anyhow!("Failed to decrypt chunk {}: {}", chunk_hash, e))?;

        let original = decompress(&compressed, false)
            .map_err(|e| anyhow!("Failed to decompress chunk {}: {}", chunk_hash, e))?;

        output_file.write_all(&original)?;
        total_written += original.len() as u64;
        reassemble_bar.inc(1);
    }

    reassemble_bar.finish_with_message("Done!");
    output_file.flush()?;

    stats.display_summary();

    println!();
    println!("✅ Download complete!");
    println!("📁 File: {}", output_path.display());
    println!("📏 Size: {} bytes", total_written);

    let file_content = std::fs::read(&output_path)?;
    let content_hash = ChunkHash::hash(&file_content);
    if content_hash == manifest.metadata.content_hash {
        println!("🔐 Content hash verified ✓");
    } else {
        println!("⚠️  Content hash mismatch! File may be corrupted.");
    }

    Ok(())
}

/// Fetch a manifest from DHT providers
async fn fetch_manifest_from_dht(
    file_id_str: &str,
    manifest_store: &ManifestStore,
) -> Result<firecloud_core::FileManifest> {
    use firecloud_core::FileManifest;

    let config = NodeConfig {
        port: 0,
        enable_mdns: true,
        bootstrap_peers: Vec::new(),
        bootstrap_relays: vec![],
    };

    let mut node = FireCloudNode::new(config).await?;
    info!("Local peer ID: {}", node.local_peer_id());

    let _peers = discover_peers(&mut node, Duration::from_secs(3)).await?;

    info!("🔍 Querying DHT for file providers...");
    let _query_id = node.find_file_providers(file_id_str);

    let timeout = tokio::time::Instant::now() + Duration::from_secs(10);
    let mut found_providers: Vec<PeerId> = Vec::new();

    while tokio::time::Instant::now() < timeout {
        tokio::select! {
            _ = tokio::time::sleep(Duration::from_millis(100)) => {}
            event = node.poll_event() => {
                if let Some(event) = event {
                    match event {
                        NodeEvent::ProvidersFound { key, providers, .. } if key.contains(file_id_str) => {
                            info!("📡 Found {} providers for file", providers.len());
                            found_providers = providers;
                            break;
                        }
                        NodeEvent::PeerDiscovered(peer) => {
                            debug!("Discovered peer: {}", peer);
                        }
                        _ => {}
                    }
                }
            }
        }
    }

    if found_providers.is_empty() {
        let discovered = node.known_peers().clone();
        if !discovered.is_empty() {
            info!("No DHT providers found, trying {} mDNS peers...", discovered.len());
            found_providers = discovered.into_iter().collect();
        } else {
            return Err(anyhow!(
                "No providers found for file {}.\nMake sure the file exists on another node that is running.",
                file_id_str
            ));
        }
    }

    // Sort providers: prefer local/low-latency peers first
    if let Some(best) = node.choose_best_peer(&found_providers) {
        // Move best peer to front
        found_providers.retain(|p| *p != best);
        found_providers.insert(0, best);
    }

    for provider in &found_providers {
        let is_local = node.is_local_peer(provider);
        info!("📥 Requesting manifest from {}{}...", provider,
            if is_local { " (local)" } else { "" });

        let request_id = node.send_transfer_request(
            provider,
            TransferRequest::GetManifest { file_id: file_id_str.to_string() },
        );

        let response_timeout = tokio::time::Instant::now() + Duration::from_secs(10);
        while tokio::time::Instant::now() < response_timeout {
            tokio::select! {
                _ = tokio::time::sleep(Duration::from_millis(100)) => {}
                event = node.poll_event() => {
                    if let Some(event) = event {
                        match event {
                            NodeEvent::TransferResponse { request_id: rid, response, peer } => {
                                if rid == request_id {
                                    match response {
                                        TransferResponse::Manifest { manifest_data, .. } => {
                                            info!("✅ Received manifest from {}", peer);
                                            let file_manifest: FileManifest = ciborium::from_reader(&manifest_data[..])
                                                .map_err(|e| anyhow!("Failed to deserialize manifest: {}", e))?;
                                            manifest_store.put(&file_manifest)?;
                                            info!("📦 Manifest cached locally");
                                            return Ok(file_manifest);
                                        }
                                        TransferResponse::NotFound { .. } => {
                                            info!("  ❌ Manifest not found on {}", peer);
                                            break;
                                        }
                                        TransferResponse::Error { message } => {
                                            warn!("  ❌ Error from {}: {}", peer, message);
                                            break;
                                        }
                                        _ => {}
                                    }
                                }
                            }
                            NodeEvent::TransferFailed { request_id: rid, error, peer } => {
                                if rid == request_id {
                                    warn!("  ❌ Transfer failed from {}: {}", peer, error);
                                    break;
                                }
                            }
                            _ => {}
                        }
                    }
                }
            }
        }
    }

    Err(anyhow!("Failed to fetch manifest from any provider for file {}", file_id_str))
}

/// Fetch a single chunk from the network
async fn fetch_chunk_from_network(hash: &str) -> Result<(Vec<u8>, u64)> {
    let config = NodeConfig {
        port: 0,
        enable_mdns: true,
        bootstrap_peers: Vec::new(),
        bootstrap_relays: vec![],
    };

    let mut node = FireCloudNode::new(config).await?;
    info!("Local peer ID: {}", node.local_peer_id());

    let peers = discover_peers(&mut node, Duration::from_secs(5)).await?;

    if peers.is_empty() {
        return Err(anyhow!("No peers found. Run `firecloud node` on another device first."));
    }

    let peers_with_chunk = find_peers_with_chunk(&mut node, &peers, hash).await?;

    if peers_with_chunk.is_empty() {
        return Err(anyhow!("Chunk not found on any peer. Hash: {}", hash));
    }

    // Use smart peer selection: prefer local/low-latency peers
    let target_peer = node.choose_best_peer(&peers_with_chunk).unwrap_or(peers_with_chunk[0]);
    info!("📥 Requesting chunk from {}{}...", target_peer,
        if node.is_local_peer(&target_peer) { " (local)" } else { "" });

    let request_id = node.send_transfer_request(
        &target_peer,
        TransferRequest::GetChunk { hash: hash.to_string() },
    );

    let timeout = tokio::time::Instant::now() + Duration::from_secs(30);
    while tokio::time::Instant::now() < timeout {
        tokio::select! {
            _ = tokio::time::sleep(Duration::from_millis(100)) => {}
            event = node.poll_event() => {
                if let Some(event) = event {
                    match event {
                        NodeEvent::TransferResponse { request_id: rid, response, peer } => {
                            if rid == request_id {
                                match response {
                                    TransferResponse::Chunk { data, original_size, .. } => {
                                        info!("✅ Received chunk ({} bytes)", data.len());
                                        return Ok((data, original_size));
                                    }
                                    TransferResponse::NotFound { .. } => {
                                        return Err(anyhow!("Chunk not found on peer {}", peer));
                                    }
                                    TransferResponse::Error { message } => {
                                        return Err(anyhow!("Error from peer {}: {}", peer, message));
                                    }
                                    _ => {}
                                }
                            }
                        }
                        NodeEvent::TransferFailed { request_id: rid, error, peer } => {
                            if rid == request_id {
                                return Err(anyhow!("Transfer failed from {}: {}", peer, error));
                            }
                        }
                        _ => {}
                    }
                }
            }
        }
    }

    Err(anyhow!("Download timed out"))
}

/// Fetch multiple chunks from network with pipelined requests and progress bar
async fn fetch_chunks_parallel(
    chunk_store: &ChunkStore,
    hash_strs: &[String],
    stats: &mut DownloadStats,
) -> Result<HashMap<String, Vec<u8>>> {
    if hash_strs.is_empty() {
        return Ok(HashMap::new());
    }

    let config = NodeConfig {
        port: 0,
        enable_mdns: true,
        bootstrap_peers: Vec::new(),
        bootstrap_relays: vec![],
    };

    let mut node = FireCloudNode::new(config).await?;

    let peers = discover_peers(&mut node, Duration::from_secs(5)).await?;

    if peers.is_empty() {
        return Err(anyhow!("No peers found. Run `firecloud node` on another device first."));
    }

    let progress = ProgressBar::new(hash_strs.len() as u64);
    progress.set_style(
        ProgressStyle::default_bar()
            .template("{msg}\n{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} chunks")
            .unwrap()
            .progress_chars("█▓░"),
    );
    progress.set_message(format!(
        "📥 Downloading {} chunks from {} peers...",
        hash_strs.len(),
        peers.len(),
    ));

    let mut chunk_to_peer: HashMap<String, PeerId> = HashMap::new();

    println!("🔍 Locating chunks on peers...");
    for hash_str in hash_strs {
        let peers_with = find_peers_with_chunk(&mut node, &peers, hash_str).await?;
        if peers_with.is_empty() {
            return Err(anyhow!("Chunk {} not found on any peer", &hash_str[..16]));
        }
        // Use smart peer selection: prefer local/low-latency peers
        let best_peer = node.choose_best_peer(&peers_with).unwrap_or(peers_with[0]);
        chunk_to_peer.insert(hash_str.clone(), best_peer);
    }

    let mut fetched: HashMap<String, Vec<u8>> = HashMap::new();
    let mut pending_requests: HashMap<OutboundRequestId, String> = HashMap::new();

    let total_chunks = hash_strs.len();
    let mut sent_count = 0;

    for hash_str in hash_strs.iter().take(MAX_PARALLEL_DOWNLOADS) {
        let peer = chunk_to_peer.get(hash_str).unwrap();
        let request_id = node.send_transfer_request(
            peer,
            TransferRequest::GetChunk { hash: hash_str.clone() },
        );
        pending_requests.insert(request_id, hash_str.clone());
        sent_count += 1;
    }

    let timeout = tokio::time::Instant::now() + Duration::from_secs(120);

    while fetched.len() < total_chunks && tokio::time::Instant::now() < timeout {
        tokio::select! {
            _ = tokio::time::sleep(Duration::from_millis(50)) => {}
            event = node.poll_event() => {
                if let Some(event) = event {
                    match event {
                        NodeEvent::TransferResponse { request_id, response, peer } => {
                            if let Some(_hash_str) = pending_requests.remove(&request_id) {
                                match response {
                                    TransferResponse::Chunk { hash, data, original_size } => {
                                        let chunk_hash = ChunkHash::from_hex(&hash)?;
                                        let metadata = firecloud_core::ChunkMetadata {
                                            hash: chunk_hash,
                                            size: data.len() as u64,
                                            original_size,
                                            compression: firecloud_core::CompressionType::None,
                                            encrypted: true,
                                        };
                                        let chunk = firecloud_core::Chunk {
                                            metadata,
                                            data: data.clone().into(),
                                        };
                                        chunk_store.put(&chunk)?;

                                        let bytes = data.len() as u64;
                                        stats.add_downloaded(bytes);
                                        fetched.insert(hash, data);
                                        progress.inc(1);

                                        if sent_count < total_chunks {
                                            let next_hash = &hash_strs[sent_count];
                                            let next_peer = chunk_to_peer.get(next_hash).unwrap();
                                            let next_req_id = node.send_transfer_request(
                                                next_peer,
                                                TransferRequest::GetChunk { hash: next_hash.clone() },
                                            );
                                            pending_requests.insert(next_req_id, next_hash.clone());
                                            sent_count += 1;
                                        }
                                    }
                                    TransferResponse::NotFound { hash } => {
                                        warn!("  ❌ Chunk {} not found on {}", &hash[..16], peer);
                                    }
                                    TransferResponse::Error { message } => {
                                        warn!("  ❌ Error from {}: {}", peer, message);
                                    }
                                    _ => {}
                                }
                            }
                        }
                        NodeEvent::TransferFailed { request_id, error, peer } => {
                            if let Some(hash_str) = pending_requests.remove(&request_id) {
                                warn!("  ❌ Transfer failed for {} from {}: {}", &hash_str[..16], peer, error);
                            }
                        }
                        _ => {}
                    }
                }
            }
        }
    }

    progress.finish_with_message("Downloads complete!");

    if fetched.len() < total_chunks {
        return Err(anyhow!(
            "Failed to fetch all chunks: got {}/{}",
            fetched.len(),
            total_chunks
        ));
    }

    Ok(fetched)
}

/// Discover peers on the network
async fn discover_peers(node: &mut FireCloudNode, duration: Duration) -> Result<Vec<PeerId>> {
    let mut peers = Vec::new();
    let deadline = tokio::time::Instant::now() + duration;

    info!("🔍 Discovering peers...");

    while tokio::time::Instant::now() < deadline {
        tokio::select! {
            _ = tokio::time::sleep(Duration::from_millis(100)) => {}
            event = node.poll_event() => {
                if let Some(event) = event {
                    match event {
                        NodeEvent::PeerDiscovered(peer_id) => {
                            if peer_id != node.local_peer_id() && !peers.contains(&peer_id) {
                                info!("📡 Found peer: {}", peer_id);
                                peers.push(peer_id);
                            }
                        }
                        NodeEvent::Listening(addr) => {
                            info!("👂 Listening on {}", addr);
                        }
                        _ => {}
                    }
                }
            }
        }
    }

    info!("Found {} peer(s)", peers.len());
    Ok(peers)
}

/// Find which peers have a specific chunk
async fn find_peers_with_chunk(
    node: &mut FireCloudNode,
    peers: &[PeerId],
    hash: &str,
) -> Result<Vec<PeerId>> {
    let mut pending: HashMap<_, _> = HashMap::new();
    let mut peers_with_chunk = Vec::new();

    for peer in peers {
        let request_id = node.send_transfer_request(
            peer,
            TransferRequest::HasChunk { hash: hash.to_string() },
        );
        pending.insert(request_id, *peer);
    }

    let timeout = tokio::time::Instant::now() + Duration::from_secs(10);

    while !pending.is_empty() && tokio::time::Instant::now() < timeout {
        tokio::select! {
            _ = tokio::time::sleep(Duration::from_millis(100)) => {}
            event = node.poll_event() => {
                if let Some(event) = event {
                    match event {
                        NodeEvent::TransferResponse { request_id, response, peer } => {
                            if pending.remove(&request_id).is_some() {
                                match response {
                                    TransferResponse::HasChunk { has_it: true, .. } => {
                                        debug!("  ✅ Peer {} has chunk", peer);
                                        peers_with_chunk.push(peer);
                                    }
                                    TransferResponse::HasChunk { has_it: false, .. } => {
                                        debug!("  ❌ Peer {} doesn't have chunk", peer);
                                    }
                                    _ => {}
                                }
                            }
                        }
                        NodeEvent::TransferFailed { request_id, .. } => {
                            pending.remove(&request_id);
                        }
                        _ => {}
                    }
                }
            }
        }
    }

    Ok(peers_with_chunk)
}