use std::io::{self, Write};
use anyhow::{bail, Result};
use colored::Colorize;
use crate::storage::Database;
#[derive(clap::Args)]
#[command(after_help = "EXAMPLES:\n \
lore delete abc123 Delete session (prompts for confirmation)\n \
lore delete abc123 --force Delete without confirmation")]
pub struct Args {
#[arg(value_name = "SESSION")]
#[arg(
long_help = "The session ID prefix to delete. Must uniquely identify a\n\
single session. Use 'lore sessions' to find session IDs."
)]
pub session: String,
#[arg(long)]
#[arg(
long_help = "Skip the confirmation prompt and proceed with deletion.\n\
Use with caution as this operation cannot be undone."
)]
pub force: bool,
}
pub fn run(args: Args) -> Result<()> {
let db = Database::open_default()?;
let all_sessions = db.list_sessions(1000, None)?;
let matching: Vec<_> = all_sessions
.iter()
.filter(|s| s.id.to_string().starts_with(&args.session))
.collect();
if matching.is_empty() {
if all_sessions.is_empty() {
bail!(
"No session found matching '{}'. No sessions in database. \
Run 'lore import' to import sessions first.",
args.session
);
} else {
bail!(
"No session found matching '{}'. \
Run 'lore sessions' to list available sessions.",
args.session
);
}
}
if matching.len() > 1 {
println!("{}", "Multiple sessions match that prefix:".yellow());
for s in &matching {
let id_short = &s.id.to_string()[..8];
println!(
" {} - {}",
id_short.cyan(),
s.started_at.format("%Y-%m-%d %H:%M")
);
}
bail!(
"Multiple sessions match '{}'. Please use a more specific prefix from the list above.",
args.session
);
}
let session = matching[0];
let session_short = &session.id.to_string()[..8];
let messages = db.get_messages(&session.id)?;
let links = db.get_links_by_session(&session.id)?;
println!();
println!("{} {}", "Session".bold(), session.id.to_string().cyan());
println!(" {} {}", "Tool:".dimmed(), session.tool);
println!(
" {} {}",
"Started:".dimmed(),
session.started_at.format("%Y-%m-%d %H:%M:%S")
);
println!(" {} {}", "Directory:".dimmed(), session.working_directory);
if let Some(ref branch) = session.git_branch {
println!(" {} {}", "Branch:".dimmed(), branch);
}
println!();
println!(
"{}",
format!(
"This will permanently delete {} messages and {} links.",
messages.len(),
links.len()
)
.yellow()
);
if !args.force {
print!("Delete session {}? [y/N] ", session_short.cyan());
io::stdout().flush()?;
let mut input = String::new();
io::stdin().read_line(&mut input)?;
if !input.trim().eq_ignore_ascii_case("y") {
println!("{}", "Cancelled".dimmed());
return Ok(());
}
}
let (messages_deleted, links_deleted) = db.delete_session(&session.id)?;
println!(
"{} session {} ({} messages, {} links)",
"Deleted".green(),
session_short.cyan(),
messages_deleted,
links_deleted
);
Ok(())
}