lanflare-cli 0.1.0

Cross-platform CLI and TUI for fast local peer discovery and file transfer compatible with LocalSend
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
//! CLI command implementations.

use anyhow::Context;
use lanflare_core::config::AppConfig;
use lanflare_core::crypto::cert::TlsIdentity;
use lanflare_core::discovery::interface::{local_interfaces, InterfaceFilter};
use lanflare_core::discovery::mdns::{start_mdns_engine, MdnsConfig, MdnsDiscoveryEvent, MdnsNodeInfo};
use lanflare_core::discovery::multicast::{
    start_multicast_engine, MulticastConfig, MulticastDiscoveryEvent, MulticastNodeInfo,
};
use lanflare_core::discovery::registry::{DiscoveredPeer, PeerRegistry};
use lanflare_proto::discovery::{DeviceType, ProtocolType};
use std::path::PathBuf;
use std::time::Duration;
use tokio::sync::{mpsc, oneshot};

use crate::tui::render_peer_table;

/// Handles the `lanflare status` command.
///
/// Prints local node configuration, identity (certificate fingerprint), and available network interfaces.
pub async fn status(config: &AppConfig, identity: &TlsIdentity) -> anyhow::Result<()> {
    println!("LanFlare Node Status:");
    println!("  Alias:       {}", config.alias);
    println!("  Port:        {}", config.port);
    println!("  Download Dir: {}", config.download_dir.display());
    println!("  Auto Accept: {}", config.auto_accept);
    println!(
        "  PIN Protect: {}",
        if config.pin.is_some() { "Enabled" } else { "Disabled" }
    );
    println!("  Fingerprint: {}", identity.fingerprint);
    println!();

    println!("Local Network Interfaces:");
    let filter = InterfaceFilter::default();
    match local_interfaces(&filter) {
        Ok(interfaces) => {
            if interfaces.v4.is_empty() && interfaces.v6.is_empty() {
                println!("  (No active non-loopback network interfaces found)");
            } else {
                for iface in &interfaces.v4 {
                    println!("  - IPv4: {} ({})", iface.address, iface.name);
                }
                for iface in &interfaces.v6 {
                    println!("  - IPv6: index {} ({})", iface.index, iface.name);
                }
            }
        }
        Err(err) => {
            println!("  Failed to enumerate network interfaces: {err}");
        }
    }

    Ok(())
}

/// Options for the `lanflare scan` command.
#[derive(Clone, Debug)]
pub struct ScanOptions {
    /// Scan timeout in seconds.
    pub duration_secs: u64,
    /// Whether to enable UDP Multicast discovery.
    pub multicast: bool,
    /// Whether to enable mDNS discovery.
    pub mdns: bool,
    /// Force active /24 subnet IP scan.
    pub subnet: bool,
    /// Fall back to /24 subnet scan if multicast/mDNS yield 0 peers.
    pub fallback_subnet: bool,
}

impl Default for ScanOptions {
    fn default() -> Self {
        Self {
            duration_secs: 3,
            multicast: true,
            mdns: true,
            subnet: false,
            fallback_subnet: true,
        }
    }
}

