use inquire::Select;
use tabled::Table;
use crate::client::ApiClient;
use crate::error::Result;
use crate::output::ProjectRow;
pub fn run_list(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 projects = client.list_projects(&workspace_id)?;
if projects.is_empty() {
println!("No projects found in this workspace.");
return Ok(());
}
let rows: Vec<ProjectRow> = projects
.into_iter()
.map(|p| ProjectRow {
name: p.name,
environment: p.environment,
id: p.id,
})
.collect();
println!("{}", Table::new(rows));
Ok(())
}