use clap::{Parser, Subcommand};

mod choose;
mod confirm;
mod file;
mod filter;
mod input;
mod keybinds;
mod log;
mod menu;
mod spin;
mod style;
mod update;

const PANDORA_GUIDE: &str = include_str!("../PANDORA_GUIDE.md");
pub const BIN_NAME: &str = "pan";

#[derive(Parser)]
#[command(name = crate::BIN_NAME, about = "CLI toolkit with interactive TUI popups", version = env!("CARGO_PKG_VERSION"))]
struct Cli {
    /// Display the complete pan guide
    #[arg(long)]
    guide: bool,

    #[command(subcommand)]
    command: Option<Commands>,
}

#[derive(Subcommand)]
enum Commands {
    /// Show a confirmation popup, returns 0 on accept, 1 on cancel
    Confirm(confirm::ConfirmArgs),
    /// Show an interactive item selector
    Choose(choose::ChooseArgs),
    /// Show an interactive filter & select popup
    Filter(filter::FilterArgs),
    /// Show an animated spinner popup
    Spin(spin::SpinArgs),
    /// Print a formatted log line
    Log(log::LogArgs),
    /// Open an interactive file browser
    File(file::FileArgs),
    /// Capture text input from the user
    Input(input::InputArgs),
    /// Interactive tree menu from JSON or plano input
    Menu(menu::MenuArgs),
    /// Self-update to the latest release
    Update(update::UpdateArgs),
}

fn main() {
    let cli = Cli::parse();

    if cli.guide {
        println!("{}", PANDORA_GUIDE);
        return;
    }

    match cli.command {
        Some(Commands::Confirm(args)) => confirm::run(args),
        Some(Commands::Choose(args)) => choose::run(args),
        Some(Commands::Filter(args)) => filter::run(args),
        Some(Commands::Spin(args)) => spin::run(args),
        Some(Commands::Log(args)) => log::run(args),
        Some(Commands::File(args)) => file::run(args),
        Some(Commands::Input(args)) => input::run(args),
        Some(Commands::Menu(args)) => menu::run(args),
        Some(Commands::Update(args)) => update::run(args),
        None => {
            // No subcommand and no --guide, just show help
            let mut cmd = <Cli as clap::CommandFactory>::command();
            cmd.print_help().unwrap();
            println!();
        }
    }
}