/// Handles the `lanflare scan` command.
///
/// Spins up dual discovery engines (Multicast UDP and mDNS), listens for announcements,
/// aggregates them in `PeerRegistry`, and displays a formatted table of active peers.
pub async fn scan(
    config: &AppConfig,
    identity: &TlsIdentity,
    options: &ScanOptions,
) -> anyhow::Result<Vec<DiscoveredPeer>> {
    println!(
        "Scanning local network for LanFlare / LocalSend peers (timeout: {}s)...",
        options.duration_secs
    );

    let registry = PeerRegistry::new();

    // 1. Setup UDP multicast discovery if enabled
    let (mc_stop_tx, mc_stop_rx) = oneshot::channel();
    let (mc_event_tx, mut mc_event_rx) = mpsc::channel::<MulticastDiscoveryEvent>(100);

    let mc_handle = if options.multicast {
        let mc_config = MulticastConfig {
            port: config.port,
            node_info: MulticastNodeInfo {
                alias: config.alias.clone(),
                version: "2.0".to_string(),
                device_model: Some("LanFlare Node".to_string()),
                device_type: Some(DeviceType::Desktop),
                fingerprint: identity.fingerprint.clone(),
                port: config.port,
                protocol: ProtocolType::Https,
                download: false,
            },
            ..MulticastConfig::default()
        };

        match start_multicast_engine(mc_config, mc_event_tx, mc_stop_rx).await {
            Ok(handle) => {
                // Send an announcement burst so peers respond/discover us
                let h_clone = handle.clone();
                tokio::spawn(async move {
                    h_clone.announce_burst().await;
                });
                Some(handle)
            }
            Err(e) => {
                tracing::warn!("Failed to start multicast engine: {e}");
                None
            }
        }
    } else {
        None
    };

    // 2. Setup mDNS discovery if enabled
    let (mdns_stop_tx, mdns_stop_rx) = oneshot::channel();
    let (mdns_event_tx, mut mdns_event_rx) = mpsc::channel::<MdnsDiscoveryEvent>(100);

    let mdns_handle = if options.mdns {
        let mdns_config = MdnsConfig {
            node_info: MdnsNodeInfo {
                alias: config.alias.clone(),
                instance_name: String::new(),
                version: "2.0".to_string(),
                device_model: Some("LanFlare Node".to_string()),
                device_type: Some(DeviceType::Desktop),
                fingerprint: identity.fingerprint.clone(),
                port: config.port,
                protocol: ProtocolType::Https,
                download: false,
            },
            enable_responder: true,
            enable_browser: true,
            ..MdnsConfig::default()
        };

        match start_mdns_engine(mdns_config, mdns_event_tx, mdns_stop_rx).await {
            Ok(handle) => Some(handle),
            Err(e) => {
                tracing::warn!("Failed to start mDNS engine: {e}");
                None
            }
        }
    } else {
        None
    };

    // 3. Collector loop for discovery events
    let registry_clone = registry.clone();
    let collector_task = tokio::spawn(async move {
        loop {
            tokio::select! {
                Some(event) = mc_event_rx.recv() => {
                    if let MulticastDiscoveryEvent::PeerDiscovered { remote_ip, scope_id, message } = event {
                        registry_clone.handle_multicast(remote_ip, scope_id, &message).await;
                    }
                }
                Some(event) = mdns_event_rx.recv() => {
                    match event {
                        MdnsDiscoveryEvent::PeerDiscovered(peer) => {
                            registry_clone.handle_mdns(&peer).await;
                        }
                        MdnsDiscoveryEvent::PeerRemoved { fullname, .. } => {
                            // Extract instance name or remove
                            tracing::debug!("mDNS service removed: {fullname}");
                        }
                    }
                }
                else => break,
            }
        }
    });

    // 4. Escalation worker: if subnet scan requested or fallback enabled,
    // escalate after a brief multicast grace period (500ms, matching official LocalSend)
    let registry_subnet = registry.clone();
    let identity_subnet = identity.clone();
    let port_subnet = config.port;
    let should_run_subnet = options.subnet || options.fallback_subnet;
    let immediate_subnet = options.subnet;

    let subnet_task = if should_run_subnet {
        Some(tokio::spawn(async move {
            if !immediate_subnet {
                // Multicast grace delay before escalating to subnet scan
                tokio::time::sleep(Duration::from_millis(500)).await;
                // If peers were already found via multicast/mDNS, skip subnet sweep
                if !registry_subnet.get_all().await.is_empty() {
                    return;
                }
            }

            let filter = InterfaceFilter::default();
            if let Ok(interfaces) = local_interfaces(&filter) {
                let valid_ifaces: Vec<_> = interfaces
                    .v4
                    .into_iter()
                    .filter(|iface| is_sweepable_interface(&iface.name, &iface.address))
                    .collect();

                if !valid_ifaces.is_empty() {
                    let descs: Vec<String> = valid_ifaces
                        .iter()
                        .map(|i| format!("{}/24 via {}", i.address, i.name))
                        .collect();
                    println!("Probing subnets in parallel: {}...", descs.join(", "));

                    let scan_tasks = valid_ifaces.into_iter().map(|iface| {
                        let reg = registry_subnet.clone();
                        let ident = identity_subnet.clone();
                        async move {
                            lanflare_core::discovery::scanner::scan_subnet(
                                iface.address,
                                port_subnet,
                                &ident,
                                &reg,
                            )
                            .await
                        }
                    });

                    futures_util::future::join_all(scan_tasks).await;
                }
            }
        }))
    } else {
        None
    };

    // Wait for discovery to complete:
    // - Exits early as soon as peers are discovered (plus brief 250ms grace window for remaining packets)
    // - If immediate subnet scan was requested (-s), exits as soon as the subnet scan completes
    // - Otherwise waits for the configured scan duration
    let start = std::time::Instant::now();
    let scan_timeout = Duration::from_secs(options.duration_secs);

    loop {
        tokio::time::sleep(Duration::from_millis(100)).await;

        if !registry.get_all().await.is_empty() {
            tokio::time::sleep(Duration::from_millis(250)).await;
            break;
        }

        if let Some(ref st) = subnet_task {
            if st.is_finished() && (immediate_subnet || start.elapsed() >= scan_timeout) {
                break;
            }
        } else if start.elapsed() >= scan_timeout {
            break;
        }

        if start.elapsed() >= scan_timeout.max(Duration::from_secs(5)) {
            break;
        }
    }

    if let Some(st) = subnet_task {
        let _ = st.await;
    }

    // Shutdown discovery engines
    let _ = mc_stop_tx.send(());
    let _ = mdns_stop_tx.send(());

    if let Some(h) = mc_handle {
        h.stop().await;
    }
    if let Some(h) = mdns_handle {
        h.stop().await;
    }

    collector_task.abort();

    // 5. Retrieve active peers
    let peers = registry.get_all().await;

    print!("{}", render_peer_table(&peers));

    Ok(peers)
}

