use clap::{Parser, Subcommand};
use scx_loader::SchedMode;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Parser, Debug)]
#[group(required = true)]
pub struct StartArgs {
    #[arg(short, long, help = "Scheduler to start", required = true)]
    pub sched: String,
    #[arg(
        short,
        long,
        value_enum,
        default_value = "auto",
        conflicts_with = "args",
        help = "Mode to start in"
    )]
    pub mode: Option<SchedMode>,
    #[arg(
        short,
        long,
        value_delimiter(','),
        requires = "sched",
        conflicts_with = "mode",
        help = "Scheduler arguments separated by commas or shell-style whitespace; quote values containing spaces, but quotes cannot span commas (e.g. \"--verbose --slice-us 5000\")"
    )]
    pub args: Option<Vec<String>>,
}

#[derive(Parser, Debug)]
#[group(required = true)]
pub struct SwitchArgs {
    #[arg(short, long, help = "Scheduler to switch to")]
    pub sched: Option<String>,
    #[arg(
        short,
        long,
        value_enum,
        conflicts_with = "args",
        help = "Mode to switch to"
    )]
    pub mode: Option<SchedMode>,
    #[arg(
        short,
        long,
        value_delimiter(','),
        requires = "sched",
        conflicts_with = "mode",
        help = "Scheduler arguments separated by commas or shell-style whitespace; quote values containing spaces, but quotes cannot span commas (e.g. \"--verbose --slice-us 5000\")"
    )]
    pub args: Option<Vec<String>>,
}

#[derive(Parser, Debug)]
pub struct ModesArgs {
    #[arg(short, long, help = "Scheduler to query", required = true)]
    pub sched: String,
    #[arg(
        long,
        help = "Show the resolved arguments for every mode, not just which ones are configured"
    )]
    pub show_args: bool,
}

#[derive(Subcommand, Debug)]
pub enum Commands {
    #[command(about = "Get the info on the running scheduler")]
    Get,
    #[command(about = "List all supported schedulers")]
    List,
    #[command(about = "List the modes that are actually configured for a scheduler")]
    Modes {
        #[clap(flatten)]
        args: ModesArgs,
    },
    #[command(about = "Start a scheduler in a mode or with arguments")]
    Start {
        #[clap(flatten)]
        args: StartArgs,
    },
    #[command(about = "Switch schedulers or modes, optionally with arguments")]
    Switch {
        #[clap(flatten)]
        args: SwitchArgs,
    },
    #[command(about = "Stop the current scheduler")]
    Stop,
    #[command(about = "Restart the current scheduler with original configuration")]
    Restart,
    #[command(about = "Restore the default scheduler from configuration")]
    Restore,
}
