use anyhow::{Result, bail};
use smartpasslib::{SmartPassword, SmartPasswordManager};
use crate::output;
use crate::secret;
use super::{confirm, short_key};
pub fn run(
description: Option<String>,
length: usize,
secret_arg: Option<String>,
secret_stdin: bool,
) -> Result<()> {
let mut manager = SmartPasswordManager::new()?;
let description = match description {
Some(d) => d.trim().to_string(),
None => {
bail!("--description is required")
}
};
if description.is_empty() {
bail!("Description cannot be empty");
}
if length < 12 || length > 100 {
bail!("Length must be between 12 and 100");
}
let secret = secret::resolve_secret(secret_arg, secret_stdin, "Secret phrase: ")?;
let public_key = smartpasslib::generate_public_key(&secret)?;
if manager.get(&public_key).is_some() {
bail!("An entry with this secret already exists");
}
let entry = SmartPassword::new(public_key.clone(), description.clone(), length)?;
manager.add(entry)?;
output::success(&format!(
"Added entry '{}' ({})",
description,
short_key(&public_key)
));
output::note(&format!(" public key: {public_key}"));
output::note(&format!(" length: {length}"));
let _ = confirm;
Ok(())
}