//! `sprouts rollback` — point a sprout back at a previous deployed version.

use std::path::PathBuf;

use clap::Args as ClapArgs;

use crate::api;
use crate::commands::session;

#[derive(ClapArgs)]
pub struct Args {
    /// The version number to roll back to.
    version: i32,
    /// The sprout id (defaults to the sprout named in ./sprout.toml).
    #[arg(long)]
    id: Option<String>,
    /// The sprout directory (used to resolve the name when `--id` is omitted).
    #[arg(long, default_value = ".")]
    path: PathBuf,
}

pub fn run(args: Args) -> anyhow::Result<()> {
    let sprout_id = session::resolve_sprout_id(args.id, &args.path)?;
    session::authed(|u, t| api::rollback(u, t, &sprout_id, args.version))?;
    println!("Rolled back to v{}.", args.version);
    Ok(())
}
