pub mod cmd_edit;
pub mod cmd_new;
pub mod cmd_run;
pub mod cmd_show;

use clap::{
    Arg, ColorChoice, Command,
    builder::styling::{AnsiColor, Effects, Styles},
};
use std::{env, path::PathBuf};

pub fn new(config_path: PathBuf) -> Command {
    let styles = Styles::styled()
        .header(AnsiColor::Yellow.on_default() | Effects::BOLD)
        .usage(AnsiColor::Green.on_default() | Effects::BOLD)
        .literal(AnsiColor::Blue.on_default() | Effects::BOLD)
        .placeholder(AnsiColor::Green.on_default());

    Command::new("backup")
        .about("Create compressed and encrypted backups")
        .after_help("Note: To exclude specific files or directories from backups, create a `.gitignore` file in the backup directory and list the patterns of the files or directories to exclude.")
        .arg_required_else_help(true)
        .version(env!("CARGO_PKG_VERSION"))
        .color(ColorChoice::Auto)
        .styles(styles)
        .arg(
            Arg::new("config")
                .short('c')
                .long("config")
                .help("Path to the configuration files")
                .default_value(config_path.into_os_string())
                .global(true),
        )
        .arg(
            Arg::new("verbose")
                .short('v')
                .long("verbose")
                .help("Increase output verbosity (use -vv for more verbosity)")
                .global(true)
                .action(clap::ArgAction::Count),
        )
        .subcommand(cmd_edit::command())
        .subcommand(cmd_new::command())
        .subcommand(cmd_run::command())
        .subcommand(cmd_show::command())
}

#[cfg(test)]
mod tests {
    use super::*;
    use anyhow::{Context, Result};
    use std::path::PathBuf;

    #[test]
    fn test_new() -> Result<()> {
        let command = new(PathBuf::from("."));

        assert_eq!(command.get_name(), "backup");
        assert_eq!(
            command.get_about().context("missing about")?.to_string(),
            "Create compressed and encrypted backups"
        );
        assert_eq!(
            command
                .get_version()
                .context("missing version")?
                .to_string(),
            env!("CARGO_PKG_VERSION")
        );

        Ok(())
    }
}
