partiri-cli 0.1.5

partiri CLI — Deploy and manage services on Partiri Cloud
use inquire::{Select, Text};
use owo_colors::OwoColorize;

use crate::client::ApiClient;
use crate::error::Result;
use crate::output::print_success;

pub fn run_create(client: &ApiClient) -> Result<()> {
    let workspaces = client.list_workspaces()?;

    if workspaces.is_empty() {
        return Err("No workspaces found. Create one at https://partiri.cloud".into());
    }

    let workspace_id = if workspaces.len() == 1 {
        workspaces[0].id.clone()
    } else {
        let options: Vec<String> = workspaces
            .iter()
            .map(|w| format!("{} ({})", w.name, w.email.as_deref().unwrap_or("")))
            .collect();
        let choice = Select::new("Select workspace:", options.clone())
            .prompt()
            .map_err(|e| format!("Selection cancelled: {e}"))?;
        let (_, workspace) = options
            .into_iter()
            .zip(workspaces.into_iter())
            .find(|(label, _)| label == &choice)
            .ok_or("Selected workspace not found in list")?;
        workspace.id
    };

    let name = Text::new("Project name:")
        .prompt()
        .map_err(|_| "Cancelled.")?;

    let env_options = vec!["Development", "Staging", "Production"];
    let env_choice = Select::new("Environment:", env_options)
        .prompt()
        .map_err(|_| "Cancelled.")?;

    let environment = match env_choice {
        "Development" => "dev",
        "Staging" => "staging",
        "Production" => "prod",
        _ => unreachable!(),
    };

    client.create_project(&name, environment, &workspace_id)?;

    print_success(&format!("Project '{}' created.", name.bold()));

    Ok(())
}