use std::path::PathBuf;

use clap::{ArgAction, Parser, Subcommand};

/// Defines a `Cli`.
#[derive(Parser)]
#[command(
    name = "cuba",
    version = env!("CARGO_PKG_VERSION"),
    author = "Stefan Pflumm",
    about = "Cuba - a lightweight backup tool"
)]
pub struct Cli {
    #[command(subcommand)]
    pub command: MainCommands,
}

/// Defines a `ConfigArg`.
#[derive(Parser)]
pub struct ConfigArg {
    /// Path to the configuration file.
    /// Defaults to ~/.config/cuba.toml if not specified.
    #[arg(long, short = 'c')]
    pub config: Option<PathBuf>,
}

/// Defines a `MainCommands`.
#[derive(Subcommand)]
pub enum MainCommands {
    /// Run a backup
    Backup {
        /// The name of the backup profile.
        backup: String,

        /// Path to the configuration file.
        /// Defaults to ~/.config/cuba.toml if not specified.
        #[arg(long, short = 'c')]
        config: Option<PathBuf>,
    },
    /// Run a restore
    Restore {
        /// The name of the restore profile.
        restore: String,

        /// Path to the configuration file.
        /// Defaults to ~/.config/cuba.toml if not specified.
        #[arg(long, short = 'c')]
        config: Option<PathBuf>,
    },
    /// Run a verify
    Verify {
        /// The name of the backup profile.
        backup: String,

        /// Verify all files.
        #[arg(long, action = ArgAction::SetTrue)]
        all: bool,

        /// Path to the configuration file.
        /// Defaults to ~/.config/cuba.toml if not specified.
        #[arg(long, short = 'c')]
        config: Option<PathBuf>,
    },
    /// Run a clean
    Clean {
        /// The name of the backup profile.
        backup: String,

        /// Path to the configuration file.
        /// Defaults to ~/.config/cuba.toml if not specified.
        #[arg(long, short = 'c')]
        config: Option<PathBuf>,
    },
    /// Manage passwords.
    Password {
        #[command(subcommand)]
        command: PasswordCommands,
    },
    /// Show/write config.
    Config {
        #[command(subcommand)]
        command: ConfigCommands,
    },
    /// Browses a backup.
    Browse {
        /// The name of the backup profile.
        backup: String,

        /// Path to the configuration file.
        /// Defaults to ~/.config/cuba.toml if not specified.
        #[arg(long, short = 'c')]
        config: Option<PathBuf>,
    },
}

/// Defines a `PasswordCommands`.
#[derive(Subcommand)]
pub enum PasswordCommands {
    /// Sets a password.
    Set {
        /// The password id.
        id: String,

        /// Read password from stdin instead of interactive prompt.
        #[arg(long, action = ArgAction::SetTrue)]
        stdin: bool,
    },
    /// Deletes a password.
    Delete {
        /// The password id.
        id: String,
    },
    /// Lists the password ids.
    List,
}

/// Defines a `ConfigCommands`.
#[derive(Subcommand)]
pub enum ConfigCommands {
    /// A config example.
    Example {
        #[command(subcommand)]
        command: ConfigExampleCommands,
    },
}

/// Defines a `ConfigExampleCommands`.
#[derive(Subcommand)]
pub enum ConfigExampleCommands {
    /// Show config example.
    Show,
    /// Write config example.
    Write,
}
