firecloud-cli 0.2.0

Command-line interface for FireCloud P2P messaging and file sharing
//! Quota command - Show storage quota and usage statistics

use anyhow::Result;
use firecloud_storage::QuotaManager;
use std::path::Path;
use tracing::info;

pub async fn quota(data_dir: &Path) -> Result<()> {
    info!("Checking storage quota");

    let quota_path = data_dir.join("quota");
    let manager = QuotaManager::open(&quota_path)?;

    let stats = manager.get_stats()?;

    println!("\n📊 Storage Quota Statistics\n");
    println!("{}", "=".repeat(50));
    
    if stats.quota_limit == 0 {
        println!("📦 Total Storage:      {}", format_bytes(stats.total_bytes));
        println!("🗂️  Total Chunks:       {}", stats.total_chunks);
        println!("🔓 Quota Limit:        Unlimited");
    } else {
        println!("📦 Total Storage:      {}", format_bytes(stats.total_bytes));
        println!("💾 Quota Limit:        {}", format_bytes(stats.quota_limit));
        println!("📊 Usage:              {:.1}%", stats.usage_percentage);
        println!("🆓 Remaining:          {}", format_bytes(stats.remaining_bytes));
        println!("🗂️  Total Chunks:       {}", stats.total_chunks);
        
        if stats.is_full {
            println!("\n⚠️  WARNING: Storage quota is FULL!");
        } else if stats.is_nearly_full {
            println!("\n⚠️  WARNING: Storage quota is nearly full (>90%)");
        }
    }
    
    println!("{}", "=".repeat(50));
    println!("\n✅ Summary: {}\n", stats.summary());

    Ok(())
}

/// Set storage quota limit
pub async fn set_quota(data_dir: &Path, limit_gb: f64) -> Result<()> {
    info!("Setting storage quota to {} GB", limit_gb);

    let quota_path = data_dir.join("quota");
    let manager = QuotaManager::open(&quota_path)?;

    let limit_bytes = (limit_gb * 1_000_000_000.0) as u64;
    manager.set_quota_limit(limit_bytes)?;

    println!("\n✅ Storage quota set to {} GB ({} bytes)\n", limit_gb, limit_bytes);

    Ok(())
}

fn format_bytes(bytes: u64) -> String {
    const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"];
    let mut size = bytes as f64;
    let mut unit_idx = 0;

    while size >= 1024.0 && unit_idx < UNITS.len() - 1 {
        size /= 1024.0;
        unit_idx += 1;
    }

    format!("{:.2} {}", size, UNITS[unit_idx])
}