use std::path::PathBuf;

use clap::{Parser, Subcommand};

#[derive(Debug, Parser)]
#[command(
    name = "bookyard",
    version,
    about = "Build and edit a local bookshelf for multiple mdBook projects."
)]
pub struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Debug, Subcommand)]
pub enum Command {
    Init {
        #[arg(long, default_value = "Bookyard")]
        title: String,
        #[arg(long)]
        force: bool,
    },
    Add {
        source: PathBuf,
        #[arg(long)]
        folder: String,
        #[arg(long)]
        title: Option<String>,
        #[arg(long)]
        id: Option<String>,
        #[arg(long = "tag")]
        tags: Vec<String>,
    },
    Build {
        #[arg(long, hide = true)]
        no_mdbook: bool,
    },
    Serve {
        #[arg(long, default_value = "127.0.0.1")]
        host: String,
        #[arg(long, default_value_t = 3000)]
        port: u16,
        #[arg(long)]
        edit: bool,
    },
    Doctor,
}

pub async fn run() -> anyhow::Result<()> {
    match Cli::parse().command {
        Command::Init { title, force } => crate::commands::init::run(&title, force),
        Command::Add {
            source,
            folder,
            title,
            id,
            tags,
        } => crate::commands::add::run(source, folder, title, id, tags),
        Command::Build { no_mdbook } => crate::commands::build::run(no_mdbook),
        Command::Serve { host, port, edit } => crate::commands::serve::run(host, port, edit).await,
        Command::Doctor => crate::commands::doctor::run(),
    }
}
