//! Command-line interface for the Arc package manager

use clap::{Parser, Subcommand};

const VERSION: &str = env!("CARGO_PKG_VERSION");
const ABOUT: &str = "Arc Package Manager - Unified pacman and flatpak management";

/// Arc Package Manager CLI
#[derive(Parser)]
#[command(name = "arc")]
#[command(version = VERSION)]
#[command(about = ABOUT)]
#[command(author = "Blossom OS")]
#[command(long_about = None)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Option<Commands>,

    /// Enable verbose output
    #[arg(short, long, global = true)]
    pub verbose: bool,
}

#[derive(Subcommand)]
pub enum Commands {
    /// Search for packages
    Search {
        /// Package name or pattern to search for
        query: String,

        /// Search in pacman only
        #[arg(long)]
        pacman_only: bool,

        /// Search in flatpak only
        #[arg(long)]
        flatpak_only: bool,
    },

    /// Install a package
    Install {
        /// Package name or alias to install
        package: String,
    },

    /// Uninstall a package
    #[command(alias = "remove")]
    Uninstall {
        /// Package name to uninstall
        package: String,

        /// Force uninstall (requires root)
        #[arg(short, long)]
        force: bool,
    },

    /// Check if a package is installed
    Info {
        /// Package name to check
        package: String,
    },

    /// List all installed packages
    List {
        /// Show only pacman packages
        #[arg(long)]
        pacman_only: bool,

        /// Show only flatpak packages
        #[arg(long)]
        flatpak_only: bool,
    },

    /// Show configuration information
    Config {
        /// Show all configuration details
        #[arg(short, long)]
        all: bool,
    },
}

impl Cli {
    /// Parse command line arguments and return the CLI structure
    pub fn parse() -> Self {
        Parser::parse()
    }

    /// Check if help should be displayed
    pub fn should_show_help(&self) -> bool {
        self.command.is_none()
    }
}

impl Commands {
    /// Get a description of the command
    #[allow(dead_code)]
    pub fn description(&self) -> &str {
        match self {
            Commands::Search { .. } => "Search for packages in repositories",
            Commands::Install { .. } => "Install a package",
            Commands::Uninstall { .. } => "Uninstall a package",
            Commands::Info { .. } => "Show package information",
            Commands::List { .. } => "List installed packages",
            Commands::Config { .. } => "Show configuration information",
        }
    }
}

/// Print help message
pub fn print_help() {
    println!("{}", ABOUT);
    println!("version {}\n", VERSION);
    println!("USAGE:");
    println!("    arc [OPTIONS] [COMMAND]\n");
    println!("COMMANDS:");
    println!("    search              Search for packages");
    println!("    install             Install a package");
    println!("    uninstall, remove   Uninstall a package");
    println!("    info                Check if a package is installed");
    println!("    list                List all installed packages");
    println!("    config              Show configuration information");
    println!("    help                Print this help message\n");
    println!("OPTIONS:");
    println!("    -v, --verbose    Enable verbose output");
    println!("    -h, --help       Print help information");
    println!("    -V, --version    Print version information\n");
    println!("EXAMPLES:");
    println!("    arc search firefox");
    println!("    arc install vim");
    println!("    arc uninstall code");
    println!("    arc remove code");
    println!("    arc list --pacman-only");
    println!("    arc config --all");
}
