//! `cellctl delete formation {name}` — `DELETE /v1/formations/{name}`.

use std::io::{self, BufRead, Write};

use crate::client::{formation_path, CellosClient};
use crate::exit::{CtlError, CtlResult};

pub async fn formation(client: &CellosClient, name: &str, assume_yes: bool) -> CtlResult<()> {
    if !assume_yes {
        let confirmed = confirm(&format!("Delete formation '{name}'? This is irreversible."))?;
        if !confirmed {
            return Err(CtlError::usage("delete cancelled by user"));
        }
    }
    // CTL-002: UUID vs name routing — see `client::formation_path`.
    client.delete(&formation_path(name)).await?;
    println!("formation/{name} deleted");
    Ok(())
}

fn confirm(prompt: &str) -> CtlResult<bool> {
    eprint!("{prompt} [y/N] ");
    io::stderr().flush().ok();
    let mut line = String::new();
    io::stdin().lock().read_line(&mut line)?;
    let trimmed = line.trim().to_ascii_lowercase();
    Ok(matches!(trimmed.as_str(), "y" | "yes"))
}
