use anyhow::Result;
use anyhow::bail;
use colored::Colorize;
use std::env;
use std::process::Command;

use crate::config::RepoConfigManager;
use crate::git::utils::get_control_repo_root;
use crate::utils::paths;

pub async fn execute() -> Result<()> {
    let argv = agentic_tools_utils::editor_argv()?;

    let repo_root = get_control_repo_root(&env::current_dir()?)?;
    let config_path = paths::get_repo_config_path(&repo_root);

    if !config_path.exists() {
        bail!("No repository configuration found. Run 'thoughts init' first.");
    }

    // Open in editor
    let status = Command::new(&argv.program)
        .args(&argv.args)
        .arg(&config_path)
        .status()?;

    if !status.success() {
        bail!("Editor exited with error");
    }

    // Validate after editing (V2 only - V1 is no longer supported)
    let mgr = RepoConfigManager::new(repo_root);
    match mgr.peek_config_version()? {
        Some(v) if v == "1.0" => {
            bail!(
                "V1 configuration is no longer supported. Please reinitialize with 'thoughts init'."
            );
        }
        Some(_) => {
            let cfg = mgr.load_v2_or_bail()?;
            let warnings = mgr.save_v2_validated(&cfg)?;
            for w in warnings {
                eprintln!("Warning: {w}");
            }
            println!("✓ Saved and validated v2 configuration");
        }
        None => bail!("No configuration found after edit"),
    }

    // Update mounts after config change
    println!("\n{} active mounts...", "Updating".cyan());
    crate::mount::auto_mount::update_active_mounts().await?;

    Ok(())
}
