use clap::{Parser, Subcommand, ValueEnum};

#[derive(Parser, Debug)]
#[command(name = "spacesh", about = "spacesh CLI — thin client to the spacesh daemon")]
pub struct Cli {
    /// Print raw JSON instead of human output.
    #[arg(long, global = true)]
    pub json: bool,
    #[command(subcommand)]
    pub cmd: Sub,
}

#[derive(ValueEnum, Clone, Copy, Debug, PartialEq)]
pub enum StateArg { Work, Wait, Done, Error, Idle }

#[derive(ValueEnum, Clone, Copy, Debug, PartialEq)]
pub enum DirArg { Right, Down }

#[derive(ValueEnum, Clone, Copy, Debug, PartialEq)]
pub enum EdgeArg { Left, Right, Top, Bottom }

#[derive(Subcommand, Debug)]
pub enum Sub {
    Open { path: String },
    Status,
    NewSurface {
        workspace_id: String,
        #[arg(long)] cmd: Option<String>,
        #[arg(long = "arg")] args: Vec<String>,
        #[arg(long, default_value_t = 80)] cols: u16,
        #[arg(long, default_value_t = 24)] rows: u16,
    },
    Split {
        surface_id: String,
        #[arg(long, value_enum, default_value_t = DirArg::Right)] dir: DirArg,
        #[arg(long)] cmd: Option<String>,
        #[arg(long = "arg")] args: Vec<String>,
    },
    Close { surface_id: String },
    Focus { surface_id: String },
    Restart {
        surface_id: String,
        /// Relaunch the agent with its session-continue flag (e.g. claude --continue).
        #[arg(long)] resume: bool,
    },
    Notify {
        #[arg(long)] surface: String,
        #[arg(long, value_enum)] state: StateArg,
    },
    ApplyPreset {
        workspace_id: String,
        #[arg(long)] preset: String,
        #[arg(long = "agent")] agents: Vec<String>,
    },
    SetRatios {
        workspace_id: String,
        #[arg(long, value_delimiter = ',')] path: Vec<u32>,
        #[arg(long, value_delimiter = ',')] ratios: Vec<f32>,
    },
    Move {
        surface_id: String,
        #[arg(long)] target: String,
        #[arg(long, value_enum)] edge: EdgeArg,
    },
    CloseWorkspace { workspace_id: String },
    Group {
        #[command(subcommand)] action: GroupAction,
    },
    SetMeta {
        workspace_id: String,
        #[arg(long)] name: Option<String>,
        #[arg(long)] group: Option<String>,
        #[arg(long)] unread: Option<bool>,
        #[arg(long)] order: Option<u32>,
    },
    Shutdown,
    Completions { shell: clap_complete::Shell },
}

#[derive(Subcommand, Debug)]
pub enum GroupAction {
    Create { #[arg(long)] name: String, #[arg(long)] color: String },
    Set { group_id: String, #[arg(long)] name: Option<String>, #[arg(long)] color: Option<String>, #[arg(long)] order: Option<u32> },
    Delete { group_id: String },
}
