use std::{fs, path::PathBuf};
use crate::{
action::{SERVICE_CONFIGURATION_PATH, ServiceConfiguration, carry_forward_actions},
bundle::{BundleSource, SetupBundle},
command::setup::run_wizard,
env_file,
runner::{Runner, default_runner},
template::render_setup_templates,
ui,
};
const ENV_PATH: &str = ".codezero/.env";
pub fn configure(bundle_path: Option<PathBuf>) -> anyhow::Result<()> {
if !fs::exists(ENV_PATH)? {
anyhow::bail!("No CodeZero setup found. Run `codezero setup` first.");
}
let source = BundleSource::from_arg(bundle_path);
let bundle = SetupBundle::load(&source)?;
let theme = ui::wizard_theme();
let seed = env_file::read_all(ENV_PATH)?;
let context = run_wizard(&bundle, &theme, false, Some(&seed))?;
let installed_actions =
ServiceConfiguration::load_or_default(SERVICE_CONFIGURATION_PATH)?.actions;
render_setup_templates(&source, &bundle.templates, &context, ".codezero")?;
carry_forward_actions(installed_actions)?;
println!();
ui::muted_line("Applying changes...");
default_runner().start()?;
ui::success_line("CodeZero reconfigured.");
Ok(())
}