/// Determines if an interface is an active LAN subnet suitable for a /24 sweep.
///
/// Filters out loopback, virtual containers (docker, veth, vboxnet), and point-to-point
/// VPN / CGNAT ranges (Tailscale `100.64.0.0/10`, wireguard, tun/tap) which do not carry /24 LANs.
fn is_sweepable_interface(name: &str, ip: &std::net::Ipv4Addr) -> bool {
    if ip.is_loopback() {
        return false;
    }

    // Skip CGNAT / Carrier Grade NAT (100.64.0.0/10) used by Tailscale, VPNs, etc.
    let octets = ip.octets();
    if octets[0] == 100 && (octets[1] & 0xC0) == 64 {
        return false;
    }

    let lower = name.to_ascii_lowercase();
    if lower.starts_with("tailscale")
        || lower.starts_with("docker")
        || lower.starts_with("vboxnet")
        || lower.starts_with("veth")
        || lower.starts_with("br-")
    {
        return false;
    }

    true
}

use std::io::Write;
use std::net::IpAddr;
use lanflare_core::client::transfer::{
    OutgoingFile, TransferOptions, TransferOrchestrator, TransferProgress,
};
use lanflare_core::client::{LsClient, LsClientConfig};
use lanflare_core::server::{start_server, PrepareUploadDecision, ServerTransferEvent};
use lanflare_proto::discovery::RegisterDtoV2;

use crate::tui::{shorten_fingerprint, TransferProgressTracker};

/// Prompt user in terminal synchronously or read from reader.
pub fn prompt_user_input(prompt: &str) -> anyhow::Result<String> {
    print!("{prompt}");
    std::io::stdout().flush().context("Failed to flush stdout")?;
    let mut input = String::new();
    std::io::stdin()
        .read_line(&mut input)
        .context("Failed to read user input from stdin")?;
    Ok(input.trim().to_string())
}

/// Resolves target peer from query string (IP, alias, or fingerprint) against registry.
pub fn match_target_peer<'a>(
    query: &str,
    peers: &'a [DiscoveredPeer],
) -> Option<&'a DiscoveredPeer> {
    let q = query.trim();
    if q.is_empty() {
        return None;
    }

    // 1. Exact canonical fingerprint match (or prefix)
    let q_upper = q.to_ascii_uppercase();
    if let Some(p) = peers.iter().find(|p| p.fingerprint == q_upper || p.fingerprint.starts_with(&q_upper)) {
        return Some(p);
    }

    // 2. Exact IP match
    if let Ok(ip) = q.parse::<IpAddr>()
        && let Some(p) = peers.iter().find(|p| p.endpoints.iter().any(|ep| ep.ip == ip))
    {
        return Some(p);
    }

    // 3. Exact alias match (case-insensitive)
    if let Some(p) = peers.iter().find(|p| p.alias.eq_ignore_ascii_case(q)) {
        return Some(p);
    }

    // 4. Substring alias match
    peers.iter().find(|p| p.alias.to_ascii_lowercase().contains(&q.to_ascii_lowercase()))
}

