syntarq-cli 0.1.0

Command-line interface for Syntarq identity management
//! Initialize command - Creates a new Syntarq vault

use crate::ui;
use anyhow::{Context, Result};
use colored::Colorize;
use dialoguer::Password;
use syntarq_core::identity::{ServiceType, SyntarqIdentity};

/// Run the init command to initialize a new vault
pub async fn run() -> Result<()> {
    ui::header("🔐 Initializing Syntarq Vault");

    // Get master password
    let password: String = Password::new()
        .with_prompt("Master Password")
        .with_confirmation("Confirm Password", "Passwords don't match")
        .interact()
        .context("Failed to read password")?;

    // Validate password strength
    if password.len() < 10 {
        ui::error("Password must be at least 10 characters");
        anyhow::bail!("Weak password");
    }

    if !password.chars().any(|c| c.is_numeric()) {
        ui::error("Password must contain at least one number");
        anyhow::bail!("Weak password");
    }

    if !password.chars().any(|c| c.is_alphabetic()) {
        ui::error("Password must contain at least one letter");
        anyhow::bail!("Weak password");
    }

    ui::info("Creating vault...");

    // Create identity
    let (identity, password_hash) =
        SyntarqIdentity::create(&password).context("Failed to create identity")?;

    ui::separator();
    ui::success("Vault initialized successfully!");

    // Display identity information
    ui::section("Identity Information");
    ui::kv_pair("Identity ID", &identity.id().to_string());

    ui::section("Password Hash");
    ui::dimmed(&format!("Hash: {}...", &password_hash[..50]));

    // Test service key derivation
    ui::section("Service Keys");
    for service_type in ServiceType::all() {
        let _service_key = identity
            .derive_service_key(*service_type)
            .context("Failed to derive service key")?;
        ui::info(&format!(
            "  {} - Key derived successfully",
            service_type.to_string().yellow()
        ));
    }

    ui::separator();
    ui::info("💡 Use 'syntarq test-crypto' to test cryptographic functions");

    Ok(())
}