use crate::account::{AccountManager, KeyPair, KeyRollover};
use crate::error::Result;
use crate::protocol::{DirectoryManager, NonceManager};
use crate::types::Contact;
use tracing::info;
pub async fn handle_register(email: String, prod: bool, key_path: String) -> Result<()> {
info!("Registering new account for {}", email);
let key_pair = KeyPair::generate()?;
let acme_url = if prod {
"https://acme-v02.api.letsencrypt.org/directory"
} else {
"https://acme-staging-v02.api.letsencrypt.org/directory"
};
let http_client = reqwest::Client::new();
let dir_mgr = DirectoryManager::new(acme_url, http_client.clone());
let directory = dir_mgr.get().await?;
let nonce_mgr = NonceManager::new(&directory.new_nonce, http_client.clone());
let account_mgr = AccountManager::new(&key_pair, &nonce_mgr, &dir_mgr, &http_client)?;
let contact = Contact::email(email);
let account = account_mgr.register(vec![contact], true).await?;
info!("Account registered: {}", account.id);
println!("✅ Account registered successfully");
println!(" ID: {}", account.id);
println!(" Status: {}", account.status);
key_pair.save_to_file(&key_path)?;
println!(" Key saved to: {}", key_path);
Ok(())
}
pub async fn handle_update(key_path: String, email: String, prod: bool) -> Result<()> {
info!("Updating account contact to {}", email);
let key_pair = KeyPair::load_from_file(&key_path)?;
let acme_url = if prod {
"https://acme-v02.api.letsencrypt.org/directory"
} else {
"https://acme-staging-v02.api.letsencrypt.org/directory"
};
let http_client = reqwest::Client::new();
let dir_mgr = DirectoryManager::new(acme_url, http_client.clone());
let directory = dir_mgr.get().await?;
let nonce_mgr = NonceManager::new(&directory.new_nonce, http_client.clone());
let account_mgr = AccountManager::new(&key_pair, &nonce_mgr, &dir_mgr, &http_client)?;
let contact = Contact::email(email.clone());
let account = account_mgr.register(vec![contact.clone()], true).await?;
let updated = account_mgr
.update_contacts(&account.id, vec![contact])
.await?;
info!("Account updated: {}", updated.id);
println!("✅ Account updated successfully");
println!(" ID: {}", updated.id);
println!(" Contacts: {:?}", updated.contact);
Ok(())
}
pub async fn handle_deactivate(key_path: String, prod: bool) -> Result<()> {
info!("Deactivating account");
let key_pair = KeyPair::load_from_file(&key_path)?;
let acme_url = if prod {
"https://acme-v02.api.letsencrypt.org/directory"
} else {
"https://acme-staging-v02.api.letsencrypt.org/directory"
};
let http_client = reqwest::Client::new();
let dir_mgr = DirectoryManager::new(acme_url, http_client.clone());
let directory = dir_mgr.get().await?;
let nonce_mgr = NonceManager::new(&directory.new_nonce, http_client.clone());
let account_mgr = AccountManager::new(&key_pair, &nonce_mgr, &dir_mgr, &http_client)?;
let account = account_mgr.register(vec![], true).await?;
account_mgr.deactivate(&account.id).await?;
info!("Account deactivated: {}", account.id);
println!("✅ Account deactivated successfully");
Ok(())
}
pub async fn handle_rotate_key(key_path: String, new_key_path: String, prod: bool) -> Result<()> {
info!("Rotating account key");
let key_pair = KeyPair::load_from_file(&key_path)?;
let acme_url = if prod {
"https://acme-v02.api.letsencrypt.org/directory"
} else {
"https://acme-staging-v02.api.letsencrypt.org/directory"
};
let http_client = reqwest::Client::new();
let dir_mgr = DirectoryManager::new(acme_url, http_client.clone());
let directory = dir_mgr.get().await?;
let nonce_mgr = NonceManager::new(&directory.new_nonce, http_client.clone());
let account_mgr = AccountManager::new(&key_pair, &nonce_mgr, &dir_mgr, &http_client)?;
let account = account_mgr.register(vec![], true).await?;
let rollover = KeyRollover::new(&account_mgr)?;
let updated_account = rollover.execute(&account.id).await?;
rollover.new_key_pair().save_to_file(&new_key_path)?;
info!("Account key rotated: {}", updated_account.id);
println!("✅ Account key rotated successfully");
println!(" New key saved to: {}", new_key_path);
Ok(())
}