use anyhow::{Result, anyhow};
use clap::Args;
use systemprompt_runtime::AppContext;
use systemprompt_users::BannedIpRepository;
use crate::CliConfig;
use crate::commands::admin::users::types::BanRemoveOutput;
use crate::shared::CommandOutput;
#[derive(Debug, Args)]
pub struct RemoveArgs {
pub ip: String,
#[arg(short = 'y', long)]
pub yes: bool,
}
pub(super) async fn execute(args: RemoveArgs, _config: &CliConfig) -> Result<CommandOutput> {
if !args.yes {
return Err(anyhow!(
"This will remove the IP ban. Use --yes to confirm."
));
}
let ctx = AppContext::new().await?;
let ban_repository = BannedIpRepository::new(ctx.db_pool())?;
let removed = ban_repository.unban_ip(&args.ip).await?;
let output = BanRemoveOutput {
ip_address: args.ip.clone(),
removed,
message: if removed {
format!("IP address '{}' has been unbanned", args.ip)
} else {
format!("IP address '{}' was not banned", args.ip)
},
};
Ok(CommandOutput::card_value("Ban Removed", &output))
}