//! Marketplace command implementations

use rustelo_core::Result;

use crate::marketplace::{registry::FeatureRegistry, MarketplaceConfig};

pub async fn search(query: String) -> Result<()> {
    let config = MarketplaceConfig::default();
    let registry = FeatureRegistry::new(config);

    println!("🔍 Searching for features: {}", query);
    let results = registry.search(&query).await?;

    if results.is_empty() {
        println!("No features found matching \'{}\'.", query);
        return Ok(());
    }

    println!("\\nFound {} feature(s):", results.len());
    for package in results {
        println!("\\n📦 {}", package.name);
        println!("   Version: {}", package.version);
        println!("   Author: {}", package.author);
        println!("   Description: {}", package.description);
        if !package.keywords.is_empty() {
            println!("   Keywords: {}", package.keywords.join(", "));
        }
    }

    Ok(())
}

pub async fn install(name: String, version: Option<String>) -> Result<()> {
    let config = MarketplaceConfig::default();
    let registry = FeatureRegistry::new(config);

    registry.install(&name, version.as_deref()).await
}

pub async fn uninstall(name: String) -> Result<()> {
    let config = MarketplaceConfig::default();
    let registry = FeatureRegistry::new(config);

    registry.uninstall(&name).await
}

pub async fn list_installed() -> Result<()> {
    let config = MarketplaceConfig::default();
    let registry = FeatureRegistry::new(config);

    let features = registry.list_installed().await?;

    if features.is_empty() {
        println!("No features installed from marketplace.");
        return Ok(());
    }

    println!("Installed marketplace features:");
    for feature in features {
        println!("  📦 {}", feature);
    }

    Ok(())
}

pub async fn publish(feature_path: String) -> Result<()> {
    println!("📤 Publishing feature from: {}", feature_path);

    // TODO: Implement feature publishing
    // - Validate feature structure
    // - Create package archive
    // - Upload to registry
    // - Generate signature

    println!("✅ Feature published successfully");
    Ok(())
}
