use std::fs;

use anyhow::Result;
use inquire::Confirm;

use crate::config;
use crate::ui::style;

pub fn run(yes: bool) -> Result<()> {
    let cfg_home = config::config_home()?;
    let state_home = config::state_home()?;
    println!("{} aimgr implode will remove:", style::warn_mark());
    println!("  {}", cfg_home.display());
    println!("  {}", state_home.display());

    if !yes {
        let ok = Confirm::new("Really delete these directories?")
            .with_default(false)
            .prompt()?;
        if !ok {
            return Ok(());
        }
    }

    if cfg_home.exists() {
        fs::remove_dir_all(&cfg_home).ok();
        println!("{} removed {}", style::ok_mark(), cfg_home.display());
    }
    if state_home.exists() {
        fs::remove_dir_all(&state_home).ok();
        println!("{} removed {}", style::ok_mark(), state_home.display());
    }
    Ok(())
}
