firecloud-cli 0.2.0

Command-line interface for FireCloud P2P messaging and file sharing
//! List command - List all uploaded files

use anyhow::Result;
use firecloud_storage::ManifestStore;
use std::path::PathBuf;
use tracing::info;

pub async fn run(data_dir: PathBuf) -> Result<()> {
    // Open manifest store
    let manifest_store_path = data_dir.join("manifests");
    let manifest_store = ManifestStore::open(&manifest_store_path)?;

    let manifests = manifest_store.list()?;

    if manifests.is_empty() {
        info!("No files uploaded yet.");
        info!("Use `firecloud upload <file>` to upload a file.");
        return Ok(());
    }

    info!("📁 Uploaded Files ({}):", manifests.len());
    info!("");

    for summary in &manifests {
        let size_str = format_size(summary.size);
        let created = chrono::DateTime::from_timestamp_millis(summary.created_at)
            .map(|dt| dt.format("%Y-%m-%d %H:%M").to_string())
            .unwrap_or_else(|| "unknown".to_string());

        info!(
            "  {} | {} | {} | {} chunks",
            summary.file_id, summary.name, size_str, summary.chunk_count
        );
        info!("    Created: {}", created);
        info!("");
    }

    info!("To download a file, use:");
    info!("  firecloud download --file <file_id>");

    Ok(())
}

fn format_size(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;
    const GB: u64 = MB * 1024;

    if bytes >= GB {
        format!("{:.2} GB", bytes as f64 / GB as f64)
    } else if bytes >= MB {
        format!("{:.2} MB", bytes as f64 / MB as f64)
    } else if bytes >= KB {
        format!("{:.2} KB", bytes as f64 / KB as f64)
    } else {
        format!("{} B", bytes)
    }
}