use anyhow::{Result, anyhow};
use clap::Args;
use systemprompt_runtime::AppContext;
use systemprompt_users::BannedIpRepository;
use crate::CliConfig;
use crate::commands::admin::users::types::BanCleanupOutput;
use crate::shared::CommandOutput;
#[derive(Debug, Clone, Copy, Args)]
pub struct CleanupArgs {
#[arg(short = 'y', long)]
pub yes: bool,
}
pub(super) async fn execute(args: CleanupArgs, _config: &CliConfig) -> Result<CommandOutput> {
let ctx = AppContext::new().await?;
let ban_repository = BannedIpRepository::new(ctx.db_pool())?;
if !args.yes {
return Err(anyhow!(
"This will delete all expired bans. Use --yes to confirm."
));
}
let cleaned = ban_repository.cleanup_expired().await?;
let output = BanCleanupOutput {
cleaned,
message: format!("Cleaned up {} expired ban(s)", cleaned),
};
Ok(CommandOutput::card_value("Ban Cleanup", &output))
}