/// Discovers peers and presents an interactive selection if multiple are found.
pub async fn discover_or_select_target(
    config: &AppConfig,
    identity: &TlsIdentity,
    query: Option<&str>,
) -> anyhow::Result<DiscoveredPeer> {
    if let Some(q) = query {
        // Fast-path: Check if user provided direct IP address (e.g. 192.168.1.50)
        let trimmed = q.trim();
        if let Ok(ip) = trimmed.parse::<IpAddr>() {
            println!("Probing direct IP address {ip}...");
            let probe_config = LsClientConfig {
                timeout: Duration::from_secs(3),
                connect_timeout: Duration::from_secs(2),
                tls_identity: Some(identity.clone()),
                target_fingerprint: None,
            };
            if let Ok(probe_client) = LsClient::new(probe_config) {
                for proto in [ProtocolType::Https, ProtocolType::Http] {
                    let proto_str = match proto {
                        ProtocolType::Https => "https",
                        ProtocolType::Http => "http",
                    };
                    let base_url = match ip {
                        IpAddr::V4(v4) => format!("{proto_str}://{v4}:{}", config.port),
                        IpAddr::V6(v6) => format!("{proto_str}://[{v6}]:{}", config.port),
                    };
                    if let Ok(info) = probe_client.get_info(&base_url).await {
                        let peer_port = info.port.unwrap_or(config.port);
                        let peer_proto = info.protocol.unwrap_or(proto);
                        let ep = lanflare_core::discovery::registry::PeerEndpoint::new(
                            ip,
                            None,
                            lanflare_core::discovery::DiscoverySource::ManualScan,
                            0,
                        );
                        return Ok(DiscoveredPeer {
                            fingerprint: info.fingerprint,
                            alias: info.alias,
                            version: info.version,
                            device_model: info.device_model,
                            device_type: info.device_type,
                            port: peer_port,
                            protocol: peer_proto,
                            download: info.download,
                            endpoints: vec![ep],
                            discovery_sources: std::collections::HashSet::new(),
                            first_seen_ms: 0,
                            last_seen_ms: 0,
                            metadata: std::collections::HashMap::new(),
                        });
                    }
                }
            }

            // Fallback unprobed direct peer
            let ep = lanflare_core::discovery::registry::PeerEndpoint::new(
                ip,
                None,
                lanflare_core::discovery::DiscoverySource::ManualScan,
                0,
            );
            return Ok(DiscoveredPeer {
                fingerprint: String::new(),
                alias: format!("Direct Peer ({ip})"),
                version: "2.0".to_string(),
                device_model: None,
                device_type: None,
                port: config.port,
                protocol: ProtocolType::Https,
                download: false,
                endpoints: vec![ep],
                discovery_sources: std::collections::HashSet::new(),
                first_seen_ms: 0,
                last_seen_ms: 0,
                metadata: std::collections::HashMap::new(),
            });
        }
    }

    // Run network scan
    println!("Scanning network to find target peer...");
    let scan_opts = ScanOptions {
        duration_secs: 2,
        multicast: true,
        mdns: true,
        subnet: false,
        fallback_subnet: true,
    };
    let peers = scan(config, identity, &scan_opts).await?;

    if peers.is_empty() {
        if let Some(q) = query {
            // If query is an IP or host, fall back to direct attempt
            anyhow::bail!("No peers found matching query '{q}'. Use explicit IP if known.");
        } else {
            anyhow::bail!("No peers discovered on local network.");
        }
    }

    if let Some(q) = query {
        if let Some(matched) = match_target_peer(q, &peers) {
            return Ok(matched.clone());
        }
        anyhow::bail!("No discovered peer matched query '{q}'.");
    }

    // Interactive picker when query is None
    if peers.len() == 1 {
        let p = &peers[0];
        println!("Single peer found: {} ({})", p.alias, p.primary_ip().map(|ip| ip.to_string()).unwrap_or_default());
        return Ok(p.clone());
    }

    println!("\nSelect a peer to send files to:");
    for (idx, p) in peers.iter().enumerate() {
        let ip_str = p.primary_ip().map(|ip| ip.to_string()).unwrap_or_else(|| "-".to_string());
        let fp_str = shorten_fingerprint(&p.fingerprint);
        println!("  [{}] {} | IP: {} | Port: {} | FP: {}", idx + 1, p.alias, ip_str, p.port, fp_str);
    }

    loop {
        let choice_str = prompt_user_input(&format!("Enter peer number (1-{}): ", peers.len()))?;
        if let Ok(choice) = choice_str.parse::<usize>()
            && (1..=peers.len()).contains(&choice)
        {
            return Ok(peers[choice - 1].clone());
        }
        println!("Invalid choice. Please enter a number between 1 and {}.", peers.len());
    }
}

