use clap::{Parser, Subcommand, Args};
use clap_complete::Shell;

#[derive(Parser)]
#[command(name = "nom", version, about = "Nomad headless browser CLI")]
pub struct Cli {
    /// Generate shell completions
    #[arg(long, value_enum)]
    pub completions: Option<Shell>,
    #[command(subcommand)]
    pub command: Option<Command>,
}

#[derive(Subcommand)]
pub enum Command {
    /// Fetch a URL and print formatted output
    Browse(BrowseArgs),
    /// Manage the background daemon process
    Daemon(DaemonArgs),
    /// Quick daemon health check
    Health(HealthArgs),
    /// List output formats
    Format(FormatArgs),
}

#[derive(Args)]
pub struct BrowseArgs {
    /// URL to fetch
    pub url: String,
    /// Output format: raw|json|markdown|agent|summary|minimal|section|a11y|tree|reader|form (or 0-10)
    #[arg(short, long, default_value = "summary")]
    pub format: String,
    /// Output to file instead of stdout
    #[arg(short, long)]
    pub output: Option<String>,
    /// Daemon port (auto-detected if not set)
    #[arg(long, default_value = "8000")]
    pub port: u16,
    /// Force oneshot mode (don't try daemon)
    #[arg(long)]
    pub oneshot: bool,
    /// JS timeout in ms (oneshot only)
    #[arg(long)]
    pub js_timeout: Option<u32>,
}

#[derive(Args)]
pub struct DaemonArgs {
    #[command(subcommand)]
    pub action: DaemonAction,
}

#[derive(Subcommand)]
pub enum DaemonAction {
    /// Start the background daemon
    Start {
        /// HTTP API port
        #[arg(long, default_value = "8000")]
        port: u16,
        /// Page cache capacity
        #[arg(long, default_value = "1")]
        cache_cap: usize,
        /// Max concurrent HTML parses
        #[arg(long, default_value = "2")]
        parse_concurrency: usize,
    },
    /// Stop the daemon
    Stop,
    /// Show daemon status
    Status,
    /// Restart the daemon
    Restart {
        /// HTTP API port
        #[arg(long, default_value = "8000")]
        port: u16,
    },
}

#[derive(Args)]
pub struct HealthArgs {
    /// Daemon port
    #[arg(long, default_value = "8000")]
    pub port: u16,
    /// JSON output
    #[arg(long)]
    pub json: bool,
}

#[derive(Args)]
pub struct FormatArgs {
    /// Show detailed info for a specific format (0-10 or name)
    pub format: Option<String>,
}