/// Options for the `lanflare send` command.
#[derive(Clone, Debug)]
pub struct SendOptions {
    pub files: Vec<PathBuf>,
    pub to: Option<String>,
    pub pin: Option<String>,
}

/// Handles the `lanflare send` command.
///
/// Discovers or resolves target peer, calculates file sizes & SHA-256 checksums,
/// establishes transfer session with `TransferOrchestrator`, and displays real-time
/// multi-progress bars.
pub async fn send(
    config: &AppConfig,
    identity: &TlsIdentity,
    options: &SendOptions,
) -> anyhow::Result<()> {
    if options.files.is_empty() {
        anyhow::bail!("No files specified to send");
    }

    // 1. Verify and prepare all files
    let mut outgoing_files = Vec::with_capacity(options.files.len());
    let mut total_bytes = 0u64;

    for f in &options.files {
        if !f.exists() {
            anyhow::bail!("File not found: {}", f.display());
        }
        println!("Preparing file: {}...", f.display());
        let outgoing = OutgoingFile::from_path(f)
            .await
            .with_context(|| format!("Failed to read file metadata for: {}", f.display()))?
            .with_computed_sha256()
            .await
            .with_context(|| format!("Failed to compute SHA-256 for: {}", f.display()))?;

        total_bytes += outgoing.size;
        outgoing_files.push(outgoing);
    }

    println!(
        "Prepared {} file(s) ({:.2} MB total).",
        outgoing_files.len(),
        total_bytes as f64 / (1024.0 * 1024.0)
    );

    // 2. Resolve destination peer
    let target = discover_or_select_target(config, identity, options.to.as_deref()).await?;
    let base_url = target
        .primary_base_url()
        .context("Target peer does not have a valid IP endpoint")?;

    println!("Target resolved: {} at {}", target.alias, base_url);

    // 3. Configure HTTP client
    let target_fp = if target.fingerprint.is_empty() {
        None
    } else {
        Some(target.fingerprint.clone())
    };

    let client_config = LsClientConfig {
        timeout: Duration::from_secs(60),
        connect_timeout: Duration::from_secs(5),
        tls_identity: Some(identity.clone()),
        target_fingerprint: target_fp,
    };
    let client = LsClient::new(client_config)
        .context("Failed to initialize LocalSend client with TLS identity")?;

    let sender_info = RegisterDtoV2 {
        alias: config.alias.clone(),
        version: "2.0".to_string(),
        device_model: Some("LanFlare CLI".to_string()),
        device_type: Some(DeviceType::Desktop),
        fingerprint: identity.fingerprint.clone(),
        port: config.port,
        protocol: ProtocolType::Https,
        download: false,
    };

    let orchestrator = TransferOrchestrator::new(client, sender_info);

    // 4. Setup progress channel and progress tracker
    let (progress_tx, mut progress_rx) = mpsc::channel::<TransferProgress>(100);
    let mut tracker = TransferProgressTracker::new(true);

    let transfer_opts = TransferOptions {
        max_concurrency: 4,
        pin: options.pin.clone().or_else(|| config.pin.clone()),
        cancel_token: None,
        progress_interval: Duration::from_millis(50),
        ..Default::default()
    };

    // Spawn orchestrator task
    let orchestrator_task = tokio::spawn(async move {
        orchestrator
            .transfer(&base_url, outgoing_files, transfer_opts, Some(progress_tx))
            .await
    });

    // Event pump for progress bars
    while let Some(ev) = progress_rx.recv().await {
        tracker.handle_event(&ev);
    }

    match orchestrator_task.await {
        Ok(Ok(())) => {
            println!("All files transferred successfully!");
            Ok(())
        }
        Ok(Err(err)) => {
            anyhow::bail!("Transfer failed: {err}");
        }
        Err(join_err) => {
            anyhow::bail!("Transfer task panicked or aborted: {join_err}");
        }
    }
}

/// Options for the `lanflare receive` command.
#[derive(Clone, Debug)]
pub struct ReceiveOptions {
    pub dir: Option<PathBuf>,
    pub port: Option<u16>,
    pub auto_accept: bool,
    pub pin: Option<String>,
}

/// Handles the `lanflare receive` command.
///
/// Starts Hyper HTTP/HTTPS server daemon with optional PIN protection,
/// receives file upload sessions, supports auto-accept or interactive approval,
/// and saves incoming files directly to the download directory.
pub async fn receive(
    config: &AppConfig,
    identity: &TlsIdentity,
    options: &ReceiveOptions,
) -> anyhow::Result<()> {
    let dest_dir = options
        .dir
        .as_ref()
        .unwrap_or(&config.download_dir);

    tokio::fs::create_dir_all(dest_dir)
        .await
        .with_context(|| format!("Failed to create download directory: {}", dest_dir.display()))?;

    let port = options.port.unwrap_or(config.port);
    let auto_accept = options.auto_accept || config.auto_accept;
    let pin = options.pin.clone().or_else(|| config.pin.clone());

    let server_config = AppConfig {
        alias: config.alias.clone(),
        port,
        download_dir: dest_dir.clone(),
        auto_accept,
        pin: pin.clone(),
    };

    let registry = PeerRegistry::new();
    let (event_tx, mut event_rx) = mpsc::channel::<ServerTransferEvent>(100);

    let server_handle = start_server(
        server_config,
        Some(identity.clone()),
        registry.clone(),
        Some(event_tx),
    )
    .await
    .with_context(|| format!("Failed to start receive server on port {port}"))?;

    let bound_port = server_handle.bound_port();
    println!("Receiver active and listening on port {bound_port}");
    println!("Saving files to: {}", dest_dir.display());
    println!("Auto-accept transfers: {}", if auto_accept { "Enabled" } else { "Disabled (interactive prompt)" });
    if let Some(ref p) = pin {
        println!("PIN protection enabled: required PIN is '{p}'");
    }
    println!("Certificate Fingerprint: {}", identity.fingerprint);
    println!("Press Ctrl+C to terminate receiver daemon.\n");

    // Event loop handling transfer events
    while let Some(event) = event_rx.recv().await {
        match event {
            ServerTransferEvent::DeviceRegistered { ip, info } => {
                println!("Device registered: {} ({}) at {}", info.alias, info.version, ip);
            }
            ServerTransferEvent::PrepareUpload {
                session_id,
                sender_ip,
                sender_info,
                files,
                decision_tx,
                ..
            } => {
                println!("\n[Incoming Transfer Request]");
                println!("  From:       {} ({})", sender_info.alias, sender_ip);
                println!("  Session ID: {session_id}");
                println!("  Files ({}) :", files.len());

                let mut total_size = 0u64;
                for (id, file) in &files {
                    total_size += file.size;
                    println!(
                        "    - {} ({:.2} MB) [ID: {}]",
                        file.file_name,
                        file.size as f64 / (1024.0 * 1024.0),
                        id
                    );
                }
                println!("  Total Size: {:.2} MB", total_size as f64 / (1024.0 * 1024.0));

                if auto_accept {
                    println!("Auto-accepting incoming transfer...");
                    let all_ids = files.keys().cloned().collect();
                    let _ = decision_tx.send(PrepareUploadDecision::Accept(all_ids));
                } else {
                    let ans = prompt_user_input("Accept this transfer? [Y/n]: ")
                        .unwrap_or_else(|_| "n".to_string());
                    if ans.is_empty() || ans.eq_ignore_ascii_case("y") || ans.eq_ignore_ascii_case("yes") {
                        let all_ids = files.keys().cloned().collect();
                        let _ = decision_tx.send(PrepareUploadDecision::Accept(all_ids));
                        println!("Accepted transfer.");
                    } else {
                        let _ = decision_tx.send(PrepareUploadDecision::Decline);
                        println!("Declined transfer.");
                    }
                }
            }
            ServerTransferEvent::FileStarted { file_name, total_bytes, .. } => {
                println!(
                    "Receiving file: {} ({:.2} MB)...",
                    file_name,
                    total_bytes as f64 / (1024.0 * 1024.0)
                );
            }
            ServerTransferEvent::FileCompleted { result, .. } => {
                println!(
                    "File saved successfully: {} (SHA-256: {}) ✓",
                    result.target_path.display(),
                    result.sha256
                );
            }
            ServerTransferEvent::FileFailed { file_id, error, .. } => {
                eprintln!("File transfer failed [ID: {file_id}]: {error}");
            }
            ServerTransferEvent::SessionEnded { session_id, reason } => {
                println!("Transfer session ended [ID: {session_id}]: {reason:?}\n");
            }
        }
    }

    server_handle.wait().await;
    Ok(())
}